Modified PID Algorithm Implementation
Hey, Welcome to the second Tutorial of Line following Robot. As In the previous Tutorial we have just covered the basics and important things for making Line following Robot.As In the previous tutorial I have just explained the best ways to make a line follower instead of choosing wrong components and wasting your time in extra activities we have to make it to the best effective and simplest. So always choose those things that are less time consuming but Effective. So before moving towards the next must Read the previous Tutorial about Line follower. I’m not going to explain the components of the basic concepts for line following.First Read this Tutorial:
ARDUINO SMART AND FAST LINE FOLLOWING ROBOT
Well in this Tutorial we will discuss the PID concept for Line following ROBOT.And I named it the modified PID algorithm.Before this intention, I was planning to write a tutorial about complete and complex line following Robots.But now I’m not going to do this.I have spent some hours surfing the internet about the PID line following Robots.And What I found is that there are already too many guides not just guides but a complex PID guides available for making Arduino code.They are not easy to modify according to the robot requirement.
Recently I have won the First prize in line following Robot competition.Later I will upload the video of the event in coming posts. My Robot Wins the Final Round and the amazing part is that I haven’t used any complex code. I just modify some points and skipped the complex part.Now my intentions for this tutorial is that I will only explain what I experienced and what is simple and effective.So we will just see the PID concept and adopt the best part in that for Line Follower.
So here are the contents:
Contents
 Necessary things for making line following Robot
 Introduction PID algorithm
 How to implement PID algorithm
 PID Algorithm Parameters
 Modify PID for line following Robot
1Necessary things for making line following Robot
As I have already explained how to make a smart line following Robot so we will not discuss each and everything again.And We have discussed the components list, What sensor do we need to use for smart and fast line following robot and How motor and Hbridges work.Read these previous tutorials for Component selection and how the code and sensor work in the line following Robot case.
MOTOR DRIVERS AND DUAL HBRIDGE L298 ARDUINO MODULES INTERFACING
ARDUINO SMART AND FAST LINE FOLLOWING ROBOT
I have recommended IR modules sensor for making a line following Robots.The QTR Pololu sensor array will work perfectly good but again I will recommend the separate modules sensor for making line followers.By this, We have more choices according to line width when we use separate sensors.But if you feel more comfortable with QTR sensor array you can use it.The sensor array is expensive but the results will be good.
2Introduction to PID algorithm
The Proportional Response:
The proportional component in PID algorithm is the difference between the set point of the system and the process variable. Now, this difference is called Error. The proportional gain (Kp) is the ratio between output response and the error signal.
The Integral Response
Integral in PID is the sum of the error term over the time. If the result is a small error the integral component will increase slowly. Now the integral term will continually increase over the time unless and until the error is going to be zero, so the ultimate effect is driving the SteadyState error towards zero. The difference between the process variable and the set point is the Steady state error.
The Derivative Response:
The function of the derivative component is to decrease the output of the process variable is going to increase. The derivative is the rate of change of the process variable. The Increase in the derivative time (Td) will make the system to react strongly and to make changes in the error term.This will increase the overall speed. The very small (Td) the derivative time is used in most practical systems.Derivative Response is sensitive and highly sensitive to the noise and in the process variable. If the feedback is noisy or the control loop rate is very much slower, The control system will be unstable due to derivative response.
3PID Control Algorithm for Line Following Robot
 In a case when robot is deviated to left it then pushes to move towards right
 In a case of deviation towards right, it is forced to move towards left
The accomplishment of this task gives a robot to follow the zigzag path.By this method, the speed we were giving was much less as error getting by using PID. And this technique was also not reliable for the turns. And To apply this algorithm it was better to use the error calculation method after calculating error giving motor it’s proportional value and then it changes speed and in turn, it gets back to the line.
e.g.the deviation ranges between 3 to +3.
 It affects the accuracy of the robot when the robot is not much effective it gets back to mean position very slowly.
 To solve this problem we have to increase its proportionality contact but it will then overshoot when the robot comes to mean position due to inertiacross to another side then again overshoot results in deviating result.
4PID Algorithm Parameters
 : error
 : time of deviation
 : overshoots
The efficiency of the PID algorithm is totally based on these three parameters these helps to reach at a mean position much faster.
These three constants have been set by hit and trial method experimentally.

 Kp
 Ki
 Kd
5Modify PID for Line Following Robot


Define your sensors in an array:

const int irPins[5] = {A1, A2, A3, A4 ,A5};
2. Further, define your sensors as a binary function:
int irSensors = B00000;
3. Define
int errorLast = 0; errorLast = error;
This is the point where an array stored the last error value and when ever the robot leaves the line it then comes back to the line by last stored value.
The last point is that use the error calculating equation to control motor speeds;
if (correction >= 0) {
motorRSpeed = maxSpeed – correction;
motorLSpeed = maxSpeed;
else if (correction < 0) {
motorRSpeed = maxSpeed;
motorLSpeed = maxSpeed + correction;
I know I just give some code snippets that will be really difficult to understand the whole concept.
See the Equation:
Use this equation to control the speed of motors.Well here is just the PID understanding.
Read this complete post about PID that I have written for PIC controller.See the post the code will help you to understand more about the concept.
in this calculation
if (correction >= 0) {
motorRSpeed = maxSpeed – correction;
motorLSpeed = maxSpeed;
else if (correction < 0) {
motorRSpeed = maxSpeed;
motorLSpeed = maxSpeed + correction;
why motorleft use +correction and motor right use correction, can u expalin it?
thanks and good post