ENGR101 T1 2017 Engineering Technology Arthur Roberts School of Engineering and Computer Science Victoria University of Wellington Arthur Roberts (ECS) ENGR101 1 / 32
Housekeeping Important ANNOUNCEMENT: CHOCOLATE is gone. Proof: Video New chocolate - reach same point but follow the line. Arthur Roberts (ECS) ENGR101 2 / 32
Lecture 26 Topic 1: Control algorithms Arthur Roberts (ECS) ENGR101 3 / 32
Reminder We talked a bit about control systems at last lecture. Summarizing: We want our system in certain state. But it is not. Difference is called error - and we want to keep it 0 If error is not 0 - we try to adjust Adjustment is constantly repeated For example, we want to keep landing plane in the middle of runway... https://www.youtube.com/watch?v=ook_dbldfa0 Arthur Roberts (ECS) ENGR101 4 / 32
Diagramatically... General Control System: Example of a closed loop system: a system that responds to its current state. All smarts are in controller - it decides what to do if there is an error. Arthur Roberts (ECS) ENGR101 5 / 32
Conditional Solution - simple and WRONG Easy, is it? What is the problem here? if left half of runway > full turn right; if right half of runway > full turn left; if middle > steady as we go. Arrgh, captain... For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken Read more at: https://www.brainyquote.com Arthur Roberts (ECS) ENGR101 6 / 32
Thermostat Let s forget about robots and airplanes for a while and consider room heating (seems to be rather important subject now, does it?). Figure: Simple! If temperature is lower than comfortable - turn heater ON full blast If temperature is higher than comfortable - turn heater OFF Arthur Roberts (ECS) ENGR101 7 / 32
What happens? 20-5 air temperature We want that A B C D E heater ON time A - too cold, turn it ON A->B - good now, turn it OFF B - heater is OFF but still hot. Temperature in the room keep rising (slower though) B->C - temperature of the heater becomes same as air temperature. Air temperature stops increasing. C - Heater is OFF, air temperature dropping Arthur Roberts (ECS) ENGR101 8 / 32
What happens? air temperature 20-5 We want that A B C D E heater ON time C->D - becoming too cold, turn it ON D - temperature of heater rising. But is not high enough to start heating the air So temperature never actually is what we want it to be but going around and around. Improve? Arthur Roberts (ECS) ENGR101 9 / 32
Response is proportional to error 20-5 air temperature error We want that A B C D E heater ON time Heater have temperature setting We measure how far air temperature is off desired value (error) and adjust heater setting accordingly error is big - heater is on HIGH error is small - heater is on LOW When time reaches point A->B heater was in LOW setting for a while already, so air temperature in B will not overshoot that much Arthur Roberts (ECS) ENGR101 10 / 32
We are not designing heating system, right? Our error is distance between middle of the image and white line position (last lecture) 240 320 ERROR We should adjust speed of left and right motor so that line stays in the middle In this case (line to the right) speed of left motor should go up and speed of right motor should go down - robot turns to the right. At the same time robot should keep going forward. How? Arthur Roberts (ECS) ENGR101 11 / 32
Speed proportional to error Declare unsigned char v_go - speed of going straight forward Declare unsigned char dv - difference in speed of left and right motors Now: Speed of right motor: v r = v_go + dv (set_motor(..,v_l)) Speed of left motor: v l = v_go dv(set_motor(..,v_r)) dv is proportional to error (scaling, overflow, type conversions!!!). Symbolically, dv = error Kp Left, right, forward and backward are confusing things - they change when you connect wires differently. Arthur Roberts (ECS) ENGR101 12 / 32
Demo Python Demo purposes only - not real thing Track is blue Robot trajectory - orange Arthur Roberts (ECS) ENGR101 13 / 32
Question! What is robot doing if you set Kp = 0.0 and implement formulas: (pseudocode, not C) dv = error Kp v_left = v_go + dv v_right = v_go dv set_motor(..., v_left) set_motor(..., v_right) 1 Go straight 2 Follow the line 3 Stay still Arthur Roberts (ECS) ENGR101 14 / 32
Kp influence What happens as Kp increases? Demo (KP1.py): Kp = 0.0 kp = 0.005 kp = 0.05 - breakdown Kp goes up Kp=0 Bigger Kp results in robot reacting more (turning further) to same error. Eventually it becomes hysterical. So answer is - choose right value of Kp and it works. Arthur Roberts (ECS) ENGR101 15 / 32
ALL PROBLEMS SOLVED!! Yeah, right... But let us have a look at heating system again... air temperature air temperature 20 We want that 20 error We want that -5 A B C D E heater ON time -5 A B C D E heater ON time It takes longer with proportional. It is understandable - controller is not pushing to the limit - its slowing down to avoid the overshooting. Can we speed things up a bit? Arthur Roberts (ECS) ENGR101 16 / 32
Brakes... kp2>kp1 Slow down here Figure: time kp1 It would be good to keep K p high - faster regulation But we risk system becoming non-stable If we can apply brakes when system approaches desired state? How to detect that? It is going faster - rate of change Arthur Roberts (ECS) ENGR101 17 / 32
How detect rate of change? e1 e0 t0 t1 time We measure error e0 at moment of time t0. We measure error e1 at moment of time t1. Rate of change (derivative) is de dt = e1 e0 t1 t0 (1) Controller should remember last measurement to calculate rate of change. Arthur Roberts (ECS) ENGR101 18 / 32
Demo Kp = -0.005 0.05 - limit Kd = -0.05 This, actually, is almost perfect adjustment - fast without much overshooting. As an exercise - there is another way to detect rate of change for this robot. How? Arthur Roberts (ECS) ENGR101 19 / 32
Our program flow (pseudocode) repeat... error = line_off _centre() recall_previous_error_value de/dt =... dv = error Kp + de/dt Kd v_left = v_go + dv v_right = v_go dv set_motor(..., v_left) set_motor(..., v_right) More on programming later. How to get Kp and Kd values? Arthur Roberts (ECS) ENGR101 20 / 32
Have to keep trying... Set Kp and Kd both to 0. Robot should go straight(ish - motors are not identical) Increase Kp until robot follows the line - including curved parts. Increase Kp until robot starts swinging but still follows the line Start changing Kd until movement is smooth Arthur Roberts (ECS) ENGR101 21 / 32
Demo videos Arthur Roberts (ECS) ENGR101 22 / 32
PID controller - workhorse of automatic control PID stands for: P: Proportional - response to current error value. I: Integral - response to total (sum) error value over time. D: Derivative - response to rate of change of error value. The strength of each response is tuned by a constant of proportionality: k p, k i or k d respectively. Arthur Roberts (ECS) ENGR101 23 / 32
Back to our closed loop system General PID Control System: Example of a closed loop PID system. Note that all responses are summed back together before the output is determined. Arthur Roberts (ECS) ENGR101 24 / 32
Integral? + + + - - - time Both proportional and derivative terms are localized Sometimes there is some offset in the system which is so small that can properly measured only over long periods of time Integral accumulates error over time so offset can be eliminated Our time periods are too short for integral term to work Arthur Roberts (ECS) ENGR101 25 / 32
Lecture 26 Topic 2: Programming AVC hint Arthur Roberts (ECS) ENGR101 26 / 32
General comments It is logical to make program run forever as there is no need really to stop it when robot reaches finish line. int main() { while(1) { // miracle happens... } } Listing 1: Caption comes handy here. Algorithms for different quadrants are quite different - better to have some variable to store current quadrant, say quad Arthur Roberts (ECS) ENGR101 27 / 32
Programming it I To engage different functions you can use either if or switch operators. int main(){ int quad = 0; while(1) { if (quad == 0) { // do quadrant 0 } if (quad == 1) { // do quadrant 1 } //... etc }... return 0; } Listing 2: if version Arthur Roberts (ECS) ENGR101 28 / 32
Programming it - switch operator switch is like if with multiple choices. Listing 3: Caption int main() { int a = 10; switch(a){ case 0: printf("value is 0\n"); break; case 1: printf("value is 1\n"); break; default: printf("not listed\n"); break; } } if value is found in the list - everything between case value and break is executed - careful here. break jumps out of switch. if value is not found - default branch is executed. value not found and no default - nothing is executed Arthur Roberts (ECS) ENGR101 29 / 32
Programming it - switch operator Listing 4: Caption int main() { int a = 1; switch(a){ case 0: printf("value is 0\n"); break; case 1: printf("value is 1\n"); break; default: printf("not listed\n"); break; } } What is an output? 1 "value is 0" 2 "value is 1" 3 "Not listed" Arthur Roberts (ECS) ENGR101 30 / 32
Programming it - switch operator Listing 5: Caption int main() { int a = 0; switch(a){ case 0: printf("value is 0\n"); case 1: printf("value is 1\n"); default: printf("not listed\n"); } } What is an output? 1 value is 0 2 value is 0 value is 1 Not listed 3 Nothing Arthur Roberts (ECS) ENGR101 31 / 32
Programming it - tricky function in Quadrant 2 There is possibly tricky function in Quadrant 2: wiggly line following. it would be good to have the function which returns error - estimation of how far from the center of camera image. Something like... 240 320 ERROR Listing 6: Caption int estim_error() { // miracle happens here return error; } would be lovely, thank you. Problem is, may be we lost white line altogether. How to make function to return this information? Right now function returns int. Arthur Roberts (ECS) ENGR101 32 / 32