C1246 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 46 · Control loops in firmware

Sampled feedback, fixed-point PI and windup

A motor holding its speed, a heater holding a temperature, a power supply holding its voltage: each is a feedback loop, and on a microcontroller it's a few lines in a timer interrupt. Read the sensor, compare with the set point, compute an output, write it to the PWM. The control theory carries over from continuous time with three twists that come from the processor: the loop only acts at sample instants, every sample adds delay, and the arithmetic is fixed point. Each one changes the gains a loop can use.

  • The stems are a sampled loop's step response: a first-order plant with a 20 ms time constant, sampled every 1 ms, under proportional control with gain K.
  • Small K: slow, with a large steady-state error. Raise K: faster and closer to the set point. Past K = 20 the response alternates sign each sample; past K = 40 it grows without limit.
  • The readouts give the loop's discrete pole, whose size sets the decay per sample, and the steady-state error.

Predict

A speed loop runs at 1 kHz and its crossover is 50 Hz. The ADC sample, the computation and the PWM update together delay the output by about 1.5 samples. How much phase margin does that cost?

Name it

The sampled loop

At each sample: error en=r−yn; output un=Kpen+In; integrator In+1=In+KiTsen. A first-order plant τy˙=Gu−y sampled every Ts becomes yn+1=ayn+bun with a=e−Ts/τ and b=G(1−a). Under proportional control the loop's pole is a−bK:

  • |a−bK|<1 for stability, so K<(1+a)/b;
  • a negative pole alternates sign each sample;
  • the steady-state error is 1/(1+KG), which the integrator removes.

Delay

Everything between reading the sensor and changing the output is delay: conversion, computation, and the zero-order hold of the PWM, worth half a sample on average. A total delay Td costs 360°fcTd of phase margin at the crossover fc.

Fixed point

Gains are stored as integers with fraction bits: Kp=0.75 in Q8 is 192, and u=(192e)≫8. Small integral gains need more fraction bits (KiTs=0.002 in Q16 is 131, an error of 0.05%) and a wide accumulator.

Windup

When the output saturates (PWM at 100%), the error persists and the integrator keeps growing; when the error finally reverses, the integrator must unwind before the output leaves the limit, causing a large overshoot. Anti-windup: stop integrating while the output is saturated in the direction of the error, or clamp the integrator.

Worked example

The play's plant (τ = 20 ms, Ts = 1 ms, G = 1) runs with K = 4. What are its pole, its steady-state error and its gain limit?

Show the solution
  1. Plant coefficients. With Ts/τ=0.05: a=e−0.05=0.951 and b=0.049.
  2. The pole. p=a−bK=0.95123−0.04877×4=0.756 so errors shrink by a quarter each sample.
  3. Steady-state error. 11+4=20%
  4. Gain limit. K<1+ab=40.0
  5. Where it starts to alternate. Past K=a/b=19.5 the pole goes negative and the output alternates.

Answer the pole is 0.756, the steady-state error is 20%, and the gain limit is K < 40.0.

Worked example

Encode Kp=0.75 in Q8 and KiTs=0.002 in Q16. What does an error of 100 codes produce from the proportional term?

Show the solution
  1. Encode Kp in Q8. 0.75×256=192 exactly.
  2. Encode KiTs in Q16. 0.002×65536=131.07 stored as 131, which is 0.0019989, 0.05% low.
  3. The proportional term. (192×100)≫8=75
  4. Why the integrator gets more fraction bits. With Q8, the smallest nonzero integral gain would be 1/256 = 0.0039, twice too big.

Answer Kp is 192 in Q8 and KiTs is 131 in Q16, and an error of 100 codes gives a proportional term of 75.

Worked example

A heater's PWM saturates at 100% for 30 s while a cold tank warms up, with an error of 500 codes, and the integrator adds KiTse = 0.002 × 500 each millisecond. How much does it wind up?

Show the solution
  1. What each sample adds. 0.002×500=1.0
  2. Over 30 s. 30,000 samples add 30,000 output units, far beyond the 100% limit.
  3. What happens next. When the tank reaches temperature, the output stays saturated until that has unwound, so the tank overshoots badly.
  4. The fix. Conditional integration (skipping the integrator update while the output is saturated and the error would push it further) keeps the integrator at whatever value holds the set point.

Answer it winds up by 30,000 output units, far beyond the 100% limit.

Check

A plant with G = 1 and τ = 20 ms is sampled every 0.5 ms under proportional gain 10.

(a)

Where is the loop's pole?

(b)

What is the largest stable gain?

Check

A loop has proportional gain 5 and plant DC gain 0.4.

(a)

What is the steady-state error, as a share of the set point?

(b)

Where does the output settle?

Check

A loop sampled at 5 kHz crosses over at 20 Hz with a total delay of 2.5 samples.

(a)

How much phase margin does the delay cost?

(b)

How many times the crossover is the sample rate?

Check

A gain of 0.75 is stored in Q8 and multiplies an error of 250 codes.

(a)

What integer stores the gain?

(b)

What does the product, shifted right by 8, give?

Check

An integrator adds 0.002 × e every 10 ms; the output is saturated for 30 s with an error of 100 codes.

(a)

How much does the integrator wind up?

(b)

Over how many samples?

Check

A loop needs a crossover of 50 Hz and is sampled at 10 times that.

(a)

What sample rate?

Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.

(b)

With 1.5 samples of delay, what phase does it lose?

Check

A motor's speed loop works well in testing, but after a long stall the motor overshoots wildly when it's freed. What's the likely fix?

Check

Write pi_step(i_acc, e, kp_q8, ki_q16, umin, umax): one sample of an integer PI controller. The output is ((kp_q8 * e) >> 8) + (i_acc >> 16), clamped to [umin, umax]. Update the accumulator by ki_q16 * e only if that doesn't push a saturated output further into its limit. Return (u, new i_acc).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does a sampled control loop tolerate less gain than the same loop in continuous time?

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.