Digital control: sampling, discretisation and anti-windup
A controller running on an MCU is not continuous in time; it is a calculation at a fixed period. Choosing the sampling period, and the traps in implementation.
Sampling period
The rule of thumb is to sample at 10 to 20 times the control bandwidth.
| Loop | Bandwidth | Example rate |
|---|---|---|
| Motor current | ~1 kHz | 10 to 20 kHz (synchronised with PWM) |
| Motor speed | ~100 Hz | 1 to 2 kHz |
| Position / attitude | ~10 Hz | 100 Hz to 1 kHz |
| Temperature | ~0.1 Hz | 1 to 10 Hz |
Discretisation
Turn the continuous controller into difference equations.
- Integral:
integ += e * Ts(Euler) or trapezoidal - Derivative:
(e - e_prev) / Ts
Ts must be the actual period. If the period jitters, fix it exactly with a timer.
RoadmapSee where this note sits in Control Engineering→Anti-windup
If the integrator keeps accumulating while the output is saturated (100% PWM), then after the error shrinks the stored integral makes the output run past the target for a long time.
- Clamping: stop integrating in the direction of saturation
- Back-calculation: pull the integrator back by the amount that saturation cut off
Other traps
- The calculation time adds delay. Keep measure → calculate → output at the same timing every period
- On an MCU without floating point, handle fixed-point scaling and saturation
- When changing gains at run time, keep the output from jumping (correct the integrator)