D3 · Lesson 8 · Digital control
Controllers that run as code on a microcontroller, sampling and the zero-order hold, the half-sample delay a hold adds, PID written as a difference equation, mapping poles from s to z, and a sampled loop that goes unstable when its gain times its sample period exceeds two
Nearly every controller built today is a few lines of code in a loop: read the sensor through an ADC, compute the control law, write the result to a PWM or DAC, wait for the next tick. The continuous-time ideas of the earlier lessons carry over almost unchanged as long as the loop runs fast enough. When it doesn't, sampling shows up as a delay, and delay, as lesson 7 showed, eats phase margin. A sampled loop can go unstable at a gain the continuous design would have tolerated forever.
- A sampled loop around an integrating plant (for example, a motor's speed driven by a current command), with proportional gain K and sample period T: .
- The closed-loop pole is at . For KT < 1 the output creeps up smoothly; KT = 1 reaches the setpoint in one sample (deadbeat); between 1 and 2 it overshoots and alternates; above 2 it diverges.
- The continuous version of this loop is stable at any gain: the instability comes entirely from sampling.
Predict
A sampled proportional loop around an integrator runs every 10 ms. Above what gain K does it go unstable?
The pole is at z = 1 − KT, and a discrete system is stable while all its poles lie inside the unit circle, |z| < 1. That requires 0 < KT < 2, so K < 2/0.01 = 200 s⁻¹. Halving the sample period doubles the allowed gain.
Name it
Sampling and the hold
The controller sees the measurement only every T seconds, and a zero-order hold (the DAC or PWM register) keeps each output constant until the next update. On average the hold delays the signal by half a sample, costing phase
at frequency ω. A common rule is to sample 10 to 30 times faster than the closed-loop bandwidth, which keeps that loss at the crossover to a few degrees.
From s to z
A continuous pole at s maps to a discrete pole at . The left half of the s-plane maps inside the unit circle, so a discrete system is stable when every pole has |z| < 1 (C9's z-plane).
PID as code
Approximating the integral by a running sum and the derivative by a difference gives
In practice the derivative is filtered, the integrator is clamped when the output saturates (anti-windup), and the loop runs from a timer interrupt so that T is steady (C12).
Worked example
A loop crosses over at 50 rad/s and is sampled every 2 ms. How much phase does the hold cost? Where does a continuous pole at s = −10 land?
Show the solution
- Phase cost of the hold. or 2.9°, a small price.
- Mapping the pole. With T = 2 ms: close to 1, since the pole's time constant (100 ms) is fifty samples long.
Answer the hold costs 2.9° of phase, and the pole lands at z = 0.980.
Worked example
The integrator loop runs with K = 50 s⁻¹ and T = 10 ms. Where's its pole, and how does it respond to a step?
Show the solution
- The pole. so z = 0.5.
- Step response. Each sample halves the remaining error. After four samples (40 ms) it has reached of the step.
Answer the pole is at z = 0.5, and the output reaches 94% of the step after four samples (40 ms).
Worked example
Map a continuous pole at s = −100 with T = 10 ms, exactly and by the Tustin (bilinear) approximation.
Show the solution
- Exact mapping.
- Tustin.
- Compare. The two agree for slow poles and drift apart as grows. Tustin keeps every stable pole inside the unit circle, which is why it's used to convert continuous designs.
Answer z = 0.368 exactly, and 0.333 by Tustin.
Check
A loop crosses over at 100 rad/s and is sampled every 0.5 ms. About how much phase does the zero-order hold cost at crossover?
Check
A continuous pole at s = −10 s⁻¹ is sampled every 1 ms. Where does it land in the z-plane?
Check
A sampled proportional loop around an integrator runs every 5 ms. Above what gain does it go unstable?
Check
A loop needs a closed-loop bandwidth of 20 Hz, and you want to sample 10 times faster than that. What sample rate do you need?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A continuous pole at s = −50 is sampled every 1 ms.
(a)
Where does it map exactly?
(b)
Where does Tustin put it?
Check
A loop crossing over at 50 rad/s is sampled every 1 ms.
(a)
How much phase does the zero-order hold cost?
(b)
How many times the crossover is the sample rate (in rad/s)?
Explain why
Why can sampling make a feedback loop unstable when the continuous version wasn't?
A sampled controller only learns about the output at discrete instants and holds its output constant between updates, so it always acts on information that is up to one sample old and keeps applying a correction after the plant has moved. That staleness behaves like a delay of about half a sample, which adds phase lag that grows with frequency and erodes the loop's phase margin. At high gain the controller overcorrects on each sample: in the simplest case each update overshoots the setpoint by more than the previous error, and the alternating error grows. Sampling faster shrinks the delay and pushes this limit to higher gains.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.