0% found this document useful (0 votes)
86 views5 pages

PID For Line Following

This document discusses PID controllers for line following robots. It explains that PID uses proportional, integral, and derivative terms to more precisely control a robot's direction and speed to stay on course compared to simply turning left/right based on sensor readings. It provides details on calculating the proportional, integral, and derivative values and combining them to generate an output value to control the robot. Tuning the P, I, and D factors is also discussed to match the controller to the robot's characteristics.

Uploaded by

Santii Romero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views5 pages

PID For Line Following

This document discusses PID controllers for line following robots. It explains that PID uses proportional, integral, and derivative terms to more precisely control a robot's direction and speed to stay on course compared to simply turning left/right based on sensor readings. It provides details on calculating the proportional, integral, and derivative values and combining them to generate an output value to control the robot. Tuning the P, I, and D factors is also discussed to match the controller to the robot's characteristics.

Uploaded by

Santii Romero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PIDforLineFollowing

Atslowerspeeds,linefollowing ispretty simple ifthesensorssayitisgoingleft,steer


right and if going right, steer left. Thisprocesshasitslimitationsthough,mainly when
thespeedisincreased.ThisiswhenaPIDcontrollerstartstoshine.
PIDstands forProportional,Integral and Derivative. APIDcontroller is a
mathematics based procedure that processes sensor data and uses it to control the
direction(and/or speed)ofarobot tokeepitoncourse.WhydoesPIDworkbetterthan
our simple model described above? Let's talk about how robot acts (or behaves) as it
followsalinetoseewhy.
Behaviourofalinefollower

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)

Therangeofpossiblevalues forthemeasuredpositionis 5to5.We willmeasurethe


position of the robot over the line several times a second and use these value to
determineProportional,IntegralandDerivativevalues.
PIDFormula

PID is aseries of mathematical calculations. We determine Proportional, Integral and


Derivativethenadd themtogethertocomeupwithavalueusedtocontroltherobot.Kp,
KiandKdareusedtotunethePIDcontroller.
Proportional

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.

You might also like