TExcelTAD
Home › Control Systems & Automation › Sampled-Data Loops: Sampling, Zero-Order Hold and Sample Rate

Sampled-Data Loops: Sampling, Zero-Order Hold and Sample Rate

The PI speed controller of the Bode lesson was designed in continuous time with a phase margin of 81∘81^\circ. On the bench it runs on a microcontroller: every TsT_s seconds an ADC or encoder counter samples the speed, the code computes a new duty cycle, and the PWM holds it until the next update. At 1 kHz1\ \text{kHz} the loop behaves almost like the design. At 200 Hz200\ \text{Hz} it overshoots by a third. With one sample of computation delay at 200 Hz200\ \text{Hz} it is unstable. What changed is not the controller but the sampling. This lesson shows how to model a computer in the loop, how much phase the sampling costs and how to choose the sample rate. The z-transform itself is in Signals and Systems, sampling and aliasing in The Sampling Theorem in Practice.

The sampled-data loop

r[k] + - C(z) controller u[k] DAC + ZOH hold Ts u(t) G(s) plant y(t) ADC sample Ts y[k] 0 2 4 6 8 10 t / Ts staircase = ZOH output smooth command

A digital loop mixes two worlds. The plant lives in continuous time; the controller sees only samples y[k]=y(kTs)y[k] = y(kT_s) and outputs a sequence u[k]u[k]; the DAC (or PWM) applies a zero-order hold (ZOH): u(t)=u[k]u(t) = u[k] for kTs≤t<(k+1)TskT_s \le t < (k + 1)T_s. Seen from the samples, the plant plus ZOH is exactly a discrete-time system:

Gd(z)=(1−z−1) Z{G(s)s},every plant pole p maps to z=epTsG_d(z) = (1 - z^{-1})\,\mathcal{Z}\left\{\frac{G(s)}{s}\right\}, \qquad \text{every plant pole } p \text{ maps to } z = e^{p T_s}

This ZOH discretisation (ct.c2d(G, Ts, 'zoh')) is exact at the sampling instants for any input that the ZOH produces. The controller, on the other hand, is usually designed in continuous time and converted with an approximation, most often Tustin's bilinear rule s→2Tsz−1z+1s \to \frac{2}{T_s}\frac{z - 1}{z + 1} (Bilinear Transform), and checked on the discrete loop.

What sampling costs: delay

On average the staircase lags the smooth signal by half a sample: the ZOH behaves approximately like a delay e−sTs/2e^{-sT_s/2}. If the code reads the input, computes, and writes the output at the next sampling instant (a common, deterministic scheme), add one full sample. The phase lost at the crossover frequency ωc\omega_c is about

Δϕ≈ωc Ts(12+nd) rad,nd=computation delay in samples (0 or 1)\Delta\phi \approx \omega_c\,T_s\left(\frac{1}{2} + n_d\right) \ \text{rad}, \qquad n_d = \text{computation delay in samples (0 or 1)}

The ZOH also adds a gain droop of sin⁡(ωTs/2)ωTs/2\frac{\sin(\omega T_s/2)}{\omega T_s/2} and the sampling folds high-frequency measurement noise down to low frequencies (aliasing), which the loop then dutifully follows.

Choosing the sample rate

Rule of thumb Meaning
ωs=2π/Ts≥10\omega_s = 2\pi/T_s \ge 10 to 30×30 \times the closed-loop bandwidth keeps the ZOH phase loss ωcTs/2=π ωc/ωs\omega_c T_s/2 = \pi\,\omega_c/\omega_s at crossover to about 18∘18^\circ (10×) down to 6∘6^\circ (30×)
4 to 10 samples per rise time the step response looks smooth
an analog anti-alias filter below fs/2f_s/2, well above the bandwidth noise above fs/2f_s/2 is removed before it folds
inner loops sampled faster than outer loops (5–10×) a current loop at 10–20 kHz, speed at 1–2 kHz, position at 100–500 Hz in a typical drive

Too fast is also a problem: the discrete poles crowd towards z=1z = 1, coefficients of slow filters need many bits (Discrete PID), and a derivative computed from samples one encoder count apart is mostly quantisation noise.

Worked example: the motor speed loop at three sample rates

Given: the reference motor G(s)G(s), PI Kp=0.0575 V/(rad/s)K_p = 0.0575\ \text{V/(rad/s)}, Ki=6.105 V/radK_i = 6.105\ \text{V/rad}, ωc=300 rad/s\omega_c = 300\ \text{rad/s}, continuous PM=81.0∘PM = 81.0^\circ.

