2. Intro..
Line followers are autonomous robots that follow a line.
They might follow a visual line painted or embedded in
the floor or ceiling or an electrical wire in the floor. Most
of these robots operates on some specific algorithms.
This tutorial guides you through a common control
system algorithm used in the industry, PID control and
how it can be implemented in a line follower system.
4. The Basics..
All basic control systems rely on feedback.. feedback of
the system output, to know what state the system is in at
that instant and adapt the system by changing its
parameters so that the system reaches the desired
output.. We call that a SETPOINT.. (S.P)
To attain the S.P we change certain elements in the
system, these elements (variables) are called
Manipulated Variables (M.V)
5. So on coming to line followers our goal is to keep the bot
on the track at all times, ideally the centre of the bot on
the centre of the line at all times.. For the time being let
this be our S.P... To achieve that we need to change the
way out bot moves, i.e we control the motors.. our M.V..
Fore more on Closed loop control basics visit [ http://hsconntrol.blogspot.in/2011/03/control-loop-basics.html ]
6. What is PID control then...
The PID control scheme is named after its three correcting
terms, whose sum constitutes the manipulated variable (MV).
The proportional, integral, and derivative terms are summed to
calculate the output of the PID controller.
7. Kp: Proportional gain
Ki: Integral gain
Kd: Derivative gain
e(t)=SP-PV : Error
t : Time or instantaneous time
(the present)
Now this dosent make much sence does it !!
8. So what are these Kp,Ki n Kd..
In simple terms, the proportional term changes the system
proportional to the error, more the error more the controller output.
Eg: if the bot is at the right extreme sensor* on the line(has to take a
right turn), the left motor is given more power to get back into the
centre of the line.
The integral term is the summation of previous errors, so it looks into
the past of the system and compensates accordingly..
The differential term does something like predicting what the future
error will be, it is not an absolute measure of the future error though..
*Assuming the bot has a odd sensor array.. 5 or 7 sensors or more..
9. The Algorithm..
1. Initialize the set point S.P
2. Read sensor data P.V
3. Calculate the error e(t)=SP-PV
4. Calculate the output of the PID controller u(t).
5. Apply controller output to the actuators(motors).
6. Go back to step 2.
10. So lets define our variables…
Our aim is to keep the bot on the centre of the line,
for feedback we have sensors*; i.e. we need to keep
the centre sensor on the centre of the line..
That’s ok, but how can we convert 5/7 or 2n+1
sensor inputs ot a single variable r(t)..
That’s where simple mathematics and number
system comes into use..
11. The conversion..
Lets assume we have 5 sensors..
Im assigning weights to each sensor
like in the figure above
Centre has 0 weight, left most has -2
etc..
So when we take the input r(t) it will be
k(t)=(-2)*r2+(-1)*r1+(0)*c+(1)*l1+(2)*l2 .
Generalizing..
-2 -1 0 +1 +2
r2 r1 c l1 l2
12. Lets make this unique..
i’m assuming that sensors on
the line return 0 and off the
line return 1.
Now that we have k(t), we will
divide this by the number of
sensors that return 1.. Here we
have 4 sensors that return 1..
So the sum is 4.
13. Lets build on this a little more….
Now that we have a single variable for the input, as a
number we can calculate what the set point S.P will be.
So we have
So our S.P is zero.. That makes the rest of the math easy
to understand.
14. Consider the bot on a 90’ right turn
So in this case we have Si as [1 1 0 0 0].
Calculate r(t)
So its negative 3/2..
1 1 0 0 0
15. Consider the bot on a 90’ left turn
This will be easy now..
Its positive..
So for all left turns we get positive
inputs, right turns we get negative inputs
and if its on the centre our input is zero.
That’s basically calssifying
mathematically all our conditions.
0 0 0 1 1
16. Building…
Initialize SP,e_int,e_diff
We read Sensors Si;
Compute r(t)
Error e(t)= SP- r(t)
Find e_int,e_diff
Find u(t)
Update e_diff
Read sensors again
SP=0;
e_int,e_diff=0;
A:
Si=read();
r=compute();
e=SP-r;
e_int +=e;
e_diff=e-e_prev;
u=Kp*e+Ki*e_int+Kd*e_diff
e_prev=e;
Goto A:
Read() is the read sensor function, returning 1 if sensor is not on the
line and 0 otherwise.
Compute() finds the value of r(t) using the formula.
17. Also..
The weight initialization Wk can also be other sequences,
like 0,1,2,3,4.. Accordingly you have to modify the error
control statement..
the set point for 0,1,2,3,4 weight system will be 2.. Values
less than 2 correspond to left turn and greater than 2
correspond to right turn..
18. Putting it all together
Now that we have the controller output u(t), we need to assign it to
the actuators, or the motors in our case..
Using PWM signals we can vary the speed of the motors. Your
signals to the motors correspond to the output u(t), but we have two
motors..
So we convert this signal u(t) to 4 signals..
right1 and right2 connected to +ve and –ve of the right motor.
left1 and left2 connected to +ve and –ve of the left motor.
19. How we do it…
if (u < 0)
{
left1 = 100% + u%;
left2 = 0% – u%;
right1 = 100%;
right2 = 0%;
}
else
{
right1 = 100% -u%;
right2 = u%;
left1 = 100%;
left2 = 0%;
}
• Following the same analogy, we have
negative output for left turn and positive
error for right turn.
• u will vary from +k to –k, where k is a
constant that can be calculated.
• Where 100% is 100 percent output (5V),
u% is the controller output in percentage(its
twice that actually).. Ie if controller output
ranges from -3 to +3, u% is (u/3)*100..
• Going through the math you find that if u
is 100% negative, the bot will turn using
differential steering.
20. How to select Kp,Ki and Kd
Selection of these parameters is called tuning. There are various
mathematical methods like Z-N method and C-N method, but that
requires the mathematical model of the plant. So we move to a practical
approach.
First we set Kp= a constant and set Ki and Kd to zero, which is
turning off differential and integral actions.
The bot will be oscillating(sweeping left and right on the line) along
the line.
Increase Ki till the oscillations subside and bot travels smoothly(small
oscillations will be there, one or two sweeps).
Put Kd as 10% of Ki or a very small value.
21. Practical experiences on using this scheme..
Though PID control is a very efficient control system logic, using a 5 sensor
array provides very small data for enough variations and hence you might
find the logic inadequate for sharp acute turns.
The system however becomes an excellent constructor helping the user
map all the 2^N conditions for an N sensor robot using equations rather than
having to use Boolean algebra to map all the conditions.
Hence using this scheme means that there will not be any unmapped
condition so that your uC gets locked in a loop.
Extremely effective and useful if using more number of sensors.