TASK 4: FOLLOW LINE
OBJECTIVE
The objective of this task is to cover the following parts:
- Follow the line as fast as possible
- IOT communication through MQTT
- Serial communication between ESP and Arduino
- If the robot loses the line, can find it again
- Detect obstacles
HOW HAVE WE DONE IT?
FOLLOW LINE
In this part we were interested in using FreeRTOS so, to follow the line in the Elegoo robot we have created 2 tasks:
- get_infrared: used for controlling the infrared module, 100 words in the stack and the priority is 2.
- command_motors: used for controlling the motors´ speed, 100 words in the stack and the priority is 2.
In get_infrared you can find 3 analog reads from each pin (left, middle and right) and we have defined the 6 possible situations:
Also, for the PD we have created for controlling the motors (we are going to make a deep explanation of it, down below) in each case we have introduced a global variable called: error. The error is 0 when there is NO LINE or is in MID, increments 1 value when is in MID-RIGHT, increments 2 values when is in RIGHT, decrements 1 value when is in MID-LEFT and decrements 2 values when is in LEFT.
In command_motors you can find 2 digital writes in each pin module.
Now, it is time to explain the PD we introduced before. The PD combines the proportional and derivative modes to control the move of the motors. This is PD´s formula:
So to get that formula, we have set proportional error equal to the error obtained in get_infrared and the derivative error is equal to the error - previous error.
Now, right motors will have the standard velocity - PD and left motors will have standard velocity + PD.
If any of the values of right or left motors is bigger than 255 or less than 0 which is the maximum and minimum of what the motor supports, set that velocity to 255 or 0 respectively.
If an obstacle is detected, the motors stop. We finally do 2 analog writes to give the velocity to each pair of motors.
This task has a delay of 1 ms before doing the following iteration.
DETECT OBSTACLES
In this part we were interested in using FreeRTOS too so, to detect obstacles in the Elegoo robot we have created 1 task:
- is_obstacle: used for controlling the ultrasonic module and sending its correspondent messages , 100 words in the stack and the priority is 1 (the lowest priority) since we just detect an obstacle at the end of the lap.
In is_obstacle you can find how to get the distance of the ultrasonic in cm and if that distance is between 5 cm to 8 cm it should stops. However, as priority is lower than the infrared and motors, the maximun distance we have decided is 10 cm and when the robot reacts to that distance, it stops between 5 cm to 8 cm most times.
If the obstacle is detected and its message has not been sent, Elegoo sends "OBSTACLE_DETECTED" and "END_LAP" .
This task has a delay of 1 ms before doing the following iteration.
IOT COMMUNICATION AND SERIAL COMMUNICATION
The ESP has 2 serials and in the arduino used in Elegoo robot has just only 1 serial.
We declare in ESP the Serial.begin and the Serial2.begin to communicate with Elegoo robot.
We connect ESP to internet as it was given in the statement and while the ESP is not connected to mqtt, tries 3 times to connect. After connecting, ESP sends to the broker "start lap" (message to indicates that the robot can start moving as it is connected succesfully) and ESP sends (Serial.write) a 0 to Elegoo robot, to tell that the ESP is connected and starts the lap (give the ability to Elegoo robot to start working). That is the only serial communication from ESP to Elegoo robot.
For the rest of the time, the serial communication will be from Elegoo robot to ESP. So, in ESP the only thing that it does is looking for any communication from Elegoo robot (Serial2.available()) and sends the "PING".
The message called "PING" is different and it is sent after "START-LAP" and every each 4 seconds, starting in 0.
To calculate ping you can find using millis, that if the counter is 4 seconds, send "PING" message and starts again the counter. This "PING" message will be sent until "END_LAP" is sent, that means until an obstacle is detected.
After receiving from the ESP that MQTT connection has been established, the led RGB turns into green color, to have a visual guide.
In Elegoo robot, we have decided to use FreeRTOS in communication from Elegoo robot to ESP. To do so, we have created 1 task:
- send_message with 100 words in the stack and the priority is 0, so it will only work when the MCU is idle.
So, what should Elegoo robot transmit to the ESP? The answer is, messages.
Before, we explained that ESP sends the message called "START_LAP" (and "PING") after connecting succesfully.
These are the messages that Elegoo transmits:
ELEGOO MESSAGES
- If Elegoo loses the line, it sends "LINE_LOST".
- After 3. Elegoo sends "INIT_LINE_SEARCH" to start finding the line.
- When the line is found, Elegoo sends "LINE_FOUND".
- After 5. Elegoo sends "STOP_LINE_SEARCH" and keep doing the circuit
Repeat them when it is necessary.
In send_message you can find two different situations when 5 messages explained before can be sent:
If there is no line and the message line lost has not been sent means that the robot has lost the line, send "LINE_LOST" and "INIT_LINE_SEARCH".
If the message of line lost is sent and there is line means that the robot has found again the line so, sends "LINE_FOUND" and "STOP_LINE_SEARCH"
This task has a delay of 10 ms before doing the following iteration.
OUR PROBLEMS AND HOW WE SOLVED THEM
Ultrasound sensor: The robot stopped at a different
distance each time an obstacle was detected. By increasing the threshold it stopped at a correct distance.
Serial
communication ESP - Arduino UNO: The communication from ESP to Arduino
wasn't working. To allow the ESP to send data to the Arduino the switch
on the expansion board of the Arduino UNO must be set to CAM instead of
UPLOAD.
DETECTED OBSTACLE: to assure that the robot detects the obstacle and stops, we set the motors velocities to 0 so it does not have to do "command_motors" again.
VIDEOS
Training L3203 Circuit
Values:
- STD_VELOCITY = 120
- LOST_LINE_VEL = 150
- IR_R_THRESHOLD = 450
- IR_M_THRESHOLD = 150
- IR_L_THRESHOLD = 650
- Kp = 2
- Kd = 15
exam test (round 1):
- STD_VELOCITY = 120
- LOST_LINE_VEL = 120
- IR_R_THRESHOLD = 650
- IR_M_THRESHOLD = 450
- IR_L_THRESHOLD = 750
- Kp = 0.1
- Kd = 20
exam test (round 2):
- STD_VELOCITY = 175
- LOST_LINE_VEL =175
- IR_R_THRESHOLD = 650
- IR_M_THRESHOLD = 450
- IR_L_THRESHOLD = 750
- Kp = 1.5
- Kd = 20
We hope you like them!
This task has been handled by Gonzalo Vega Pérez and Julia López Augusto
Comments
Post a Comment