Modified PID Algorithm Implementation
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
Step: 01Necessary 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
Step: 02Introduction to PID algorithm
The Proportional Integral and Derivative (PID) controller is the control algorithm and mostly used in industrial control. PID algorithm allows engineers to operate them in a simple and straightforward way. The name shows PID, The three words Proportional, Integral, and Derivative.
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.
Step: 03PID 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.
Step: 04PID 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.

 Kp
 Ki
 Kd
Kp: Using only kp parameter the robot will be applied and give the results as the classical Line following algorithm.
Step: 05Modify 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.
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.