Categories
Algorithms Robotics

Efficient Single-Sensor Line Following Algorithm

The line following algorithm uses different combinations of sensors. We are focusing single sensor line following algorithm in this post. There are also other algorithms available which we can implement for making our line following robot. We also explained these algorithms in our others posts under the robotics category and Arduino section of this blog. There are also some more line following posts available in our algorithms section. You may check these posts once you got knowledge of basic line following algorithms which is using a single line detection sensor.

What is a Line Following Robot?

A line following robot in robotics is very basic and simple to implement robot which follows a line. The line is usually a black single line on white surface. But this could be more complicated with complex path and different turning points. To keep the definition simple, we can simply say any robot which follows any kind of line is a line following robot.

line following robot following black line on white surface
A line following robot which follows black line on white surface

Why should we use Single Sensor Algorithm?

The single sensor algorithm of line following will help to implement the simplest line following robot. It is good for rapid prototyping. Raid implementation of proof of concept. They are cheaper to implement and require low power consumption. Also, it is good starting point. If you are new to robotics. Taking a good start from building a line following robot with one sensor will help you to understand the basics of robot dynamics as well as the basic principles of implementing the algorithms for robot path planning.

Another one major key benefit of using the single sensor line following robot is you can also implement this line following robot without microcontrollers. So, if you are trying to build the line following robot without microcontrollers or a line following robot with digital gates, this will help you to get ready for your first line following robot competition for beginners.

Simple Desing line following robot one sensor
Simple design line following

What is Bang-Bang or On-Off Control Algorithm?

The algorithm which is typically used for the line following with one line detection sensor is called the Bang-Bang control algorithm. This algorithm is also referred to as the On-OFF Control algorithm. This is a general close loop algorithm that could be used for the line follower robot. Either you are implementing your line following robot using Arduino or you are implementing line following robot without the microcontroller. You can implement this On-Off Control algorithm with ease if you are using a single sensor. This On-Off Control algorithm is used where sensor input is binary. Which is perfectly the case for our single sensor line following robot.

One Sensor Line Following robot under Bang-Bang Control

This algorithm is well explained in the control theory. Which means this algorithm is a close loop algorithm to control the process which are closed loop. The basic idea of this algorithm is that this deviate abruptly within two states. This is why it is also called on-off control. This algorithm is well suited with any problem which have to be restricted on upper or lower bound and no in between control is available.

For example, in water boiling we have to apply the heater to the maximum until the water reaches the desired temperature and then we cutoff the heater. Or we have to reach till a maximum point from starting point and we start with full acceleration and then brake with full control. This is the similar case in our one sensor line following robot.

How to use On-Off Control as Line following Algorithm?

To use the Bang-Bang Control algorithm or On-Off Control algorithm for line following robot is pretty simple. We are using one IR sensor on our line following robot and the IR sensor gives the binary output. Which means it will give the Logic low when it is one the black line and gives the Logic high when it is on the white surface. We have to tune the IR sensor to clearly distinguish between the Black line and the white surface. If we did not tune the IR sensor properly it will oscillate a lot and could go Astry from the actual robotic path and domain.

Properly Tune IR sensor for Line detection

Once we properly tuned the IR sensor for detecting the black line, we can use that IR senor in our Arduino or any other digital logic controller to implement the on-off control by using this IR sensor as feedback. We have feedback and we set the setpoint of Active LOW logic which means we have to wait for Active Low signal from the sensor.

Create a Set Point: Active Low or Active High

If the sensor is giving the Active Low logic, it means it is on the Black line and we move the robot in forward direction. But if it gives the Active high signal, we immediately know that it deviates from the set point, and we have to switch the states which is to change the direction of the robot. The deviation could be due to the external noise, internal sensor noise, or end of the line. In either case we have to turn the robot.

Set Direction Priority

Once we tuned our IR sensor for line detection and create our set point as Active Low or Active High signal, it is time for setting the direction Priority. The direction priority in line following robot is very important as it has to choose the best optimum direction to follow the path. If we set a wrong direction which is not suitable for our path, then the robot would get lost in the surface. So, we have to choose either right or left direction once the set point is lost, to move in that direction. The choice will be based on the layout of the track the robot is following. When we give priority to one direction the robot behavior became more predictable and stable. Which results in smoother line following.

Achieving Goal

Now that we have implemented the basic close loop for the bang-bang control algorithm, we are good to focus on our end goal. The ultimate goal is to stabilize the robot to the black line and ensuring that it follows the line accurately and without unnecessary oscillations. In the above explain setup, whenever the robot deviates from its set point, it would aggressively switch to the priority direction to quickly align itself back to the line.

line following robot aligning to the line
Line following robot achieving controlled trajectory

Single sensor Line following algorithm (Pseudo Code):

Although we had explained a lot about the bang-bang algorithm but let’s focus on the Pseudo code. Here is the simple representation of the pseudo code of the bang-bang control process for the line following algorithm.

Line Following robot pseudo code for one sensor line following algorithm
Algorithm: BangBangLineFollowing

Input: SensorReading (Boolean value indicating line presence)
Output: RobotMovement (Directional instruction for the robot)

1. Procedure BangBangLineFollowing(SensorReading):
2.     If SensorReading is True:
3.         RobotMovement ← Move Forward   // Robot stays on the line, no adjustment needed
4.     Else:
5.         RobotMovement ← QuickTurn      // Robot initiates a quick turn to realign with the line
6.     End If
7. End Procedure
Code language: PHP (php)

Bang-Bang Control Algorithm C code

Here we try to implement the code into a generic Embedded C code representation. Which will explain how we can adopt this control algorithm for line following robot on any microcontroller which supports the C language. Almost every microcontroller supports the C language even the microcontrollers like Arduino and STM32 nowadays have very advance C++ compilers. Here is the simplest representation of Bang-Bang Control/On-Off Control algorithm for line following robot in pure C language.

#include <stdbool.h>

// Function to implement Bang-Bang Line Following Algorithm
int bangBangLineFollowing(bool sensorReading) {
    int robotMovement; // 0 for move forward, 1 for quick turn

    if (sensorReading) {
        robotMovement = 0; // Move forward
    } else {
        robotMovement = 1; // Quick turn
    }

    return robotMovement;
}

// Example usage of the function
int main() {
    bool sensorReading = true; // Assume line is detected
    int movement = bangBangLineFollowing(sensorReading);

    if (movement == 0) {
        // Move forward
        // Code for moving forward...
    } else {
        // Perform quick turn
        // Code for quick turn...
    }

    return 0;
}
Code language: PHP (php)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.