ENع
→Sampled loop
الدرس 48 · State space and digital control

The sampled control loop: ADC, computer, DAC

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

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

A real controller is a program running on a clock. Follow one tick of a computer-controlled motor: the ADC takes a snapshot of the speed and rounds it to a number, a few lines of Python decide a voltage, and the DAC holds that voltage flat until the next tick. Then let it run, write the exact tick-to-tick model of the motor, and see why the same rule that settles calmly at 10 ms rings at 80 ms and goes unstable beyond 102 ms.

ستتعلّم

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

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

A motor in a smooth world

Our plant is a small DC motor spinning at 1000 rpm, and we want 1500 rpm. Its speed changes smoothly at every instant. A tachometer, a speed sensor, turns it into a voltage: 1 V per 1000 rpm. A computer cannot read that directly.

plant    DC motor, speed 1000 rpm now
goal     1500 rpm
sensor   tachometer, 1 V per 1000 rpm
         1000 rpm → 1.000 V

motor model  200 ms time constant,
             100 rpm per volt when settled

The speed is an analog signal: it has a
value at every instant. A computer only
handles numbers, one at a time.
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A motor in a smooth world

    Our plant is a small DC motor spinning at 1000 rpm, and we want 1500 rpm. Its speed changes smoothly at every instant. A tachometer, a speed sensor, turns it into a voltage: 1 V per 1000 rpm. A computer cannot read that directly.

    plant    DC motor, speed 1000 rpm now
    goal     1500 rpm
    sensor   tachometer, 1 V per 1000 rpm
             1000 rpm → 1.000 V
    
    motor model  200 ms time constant,
                 100 rpm per volt when settled
    
    The speed is an analog signal: it has a
    value at every instant. A computer only
    handles numbers, one at a time.
  2. A clock sets the beat

    A clock ticks every T = 10 ms. T is the sample period; 100 ticks per second is the sample rate. Each tick starts one round of the loop. Between ticks the computer learns nothing new about the motor.

    sample period   T = 10 ms
    sample rate     1 / T = 100 per second
    
    tick k happens at t = k · T
      k = 0   t = 0 ms
      k = 1   t = 10 ms
      k = 2   t = 20 ms ...
    
    Every value the computer uses gets a
    tick number: speed[k], volts[k].
  3. ADC: snapshot and round

    On a tick the ADC (analog-to-digital converter) takes a snapshot of the sensor voltage and rounds it down to one of 1024 levels (10 bits). One level is 2.93 rpm, so 1000 rpm becomes code 341.

    ADC   10 bits = 2^10 = 1024 levels over 0 to 3 V
          one level = 3 V / 1024 = 2.93 mV = 2.93 rpm
    
    sample   1.000 V
    code     floor(1.000 / 3 × 1024) = 341
    
    Sampling picks the instants;
    quantising (rounding to a level)
    picks the value. Both lose a little.
  4. The computer runs the rule

    The computer turns code 341 back into 999.0 rpm and finds the error, 501.0 rpm. Its rule: 15 V, enough to hold 1500 rpm, plus 0.04 V for every rpm of error. That asks for 35.04 V.

    speed = 341 * 3000 / 1024   = 999.0 rpm
    error = 1500 - 999.0       = 501.0 rpm
    volts = 15 + 0.04 * 501.0 = 35.04 V
    
    15 V      feedforward: 1500 / 100 rpm per V
    0.04 V    per rpm of error (proportional)
    loop gain 100 × 0.04 = 4
  5. DAC: a number becomes volts

    The computer writes the result to the DAC (digital-to-analog converter). A 12-bit DAC with its amplifier makes 0 to 48 V in 4096 steps, so it receives code 2990 and puts out 35.04 V to the motor.

    DAC   12 bits = 4096 levels over 0 to 48 V
          one level = 11.7 mV
    
    code   round(35.04 / 48 × 4096) = 2990
    out    2990 × 11.72 mV = 35.04 V
    
    We assume the computing takes almost
    no time compared with T.
  6. Hold until the next tick

    The DAC keeps 35.04 V perfectly flat for the whole 10 ms. Holding each value for one period is a zero-order hold. The motor speeds up on its own curve, reaching 1122 rpm. The computer is blind until the next tick.

    zero-order hold (ZOH)
      volts(t) = volts[k]  for kT ≤ t < (k+1)T
    
    during tick 0:
      volts held at 35.04 V
      the motor heads for 100 × 35.04 = 3504 rpm
      after 10 ms it is at 1122 rpm
    
    The voltage the motor sees is a staircase.
  7. Tick after tick

    Sample, compute, hold, repeat. The steps of voltage shrink as the error shrinks. After 13 ticks (130 ms) the speed stays within 1% of 1500 rpm. The dots on the plot are the only speeds the computer ever saw.

    import math
    TAU, K, T = 0.2, 100, 0.01     # s, rpm per V, s
    a = math.exp(-T / TAU)
    speed = 1000.0
    for k in range(13):
        code = int(speed / 1000 / 3.0 * 1024)  # ADC
        meas = code * 3000 / 1024
        volts = 15 + 0.04 * (1500 - meas)
        volts = round(volts / 48 * 4096) * 48 / 4096  # DAC
        speed = K * volts + (speed - K * volts) * a  # hold
    print(round(speed))                 # 1488
  8. An exact model at the ticks

    Because the voltage is flat between ticks, the motor can be solved exactly from tick to tick: speed[k+1] = a·speed[k] + (1 − a)·100·volts[k], with a = e^(−T/τ) = 0.951. τ, the motor's time constant, is 0.2 s. This is the ZOH equivalent.

    motor        τ · d(speed)/dt = 100 · volts − speed
    held volts   solve over one period T:
    
    speed[k+1] = a · speed[k] + (1 − a) · 100 · volts[k]
    a = e^(−T/τ) = e^(−0.01/0.2) = 0.951
    
    In state-space form A_d = e^(AT),
    B_d = A⁻¹(A_d − I)B. In python-control:
    control.sample_system(sys, 0.01, method="zoh")
  9. The loop as one number

    Put the rule into that equation (ignore the rounding) and the error obeys error[k+1] = z·error[k], with z = a − 4(1 − a) = 0.756. Each tick keeps 75.6% of the error. |z| < 1 means the error dies away.

    volts[k] = 15 + 0.04 · (1500 − speed[k])
    
    error[k+1] = (a − 4(1 − a)) · error[k]
               = z · error[k]
    
    T = 10 ms   a = 0.951   z = 0.756
    
    z is the closed-loop pole in the z-plane.
    Inside the unit circle (|z| < 1): stable.
    Positive z: the error shrinks without
    changing sign.
  10. Sample too slowly

    Same rule, but the clock ticks every 80 ms. Now a = 0.670 and z = −0.648: the error flips sign every tick. The 35 V held for 80 ms throws the speed to 1825 rpm, then the voltage drops to 1.99 V. It rings.

    T = 80 ms   a = 0.670   z = −0.648
    
    negative z: the error changes sign
    every tick (overshoot, undershoot ...)
    |z| = 0.648: it still shrinks, slowly
    
    import math
    TAU, K, T = 0.2, 100, 0.08     # s, rpm per V, s
    a = math.exp(-T / TAU)
    speed = 1000.0
    for k in range(1):
        code = int(speed / 1000 / 3.0 * 1024)  # ADC
        meas = code * 3000 / 1024
        volts = 15 + 0.04 * (1500 - meas)
        volts = round(volts / 48 * 4096) * 48 / 4096  # DAC
        speed = K * volts + (speed - K * volts) * a  # hold
    print(round(speed))                 # 1825
  11. How fast is fast enough?

    z reaches −1 when T = τ·ln(5/3) = 102 ms; any slower and the loop is unstable. Run continuously, this rule could never ring. Sampling adds a delay, so tick several times faster than the loop responds: here 10 ms against 40 ms.

    |z| < 1  ⇔  a − 4(1 − a) > −1
             ⇔  a > 3/5
             ⇔  T < τ · ln(5/3) = 102 ms
    
    continuous rule:   pole s = −(1 + 4)/τ
                       = −25 per second
                       time constant 40 ms
    
    T = 10 ms   z = 0.756   calm
    T = 80 ms   z = −0.648  rings
    T > 102 ms  |z| > 1   unstable

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