ADVANCED EV3 PROGRAMMING LESSON EV3 Classroom: PID Line Follower

Transcription

ADVANCED EV3PROGRAMMING LESSONEV3 Classroom:PID Line Follower

Lesson Objectivesì Learn the limitations of proportional controlì Learn what PID meansì Learn how to program PID and how to tuneì Prerequisites: Math Blocks, Color Sensor Calibration, Data Wires,Variables, Proportional ControlWe highly recommend knowledge of Algebra at a minimum. PID is a calculus-based concept and students shouldunderstand why it is used and the math behind it before using it.Please use Presentation Mode as there are lots of animations. 2020 EV3Lessons.com, Last edit 12/27/2019

When does Proportional Control HaveTrouble?What would ahuman do?What would proportionalcontrol do?On line à go straightOn line à go straightOn white à turn leftOn white à turn leftMoving across line à turn rightOn white à turn leftGetting further from line àturn even more!Moving across line à go straight!On white à turn leftGetting further from line à turnleft the same amount!LIGHT READING 2020 EV3Lessons.com, Last edit 12/27/201950%100%

How can we fix Proportional Control?What would ahuman do?What would proportionalcontrol do?Turning left/on line àturn rightTurning left/on line à gostraight!Getting further from lineà turn even more!Getting further from lineà turn left the sameamount!1. Predict what the nextsensor reading will be2. Has past steering fixeshelped reduce error? 2020 EV3Lessons.com, Last edit 12/27/2019

Integrals and Derivatives1. Predict what the next sensorreading will be? If readings are: 75, 65, 55à what do you think thenext reading will be? What if the readingswere 57, 56, 55 What information did youuse to guess? Derivative è the rate atwhich a value is changing2. Have past steering fixeshelped reduce error? When the correction is working well,what does error readings look like? 5, -6, 4 -3 . i.e. bouncing around 0 When steering is not working, what doeserror look like? 5, 5, 6, 5 i.e. always on one sideof 0 How can we detect this easily? Hint: look at the sum of all past errors What is an ideal value for this sum? Whatdoes it mean if the sum is large? Integral è the “sum” of values 2020 EV3Lessons.com, Last edit 12/27/2019

What is PID?ì Proportional [Error] à How bad is the situation now?ì Integral à Have my past fixes helped fix things?ì Derivative à How is the situation changing?ì PID control à combine the error, integral and derivative valuesto decide how to steer the robot 2020 EV3Lessons.com, Last edit 12/27/2019

Errorì Solid line represents what you have seen, dotted line is the futureì At time 20, you see light reading 40 and error -10 (red X)Light IntensityError806040200-20Subtracttarget (50)0102030Time (sec) 2020 EV3Lessons.com, Last edit 12/27/20194050806040200-200102030Time (sec)4050

Integralfollower50Errorì Looks at past history of line0-50 0ì Like area under the curve200in graph (integral)ì Green positive areaì Red negative area 2020 EV3Lessons.com, Last edit 12/27/2019Integralì Sum of past error1020Time (sec)1000010Time (sec)20

Derivativeì How quickly is position changing?in the immediate futureì Same as how fast is errorchangingErrorì Predicts where the robot will be1530Time (sec)Derivative50-5 2020 EV3Lessons.com, Last edit 12/27/20192010to measurements à derivativepoints on graph-5 10-15ì Can be measured using tangent lineì Approximated using two nearbyTangent line51020Time (sec)30

Pseudocode1. Take a new light sensor reading2. Compute the “error”3. Scale error to determine contribution to steering update (proportional control)4. Use error to update integral (sum of all past errors)5. Scale integral to determine contribution to steering update (integral control)6. Use error to update derivative (difference from last error)7. Scale derivative to determine contribution to steering update (derivative control)8. Combine P, I, and D feedback and steer robot 2020 EV3Lessons.com, Last edit 12/27/2019

Code - Proportionalì This is the same as the proportional control codeError distance from line reading - targetCorrection (P fix) Error scaled by proportional constant (Kp) 0.5 2020 EV3Lessons.com, Last edit 12/27/2019

Code - Integralì This section calculates the integral. It adds the current error toa variable that has the sum of all the previous errors.ì The scaling constant is usually small since Integral can be largeIntegral sum of all past errors last integral newest errorCorrection (I fix) Integral scaled by proportional constant (Ki) 0.01 2020 EV3Lessons.com, Last edit 12/27/2019

Code - Derivativeì This section of code calculates the derivative. It subtracts the currenterror from the past error to find the change in error.Derivative rate of change of error current error – last errorCorrection (D fix) Derivative scaled by proportional constant (Kd) 4.0 2020 EV3Lessons.com, Last edit 12/27/2019

Putting it all Togetherì Each of the components have already been scaled. At thispoint we can simply add them together.Apply the correction the the steering of a move steering blockAdd the three fixes for P, I, and D together. This will compute the final correction 2020 EV3Lessons.com, Last edit 12/27/2019

Full Codeì This is what you get if you put all these parts together.ì We hope you now understand how PID works a bit better.ProportionalIntegralDerivativePutting it all Together 2020 EV3Lessons.com, Last edit 12/27/2019

Full CodeSet up the variablesfor the last error andintegral before theloop and initialize to 0because they are readbefore being written.ProportionalIntegralDerivativePutting it all Together 2020 EV3Lessons.com, Last edit 12/27/2019

Key Step: Tuning The PID constantsì The most common way to tune your PID constants is trial and error.ì This can take time. Here are some tips:ì Disable everything but the proportional part (set the other constants to zero). Adjust justthe proportional constant until robot follows the line well.ì Then, enable the integral and adjust until it provides good performance on a range oflines.ì Finally, enable the derivative and adjust until you are satisfied with the line following.ì When enabling each segment, here are some good numbers to start with for theconstants:ì P: 1.0 adjust by 0.5 initially and 0.1 for fine tuningì I: 0.05 adjust by 0.01 initial and 0.005 for fine tuningì D: 1.0 adjust by 0.5 initially and 0.1 for fine tuning 2020 EV3Lessons.com, Last edit 12/27/2019

Evaluating Line followersProportionalì Uses the “P” in PIDì Makes proportional turnsì Works well on both straightand curved linesì Good for intermediate toadvanced teams à need toknow math blocks and datawires 2020 EV3Lessons.com, Last edit 12/27/2019PIDì It is better than proportionalcontrol on a very curved line,as the robot adapts to thecurvinessì However, for FIRST LEGOLeague, which mostly hasstraight lines, proportionalcontrol can be sufficient

Proportional Control (0.6 Constant) 2020 EV3Lessons.com, Last edit 12/27/2019

Proportional Control (0.8 Constant) 2020 EV3Lessons.com, Last edit 12/27/2019

PID Control 2020 EV3Lessons.com, Last edit 12/27/2019

CREDITSì This tutorial was created by Sanjay Seshan and Arvind Seshanì More lessons at www.ev3lessons.comThis work is licensed under a Creative Commons AttributionNonCommercial-ShareAlike 4.0 International License. 2020 EV3Lessons.com, Last edit 12/27/2019

Evaluating Line followers Proportional ìUses the "P" in PID ìMakes proportional turns ìWorks well on both straight and curved lines ìGood for intermediate to advanced teams àneed to know math blocks and data wires PID ìIt is better than proportional control on a very curved line,