October 5, 2013 Proportional, Integral, Derivative Line Following Welcome and Introductions Page 2 Line Following •Why is line following useful in FLL? •Wide black lines with surrounding white area can be used for successful navigation. •Light sensor is preferable to color sensor due to: (a) higher resolution, and (b) ability to calibrate. •As with all line following, steering the robot is accomplished by independently controlling the power of the left and right motors. The motors work to keep the light sensor at the edge of the line. Page 3 PID What is PID? A PID (Proportional, Integral, Derivative) controller is a common technique used to control a wide variety of machinery including vehicles, robots, and rockets. A PID controller compared to a standard 2 and 3 level line follower. The x-axis is the light sensor reading. The y-axis is the amount of turn. Note that the threshold for the 2 level and 3 level line following is 50. Page 4 We will construct a PID line follower which… • Moves the robot forward. • Uses light sensor in port 2 to follow the line. • Follows the left-hand-side edge of the line. 2 B C Page 5 PID Pseudocode – Initial variables to be used Let “Kp” = some number Let “Ki” = some number Let “Kd” = some number Let “Tp” (target power) = some power level Let “Integral” = 0 Let “Derivative” = 0 Let “Last Error” = 0 Page 6 Pseudocode – some calculations to perform Reset rotation sensor B Begin Loop Let “Error” = light sensor intensity reading – (minus) 50 Let “Integral” = “Error” + (plus) “Integral” Let “Derivative” = “Error” – (minus) “Last Error” Let “PTerm” = “Kp” x (times) “Error” Let “ITerm” = “Ki” x (times) “Integral” Let “DTerm” = “Kd” x (times) “Derivative” Let “Turn” = “PTerm” + “ITerm” + “DTerm” Let “Turn” = Turn / (divided by) 100 Let “PowerB” = “Tp” + (plus) “Turn” Let “PowerC” = “Tp” - (minus) “Turn” Let “LastError” = “Error” Page 7 Pseudocode – establish power levels If “Power B” > 0, then motor block B forward unlimited at “Power B” power level, else motor block B reverse unlimited at negative “Power B” power level but with direction or movement reversed (We want a negative number to mean that the motor should reverse direction. But the data port on a NXT-G MOTOR block does not understand that. The power level is always a number between 0 and +100. The motor's direction is controlled by a different input port. Negative motor power needs to be made into a positive number and the motor direction needs to be reversed on the control panel If “Power C” > 0, then motor block C forward unlimited at “Power C” power level, else motor block CB reverse unlimited at negative “Power C” power level but with direction or movement reversed End Loop Stop Motors C and B Page 8 PID Calculations for “P” PID - Illustration of Power Levels for Each Motor Error Kp -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 Trunc Kp*Error (Kp*Erro Kp*Error /100 r/100) -2000 -1960 -1920 -1880 -1840 -1800 -1760 -1720 -1680 -1640 -1600 -1560 -1520 -1480 -1440 -1400 -1360 -1320 -1280 -1240 -1200 -1160 -1120 -1080 -1040 -1000 -960 -920 -880 -840 -800 -760 -720 -680 -20.0 -19.6 -19.2 -18.8 -18.4 -18.0 -17.6 -17.2 -16.8 -16.4 -16.0 -15.6 -15.2 -14.8 -14.4 -14.0 -13.6 -13.2 -12.8 -12.4 -12.0 -11.6 -11.2 -10.8 -10.4 -10.0 -9.6 -9.2 -8.8 -8.4 -8.0 -7.6 -7.2 -6.8 -20 -19 -19 -18 -18 -18 -17 -17 -16 -16 -16 -15 -15 -14 -14 -14 -13 -13 -12 -12 -12 -11 -11 -10 -10 -10 -9 -9 -8 -8 -8 -7 -7 -6 TP 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 PID - Illustration of Power Levels for Each Motor Power Power Motor B Motor C Error 20 21 21 22 22 22 23 23 24 24 24 25 25 26 26 26 27 27 28 28 28 29 29 30 30 30 31 31 32 32 32 33 33 34 60 59 59 58 58 58 57 57 56 56 56 55 55 54 54 54 53 53 52 52 52 51 51 50 50 50 49 49 48 48 48 47 47 46 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Kp 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 Trunc Kp*Error (Kp*Erro Kp*Error /100 r/100) -640 -600 -560 -520 -480 -440 -400 -360 -320 -280 -240 -200 -160 -120 -80 -40 0 40 80 120 160 200 240 280 320 360 400 440 480 520 560 600 640 -6.4 -6.0 -5.6 -5.2 -4.8 -4.4 -4.0 -3.6 -3.2 -2.8 -2.4 -2.0 -1.6 -1.2 -0.8 -0.4 0.0 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0 4.4 4.8 5.2 5.6 6.0 6.4 -6 -6 -5 -5 -4 -4 -4 -3 -3 -2 -2 -2 -1 -1 0 0 0 0 0 1 1 2 2 2 3 3 4 4 4 5 5 6 6 TP 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 Page 9 PID - Illustration of Power Levels for Each Motor Power Power Motor B Motor C 34 34 35 35 36 36 36 37 37 38 38 38 39 39 40 40 40 40 40 41 41 42 42 42 43 43 44 44 44 45 45 46 46 46 46 45 45 44 44 44 43 43 42 42 42 41 41 40 40 40 40 40 39 39 38 38 38 37 37 36 36 36 35 35 34 34 Error Kp 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 Trunc Kp*Error (Kp*Erro Kp*Error /100 r/100) 720 760 800 840 880 920 960 1000 1040 1080 1120 1160 1200 1240 1280 1320 1360 1400 1440 1480 1520 1560 1600 1640 1680 1720 1760 1800 1840 1880 1920 1960 2000 7.2 7.6 8.0 8.4 8.8 9.2 9.6 10.0 10.4 10.8 11.2 11.6 12.0 12.4 12.8 13.2 13.6 14.0 14.4 14.8 15.2 15.6 16.0 16.4 16.8 17.2 17.6 18.0 18.4 18.8 19.2 19.6 20.0 7 7 8 8 8 9 9 10 10 10 11 11 12 12 12 13 13 14 14 14 15 15 16 16 16 17 17 18 18 18 19 19 20 TP 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 Power Power Motor B Motor C 47 47 48 48 48 49 49 50 50 50 51 51 52 52 52 53 53 54 54 54 55 55 56 56 56 57 57 58 58 58 59 59 60 33 33 32 32 32 31 31 30 30 30 29 29 28 28 28 27 27 26 26 26 25 25 24 24 24 23 23 22 22 22 21 21 20 Questions? Page 10