ENع
→Euler versus Tustin
الدرس 49 · State space and digital control

Discretising a controller: Euler versus Tustin

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

A controller designed with s has to become a few lines of arithmetic before a computer can run it. Follow one lead controller for a motor through two recipes: forward Euler, s ≈ (z − 1)/T, and Tustin's trapezoid rule, s ≈ (2/T)(z − 1)/(z + 1). Both give three numbers for the same four-line loop. Sampled fast, both work. Sampled slowly, Euler's controller turns unstable and the loop blows up, while Tustin's stays stable.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 49 · تفاعلي
01 / 11

A controller designed in s

A motor's shaft position is 1/(s(s + 1)). The lead controller C(s) = 50(s + 1)/(s + 10) makes the closed loop 50/(s² + 10s + 50): poles −5 ± 5j and 4.3% overshoot. It is a continuous-time design. A computer must now run it.

plant       G(s) = 1 / (s(s + 1))      y'' + y' = u
controller  C(s) = 50(s + 1) / (s + 10)

C·G = 50 / (s(s + 10))   (the zero cancels the pole at -1)
closed loop  50 / (s² + 10s + 50)
poles        -5 ± 5j   ζ = 0.707   ωn = 7.07 rad/s
step         y = 1 - e^(-5t)(cos 5t + sin 5t)
peak         1.043 at t = 0.63 s (4.3 % overshoot)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A controller designed in s

    A motor's shaft position is 1/(s(s + 1)). The lead controller C(s) = 50(s + 1)/(s + 10) makes the closed loop 50/(s² + 10s + 50): poles −5 ± 5j and 4.3% overshoot. It is a continuous-time design. A computer must now run it.

    plant       G(s) = 1 / (s(s + 1))      y'' + y' = u
    controller  C(s) = 50(s + 1) / (s + 10)
    
    C·G = 50 / (s(s + 10))   (the zero cancels the pole at -1)
    closed loop  50 / (s² + 10s + 50)
    poles        -5 ± 5j   ζ = 0.707   ωn = 7.07 rad/s
    step         y = 1 - e^(-5t)(cos 5t + sin 5t)
    peak         1.043 at t = 0.63 s (4.3 % overshoot)
  2. A computer only has samples

    The computer reads y once every T seconds and holds u between readings, as in "The sampled control loop: ADC, computer, DAC". It cannot integrate or differentiate a signal. It can only do arithmetic on stored numbers, so C(s) must become a difference equation.

    C(s) = U/E means  u' + 10u = 50(e' + e)
    
    derivatives need the signal at every instant;
    the computer only has e[k] = e(kT), k = 0, 1, 2 ...
    
    wanted: a rule of the form
      u[k] = A·u[k-1] + B0·e[k] + B1·e[k-1]
    three numbers, four lines of code.
    
    z means 'one sample later' (the Z-transform):
      z·x[k] = x[k+1]
  3. Forward Euler: s ≈ (z − 1)/T

    Forward Euler estimates a slope from the next sample: dx/dt ≈ (x[k+1] − x[k])/T. Writing z for 'one sample later', that is s ≈ (z − 1)/T. Put it into C(s) and the pole at s = −10 lands at z = 1 − 10T.

    forward Euler (forward rectangle)
      dx/dt ≈ (x[k+1] - x[k]) / T   →   s ≈ (z - 1)/T
    
    C(z) = 50·((z-1)/T + 1) / ((z-1)/T + 10)
         = 50·(z - (1 - T)) / (z - (1 - 10T))
    
    pole  s = -10   →   z = 1 - 10T
    zero  s = -1    →   z = 1 - T
    
    (exact mapping would be z = e^(sT) = e^(-10T))
  4. Euler gives three numbers

    That becomes u[k] = A·u[k−1] + B0·e[k] + B1·e[k−1], with A = 1 − 10T, B0 = 50, B1 = −50(1 − T). At T = 0.02 s: A = 0.80, B0 = 50, B1 = −49. The code stays fixed; only the numbers change.

    import math
    K, a, p, T = 50, 1, 10, 0.02
    A, B0, B1 = 1 - p*T, K, -K*(1 - a*T)                     # forward Euler
    y = v = u1 = e1 = 0.0
    peak, h, us = 0.0, 0.005, []
    for k in range(round(3 / T)):
        e = 1.0 - y                    # sample the position
        u = A*u1 + B0*e + B1*e1        # the difference equation
        u1, e1 = u, e
        us.append(u)
        for _ in range(round(T / h)):  # motor y'' + y' = u, u held
            E = math.exp(-h)
            y, v = y + v*(1 - E) + u*(h - 1 + E), v*E + u*(1 - E)
            peak = max(peak, y)
    print(round(peak, 3))    # 1.043
    
    A = 1 - 10·0.02 = 0.80   B0 = 50   B1 = -49
  5. Tustin: the trapezoid rule

    Tustin's method adds up area with trapezoids, averaging the old and new samples. That gives s ≈ (2/T)(z − 1)/(z + 1), the bilinear transform from the Signals course. The pole at s = −10 now lands at z = (2 − 10T)/(2 + 10T).

    trapezoid rule (Tustin, bilinear)
      x[k+1] = x[k] + T·(x'[k] + x'[k+1]) / 2
      →   s ≈ (2/T)·(z - 1)/(z + 1)
    
    C(z) = 50·((2+T)z - (2-T)) / ((2+10T)z - (2-10T))
    
    pole  s = -10   →   z = (2 - 10T)/(2 + 10T)
    zero  s = -1    →   z = (2 - T)/(2 + T)
  6. Tustin's three numbers

    Same code, different numbers: A = (2 − 10T)/(2 + 10T), B0 = 50(2 + T)/(2 + 10T), B1 = −50(2 − T)/(2 + 10T). At T = 0.02 s: A = 0.818, B0 = 45.9, B1 = −45.0. The exact pole e^(−0.2) is 0.819.

    import math
    K, a, p, T = 50, 1, 10, 0.02
    A, B0, B1 = (2-p*T)/(2+p*T), K*(2+a*T)/(2+p*T), -K*(2-a*T)/(2+p*T)  # Tustin
    y = v = u1 = e1 = 0.0
    peak, h, us = 0.0, 0.005, []
    for k in range(round(3 / T)):
        e = 1.0 - y                    # sample the position
        u = A*u1 + B0*e + B1*e1        # the difference equation
        u1, e1 = u, e
        us.append(u)
        for _ in range(round(T / h)):  # motor y'' + y' = u, u held
            E = math.exp(-h)
            y, v = y + v*(1 - E) + u*(h - 1 + E), v*E + u*(1 - E)
            peak = max(peak, y)
    print(round(peak, 3))    # 1.058
    
    A = 1.8/2.2 = 0.818   B0 = 45.9   B1 = -45.0
    Euler's A = 0.80; exact e^(-0.2) = 0.819
  7. Sample fast: both work

    Run both at T = 0.02 s, fifty samples a second. Both stay close to the continuous design: peaks 1.043 (Euler) and 1.058 (Tustin) against 1.043. When T is small next to the system's time scales, any sensible recipe works.

    T = 0.02 s (50 Hz)
    
                  peak     controller pole
    continuous    1.043    s = -10
    Euler         1.043    z = 0.80
    Tustin        1.058    z = 0.818
    exact e^(-pT)          z = 0.819
    
    The small extra overshoot comes mostly from the
    DAC holding u for T: on average half a sample late.
  8. Where the pole lands

    Now slow the sampling and follow the controller's pole. A difference equation is stable only while its pole stays inside |z| < 1. Euler's 1 − 10T passes −1 at T = 0.2 s. Tustin's (2 − 10T)/(2 + 10T) never reaches −1.

    controller pole against sample period T
    
    T (s)   exact     Euler     Tustin
    0.02    0.819     0.800     0.818
    0.10    0.368     0.000     0.333
    0.20    0.135    -1.000    0.000
    0.25    0.082    -1.500    -0.111
    
    stable while |z| < 1
    Euler:  |1 - 10T| < 1   ⇔   T < 2/10 = 0.2 s
    Tustin: |2 - 10T| < 2 + 10T for every T > 0
  9. Sample slowly: Euler blows up

    At T = 0.25 s Euler sets A = −1.50: every sample the controller multiplies its last output by −1.50. The drive signal u goes 50, −135, 268, −541 and keeps growing, flipping sign each time. The motor leaves ±2 after 0.73 s.

    import math
    K, a, p, T = 50, 1, 10, 0.25
    A, B0, B1 = 1 - p*T, K, -K*(1 - a*T)                     # forward Euler
    y = v = u1 = e1 = 0.0
    peak, h, us = 0.0, 0.005, []
    for k in range(round(3 / T)):
        e = 1.0 - y                    # sample the position
        u = A*u1 + B0*e + B1*e1        # the difference equation
        u1, e1 = u, e
        us.append(u)
        for _ in range(round(T / h)):  # motor y'' + y' = u, u held
            E = math.exp(-h)
            y, v = y + v*(1 - E) + u*(h - 1 + E), v*E + u*(1 - E)
            peak = max(peak, y)
    print([round(x) for x in us[:4]])   # [50, -135, 268, -541]
    
    A = 1 - 10·0.25 = -1.50  (outside the unit circle)
    the stable C(s) became an unstable C(z)
  10. Tustin bends but holds

    At T = 0.25 s Tustin gives A = −0.111, inside the circle: it settles, but overshoots 53%. The hold adds a delay the design never saw. Tustin kept it stable. To keep it accurate, sample faster or design the controller with the hold included.

    import math
    K, a, p, T = 50, 1, 10, 0.25
    A, B0, B1 = (2-p*T)/(2+p*T), K*(2+a*T)/(2+p*T), -K*(2-a*T)/(2+p*T)  # Tustin
    y = v = u1 = e1 = 0.0
    peak, h, us = 0.0, 0.005, []
    for k in range(round(3 / T)):
        e = 1.0 - y                    # sample the position
        u = A*u1 + B0*e + B1*e1        # the difference equation
        u1, e1 = u, e
        us.append(u)
        for _ in range(round(T / h)):  # motor y'' + y' = u, u held
            E = math.exp(-h)
            y, v = y + v*(1 - E) + u*(h - 1 + E), v*E + u*(1 - E)
            peak = max(peak, y)
    print(round(peak, 2))    # 1.53
    
    A = (2 - 2.5)/(2 + 2.5) = -0.111   B0 = 25.0   B1 = -19.44
    exact e^(-2.5) = 0.082
    peak 1.53 (continuous design 1.043)
  11. Why Tustin is the safe choice

    Euler maps the stable left half s-plane onto everything left of z = 1, which spills outside the unit circle. Tustin maps it exactly onto the inside of the circle, so a stable C(s) always gives a stable C(z). The price: warped frequencies.

    s-plane left half (Re s < 0) maps to
      Euler    z = 1 + sT              Re z < 1  (too big)
      Tustin   z = (1 + sT/2)/(1 - sT/2)  |z| < 1   (exactly)
    
    Tustin's warp: a frequency w in C(s) appears at
      (2/T)·atan(w·T/2)  in C(z); 'prewarping' fixes one w.
    
    in practice (python-control):
      import control as ct
      C = ct.tf([50, 50], [1, 10])
      Cd = ct.sample_system(C, 0.02, method='tustin')
      # method='euler' gives forward Euler

اختبار يتاح بعد الخطوة الأخيرة.