PID For Line Following
PID For Line Following
Let's say our robot has 3 sensors Left, Centre and Right. When the Centre
sensor sees the line, the robot is programmed to go straight. When the Left
sensor sees the line, the robot is programmed to turn right. When the Right
sensor sees the line, the robot is programmed to turn left. This will typically
cause the robot thewobblebackandforthovertheline andif goingtoofast,it
may lose control and stop following the line (the red line in the picture to the
right)atall.
Thismethodonlytakesonefactorintoconsiderationistherobotcenteredover
the line. To improve performance, we should also take into consideration 2
morefactorshowrapidlyistherobotmovingfromsidetosideandhowlongit
is not centered over the line. These 3 behaviours are called Proportional, Integral and
DerivativeintermsofaPIDcontroller.
TodiscussPIDhereisthedefinitionofsometermscommonlyused:
TargetPositionForlinefollowing,thisiscenteredovertheline.Wewillrepresentthis
asthevalue0(zero).
Measured Position Thisishowfarleft orrightrobotisfromtheline.Thisvaluewill
beanegativeorpositivevaluetorepresenttherelativepositiontotheline.
ErrorThedifferencebetweenthetargetpositionandthemeasuredpositiontheError.
Proportional Measures how far your robot is away from theline.Themoregranular
thesensordatais,themoreaccuratelyyoucanmeasuretherobotspositionovertheline.
Integral Measures the accumulatedErrorover time. The Integral value increases
whilethe robotisnotcenteredovertheline.Thelongertherobotisnotcenteredoverthe
line,thehighertheIntegralvaluebecomes.
Derivative Measurestherateatwhichthe robot ismovinglefttorightorrighttoleft.
Thefastertherobotmovessidetoside,thehighertheDerivativevalueis.
PFactor(Kp)AconstantvalueusedtoincreaseordecreasetheimpactofProportional
IFactor(Ki)AconstantvalueusedtoincreaseordecreasetheimpactofIntegral
DFactor (Kd) A constant value used to increase or decrease the impact of
Derivative
Bycombining Proportional,IntegralandDerivativevalues,wecancontrolthemotion
of ourrobotmoreprecisely. Theidealbehaviouris representedbytheredlineinthe
image to the right. The robot's wavy motion is minimized and the robot stays more
centeredoverthelinethanitdidbefore.
The overall performance of PID with your robot will depend on the number and
precisionofthesensorsusedandthecapabilitiesofthemicrocontrolleryouareusing.
TheHardware
Forthisexamplewewillbeusing5sensorsfordetectingawhitelineonablacksurface.
Thesensors areusedasadigitalinputof0(noline)or1(line).Thesensorsaresoplaced
as shown in the fig above to increase the precision of the sensor array. The sensors
values are read in and converted to a binary value to help visualize the line position
underthesensors.
00100
Robotiscenteredovertheline
10000
Robotistotherightoftheline
00001
Robotistotheleftoftheline
Thefullrangeofweightedvaluesisshownbelow.Weassignanumericalvaluetoeach
one.
BINARYVALUE
WEIGHTEDVALUES
00001
00011
00010
00110
00100
01100
01000
11000
10000
00000
5or5(dependingon
previousvalues)
Proportional=Kp*(Difference)
Difference=(TargetPosition)(MeasuredPosition)
Integral
Integral=Ki*(Integral)
SinceIntegralstorestheaccumulatedDifferencevalue,therefore
Integral=Integral+Difference
Derivative
Derivative=Kd*(RateofChange)
RateofChange=(Difference)(PreviousDifference)
Derivativeissometimesdividedbytheintervalortimebetweenmeasurements
Therefore,Controlvalueusedtoadjusttherobot'smotion=
(Proportional)+(Integral)+(Derivative)
ThatrepresentsthemathofPID butthereal secretofitsusefulnessis intuningthePID
controllertomatchthephysicalcharacteristicsofyourrobot.
PSEUDOCODE
HereisasimpleloopthatimplementsthePIDcontrol:
previous_error=(target_position)(theoretical_position)
start:
error=(target_position)(theoretical_position)
integral=integral+(error*dt)
derivative=((error)(previous_error))/dt
output=(Kp*error)+(Ki*integral)+(Kd*derivative)
previous_error=error
wait(dt)
gotostart
TuningPID
Once you have PID running in your robot, you will probably notice thatitstilldoesn't
follow the line properly. It may even perform worsethanit didwithjustproportional!
Thereasonbehindthisisyou haven'ttunedthePIDroutineyet.PIDrequirestheKp,Ki
and Kd factors to be set tomatch yourrobot'scharacteristicsandthesevalueswill vary
considerably from robot to robot. Unfortunately, there is no easy way to tune PID. It
requiresmanualtrialanderroruntilyougetthedesiredbehaviour.Therearesomebasic
guidelinesthatwillhelpreducethetuningeffort.
1.
2.
3.
4.
StartwithKp,KiandKdequalling0andworkwithKpfirst.TrysettingKptoavalueof1
and observe the robot. The goal is to get the robot to follow the line even if it is very
wobbly. If the robot overshoots and loses the line, reduce the Kp value. If the robot
cannotnavigateaturnorseemssluggish,increasetheKpvalue.
Oncetherobot isabletosomewhatfollowthe line,assignavalueof1toKd(skipKifor
themoment).Tryincreasingthisvalueuntilyouseelesseramountofwobbling.
Oncetherobot isfairlystableatfollowingthe line,assignavalueof 0.5to1.0toKi.If
the Ki value is too high, the robot will jerk left and right quickly. If it is too low, you
won't see any perceivable difference. Since Integral is cumulative, the Ki value has a
significantimpact.Youmayendupadjustingitby.01increments.
Oncetherobot isfollowingthelinewith good accuracy,youcanincreasethespeedand
see if it still is able tofollowtheline.Speedaffectsthe PIDcontrollerandwillrequire
retuningasthespeedchanges.
Lastly, please keep in mind that you do not need to implement all three controllers
(proportional, derivative, and integral) into a single system, if not necessary. For
example, if a PI controller gives a good enough response, then you don't need to
implementderivativecontrollertothesystem.Keepthecontrollerassimpleaspossible.