Autonomous car that follows traffic rules using PID and ESP32.
Robotraffic Careful Driving Winner 3rd Place 2022
ESP32 is a microcontroller manufactured by Epressif that has built-in WiFi, dual-core. In contrast to Arduino, ESP32 has way better performance.
More info about ESP32: https://www.espressif.com/en/products/socs/esp32
ESP32
ESP32 vs ESP8266 vs Arduino
To control motor, we must have a motor driver that meets the motor requirement.
Our motor doesn't require much power so a basic typical motor driver like L298n is good enough.
L298n
Typical line follower sensors in the market, such as TCRT5000 or CNY70, only outputs binary numbers instead of continuous value and that is a big disadvantage since PID (PID explanation below) needs a continuous value. Therefore, we used QTR-5RC sensors made by Polulu. QTR-5RC range is from 0 to 5000 (our case is 10000 since we use 2 sensors combined) which makes it perfect for PID.
Our line follower sensors
More info about QTR sensors: https://www.pololu.com/category/123/pololu-qtr-reflectance-sensors
We use 2s lipo battery to power our motor and lithium-ion battery to power ESP32.
Our 2S liPo battery
Our Lithium-ion battery
We don't think using 2s lipo battery for both motor and ESP32 is a good idea because that will make ESP32 reboot itself due to current drop caused by motor and servo.
Ackerman steering mechanism is used as a way to make a car as realistic as possible.
In this case, our servo range is from 0 to 110 out of 180, otherwise the servo will be stuck and damaged.
Proportional - Integral - Derivative is one of the most common control loop mechanisms. PID will output feedback from a setpoint and with that value, we will control servo in a way that our car moves automatically on track. In this case, we use line sensor values as inputs. Our setpoint is a line sensor's value when the car is moving straight on track (which is around 4000). Once calculated, the outputs will be fed to servo and control the direction of a car.
PID controller
Tuning is required a lot to be stable. We only tune P and D since I is quite hard to implement and control. We had to tune it every time because PID configuration will not work properly if motor speed changes. We haven't found the way to make motor speed static yet since it totally depends on battery.
Bad tuning example
Tuning is tedious and time-consuming. If we want to change a configuration, we have to upload code again, which takes around 2 minutes, and doing that a lot of time will take forever. Therefore, we create a website especially for tuning. With that, tuning is a lot faster and we don't need to upload code again.
Our website
At first, We all decided that the car and traffic lights would share data with each other through database. Then, we chose Firebase as our database and used Firebase Realtime Database Arduino made by Mobizt. We thought it was a good idea until we discovered that it seems like this library affects ESP32 timer and that somehow causes the servo to weirdly behave. Another drawback is that it takes around 300ms to communicate with each other through Firebase, which is not quick enough for a car to stop at the right time.
To fix this, we used ESPNOW feature supported by ESP development board itself. ESPNOW is Espressif feature that let every ESP communicate with each other. Using this works wonderfully, the communication time is much less (around 5ms) and servo works incredibly well with PID.
More info about ESPNOW: https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/
P/s: Lego Mindstorms EV3 in the picture is another section of the contest. It has nothing to do with a traffic light
However, there are still many problems that haven't been fixed yet:
- Motor speed is not always the same, which means we have to tune PID configuration it every time to adapt to the line.
- The car has high possibility to move out of the line after a 15-minute run.
- We just only use simple PD control, we haven't used I yet. That is why the car is shaky sometimes.
Thanks for reading
Leave a star to support us. If you have any questions, leave it below.
Libraries used:
- Firebase RTDB Arduino Library for ESP32 by Mobizt
- QTRSensors by Polulu
Everything is made possible by some junior, senior students at Le Hong Phong High school in Ho Chi Minh, Vietnam