Find: the phase margin and step overshoot when the loop runs at Ts=0.2T_s = 0.2, 11 and 5 ms5\ \text{ms}, with and without one sample of computation delay.

Solve (estimate): ωcTs/2=1.7∘\omega_c T_s/2 = 1.7^\circ, 8.6∘8.6^\circ and 43∘43^\circ for the three rates; one extra sample of delay adds twice that. At 1 ms1\ \text{ms}: 81−8.6=72.4∘81 - 8.6 = 72.4^\circ without delay, 81−25.8=55.2∘81 - 25.8 = 55.2^\circ with it. At 5 ms5\ \text{ms} with delay the loss is 129∘129^\circ: unstable.

Check: the program discretises the plant with ZOH and the PI with Tustin, and computes the margins from the discrete frequency response L(ejωTs)L(e^{j\omega T_s}) up to the Nyquist frequency:

python · try it yourself
import numpy as np
import control as ct

J, b, Ra, La, k = 2e-6, 1e-6, 2.0, 1e-3, 0.02
G = ct.tf([k], np.polyadd(np.polymul([J, b], [La, Ra]), [k * k]))
C = ct.tf([0.05754, 6.105], [1, 0])

def margins(L, Ts):
    w = np.linspace(1, np.pi / Ts, 200000)
    Lw = L(np.exp(1j * w * Ts))
    ic = np.argmin(abs(abs(Lw) - 1))
    return w[ic], (np.degrees(np.angle(Lw[ic])) + 360) % 360 - 180    # PM in (-180, 180]

print("plant poles mapped by z = exp(p Ts) at 1 ms:",
      np.round(np.sort(ct.poles(ct.c2d(G, 1e-3, 'zoh')).real), 5), np.round(np.sort(np.exp(ct.poles(G).real * 1e-3)), 5))
for Ts in (0.2e-3, 1e-3, 5e-3):
    Gd, Cd = ct.c2d(G, Ts, 'zoh'), ct.c2d(C, Ts, 'tustin')
    for nd in (0, 1):
        L = Cd * Gd * (ct.tf([1], [1, 0], Ts) if nd else 1)
        wc, pm = margins(L, Ts)
        T = ct.feedback(L, 1)
        stable = bool(np.all(abs(ct.poles(T)) < 1))
        txt = "unstable"
        if stable:
            _, y = ct.step_response(T, T=np.arange(0, 0.1, Ts))
            txt = f"overshoot {100 * (y.max() - 1):5.1f} %"
        print(f"Ts = {1e3 * Ts:3.1f} ms, compute delay {nd}: PM {pm:6.1f} deg at {wc:5.1f} rad/s, {txt}")
plant poles mapped by z = exp(p Ts) at 1 ms: [0.15041 0.89933] [0.15041 0.89933]
Ts = 0.2 ms, compute delay 0: PM   79.3 deg at 300.0 rad/s, overshoot   0.0 %
Ts = 0.2 ms, compute delay 1: PM   75.9 deg at 300.0 rad/s, overshoot   0.0 %
Ts = 1.0 ms, compute delay 0: PM   72.6 deg at 298.8 rad/s, overshoot   0.0 %
Ts = 1.0 ms, compute delay 1: PM   55.5 deg at 298.8 rad/s, overshoot  11.7 %
Ts = 5.0 ms, compute delay 0: PM   41.0 deg at 302.8 rad/s, overshoot  36.5 %
Ts = 5.0 ms, compute delay 1: PM  -45.7 deg at 302.8 rad/s, unstable

The estimate ωcTs(12+nd)\omega_c T_s(\frac{1}{2} + n_d) predicts the losses within a few degrees. A 1 kHz1\ \text{kHz} loop with one sample of delay keeps 55∘55^\circ; the phase margin of the 5 ms5\ \text{ms} loop with delay is negative (about −46∘-46^\circ). 200 Hz200\ \text{Hz} is not enough for a 300 rad/s300\ \text{rad/s} (48 Hz48\ \text{Hz}) crossover: the sampling frequency is only about four times the crossover frequency.

Lab: aliasing in a sampled loop

