Line Follower Robot Algorithm & Optimizations for Better Line Following
In this tutorial, I’ll be teaching you a Line Following algorithm for robots. I’ll be demonstrating the algorithm using pseudo-codes, thereby, it can be implemented in any language, be it Python, C or a LEGO Mindstorms Kit. I’ve posted techniques for better line following in the latter as well.
We’ll be using a robot with 2 colour sensors on each side. Alternatively, Light / Ultraviolet sensors can also work.
The Algorithm
Case 1
In this case, only the right sensor (Sensor 2) or the left sensor (Sensor 1) detects a line a.k.a they detect black colour.
If only the right sensor detects a black line, then the robot must steer right to follow the line. Alternatively, if only the left sensor detects a black line, then the robot must steer left to follow the line.
if right_sensor detects black
// Move Right
set left motor ON
set right motor OFFelse if left_sensor detects black
// Move Left
set left motor OFF
set right motor ONendif
Case 2
In this case, both the sensors do not detect a black line, so the robot should keep moving forward in the same direction and maintain its current journey path.
if right_sensor and left sensor do not detect black
set left motor ON
set right motor ON
Case 3
This case is a bit tricky. Depending on your game scenario, both the sensors detecting a black line could mean either it is a finish line (left image) or it is just a loop which is part of a bigger line-following problem (right image).
If it signifies a finish line, then the robot must stop and terminate the program.
Otherwise, if it signifies a loop, then:
1. The robot should stop itself for a duration of 1–2 seconds to stabilise itself,
2. Move the robot a bit forward (approximately 1/4th of rotation) to move past the black line and then,
3. Continue the normal line following algorithm.
The significance of both the sensors detecting a black line has to be pre-determined before the start of the program.
// Black line is the finish line
if left_sensor and right_sensor detect black
set left motor OFF
set right motor OFF// Black line is a part of a loop
if left_sensor and right_sensor detect black
set both motors OFF for 2 seconds (Stabalise)
set both motors ON for 1/4th rotation (Move past the black line)
continue to normal line-following algorithm
Complete Algorithm
if right_sensor detects black
// Move Right
set left motor ON
set right motor OFFelse if left_sensor detects black
// Move Left
set left motor OFF
set right motor ONelse if right_sensor and left sensor do not detect black
set left motor ON
set right motor ONelse if left_sensor and right_sensor detect black
set left motor OFF
set right motor OFFendif
Optimizations for better Line Following
Adjust the distance between the sensors
Through my observations, I conclude the following:
Generally, the robots start to tumble more as we try to increase the distance or the gap between the two sensors due to the harsh steering of the robots left and right as soon as one of the sensors encounter a black line.
Furthermore, if the sensors are placed too close to each other, then the movement of the robot becomes very stiff and rigid as the robot is continuously taking minute left and right turns and trying very bitterly to follow the line.
So, through thorough hit and trial, we should be able to find the right distance between the sensors to maintain the balance between the overall stability of the robot and the rigidity in movement.
Robot Steering Technique
Robot steering generally has two techniques — Either turn one motor OFF and turn the other motor ON to steer the robot left or right, or, run both the motors in opposite directions to take a turn. The latter usually results in more steeper turns.
As a general rule of thumb, if the expected line following path is smooth and does not contain a lot of steep turns, then it is better to turn only using one motor. But rough line paths demand more precise turning, so, both the motors should be used to turn the robot in such cases.
I hope you find the mentioned tips useful. Line Following is immensely useful, be it projects or competitions such as RoboCup and others. If I missed anything, let me know!