![Page 1: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/1.jpg)
ADVANCED EV3 PROGRAMMING LESSON
PID Line Follower
![Page 2: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/2.jpg)
ì 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 Control
We highly recommend knowledge of Algebra at a minimum. PID is a calculus-based concept and students should understand why it is used and the math behind it before using it.
Please use Presentation Mode as there are lots of animations.
© 2015 EV3Lessons.com, Last edit 02/10/2017 2
Lesson Objectives
![Page 3: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/3.jpg)
When does Proportional Control Have Trouble?
What would a human do?
On line à go straight
On white à turn left
Moving across line à turn right
On white à turn left
Getting further from line àturn even more!
What would proportional control do?
On line à go straight
On white à turn left
Moving across line à go straight!
On white à turn left
Getting further from line à turn left the same amount!
© 2019 EV3Lessons.com, Last edit 07/10/2019 3
LIGHT READING = 50%100%
![Page 4: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/4.jpg)
What would a human do?
What would proportional control do?
Turning left/on line à go straight!
Getting further from line à turn left the same amount!
© 2019 EV3Lessons.com, Last edit 07/10/2019 4
1. Predict what the next sensor reading will be
2. Has past steering fixes helped reduce error?
How can we fix Proportional Control?
Turning left/on line àturn right
Getting further from line à turn even more!
![Page 5: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/5.jpg)
Integrals and Derivatives
• 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 does error look like?• +5, +5, +6, +5… i.e. always on one side
of 0
• How can we detect this easily? • Hint: look at the sum of all past errors
• What is an ideal value for this sum? What does it mean if the sum is large?
• Integral è the “sum” of values
© 2019 EV3Lessons.com, Last edit 07/10/2019 5
1. Predict what the next sensor reading will be?
2. Have past steering fixes helped reduce error?
• If readings are: 75, 65, 55 à what do you think the next reading will be?• What if the readings
were 57, 56, 55…
• What information did you use to guess?
• Derivative è the rate at which a value is changing
![Page 6: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/6.jpg)
ì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 values to decide how to steer the robot
© 2019 EV3Lessons.com, Last edit 07/10/2019 6
What is PID?
![Page 7: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/7.jpg)
ì 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)
© 2019 EV3Lessons.com, Last edit 07/10/2019 7
Error
-200
20406080
0 10 20 30 40 50Time (sec)
Error
-200
20406080
0 10 20 30 40 50Time (sec)
Light Intensity
Subtract target (50)
![Page 8: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/8.jpg)
ì Looks at past history of line follower
ì Sum of past error
ì Like area under the curve in graph (integral)ì Green = positive areaì Red = negative area
© 2019 EV3Lessons.com, Last edit 07/10/2019 8
Integral
-50
0
50
0 10 20Erro
r
Time (sec)
0100200
0 10 20
Inte
gral
Time (sec)
![Page 9: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/9.jpg)
ì How quickly is position changing?ì Predicts where the robot will be
in the immediate futureì Same as how fast is error
changing
ì Can be measured using tangent line to measurements à derivativeì Approximated using two nearby
points on graph
© 2019 EV3Lessons.com, Last edit 07/10/2019 9
Derivative
-15
-5
5
15
10 20 30Erro
r
Time (sec)
Tangent line
-5
0
5
10
10 20 30De
rivat
ive
Time (sec)
![Page 10: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/10.jpg)
1. Take a new light sensor reading
2. 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
© 2019 EV3Lessons.com, Last edit 07/10/2019 10
Pseudocode
![Page 11: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/11.jpg)
ì This is the same as the proportional control code
© 2019 EV3Lessons.com, Last edit 07/10/2019 11
Code - Proportional
Error = distance from line = reading - targetCorrection (P_fix) = Error scaled by proportional constant (Kp) = 0.5
![Page 12: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/12.jpg)
ì This section calculates the integral. It adds the current error to a variable that has the sum of all the previous errors.
ì The scaling constant is usually small since Integral can be large
© 2019 EV3Lessons.com, Last edit 07/10/2019 12
Code - Integral
Integral = sum of all past errors = last integral + newest errorCorrection (I_fix) = Integral scaled by proportional constant (Ki) = 0.01
![Page 13: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/13.jpg)
ì This section of code calculates the derivative. It subtracts the current error from the past error to find the change in error.
© 2019 EV3Lessons.com, Last edit 07/10/2019 13
Code - Derivative
Derivative = rate of change of error= current error – last error
Correction (D_fix) = Derivative scaled by proportional constant (Kd) = 4.0
4
![Page 14: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/14.jpg)
ì Each of the components have already been scaled. At this point we can simply add them together.
© 2019 EV3Lessons.com, Last edit 07/10/2019 14
Putting it all Together
Add the three fixes for P, I, and D together. This will compute the final correction
Apply the correction the the steering of a move steering block
![Page 15: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/15.jpg)
ì This is what you get if you put all these parts together.
ì We hope you now understand how PID works a bit better.
© 2019 EV3Lessons.com, Last edit 07/10/2019 15
Full Code
Proportional Integral
Derivative Putting it all Together
Proportional Integral
Derivative Putting it all Together
![Page 16: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/16.jpg)
© 2019 EV3Lessons.com, Last edit 07/10/2019 16
Full Code
Proportional Integral
Derivative Putting it all Together
Set up the 5 variables before the loop and initialize to 0
Code has been split for readability. Continues below.
![Page 17: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/17.jpg)
ì 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 just
the proportional constant until robot follows the line well.ì Then, enable the integral and adjust until it provides good performance on a range of
lines.ì 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 the
constants:ì 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
© 2019 EV3Lessons.com, Last edit 07/10/2019 17
Key Step: Tuning The PID constants
![Page 18: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/18.jpg)
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, as the robot adapts to the curviness
ìHowever, for FIRST LEGO League, which mostly has straight lines, proportional control can be sufficient
© 2019 EV3Lessons.com, Last edit 07/10/2019 18
![Page 19: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/19.jpg)
© 2019 EV3Lessons.com, Last edit 07/10/2019 19
Proportional Control (0.6 Constant)
![Page 20: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/20.jpg)
© 2019 EV3Lessons.com, Last edit 07/10/2019 20
Proportional Control (0.8 Constant)
![Page 21: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/21.jpg)
© 2019 EV3Lessons.com, Last edit 07/10/2019 21
PID Control
![Page 22: PID Line Follower - EV3 Lessonsev3lessons.com/en/ProgrammingLessons/advanced/PID.pdfsteering of a move steering block ìThis is what you get if you put all these parts together. ìWe](https://reader030.vdocuments.site/reader030/viewer/2022041003/5ea5155f0be6904b96182832/html5/thumbnails/22.jpg)
ì This tutorial was created by Sanjay Seshan and Arvind Seshan ì More lessons at www.ev3lessons.com
© 2019 EV3Lessons.com, Last edit 07/10/2019 22
CREDITS
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.