Lecture 5 Introduction to control Feedback control is a way of automatically adjusting a variable to a desired value despite possible external influence or variations. Eg: Heating your house. No feedback (open loop): Actual temperature varies depending on whether windows are open, how cold it is outside etc,.. Desired temperature T Outside temperature House temperature responds to heat Determine typical heater power required Turn heater on or off
Control: Introduction Feedback control is a way of automatically adjusting a variable to a desired value despite possible external influence or variations. Feedback (closed loop): Measure actual house temperature T Desired temperature T Outside temperature House temperature responds to heat Analyze difference between Desired and Actual temperature Turn heater on or off
The purpose of control theory is to make these two numbers the same despite external influences Feedback control is a way of automatically adjusting a variable to a desired value despite possible external influence or variations. Feedback (closed loop): Measure actual house temperature T = Desired temperature T Outside temperature House temperature responds to heat Analyze difference between Desired and Actual temperature Turn heater on or off
Feedback Control Build a circuit that: Steadies a piece of paper attached to a motor by optical feedback, such that when the motor is translated, the end of the paper remains stationary. Note: Use the QRD1114 to detect the paper position. Feed this through an op-amp with controllable gain, and use the op-amp to drive a motor that turns the paper. Experiment with different gain values.
Feedback Control Build a QRD circuit and use voltmeter to check that output varies by a few volts when paper comes near QRD Buffer its output with a TL082 Build a variable gain inverting amp with a TL082 and a pot. Use a fixed positive voltage reference that lies in the range of the QRD values. Hook it all up
Control: Introduction ON/OFF control: X = distance between center of robot and center of tape x while(1) { if (x=0) go_straight(); if (x>0) turn_left(); if (x<0) turn right(); } This tends to lead to oscillations around the center of the tape.
Control: Introduction Proportional control: x > 0 X = distance between center of robot and center of tape steer(int dir); - a hypothetical function that steers robot left (dir<0) or right (dir>0) in a radius of 600 /dir. while(1) { steer(k*x); } K is the proportional gain of this feedback loop and MUST be negative. This is much better and more accurate than ON/OFF control, though it will still have significant error and oscillate for large values of K. x
Control: Introduction Proportional control: x > 0 X = distance between center of robot and center of tape steer(int dir); - a hypothetical function that steers robot left (dir<0) or right (dir>0) in a radius of 600 /dir. while(1) { steer(k*x); } x So what is the right algorithm?????
Transfer functions revisited (Laplace transform notation: s~jw) V(s) is the Laplace transform of v(t). Some rules: 1) Proportionality: v out (t) = K*v in (t) H(s) = V out (s)/ V in (s) V out (s) = K*V in (s) 10log H(s) H(s) = K q log(k) V st ( s) v( t) e dt 0 Time domain Frequency domain log w 0 log w
2) Integration: 10log H(s) v V out out ( t) ( s) H ( s) K KV K s v in s in ( t) dt ( s) q Time domain Frequency domain -10 db/dec log w 0-90 log w
3) Differentiation: v V out out dvin( t) ( t) K dt ( s) KsV ( s) in 10log H(s) H ( s) Ks q +10 db/dec 90 log w 0 log w
Feedback loops Y = variable you d like to control (eg: shaft angle of a servo motor) X = your desired value of Y (eg: 10 degrees) X(s) + Y = G(X-HY) Y(1+GH) = GX - Error G(s) H(s) Y(s) Y Motor and amplifier behavior Sensor / compensator behavior G Y/X = G/(1+GH) X 1 GH G = forward transfer function, GH = loop transfer function
Feedback loops: stability Y G X 1 GH This loop will be unstable if GH = -1 GH =1, phase(gh)= ±180 deg. G(s)H(s) = -1 implies Y X for some value of s i.e. there will exist a frequency for which the loop will provide infinite amplification
Loop Stability Y X G 1 GH Partial stability criterion: GH < 1 where the phase of GH is ± 180 deg. 10log GH STABLE q 0 db -8 db Gain Margin w 0 log w 0-180 -270 w 0 log w
Loop Stability Y X KG 1 KGH Partial stability criterion: GH < 1 where the phase of GH is ± 180 deg. 10log GH UNSTABLE q w 0 0 db log w 0 w 0 log w Increasing loop gain eventually makes all systems unstable -180-270
Steady state error Steady state error: The difference between actual and desired values when these values are not fluctuating with time (DC behavior). X(s) + Error = X-Y - Y = KG* Error Error K G(s) Y(s) SSError Make KG 0 (DC gain) large to minimize error. This can increase loop gain at high frequencies and lead to instability. Y KG(0)
Steady state error and stability Another look at negative feedback in an op-amp: Vin Vout + R1 R2 Error V in + - G(s) V out H(s) Model Op-amp as G(s) = K/s H ( s) R 1 R 1 R 2
Steady state error and stability Another look at negative feedback in an op-amp: V in + - K/s V out R 1 /(R 1 +R 2 ) V V out in 1 K / s K R s R 1 1 R 2 s K KR1 R R 1 2 Vin R1 + R2 Vout For large K and low frequency, this reduces to V V out in R 1 2 R 1
Steady state error and stability Another look at negative feedback in an op-amp: V in + - V error K/s V out Stability: GH K s R1 ( R R ) 2 1 R 1 /(R 1 +R 2 ) Phase of GH is -90 for all frequencies. This is inherently stable as GH will never = -1
Steady state error and stability Another look at negative feedback in an op-amp: V in + - V error K/s R 1 /(R 1 +R 2 ) V out GH K s R1 ( R R ) 2 1 Steady state error: V K s out V error sv V out error Verror 0 K At s=0 (DC)! Integration (1/s) in the loop reduces steady state error to zero without need for infinite loop gain at higher frequencies!
Stability Summary Y X KG 1 KGH Having one or fewer poles in the plant function KGH ensures that the loop is never unstable. The more poles exist in KGH, the harder it will be to control. Problems will start to occur when controlling at frequencies above the pole frequencies. Increasing loop gain eventually makes all systems unstable due to unexpected high frequency poles.
Compensation V in + - H(s) G(s) V out A feedback system is usually divided into two transfer functions: The plant function (G(s)) which usually you cannot alter (motor characteristics etc.) A compensator circuit H(s) that you can design to optimize the feedback loop A common type of all-purpose compensation is PID: Proportional (K p ) Integral (K i /s) Derivative (sk d )
PID Compensation V in + - H(s) G(s) V out Typical PID transfer function: H(s) = K tot (K p +K i /s+sk d ) The various gains (K tot,k p,k i,k d ) are adjusted to control how much of each type of compensation is applied for a specific plant function G(s). This adjustment is referred to as tuning and is often done iteratively (a slightly improved form of trial and error) when the plant function G is not well known.
PID example: position servo (demo) q in Knob k V set + TINAH Board knob: q in code H(s) + - - analog(0) V error V pot PID H(s) k Pot Pwm out Motor K/(s(s+a)) motor G(s) q out q out Mechanical connection k potentiometer
PID example: position servo q in Knob k V set + - V error PID H(s) Motor K/(s(s+a)) q out V pot k Motor transfer function: Pot q dt w max q dt (at high frequencies: G=K/s 2 ) (at low frequencies: G=K/s) G( s) K s( s a) Torque Inertia q w w
PID example: position servo q in Knob k V set + - V error V pot PID Motor H(s) K/(s(s+a)) k Pot q out Loop transfer function (stability analysis): G( s) K s( s H(s) =? a) Try proportional control: H(s) = K p
Stability: position servo P control Loop transfer function (P only): log GH 0 db GH ( s) Gain Margin a KK s( s p a) 10a Stable for limited gain log w q V out q -90-180 error Verror KK s( s p V a) p error s ( s a ) q KK 0 out at s=0! 10a log w
Stability: position servo I control Open loop transfer function (I only): KK GH ( s) 2 s ( s i a) H ( s) K s i q -180 log w -270 Phase crosses 180 at DC, with infinite DC gain! Inherently unstable at s=0
Stability: position servo D control Open loop transfer function (D only): GH ( s) KK ( s d a) Phase always less than 180 Stable even for large gains! 0-90 q V error H( s) ( s a ) q KK sk d log w SS error 0! Problems: May be hard to implement due to amplification of fast transients. Can be combined with P gain to add high gain stability and low SS error Model is not complete loop will still be unstable at very high gains. d out
Tuning PID Often PID tuning is done by nearly trial and error. Here is a common Procedure which works for many (but not all) plant functions. USE external pots or menus to adjust!!!!! Set P=I=D=0 Increase P slightly and ensure that the sign of the gains is correct. Increase P until oscillations begin Increase D to dampen oscillations Iterate increasing P and D until fast response is achieved with little overshoot Increase I to remove any Steady State error. If overshoot is too large try decreasing P and D. Test with step response: Crit. damped over damped under damped
Control: Introduction How to measure X (distance from tape): Use QRD1114 reflectance sensors to detect lack of reflectance from tape. tape sensor
How to measure X: Control: Introduction X = distance between center of robot and center of tape x > 0 x You can form a rough approximation of X by digital to analog conversion of your digital inputs with history: Situation Left sensor Right Sensor X Both sensors on tape 1 1 0 Left sensor off tape, right on 0 1-1 Right sensor off tape, left on 1 0 +1 Both sensors off (right was last on) 0 0-5 Both sensors off (left was last on) 0 0 +5
Please consider the following problem for a robot with differential rear drive steering: Which robot configuration has more poles in the transfer function between I (current to motors) and x (distance of sensors from tape)? sensors sensors x x pivot 1 pivot 2
x q r 1 v q in 0 x 0 in l Actual x value in time domain: x l sinq vsinqdt lq vqdt Actual X in frequency domain: vq X ~ lq at low v X lq s vq X for l = 0 s q K (we want robot to follow tape) bot I pwm s( s a) where a Tr 1 I bot I bot is the chassis moment of inertia
Design tips for stability Minimize robot polar moment of inertia (I robot ) Maximize robot torque about polar axis (r 1 /D wheel ) Maximize distance from polar axis to tape sensors (l) Minimize sensor dead band Change gear ratio / wheel size to increase torque / reduce speed if you find stability is only achieved at very low motor powers. X lq vq s q s( s Kr Kr 1 1 I I pwm pwm / D / I wheel robot D wheel )
Linearization of non-linear functions Control can be very difficult if G is non-linear. PWM drive (combined with friction) yields a very non-linear torque curve: T PWM Solution: Linearize this curve in software by mapping PWM to desired Torque PWMout PWMin
Analog PID in software (Servo control) loop pot = analog(6); set = knob(); error = set-pot; p=kp*error; d=kd*(error-lasterr); i=ki*error+i; if (i>maxi) i = maxi; if (i<-maxi) i = -maxi; g = p+i+d; motor(3,g); lasterr=error; Feedback potentiometer Set point Proportional Integration Derivative Anti-windup Because i is an integral, it will build up to large values over time for a constant error. An anti-windup check must be put in place to avoid it overwhelming P and D control when the error is removed.
Digital PID in software (Tape following) left=analog(5); right=analog(6); if ((left>thresh)&&(right>thresh)) error = 0; if ((left>thresh)&&(right<thresh)) error = -1; if ((left<thresh)&&(right>thresh)) error = +1; if ((left<thresh)&&(right<thresh)) { if (lerr>0) error = 5; if (lerr<=0) error=-5; } if (!(error==lerr)) { recerr=lerr; q=m; m=1; } p=kp*error; d=(int)((float)kd*(float)(error-recerr)/(float)(q+m)); con = p+d; if (c==30) { printf("%d %d %d %d %d %d\n",right,left,kp,kd,p,d); c=0; } c=c+1; m=m+1; motor(0,-speed+con); motor(3,+speed+con); lerr=error; QRD sensors History Derivative approximation Proportional / Derivative Motor control Display status Special care needs to be taken for the derivative to approximate its real value.