AmebaMotors – Use mobile phone to control motorcar remotely
- Ameba x 1
- L298N H-Bridge x 1
- 4-wheel motorcar or 2-wheel motorcar+Universal wheel
- Android Phone
And install the library to Ameba: https://www.arduino.cc/en/Guide/Libraries#toc4
In this example, we will do the following things:
1. We wrap the code related to control of the motorcar to Car2wd Class, in which we implement OS thread and signal to let the motorcar and the main thread execute separately.
2. AP mode is turned on in the main thread. It opens a TCP socket to be the server and waits for client to connect.
3. Download “Ameba Car Remote” APP on mobile phone, connect to WiFi network with SSID “mycar”. Open the APP, the APP would connect to Ameba as a client, then you can use the APP to control the movement of the motor car.
1. Wiring：Refer to “AmebaMotors – Control Motorcars With Ameba” example.
2. Upload the program: Upload program to Ameba, then connect L298N to power.
3. Download the APP: Search “Ameba Car Remote” in google play, or visit https://play.google.com/store/apps/details?id=app.akexorcist.joystickcontroller
4. Connect to Ameba: Connect the mobile phone to the WiFi network with ssid “mycar” (password: 12345678).
5. Open the APP:
- Pull up the left controlling bar to move the motorcar forward; and pull down to move it backward.
- Pull right the right controlling bar to rotate the motorcar in clockwise durection; pull left to rotate in counterclockwise durection.
- Control both bar simultaneously to perform move forward/backward and turn right/left.
– If you find the car does not respond to the APP, please check if the wifi is still connected to “mycar”.
– If it is, please restart the APP and try again.
– If “mycar” is not in the wifi network list, check the lights on Ameba. If the lights are off, the power supply of Ameba may have problems.
Below is the demo video of this example:
- The usage of Car2wd and OS thread, signal
Refer to the figure below:
- In setup(), we call
car.begin()to create a thread:
tid = os_thread_create(carTask, this, OS_PRIORITY_REALTIME, 1024);
– The first parameter os a function pointer, which is the function to execute after te thread is created. The prototype of the function pointer:
void carTask(const void *arg). Generally, a RTOS thread executes a infinite loop before termination.
– The second parameter is used to specify parameter of the thread funtion. That is the
carTask(const void *arg).
– The third parameter stands for the priority of the thread. In FreeRTOS, when many threads are waiting, FreeRTOS only executes the thread with highest priority value. If there are more than one threads with highest priority value, they will be executed in turn. When igh-priority thread completes its task, it release execution via non-busy delay to prevent starvation of low-priority threads.
– The fourth parameter is the memory required by this thread, including stack and local memory.
– The return value of
os_thread_create()is the thread id.
- When the thread starts running, it enters carTask() and waits for signal:
evt = os_signal_wait(0, 0xFFFFFFFF);
– The first argument is the type of signal it’s waiting for. 0 stands for any signal.
– The second argument is how long we should wait for the signal (ms).
0xFFFFFFFFstands for waiting infinitely.
– The return value is the result, for example timeout, or the signal it receives.
os_signal_wait, carTask release execution to other threads.
- Notify carTask: We call
car.setAction(CAR_FORWARD, abs(yspeed));in main thread.
setAction（in main thread）, we set the signal and use
os_signal_set() to notify carTask
– The first argument is the thread id to notify, and the second is the signal.
- In carTask,
os_signal_wait()got the signal, then it continues executing corresponding task according to the signal.
- In setup(), we call
- Ameba AP mode and TCP socket
To turn on AP mode, please refer to Ameba AP mode example. After AP mode is turned on, we create a tcp socket:
1. Specify the port for server to listen:
WiFiClient client = server.available();
3. Wait for client connection:
4. Read data from client0:
5. Parse the received data, the data format is text:
Y:12 – Stands for the value along Y-axis (ranges from -12 ~ +12). +12 means the car moves forward in full speed.
Y:-6 – Move backward in half-full speed.
X:12 – Rotate in clockwise direction in full speed.
Y:12, X:-6 – Move forward in full speed and turn left.（When X and Y both take on values, use the value of Y to represent the speed.）