Ground Embedded KO Subscribe

Notes / Control Engineering

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.

GROUND EMBEDDED · NOTEControl Engineering 09Digital controlTargetKpKiKd

Sampling period

Timer interruptRead ADCControl calculationUpdate PWMWait for the next period
What happens in one control period. It has to repeat at the same interval every time.

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

Output at limitIntegrating on (bad)Integration held (good)
Stop integrating while the output is at its limit. Otherwise the response is late until the stored value has unwound.

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)
FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →