Line following PID concept

The algorithm is the one thing that determines the performance of a line following robot more than anything else. The most basic algorithm , is the one which uses only one sensor. The sensor is placed in a position that is a little off centered to one of the sides, say right. When the sensor detects no line the robot moves to the left and when the sensor detects the line the robot moves to the right. A robot with this algorithm would follow the line like shown in the picture below

The drawback of this method is that the line following is not smooth. The robot keeps wavering left and right on the track, wasting battery power and time. A modification to this method is to add sensors on both sides of the robot and place them that they just sense the line on either side. And the algorithm would be to move forward if both the sensors sense the line or to move left if only the left sensor senses the line and move right if only the right sensor senses the line. A robot with this algorithm would follow the line like shown in the picture below

This algorithm is faster than the previous algorithm but the robot will still wobble about the line and may not be fast enough. A much better algorithm is to use the PID to follow the line. This will make line following process much smoother, faster and efficient.

PID stands for Proportional Integral and Derivative. It is a popular control loop feedback control extensively used in industrial controls systems. But why would one need a PID controller for a line following robot, when there are many simpler algorithms already available for line following. Lets us see…

A conventional robot would follow a line as shown below (red line shows the path taken by the robot)

As you can see clearly in the picture the robot oscillates a lot about the line, wasting valuable time and battery power. By now you must have realized that there is a maximum speed beyond which you cannot use this algorithm, other wise the robot will overshoot the line. A robot with PID control will follow the line as shown below

As you can see clearly the robot will follow the line smoothly keeping its centre always above the line. In straight lines, the robot will gradually stabiles and go straight unlike a robot with the left-right algorithm. This enables the robot to follow the line faster and more efficiently.

Before I tell you how to implement a PID algorithm for a line following robot, let me tell you the terms associated with it –

TargetIt is the position you want the line follower to always be(or try to be),that is, the center of the robot. Current Position – It is the current position of the robot with respect to the line.

ErrorIt is the difference between the current position and the target. It can be negative, positive or zero.

ProportionalIt tells us how far the robot is from the line like – to the right , to the extreme right, to the left or a little to the left. Proportional is the fundamental term used to calculate the other two.

IntegralIt gives the accumulated error over time. It tells us if the robot has been on the line in the last few moments or not.

DerivativeIt is the rate at which the robot oscillates to the left and right about the line.

Kp, Ki and Kd are the constants used to vary the effect of Proportional, Integral and Derivative terms respectively.

What the controller does is first calculate the current position. Then calculate the error based on the current position. It will then command the motors to take a hard turn, if the error is high or a small turn if the error is low. Basically, the magnitude of the turn taken will be proportional to the error. This is a result of the Proportional control. Even after this, if the error does not decrease or decreases slowly, the controller will then increase the magnitude of the turn further and further over time till the robot centers over the line. This is a result of the Integral control. In the process of centering over the line the robot may overshoot the target position and move to the other side of the line where the above process is followed again. Thus the robot may keep oscillating about the line in order to center over the line. To reduce the oscillating effect over time the Derivative control is used.

Implementing PID control for a line following robot

The first requirement in implementing PID for a line follower is calculating the error of the robot. To calculate the error we will need to know the current position of the robot with respect to the line. There are a number of ways of knowing this.

A simple approach would be to place two IR sensors on either side of the line. The IR sensors should be tuned to give an output voltage that is propotional to the distance between the line and the sensor. The output can then be connected to the ADC pin of a microcontroller and the error can be calculated.

Though this method may seem simple and easy to implement it has a few drawbacks. Firstly, a robot using this method will have problems following a line whose width is varying. Secondly, the output of these sensors will be highly susceptible to interference from external light. And lastly, for this method to work, you have to ensure that the track is completely flat. Any change in the distance between the sensor and the surface will affect the sensor readings.

A better approach would be to use the traditional sensor array. Using an array of sensors, you can easily calculate the error by knowing which sensor is on the line. Consider you have an array of 10 sensors each placed 1cm apart. When the 7th sensor from the left detects the line you can be sure that the centre of the robot is 2 cm to the right of the line. Using a sensor array the processor can calculate the error faster as there is no ADC required to be done and thus you can increase the sampling rate. You can also use the robot as a grid solver.

When building a sensor array, there are a few things to keep in mind. Firstly, the more sensors you have in an array, the better will be the performance of the robot, because more sensors translates to more range of error. For line following anything between 6 to 10 sensors (depending on your robot) will be sufficient. The distance between each adjacent sensor determines the resolution of the readings. It is always best to place the sensors as close to each other as possible because your resolution will increase as you place the sensors closer (a resolution of +/- 1cm is always better than a resolution of +/- 3 cm).

The next important thing in implementing PID after the sensors is the writing the code itself.

The algorithm for a PID control for line followers would be something like this :

Error = target_pos – current_pos      //calculate error
P = Error * Kp                                       //error times proportional constant gives
P I = I + Error                                          //integral stores the accumulated error
I = I * Ki                                                      //calculates the integral value
D = Error – Previos_error                  //stores change in error to derivate
Correction = P + I + D

The next step is to add this correction term to the left and right motor speed.
While calculating the derivative, the change in error can be divided by the time taken to get the rate of change of error.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: