Intro: Teaching PID Control With Lego Robots
Many young robot enthusiasts are interested in more advanced control topics, but may be stymied by the calculus often required to analyze closed loop systems. There are wonderful resources available online which simplify the construction of a "Proportional Integral Differential Controller" (PID Controller), and one great description is here: http://www.inpharmix.com/jps/PID_Controller_For_Lego_Mindstorms_Robots.html
Nevertheless, these can be difficult to follow, and might not be suitable for a classroom of 20 or so students.
This step by step Instructable shows how to successfully teach a room full of students using the Lego robot system, a number of robots (5 to 10 of them), an equal number of computer workstations running NXT 2.0, and a seven foot track of black electrical tape on the floor.
ASIDE: Thanks go to J. Sluka, who wrote the above link, Dr. Bruce Linnell, who created some early Lego labs at the ECPI University level, and Dr. Reza Jafari, who provided PID Control learning objectives mapping ideas towards the EET220 and Capstone coursework.
Step 1: Laboratory Prep Before Students Arrive
Let the students know about your hard work ;-)
Instructors and teaching assistants have been VERY busy getting ready for you to do this lab! The robot has been charged and assembled for this lab. If assembly is needed, this may take up to 90 minutes of time for one or more robots. Even more time is needed to charge the batteries or condition them with charge/discharge cycles. For detailed instructions on how to build the robot we will use today, see the NXT 2.0 or 2.1 Educational kit, the “follow a line” robot Building Guide. We will use more complex programming though… Black electrical tape on light colored linoleum makes a great track. This one is 3’ x 7’ with semi-circular curves.
Step 2: Familiarize Yourself With the Robot
First, you’ll be familiarizing yourself with the robot menu, as well as some of the parts of this particular robot. You will also be learning about the industrial style sensor technology used by the robot, including light emitting diodes, light sensors, stepper motors, and rotational position sensors. Please be sure to fill in all of the requested information (usually underlined blanks _________________).
1. Disconnect the robot from the charger and/or the USB port of your PC. Use the Orange button to turn on the robot. The orange, left and right buttons, and “back” grey rectangle button allow menu navigation. Navigate to the “Software Files” menu, and scroll through the available software files on the robot. List the names of each software file, exactly how it is spelled, including capitalization and spaces:
Step 3: Calibrate the Light Sensor
2 Examine the light sensor and calibration information. Back up to the main menu and select “View.” Select the “Reflected Light” option and the Port (it should be “Port 3”) which causes the light to turn on and a number to be displayed on the screen. Make sure everything works, and record some calibration information.
a. Maximum reading using a white sheet of paper: Number:____________________ Describe the approximate distance from the paper: ______________________________________________
b. Maximum Reading when on the light colored linoleum floor: __________________________
c. Minimum Reading when pointing to the center of the black electrical tape: _______________________
Step 4: Test Motor Calibration
3 Examine the wheel motors (left and right) as well as calibration information. Back up to the main menu and select “Motor Rotations” Select the Port (it should be either “Port B” or “Port C” for the two motors). See if you can check the calibration of this reading by turning each motor a fixed number of rotations while viewing the readout. You will do the same calibration test for both motors using the “View” à “Motor Degrees” calibration screen.
Motor on Port B
- Number of times you rotated the wheel _____
- “Motor Rotations” display value_____
- Distance in degrees wheel was rotated_____
- “Motor Degrees” display value_____
Motor on Port C
- Number of times you rotated the wheel _____
- “Motor Rotations” display value_____
- Distance in degrees wheel was rotated_____
- “Motor Degrees” display value_____
Did the display values agree with your expectations? Please explain. __________________________________________
Step 5: Run the Provided On-Off Controller
An "On-Off" (sometimes called "Bang-Bang") controller has only two options, on and off. It is similar to the thermostat control in your home. When set to a chosen temperature, the thermostat will heat the house if it's too cold, and cool the house if its too hot.The chosen temperature is called the "Set Point" and the difference between the current house temperature and the Set-Point is called the "Error." So you could say, if the Error is Positive, turn on the AC otherwise turn on the heat.
In our case, the robot will turn Left or Right, depending on whether the Set-Point of the light sensor has a positive or negative error (too much on the white floor, or too much on the black tape).
You will notice that your robot may already be loaded with a number of programs (or you can use the attached "01 line.rbt" file embedded here) stored on it with names like “1 line” and “2 line” and there also may be an additional letter after the number of the program, such as “3b line.” You will need to execute the program with the number “1” in its name and then place the robot on the tape track, with the senor on the black line. Try to keep away from other robots already on the track so that you can time your robot without interruption of bumping into other robots.
4 Measure the following time trials:
a. Time to complete one straight side of the track: _____________________________
b. Describe straight track robot motion: ____________________________
c. Time to complete one curve of the track: ______________________________
d. Describe curved track robot motion: _________________________________
e. Time to go entirely around the track once: _________________________________
Step 6: Open the "01 Line" On-Off Controller Software
You will open the “LEGO MINDSTORMS NXT 2.0” software (not the Edu 2.1 software) and you’ll load the appropriate program called "01 line.rbt" and examine and modify the software, following the instructions below:
Open the “LEGO MINDSTORMS NXT 2.0” software (not the Edu 2.1 software). Your instructor will tell you where the files are stored on your computer, and from that location you will open the “1 line” program. Simply select “File” then “Open” and choose the “1 line” program to open.
Once the program is open, you can use the “hand” icon to move the entire screen image of the program around, and you can use the “arrow” icon to click on individual objects to see how they work (and also make changes).
Step 7: Understanding the "01 Line" On-Off Controller Software
The program “1 line” uses an “On-Off” method of control. In this case, the choices are either “Turn Left” or “Turn Right”. The graphic contains a description of the program elements:
Step 8: Editing the "01 Line" On-Off Controller Software
Change the Set-point and compare results.
You discovered some real-world values of the light meter in step 2 above. You recorded values in parts b and c, numbers for the minimum and maximum values the robot would see when running the track.
5 Calculate a GOOD set-point value (the average of the min and max): ______________________
6 Choose a BAD set-pint value (a number very close to the min or the max): __________________
Change the set-point to one of these values by using the arrow icon to click on the error calculation box, and changing the number that is being subtracted (see image below). Now connect the robot to the PC using the USB cord, make sure the robot is turned on, and download the new version of the “1 line” program to the robot. You will be seeing how long the robot takes to go around the track in a clockwise direction, once with the GOOD set-point, and once with the BAD set-point.
7 Complete time trials with GOOD and BAD set-point values
a. Time to go entirely around the track once (GOOD Set-Point): _________________________________
b. Time to go entirely around the track once (BAD Set-Point): _________________________________
Your Observations / Conclusions? _______________________________________________________
Step 9: Understanding the "02 Line" On-Off With Dead-Zone Controller Software
If the AC and the Heat in your home kept turning on and off all day, it could definitely destroy your HVAC system (or at least shorten its life). Most thermostats are made with a built in "dead-zone." For example, if your set-point is 70 degrees Fahrenheit, the thermostat might not turn on the AC until it reached 72 degrees, nor would it turn on the heat until the temperature went down to 68 degrees. If the dead-zone gets too wide, the house may become uncomfortable.
In our case, we will use the 02 line program to add a dead-zone, during which the robot will simply drive straight.
Now Examine the Software File “02 line” as described in the graphic and as contained in the attached file.
This software file programs the robot to follow the line using On-Off control with a Differential Gap. This is also known as a Deadband and it means that the robot will turn left or right depending on the error, but if the error is small, the robot will just go straight.
The program “02 line” calculates the above first by subtracting the set point from the Light Measurement, and then making the comparisons as noted above. Examine the program on the PC and record the values you see.
What is the “2 line” programs current (original) value of the Set-Point? _____________________________
What is the “2 line” programs current (original) value “Large” Positive Error? ____________________________
What is the “2 line” programs current (original) value “Large” Negative Error? ____________________________
What Dead-Band error range will cause the robot to go straight? FROM __________ TO __________
Run three (3) time trials with different values for the “Large” error above. The current “2 line” settings as well as two other settings you will calculate. You have already chosen a GOOD Set-Point for your robot. Now you will choose two different Dead-Band ranges and record the time it takes the robot to make one clockwise lap:
Original settings for 02 line _____________
Dead-Band of +4 to -4 ______________
Dead-Band of +12 to -12 ______________
Step 10: Understanding the "03 Line" Proportional Controller Software
With proportional control, we don't just turn the heat on or off, we might have several settings for how much to turn up the furnace (like the size of the flames on a stove top). In the case of the robot, we don't just have three motor settings (left, right, and straight). Instead we can control the speed of the left and right wheels to get a wide variety of turning rates. The bigger the error, the quicker we want to turn back to the line.
Let’s look at Proportional Control with program “03 line”
The program for “03 line” is more complicated because it not only sets up the “Proportional” control method, but it also contains all the software to do Proportional-Integral, Proportional-Differential, and Proportional-Integral-Differential (PID) controls. When you load the software it will probably be too large to fit on the screen all at once, but it really has three parts, as shown in the attached graphic.
A – The math to calculate the error and the “calculus” to find the integral and derivative of error over time.
B – The math to calculate the left motor speed based on PID control settings of Kp, Ki, and Kd
C – The math to test motor speed limits, and send the correct motor speeds to the left and right motors.
All three of these run their own infinite loops (after initialization) and you can browse around by using the “hand” icon, but switch back to the “arrow” icon to examine box contents and change settings.
Step 11: Editing the 03 Line (Proportional Control) Program
In the middle section (section B in the previous description) you will notice that in the “03 line” program, the settings of Ki and Kd are both 0.
Let’s leave them that way. We will only be changing the value of Kp, the Proportional part of the controller.
Kp decides how smoothly the robot changes speed as it drifts away from the line. If Kp is too large, the motion will be extremely jerky (similar to the On-Off controller). If Kp is too small, then the robot will make corrections too slowly, and will drift far away from the line, especially on the curves. It may even drift so far as to lose the line altogether!
13 What Set-Point is the program “03 line” using? (subtracted after reading light setting in the A loop)___________
14 What is the value of Kp in the current “03 line” program? _____________
Time Trials for Proportional Controller (“3 line” program)
You will be using the original settings for the “03 line” program saved in your robot’s memory to do a time trial, and you will also be using two other modifications to the “03 line” program for a total of three time trial measurements. The modifications you are to make include
DRIFTY - Finding a value of Kp that makes the robot drift very slowly, and possibly lose sight of the line (but hopefully not). Try a Kp different values between 0.5 and 2.5 (or another value) till you get one where the robot Drifts, but stays on the line.
JERKY - Finding a value of Kp that makes the robot jerk back and forth, very similar to the On-Off type of motion. Try a Kp value somewhere between 1.5 and 3.5 (or another value) till you get one where the robot just starts to exhibit the back and forth motion, but not too dramatically. This is also known as the “critical” value of Kp.
Time trials for an entire clockwise turn around the track are only needed with the original “3 line” values and the two new sets of values (DRIFTY and JERKY) that you discover by having the robot follow only a short length of track. Don’t forget to download changes to your robot every time!
15 Record the Proportional Control values and Time Trials for “3 line” program (remember to download changes to the robot!) for each of these three values of Kp (the original 03 line value, and two values that you determine by trial and error to be DRIFTY and JERKY).
Step 12: Advanced PID Controllers
Before starting this step, be sure to complete the prior steps, recording all of the requested information, with the particular robot you intend to use for this lab. Each robot is slightly different, with regard to mechanical aspects, motor aspects, and especially light sensor results on the track.
Numbers that you’ll need from the prior experiments
16 Maximum Light Sensor Reading (from step 2) ____________
17 Minimum Light Sensor Reading (from step 5) ____________
18 GOOD setting for the Set-Point (average of the above) ____________
19 DRIFTY setting for Kp (from step 15) ____________
20 JERKY (critical) setting for Kp (from step 15) ____________
Understanding the PID Controller
You may have learned about the Proportional Integral Differential (PID) controller as part of an Industrial Controls course, and a good quick overview is online at Wikipedia (https://en.wikipedia.org/wiki/PID_controller).
In the case of this experiment, the measured value is the amount of light reflected from the floor. The set-point is the desired amount of light when the robot is directly above the edge of the black tape. The error is the difference between the current light reading and the set-point.
With the Proportional controller, the speed of the left motor was proportional to the error. Specifically:
Error = Light Reading – Set-Point
In this graphic, the set-point was set to 50.
Later, to find the speed of the Left Motor, we multiply the error by the Proportioning constant “Kp” specifically:
L Motor = (Kp * Error) + 35
Where in this graphic, Kp is set to 1.5, and the addition of the 35 happens in another part of the program. The value of 35 is added to convert number that is somewhere in the range of -40 to +40, to be a number that is somewhere between 10 and 60 (reasonable motor speeds).
The Integral is a kind of memory of the past. If the error has been bad for a longer time, the robot should speed up towards the set-point. Ki is used to multiply by the Integral (the integral is the running sum of errors – in this case, reduced by 1.5 each iteration so the robot will have a “fading memory” of past errors).
The Derivative is a kind of future prediction. We predict a future error by comparing the last error to the current error and assume that the rate of error change will be somewhat linear. The bigger the future error is predicted to be, the faster we need to move to the set-point. Kd is used to multiply by the Derivative (the derivative is the difference between the current error and the previous error).
L Motor = (Kp * Error) + (Ki * Integral) + (Kd * Derivative) + 35
Step 13: Finding the Best PID Parameters
There are a number of ways that can be used to find the PID parameters, but our situation has unique aspects that allow us to use a more “manual” experimental way of finding the parameters. The unique aspects that we have are:
- The experimenters (you) have a good understanding of the way the machine operates
- There is no danger of personal injury if the controller goes crazy, and also no danger of damaging the robot because of bad controller settings
- The light sensor is such a sloppy sensing device, and there is only one light sensor, so we can only hope to get a marginally good final result. Therefore a “best effort” is fine for our experiments
First, we already used “03 line” to decide on a best Kp (the GOOD Set-point and JERKY Kp values step 18 & 20 above). See the first graphic for instructions on how we found the JERKY value for Kp.
Use the software “04 line” to determine Ki. We will first modify “4 line” to have the values we recorded in items 18 and 20 above. Next we will slowly increase Ki until we get a value that really moves us to the set-point very quickly.See the second graphic for instructions on how to select the value for Ki.
21 FASTEST Value of Ki that settles on the set-point the fastest (even with some overshoot) ___________________
Use the software “05 line” to determine Kd. First modify “5 line” with values from steps 18, 20, and 21, then increase Kd until you get the final working robot that reaches the set-point quickly and with very little overshoot if any. The third graphic shows instructions on how to select Kd.
22 OPTIMAL Value of Kd _______________________________
23 HOW LONG DUES YOUR ROBOT TAKE TO CIRCLE THE TRACK NOW ??? _________________
Step 14: Conclusion
The lab experiment went very well. With about 20 students, using the 10 (ten) workstation + robot setups shown in the first graphic, there was never a logjam of resources. At most three robots were circling the track at a time for the time trials.
I recommend breaking out the PID control portion (at a minimum, programs "04 line" and "05 line") to a separate day, because of the concepts involved.
Here is a sequence of videos showing the progression of controls (from "01 line" to "05 line") using values I selected - but every student came up with slightly different values, which is to be expected!
REMEMBER: One of the top reasons very well prepared robot teams do poorly at competition events is the fact that they do not perform calibration in the exact location the event will take place. Lighting and slight position changes of the sensors due to jostling can greatly affect the parameter values!
- 01 line (On-Off) PID Control with Lego Robots - https://videos.ecpi.net/Watch/Mw29Etp7
- 02 line (On-Off with Dead-Zone) PID Control with Lego Robots - https://videos.ecpi.net/Watch/n4A5Lor7
- 03 line (Proportional) PID Control with Lego Robots - https://videos.ecpi.net/Watch/o5SEn48N
- 04 line (Proportional-Integral) PID Control with Lego Robots - https://videos.ecpi.net/Watch/w3Q4Jqc2
- 05 line (Proportional-Integral-Derivative) PID Control with Lego Robots - https://videos.ecpi.net/Watch/s6LRi5r7