In this post, I will cover how to make a ESP32 tank robot Arduino sketch, using WiFi control. The idea is to control a small tank using http async web tecnologies, creating a webserver over the esp32 device that serves a JS based joytick. This JS app gathers the joystick position, remixes the results and sends the values needed to control the motors of the tank.
I’ll use this code for my SMARS tank and for my MR-6 tank, for the later, as is based around ESP32-cam module, i’ll also add to the guide in the future how to make the video stream. But this method should work on any similar robot/tank.
The following video shows it in action.
Acknowledgement
This post would’t be possible or not as easy, withouth the contribution of the people that made the following libraries and posts, big cheers to all of them:
- ESPAsyncWebServer
- TB6612_ESP32
- jostick JS library from bobbotek
- Differential control
- Async service
- web baker
- Editing the ESP32-CAM Camera Web Server HTML
1.The Esp32 code
For this tutorial i’m going to use the code available on my SMARS-esp32 repo.
Editing the Config
We weill need to tdith the /src/config.h file, we need to put the pins we are using in our case and changing your password.
Importing our libraries
We will use for our sketch, the following libraries, they all need to be downloaded. They will be downloaded automatically if you are using PlatformIO. Here are the following links if you use Arduino program: ESPAsyncWebServer TB6612_ESP32
Creating the async webserver
in our /src/main.cpp we can find the following setup() code. It will create a Wifi hotspot and start a asyncwebserver using the ESPAsyncWebServer library. This server will be usually in the 192.168.4.1 direction.
We could also modify this to connect to a home Wifi and control the tank, in this case it will have a different Wifi Adress based on your router settings.
This code will also make our esp32, on the port 80 deliver our webpage, which is compressed as the array index_html_gz[] in the /src/web.h file.
Handling the clients and messages
To finish the sketch, we will need to control the motors based on the messages and connect to the clients if available.
2.The web Control
You can find a full webpage in the project under the /web_smars.html file, in the following steps i’ll show you how i made it so you can made a new one for your needs!
Finding a suitable jostick
To make the web interface control, we will need to use a joystick, this requires more advanced JS knowledge than i have at the moment, so i “borrowed” this wonderfull jostick program from bobbotek
You can also use any other or create your own! but this one is the one used with the sketch, and is pretty configurable, easy to setup and works pretty good.
This joystick is as easy to create as importing the module and adding the next to any html file:
Getting the joystick values and calculating motor drive
Our joystick gives us -127..128 values, but our library uses -255..255 values, so we will need to convert the data to that values, that should be as easy as some maths, but we find also another problem.
We want to have a good control of the tank, and we can not just map one motor to the Y Axis and Another one to the X axis, as it would cause our tank to move straight in the right top corner (X=127;Y127) so we need to apply some further maths to it.
I will not duplicate this info as you can find it very well explained in the following article, the code i am using is adapted from authors C solution to JS, huge thanks for sharing.
This will lead as to the creation of a aditional function, called getFuerza, which will calculate, based on the input the motor drive power for each track.
This could be also calculated on the esp32 side, but using JS for this lets us free our ESP32 cores executing the code on the client.
You can find this code in the project /web_smars.html file.
Also we need the following code on the same file to send continuosly the messages to the web server.
Getting our web changes to the ESP32
To display our modified webpage, we need to add the compressed code to the /src/web.h file. For this it needs to be first compressed, you need to add all of your webpage code to the input field in the following web baker.
Then you will need to copy all of the output data, withouth the first “,” inside of the src/web.h file, as content of the index_html_gz[] array.
Conclusion
If you followed this tutorial, you should be able to control a esp32 robot tb661nfng based vehicule using a webserver and a joystick, if you find any problems following the guide or you think anything needs any change feel free to add any comments.
You can see some short videos of the SMARS tank using this code. As you can see its pretty easy to control and responsive.