A temperature loop samples its sensor at 10 Hz10\ \text{Hz}, and 49 Hz49\ \text{Hz} interference from a nearby supply (or mains at 50 Hz50\ \text{Hz} with a slightly fast sampling clock) reaches the ADC. In the sampling lab with f=49 Hzf = 49\ \text{Hz} and fs=10 Hzf_s = 10\ \text{Hz}, the samples trace a 1.000 Hz1.000\ \text{Hz} sine: an alias well inside the loop's bandwidth, which the controller tries to "correct" by moving the heater. With the lab's 4th-order anti-alias filter at 0.4fs0.4 f_s the interference is attenuated by 87.05 dB87.05\ \text{dB} before sampling. A real loop also averages: sampling an integrating ADC over whole mains periods rejects mains frequencies.

-A0+A0.0 s0.2 s0.4 s0.6 s0.8 s1.0 sx (V)input 49.0 Hzreconstructed 1.00 Hzsamples, fs = 10.0 Hz0A0102030405060708090100f (Hz)fs/2faliasaliased: f > fs/2 = 5.00 Hz

Guided lab: lower the sample rate of the speed loop

Hand calculation. For your speed loop's crossover, compute the phase loss at 22, 11, 0.50.5 and 0.2 kHz0.2\ \text{kHz} with nd=1n_d = 1; find the rate at which the margin drops below 45∘45^\circ.

Simulation. Run the program with your gains; add your PWM frequency's own delay (half a PWM period for a centre-aligned update) if it is not much faster than TsT_s.

What you would measure. The motor on a current-limited 12 V12\ \text{V} supply with an encoder and H-bridge, the PI on a microcontroller with a timer interrupt whose period you can change. Step the speed setpoint by 100 rad/s100\ \text{rad/s} at each rate and record the overshoot. Expect the overshoot to stay small down to about 1 kHz1\ \text{kHz} and to grow quickly below it; the loop should become oscillatory near the rate the hand calculation predicts (within 20–30 %; encoder quantisation adds noise at low speeds).

Hazards and safe setup. Current limit about 1 A1\ \text{A}, motor clamped, no load on the shaft, an enable switch; start each test at the highest rate and stop lowering it as soon as the loop oscillates.

Pitfalls

  • Designing in continuous time and forgetting the half-sample ZOH delay and the computation delay.
  • No anti-alias filter: slow, mysterious wandering of the controlled variable.
  • Sampling a loop "as fast as possible": slow-filter coefficients lose precision, and the derivative turns into noise.
  • Jitter: an update period that varies (a busy main loop instead of a timer interrupt) acts like varying delay and noise.

Practice

  1. A loop crosses over at ωc=50 rad/s\omega_c = 50\ \text{rad/s} and has PM=60∘PM = 60^\circ in continuous time. It runs at Ts=4 msT_s = 4\ \text{ms} with one sample of computation delay. Estimate the new phase margin.
  2. Where does the plant pole s=−20 s−1s = -20\ \text{s}^{-1} go for Ts=10 msT_s = 10\ \text{ms}?
  3. A closed-loop bandwidth of 5 Hz5\ \text{Hz} is required. Suggest a sample rate and an anti-alias corner.
Answer
  1. 50×0.004×1.5=0.3 rad=17.2∘50 \times 0.004 \times 1.5 = 0.3\ \text{rad} = 17.2^\circ: PM≈42.8∘PM \approx 42.8^\circ.
  2. z=e−0.2=0.819z = e^{-0.2} = 0.819.
  3. fsf_s of 50 to 150 Hz (10 to 30 times), e.g. 100 Hz100\ \text{Hz}, and an anti-alias filter with its corner around 2020 to 30 Hz30\ \text{Hz} (above the bandwidth, below fs/2=50 Hzf_s/2 = 50\ \text{Hz}).

Test yourself

On average, a zero-order hold behaves like a delay of

  1. one sample
  2. half a sample
  3. two samples
  4. no delay

With ZOH discretisation, a continuous pole pp maps to

  1. z=1+pTsz = 1 + pT_s
  2. z=epTsz = e^{pT_s}
  3. z=1/(1−pTs)z = 1/(1 - pT_s)
  4. z=pTsz = p T_s

A loop with crossover 300 rad/s sampled at 1 ms with one sample of computation delay loses about

  1. 3∘3^\circ
  2. 9∘9^\circ
  3. 26∘26^\circ
  4. 90∘90^\circ

Why put an analog anti-alias filter in front of the ADC of a control loop?

  1. to raise the gain
  2. so that noise above fs/2f_s/2 does not fold into the loop's bandwidth
  3. to remove the ZOH delay
  4. to linearise the plant