Automatic Control 2 Sampling Prof. Alberto Bemporad University of rento Academic year 2010-2011 Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 1 / 31
ime-discretization of continuous-time controllers ime-discretization of continuous-time controllers r(t)! e(t) u(t) " C(s) G(s) y(t) r(t)! e(t) ẽ(k) ũ(k) " A/D C(z) D/A u(t) G(s) y(t) We have designed an analog controller C(s) in the continuous-time domain (by loop shaping, pole-placement, etc.) We want to implement C(s) in digital form (for example, in a Monday, May 10, 2010 microcontroller). We need to convert the design to discrete-time Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 2 / 31
ime-discretization of continuous-time controllers Relations between continuous/discrete-time signals u(t) r(t)! e(t) ẽ(k) ũ(k) y(t) " A/D C(z) D/A G(s) Let the digital controller operate with sampling time 1.2 1.5 1 0.8 0.6 0.4 Monday, May 10, 2010 1 0.5 0.2 0 0 0.2 0.5 0.4 0.6 1 0.8 0 1 2 3 4 5 6 7 8 9 10 tracking error ẽ(k) e(k) for k {0, 1,...} 1.5 0 1 2 3 4 5 6 7 8 9 10 control input u(t) = ũ(k) u(k) for t [k, (k + 1)) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 3 / 31
ime-discretization of continuous-time controllers ime-discretization of continuous-time controllers r(k)! ẽ(k) " C(z) ũ(k) D/A u(t) G(s) y(t) ỹ(k) A/D Alternative scheme: more often the reference signal r(t) is already given in digital form by its samples r(k), r(k) = r(k), k = 0, 1,..., =sampling time Monday, May 10, 2010 Problem How to synthesize a discrete-time control law C(z) that in closed-loop behaves as the given continuous-time controller C(s)? Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 4 / 31
ime-discretization of continuous-time controllers ime-discretization Consider the continuous-time controller C(s) u(t) = C(s)e(t) = N(s) D(s) e(t) = b n 1s n 1 +... + b 0 s n + a n 1 s n 1 +... + a 0 e(t) We can look at the control law as at a differential equation linking u(t) to e(t) d n dt n u(t) + a d n 1 n 1 dt n 1 u(t) +... + a d n 1 0u(t) = b n 1 dt n 1 e(t) +... + b 0e(t) A time-discretization of u(t) = C(s)e(t) is nothing else than a numerical approximation (with constant integration-step ) ũ(k) = C(z)ẽ(k) linking the input samples ũ(k) u(k) to the error samples ẽ(k) e(k) here are many numerical methods to integrate a differential equation (constant step, variable step, n-th order approximations, etc.) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 5 / 31
ime-discretization of continuous-time controllers Numerical integration in Simulink Fixed-step integration panel Variable-step integration panel Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 6 / 31
ime-discretization of continuous-time controllers Example of time-discretization Consider the continuous-time controller u(t) = 1 s + 2 e(t) d u(t) + 2u(t) = e(t) dt Use Euler method to approximate the derivative d dt u(t) u((k + 1)) u(k) = 1 (ũ(k + 1) ũ(k)) Recall that z represents the unit-shift operator zũ(k) = ũ(k + 1) ũ(k) = z 1 1 ẽ(k) C(z)ẽ(k) + 2 Note that formally this is equivalent to replace s = z 1 in C(s) to get C(z) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 7 / 31
ime-discretization of continuous-time controllers Finite-difference approximation of the controller Consider a state-space realization of C(s) dxc dt = A c x c + B c e u = C c x c + D c e C(s) = C c (si A c ) 1 B c + D c Integrate between time k and (k + 1) x c ((k + 1)) x c (k) = (k+1) k dx c (τ) dτ dt gives (k+1) (k+1) x c ((k + 1)) x c (k) = A c x c (τ)dτ + B c e(τ)dτ k Unfortunately, in general both x(τ) and e(τ) are not constant between consecutive sampling instants, so we can t use exact discretization (i.e., the exponential matrix e A c ) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 8 / 31 k
Monday, May 10, 2010 Sunday, May 16, 2010 Lecture: Sampling ime-discretization of continuous-time controllers Approximation of the integral of ẋ c (τ) (forward) Euler method ẋc(t) ẋc(k) ẋc((k + 1) ) k (k+1) time x c ((k + 1)) x c (k) = }{{} (z 1)x c (k) (k+1) ẋ c (τ)dτ ẋ c (k) }{{} k sx c (k) s = z 1 backward Euler method ẋc(t) ẋc((k 1) ) (k-1) ẋc(k) k time x c (k) x c ((k 1)) = }{{} (1 z 1 )x c (k) k ẋ c (τ)dτ ẋ c (k) }{{} (k 1) sx c (k) s = 1 z 1 rapezoidal rule x ẋ! (t) ẋc(k) c ((k + 1)) x c (k) = }{{} ẋc((k + 1) ) (z 1)x c (k) k (k+1) time 2(z 1) s = (z + 1) (k+1) ẋ c (τ)dτ [ẋ c((k + 1)) + ẋ c (k)] 2 k }{{} 2 (z+1)sx c(k) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 9 / 31
ime-discretization of continuous-time controllers Finite-difference approximation of the controller z 1 (z 1)x c = (A c x c + B c e) I A c x c = B c e C(z) = U(z) z 1 1 z 1 E(z) = C c I A c B c + D c = C z 1 (1 z 1 )x c = (A c x c + B c e) I A c x c = B c e z C(z) = U(z) 1 z 1 1 1 z 1 E(z) = C c I A c B c + D c = C forward Euler method backward Euler method 2(z 1) 1 2(z 1) C(z) = C c I A c B c + D c = C (z + 1) (z + 1) ustin s method Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 10 / 31
ime-discretization of continuous-time controllers Finite-difference approximation of the controller Formally, we just replace s in C(s) with the corresponding function of z: forward Euler method s = z 1 backward Euler method ẋ c (k) x c((k + 1)) x c (k) s = 1 z 1 rapezoidal rule (ustin s method) ẋ c (k) x c(k) x c ((k 1)) 2(z 1) s = (bilinear transformation) (z + 1) Note that all three methods preserve the DC gain: z = 1 s = 0 (no approximation error exists in constant steady-state!) Compare to the exact discretization method: Ã c = e A c, B c = 0 eta c dtbc, C c = C c, D c = D c Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 11 / 31
ime-discretization of continuous-time controllers Relations between poles in s and z What is the relation between the poles s i of C(s) and the poles z i of C(z)? forward Euler differences: z i 1 = s i z i = 1 + s i backward Euler differences: z i 1 z i = s i z i = 1 1 s i ustin s method: Exact method: z i = e s i 2(z i 1) (z i + 1) = s i z i = 1 + s i/2 1 s i /2 Note that the three (approximate) integration methods approximate the function z = e s by a rational function Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 12 / 31
Monday, May 10, 2010 Monday, May 10, 2010 Lecture: Sampling ime-discretization of continuous-time controllers Relations between poles in s and z forward Euler differences Rs Im s z = 1 + s Rz 1 Im z stable poles in s may be mapped to unstable poles in z backward Euler differences Rs Im s 1 z = 1 s Rz 1 Im z stable poles in s are also stable poles in z, marginally stable poles may become asymptotically stable ustin s rule Rs Im s z = 1+s/2 1 s/2 Rz 1 Im z stable poles in s are also stable poles in z Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 13 / 31
ime-discretization of continuous-time controllers ime-discretization in MALAB MALAB C2D Conversion of continuous-time models to discrete time. SYSD = C2D(SYSC,S,MEHOD) converts the continuous-time LI model SYSC to a discrete-time model SYSD with sample time S. he string MEHOD selects the discretization method among the following: zoh Zero-order hold on the inputs. foh Linear interpolation of inputs (triangle appx.) tustin prewarp Bilinear (ustin) approximation. ustin approximation with frequency prewarping. he critical frequency Wc is specified as fourth input by C2D(SYSC,S, prewarp,wc). matched Matched pole-zero method (for SISO systems only). he default is zoh when MEHOD is omitted. Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 14 / 31
Analysis of sampled-data systems Sampled-data systems continuous-time control system (Laplace transform/frequency analysis) r(t)! e(t) u(t) " C(s) G(s) y(t) t = continuous time discrete-time control system (Z-transform) r(k)! ẽ(k) ũ(k) ỹ(k) " C(z) Ḡ(z) k = discrete-time step counter sampled-data system Monday, May 10, 2010 r(t)! e(t) ẽ(k) ũ(k) " A/D C(z) D/A u(t) G(s) y(t) continuous-time and discrete-time signals Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 15 / 31
Analysis of sampled-data systems Why analyzing sampled-data systems he model G(s) of the process to be controlled is (very often) given in continuous time differential equations actuator signals (electrical voltages to motors, etc.) vary continuously in time output variables (temperature, pressure, position, etc.) vary continuously in time On the other hand, the controller is (almost always) implemented in digital form, C(z): cheaper to implement (computer code) easier to reconfigure can exploit time-sharing (multiple controllers on the same hardware) much more versatile (arbitrary nonlinear control laws) Hence the need to analyze sampled-data systems, namely a continuous process in closed loop with a digital controller Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 16 / 31
Analysis of sampled-data systems Ways to analyze sampled-data systems u(t) r(t)! e(t) ẽ(k) ũ(k) " A/D C(z) D/A G(s) y(t) 1 Convert the system G(s) to its discrete-time equivalent Ḡ(z) (use for instance exact sampling), ignoring its inter-sampling behavior (controller s point of view or stroboscopic model ) = discrete-time analysis Monday, May 10, 2010 2 Model the digital controller in continuous time U(s) 1 e s s C(e s )E(s) (this can be shown...) (process point of view) = continuous-time analysis 3 Use numerical simulation (e.g., Simulink) (only provides an answer for a certain finite set of initial states) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 17 / 31
Sampling theory Choosing the sampling time How to choose the sampling time? Example: sampling of sin(t) 1 sampling time =π/100 1 sampling time =π/10 1 sampling time =π 0.5 0.5 0.5 0 0 0 0.5 0.5 0.5 1 0 5 10 15 1 0 5 10 15 How can we say that a sampling time is good? 1 0 5 10 15 Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 18 / 31
Sampling theory Nyquist-Shannon sampling theorem original signal sampled signal reconstructed signal e(t) e(t) e(t) time k (k+1) time time e(t) sampler ẽ(k) =e(k) ideal low-pass filter e(t) Claude Elwood Shannon (1916 2001) Sampling theorem + Let e(t) be a signal and E(jω) = e(τ)e jωτ dτ its Fourier transform [e]. Let E(jω) Monday, May 10, 2010 = 0 for ω ω max. For all such that ω N π > ω max e(t) = + k= e(k) sin(ω N(t k)) ω N (t k) ω N is called the Nyquist frequency, equal to half the sampling frequency ω s = 2π Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 19 / 31
Sampling theory Shannon s reconstruction We can look at sampling as at the modulation of a Dirac comb Let s go to Fourier transforms e(k)δ(t k) = e(k)e jkω = 1 k= k= }{{} modulated Dirac comb k= δ(t k) k= E(j(ω+kω s )) 1 E s(jω) E s (jω) is the periodic repetition of E(jω) with period ω s he ideal low-pass filter can only reconstruct e(t) if E(jω) = 0 for ω ω max original signal sampled signal reconstructed signal E(jω) Es(jω) E(jω) e(t) frequency sampler frequency ẽ(k) =e(k) ideal low-pass filter π frequency e(t) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 20 / 31
Sampling theory Aliasing E s (jω) = 1 + k= E(j(ω + kω s )) he value E s (jω 1 ) at a certain frequency ω 1 not only depends on E(jω 1 ), but also on all the (infinite) contributions E(j(ω 1 ± kω s )) he frequency ω 1 ± kω s is called an alias of ω 1 Example: consider the signals sin(ω 1 t) and sin(ω 2 t) and let the sampling time = 1 s 1 0.5 0 0.5 sampling time =1 ω 1 = 2π 10 ω 2 = 2π + 2π = 22π 10 10 1 0 1 2 3 4 5 6 7 8 9 10 Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 21 / 31
Sampling theory Aliasing What if e(t) has spectral contributions E(jω) 0 for ω > ω N = π? original signal sampled signal reconstructed signal E(jω) Es(jω) E(jω) e(t) frequency sampler frequency ẽ(k) = e(k ) ideal low-pass filter π e(t) frequency Aliasing is the phenomenon for which frequencies ω > ω N make contributions in the frequency range [ ω N, ω N ] Under aliasing conditions it is impossible to reconstruct the original signal An anti-aliasing filter is a low-pass filter that removes from e(t) its high-frequency contents before sampling it. It partially mitigates the problem Prof. Alberto Bemporad Monday, (University May 10, 2010 of rento) Automatic Control 2 Academic year 2010-2011 22 / 31
Choice of sampling time Selecting the sampling time In signal processing one is interested in making the difference between the original and the reconstructed signal as small as possible (=high fidelity) In control systems one is interested that the closed-loop system behaves according to specs, not much in carefully reconstructing e(t) = y(t) r(t)! For control purposes, the sampling time is mainly related to the closed-loop bandwidth / settling-time he sampling time used in control is typically larger than the one used signal processing hat s why in control applications we are often ok with micro-controllers and don t need DSPs (digital signal processors) Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 23 / 31
Choice of sampling time Selecting the sampling time Let ω c be the desired bandwidth of the closed-loop system o avoid aliasing effects (and satisfy the sampling theorem) we must set A good choice is π > ω c 5ω c 2π 100ω c so that the (non-ideal) anti-aliasing filter has cutoff frequency ω f in between ω c and ω N By recalling the approximate relation t s ω c 5 ζ, where t s is the settling time (1%) and ζ is the damping factor, we get t s 100 t s 5 A related good choice is also to let = t r 10, where t r is the rise time of the open-loop system Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 24 / 31
Choice of sampling time Numerical errors Recall the relation between poles s i and z i z i = e s i For 0, we have z i 1 whatever the poles s i are! his can make troubles when working in finite precision (both in control design and in controller implementation) Consider the following example: we have two poles s 1 = 1 e s 2 = 10 Sample with = 1 ms and get z 1 = e 0.001 0.9990, z 2 = e 0.01 0.9900. If we truncate after two digits, we get z 1 = z 2, and then s 1 = 1 ln z 1 = 1 ln z 2 = s 2 10.05 Sample with = 100 ms and truncate, we get z 1 = 0.90, z 2 = 0.36, and then s 1 1.05, s 2 10.21 Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 25 / 31
Choice of sampling time Final remarks on choice of sampling time Make the sampling time small enough to reproduce the open-loop time response enough precisely ( = t r ), and to avoid aliasing effects (Nyquist 10 frequency π larger than closed-loop bandwidth) Make the sampling time small enough to react enough readily to disturbances affecting the system Make the sampling time large enough to avoid numerical issues Make the sampling time large enough to avoid fast and expensive control hardware Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 26 / 31
Digital PID Digital PID control Any of the time-discretization we have seen so far can be used to convert a PID design in digital form. he following is most used: proportional part P(t) = K p (br(t) y(t)) static relation, no need to approximate it! integral part I(t) = K t p i 0 is approximated by forward Euler as e(τ) dτ I((k + 1)) = I(k) + K p e(k) i Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 27 / 31
Digital PID Digital PID derivative part: is approximated by backward Euler as D(k) = Note that the discrete pole z = he complete control signal is d dd(t) dy(t) + D(t) = K p d N dt dt d d + N D((k 1)) K p d N y(k) y((k 1)) d + N d is inside the unit circle d +N u(k) = P(k) + I(k) + D(k) his type of approximation allows one to calculate P, I, and D actions separately Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 28 / 31
Digital PID Digital PID in incremental form he digital PID form we described provides the full signal u(k) (positional algorithm) An alternative form is the so called incremental form (velocity algorithm), where the PID controller computes instead the input increments where u(k) = u(k) u((k 1)) = P(k) + I(k) + D(k) P(k) = K p (br(k) br((k 1)) y(k) + y((k 1)) I(k) = α 1 (r(k) y(k)) + α 2 (r((k 1)) y((k 1))) D(k) = β 1 D((k 1)) β 2 (y(k) 2y((k 1)) + y((k 2))) Advantage: increased numerical precision in the presence of finite word-length (signal quantization) his form cannot be used for P and PD controllers Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 29 / 31
Digital PID Digital PID in incremental form r(k) incremental PID u(k) 1 u(k) G(z) 1 z 1 y(k) An integrator is needed to reconstruct the input signal u(k) from the incremental PID form 1 u(k) = u((k 1)) + u(k) u(k) = u(k) 1 z 1 Sunday, May 16, 2010 Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 30 / 31
Digital PID English-Italian Vocabulary forward Euler method metodo di Eulero in avanti backward Euler method metodo di Eulero all indietro trapezoidal rule regola dei trapezi sampled-data system sistema a dati campionati Nyquist-Shannon sampling theorem eorema di Shannon Dirac comb pettine di Dirac aliasing aliasing cutoff frequency frequenza di taglio ranslation is obvious otherwise. Prof. Alberto Bemporad (University of rento) Automatic Control 2 Academic year 2010-2011 31 / 31