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 . On the bench it runs on a microcontroller: every 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 the loop behaves almost like the design. At it overshoots by a third. With one sample of computation delay at 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
A digital loop mixes two worlds. The plant lives in continuous time; the controller sees only samples and outputs a sequence ; the DAC (or PWM) applies a zero-order hold (ZOH): for . Seen from the samples, the plant plus ZOH is exactly a discrete-time system:
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 (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 . 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 is about
The ZOH also adds a gain droop of 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 |
|---|---|
| to the closed-loop bandwidth | keeps the ZOH phase loss at crossover to about (10×) down to (30×) |
| 4 to 10 samples per rise time | the step response looks smooth |
| an analog anti-alias filter below , well above the bandwidth | noise above 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 , 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 , PI , , , continuous .
Find: the phase margin and step overshoot when the loop runs at , and , with and without one sample of computation delay.
Solve (estimate): , and for the three rates; one extra sample of delay adds twice that. At : without delay, with it. At with delay the loss is : unstable.
Check: the program discretises the plant with ZOH and the PI with Tustin, and computes the margins from the discrete frequency response up to the Nyquist frequency:
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 predicts the losses within a few degrees. A loop with one sample of delay keeps ; the phase margin of the loop with delay is negative (about ). is not enough for a () 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 , and interference from a nearby supply (or mains at with a slightly fast sampling clock) reaches the ADC. In the sampling lab with and , the samples trace a 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 the interference is attenuated by before sampling. A real loop also averages: sampling an integrating ADC over whole mains periods rejects mains frequencies.
Guided lab: lower the sample rate of the speed loop
Hand calculation. For your speed loop's crossover, compute the phase loss at , , and with ; find the rate at which the margin drops below .
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 .
What you would measure. The motor on a current-limited 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 at each rate and record the overshoot. Expect the overshoot to stay small down to about 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 , 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
- A loop crosses over at and has in continuous time. It runs at with one sample of computation delay. Estimate the new phase margin.
- Where does the plant pole go for ?
- A closed-loop bandwidth of is required. Suggest a sample rate and an anti-alias corner.
Answer
- : .
- .
- of 50 to 150 Hz (10 to 30 times), e.g. , and an anti-alias filter with its corner around to (above the bandwidth, below ).
Test yourself
On average, a zero-order hold behaves like a delay of
- one sample
- half a sample
- two samples
- no delay
With ZOH discretisation, a continuous pole maps to
A loop with crossover 300 rad/s sampled at 1 ms with one sample of computation delay loses about
Why put an analog anti-alias filter in front of the ADC of a control loop?
- to raise the gain
- so that noise above does not fold into the loop's bandwidth
- to remove the ZOH delay
- to linearise the plant