PID controllers
UAT 206 Fundamentals of Control and Autopilot Systems
Lesson
By the end of this module you will be able to
- Write a digital PID controller and distinguish the parallel and standard forms
- Prevent integral windup when the control signal saturates
- Use derivative on measurement and derivative filtering to reduce control peaks
- Choose starting gains and explain the effect of P, I and D on the response
Why this matters
The textbook PID formula is short, but a controller that really flies must handle things the formula does not mention: commands beyond the motors’ limits, sudden setpoint changes and sensor noise. Chapter 11 of Åström and Murray (2021) and Åström and Hägglund (2006) cover these in detail, and PX4 uses the same techniques in its own control loops.
Two forms of PID
- Parallel form: three separate gains .
- Standard (ideal) form: an overall gain multiplying all three terms.
The PX4 PID tuning guide explains that the rate loop supports both: setting MC_ROLLRATE_K = 1 gives the parallel form, while setting P = 1 gives the standard form, which separates the overall gain from the I and D tuning. Know which form is in use before borrowing gains from elsewhere.
Integral windup
Motors can only give so much thrust. When the command exceeds the limit, the actual signal saturates, but the I term keeps accumulating error. Once the target is reached, the large accumulated I pushes far past it: integral windup. Two common fixes:
- Clamping: stop integrating while saturated and the error still pushes the same way. PX4 uses this in its velocity loop (Controller Diagrams).
- Back-calculation: feed the difference between the command and the saturated value back to reduce I (Åström and Murray, section 11.4).
Example 1 Altitude control with saturated thrust
A climb from 0 to 10 m is commanded; the acceleration command is limited to ±3 m/s² because thrust is limited.
KP, KI, KD, AMAX, DT = 1.5, 0.4, 2.0, 3.0, 0.01
def climb(anti_windup):
z = v = integ = 0.0
zs = []
for _ in range(int(40 / DT)):
e = 10 - z
a_cmd = KP * e + KI * integ - KD * v
a = max(-AMAX, min(AMAX, a_cmd))
pushing_further = (a_cmd > AMAX and e > 0) or (a_cmd < -AMAX and e < 0)
if not (anti_windup and pushing_further):
integ += e * DT
v += a * DT
z += v * DT
zs.append(z)
settle = next(i * DT for i in range(len(zs)) if all(abs(x - 10) <= 0.2 for x in zs[i:]))
return max(zs) - 10, settle
for label, flag in (("without anti-windup", False), ("with clamping", True)):
over, ts = climb(flag)
print(f"{label:<20} overshoot {over:.2f} m, within 0.2 m after {ts:.1f} s")
without anti-windup overshoot 4.14 m, within 0.2 m after 11.0 s
with clamping overshoot 1.58 m, within 0.2 m after 9.8 s
Clamping cuts overshoot by more than half. Most of the remaining 1.58 m comes from the I term accumulated during the climb (with KI = 0 this model is left with about 0.10 m, but a real drone still needs I to remove steady errors). Example 3 reduces what is left by limiting the climb rate in the outer loop.
Derivative on measurement and filtering
If the D term uses the error, a setpoint step makes the derivative spike momentarily. Åström and Murray recommend applying D (and possibly P) to the measurement instead, called setpoint weighting. Also, the derivative amplifies high-frequency noise, so it must be filtered: with and around 5–20. PX4 applies a low-pass filter to the D term of the rate loop.
Example 2 Control peaks on a setpoint change
The setpoint steps from 0 to 10 while the measurement is still 0, with 10 ms steps; only the D term is shown.
KP, KD, DT, N = 1.5, 2.0, 0.01, 10
TF = (KD / KP) / N # s derivative filter time constant
steps = [0.0, 10.0, 10.0, 10.0] # setpoint at each time step
meas = [0.0, 0.0, 0.02, 0.08] # measurement (just starting to move)
def d_term(signal, sign, filtered):
d, peak = 0.0, 0.0
for k in range(1, len(signal)):
raw = sign * KD * (signal[k] - signal[k - 1]) / DT
d = d + DT / (TF + DT) * (raw - d) if filtered else raw
peak = max(peak, abs(d))
return peak
err = [r - y for r, y in zip(steps, meas)]
print(f"D on error, unfiltered: peak {d_term(err, 1, False):7.1f}")
print(f"D on error, filtered N={N}: peak {d_term(err, 1, True):7.1f}")
print(f"D on measurement, filtered: peak {d_term(meas, -1, True):7.1f}")
D on error, unfiltered: peak 2000.0
D on error, filtered N=10: peak 139.5
D on measurement, filtered: peak 1.1
The derivative of the error spikes to 2,000, filtering cuts it to about 140, but derivative on measurement stays around 1, removing the problem at its source because the measurement does not jump with the setpoint.
Limiting climb rate in the outer loop
The outer loop computes a desired climb rate from the altitude error, and the inner loop uses acceleration to make the actual speed follow it. The law in Example 1 can be rewritten in two steps: and , which is exactly the original law when nothing is limited. Limiting the climb rate means clamping to m/s before it reaches the inner loop, and once it is clamped, I must stop accumulating too, because further I has no effect on the command the inner loop receives. This is the same clamping principle as above, applied in the outer loop.
Example 3 A 2 m/s climb-rate limit with and without anti-windup
Climb from 0 to 10 m with the same model and the same KP, KI, KD. The acceleration command is still limited to ±3 m/s² with clamping, as in Example 1. Compare three cases.
KP, KI, KD, AMAX, VMAX, DT = 1.5, 0.4, 2.0, 3.0, 2.0, 0.01
def climb(kd=KD, ki=KI, rate_limit=False, stop_i=True):
z = v = integ = 0.0
zs = []
for _ in range(int(60 / DT)):
e = 10 - z
v_sp = (KP * e + ki * integ) / kd # climb rate the outer loop wants
v_cmd = max(-VMAX, min(VMAX, v_sp)) if rate_limit else v_sp
a_cmd = kd * (v_cmd - v) # inner loop
a = max(-AMAX, min(AMAX, a_cmd))
accel_pushing = (a_cmd > AMAX and e > 0) or (a_cmd < -AMAX and e < 0)
rate_pushing = rate_limit and v_cmd != v_sp and e * v_sp > 0 # clamped and still pushing the same way
if not (accel_pushing or (stop_i and rate_pushing)):
integ += e * DT
v += a * DT
z += v * DT
zs.append(z)
settle = next(i * DT for i in range(len(zs)) if all(abs(x - 10) <= 0.2 for x in zs[i:]))
return max(zs) - 10, settle
cases = (("no rate limit", {}),
("limit, I keeps adding", dict(rate_limit=True, stop_i=False)),
("limit, I stopped", dict(rate_limit=True, stop_i=True)))
for label, kw in cases:
over, ts = climb(**kw)
print(f"{label:<22} overshoot {over:.2f} m, within 0.2 m after {ts:.1f} s")
no rate limit overshoot 1.58 m, within 0.2 m after 9.8 s
limit, I keeps adding overshoot 5.56 m, within 0.2 m after 16.1 s
limit, I stopped overshoot 0.53 m, within 0.2 m after 10.6 s
If the climb rate is limited but I keeps accumulating, the result is worse than no limit at all (5.56 m against 1.58 m), because altitude approaches the target more slowly and the error stays large for longer. In this model the I term (KI times the integral) when 10 m is first reached is 11.29 m/s², against 2.53 m/s² with no limit. When I is stopped while the rate is limited, overshoot drops to 0.53 m and settling time is 10.6 s, close to the 9.8 s of the unlimited case (climbing 10 m at a steady 2 m/s would take at least 5.0 s).
Conditions for these numbers: the model is a mass whose acceleration is commanded directly, with no noise or delay; they use the KP, KI, KD and limits above and the stop-integrating rule written in the code. A different way of writing the limit or the stop rule gives different numbers, so test in SITL before using it on a real drone.
Tuning order
The PX4 guide says to tune the rate loop first, because it affects every flight mode, then the attitude, velocity and position loops. Test with step inputs while hovering; the guide says a well-tuned drone follows immediately without oscillating or overshooting. General guidance:
- P speeds up the response, but too much causes oscillation.
- I removes steady error, such as an offset centre of gravity, but too much causes slow overshoot.
- D adds damping to fast motion and reduces oscillation, but amplifies noise, so filter it and watch motor temperature; and with an I term present, more D does not always reduce overshoot (Example 4).
Example 4 More D does not always cut overshoot
Use the climb function from Example 3 (no climb-rate limit, only acceleration clamping) and change KD and KI.
for kd, ki in ((1.0, 0.4), (2.0, 0.4), (3.0, 0.4), (4.0, 0.4), (3.0, 0.27)):
over, ts = climb(kd=kd, ki=ki)
print(f"KD {kd:.1f} KI {ki:.2f}: overshoot {over:.2f} m, within 0.2 m after {ts:.1f} s")
KD 1.0 KI 0.40: overshoot 3.43 m, within 0.2 m after 11.4 s
KD 2.0 KI 0.40: overshoot 1.58 m, within 0.2 m after 9.8 s
KD 3.0 KI 0.40: overshoot 1.94 m, within 0.2 m after 13.0 s
KD 4.0 KI 0.40: overshoot 2.36 m, within 0.2 m after 21.8 s
KD 3.0 KI 0.27: overshoot 1.48 m, within 0.2 m after 16.3 s
From KD 1.0 to 2.0 overshoot falls as expected, but at KD 3.0 and 4.0 it rises again, with longer settling times. The reason: with a high KD the speed follows the desired speed quickly, so the altitude loop behaves like a PI loop with gains and , which shrink as KD grows. Altitude then approaches the target more slowly and I has more time to accumulate (when 10 m is first reached, the I term is 2.53 m/s² at KD 2.0 and 4.11 m/s² at KD 3.0). This lowers the damping of the slow mode tied to I, approximately . From the equation of the unsaturated model, the slow pole pair has = 0.30, 0.86, 0.67, 0.58 at KD = 1.0, 2.0, 3.0, 4.0 (the approximation gives 0.84, 0.68, 0.59 at KD 2.0 to 4.0, but fails at KD 1.0 because KD is not yet high enough). At low KD, is low because damping is insufficient; at high KD, is low because D slows the loop until I dominates.
This effect does not come from acceleration saturation, noise or the time step: with the ±3 m/s² limit removed, overshoot is still lowest at about KD 2.5 (2.62 m) and then rises to 2.86 m at KD 4.0. The model has no noise, and a 0.002 s time step gives nearly the same overshoot (1.60, 1.94, 2.36 m at KD 2.0, 3.0, 4.0). It is fixed by lowering KI together with KD: KD 3.0 with KI 0.27 gives 1.48 m, slightly below 1.58 m, but settling takes 16.3 s against 9.8 s. So D does damp oscillation when raised from a low value, but overshoot in a loop with I depends on the balance of KP, KI and KD together, and KI must be adjusted along with KD.
Module lab
Lab: altitude PID in simulation
- Run the code from Example 1, changing KP, KI and KD one at a time, and tabulate overshoot and settling time.
- Use the code from Example 3, switching
rate_limitandstop_i, and compare the overshoot of a 2 m/s climb-rate limit with and without stopping I against the unlimited case. - Use the code from Example 4, changing KD and KI together, and see whether overshoot keeps falling.
- Use the code from Example 2 with N between 5 and 20 and watch the D-term peak.
- In the PX4 or ArduPilot documentation, identify which parameters are the rate loop’s P, I, D and filters.
- Write the tuning order you will use in SITL in Module 5.
Common mistakes
Watch out
- Copying standard-form gains into a parallel form without converting.
- No anti-windup when the control signal can saturate.
- Computing D from the error, then wondering why motors twitch on setpoint changes.
- Raising D without filtering until the motors overheat.
- Tuning outer loops before inner loops.
- Limiting the climb rate without stopping I, which is worse than no limit.
- Raising D alone without revisiting KI, expecting overshoot to always fall.
Summary
- PID comes in parallel and standard forms; know which the autopilot uses.
- Clamping or back-calculation anti-windup reduces overshoot under saturation.
- Use derivative on measurement and a filter with ( = 5–20).
- A climb-rate limit in the outer loop must stop I too, or it is worse than no limit; and with an I term, more D does not always reduce overshoot, so adjust KI together with it.
- Tune the rate loop first, then the outer loops.
Check your understanding
- A standard-form PID has , s. What is in parallel form?
- When does integral windup occur?
- With , and , what is ?
- Why compute the D term from the measurement rather than the error?
- Which loop does the PX4 guide say to tune first?
- In Example 3, a 2 m/s climb-rate limit without stopping I gives what overshoot, and why is it worse than no limit?
- In Example 4, why does raising KD from 2.0 to 3.0 increase overshoot from 1.58 m to 1.94 m?
Answers
- When the control signal saturates but the I term keeps accumulating error.
- s, so s.
- The measurement does not jump with the setpoint, so there is no control peak.
- The rate loop.
- 5.56 m (against 1.58 m with no limit), because altitude approaches the target more slowly, the error stays large for longer and I accumulates more; I must stop accumulating while the rate is limited (which gives 0.53 m).
- A high KD makes altitude approach the target more slowly, so I accumulates for longer and the damping of the slow mode tied to I falls (ζ from 0.86 to 0.67). Lowering KI along with it (KI 0.27) gives 1.48 m, but settling slows to 16.3 s.
Key formulas
| Parallel PID | |
| Standard (ideal) PID | |
| Derivative filter |
Key references
- Åström, K. J., & Murray, R. M. (2021). Feedback systems: An introduction for scientists and engineers (2nd ed.). Princeton University Press. link
- Åström, K. J., & Hägglund, T. (2006). Advanced PID control. ISA–The Instrumentation, Systems, and Automation Society.
- PX4 Autopilot. Multicopter PID tuning guide (manual/advanced). PX4 user guide (main). link
- PX4 Autopilot. Controller diagrams. PX4 user guide (main). link
Further reading
Study the assigned knowledge units in advance, review media and take the module quiz
การควบคุมป้อนกลับและตัวควบคุม PID
gym-pybullet-drones
PID, failsafe and firmware
In class / field
Lab or field practice from worksheets with a safety checklist
Learning evidence: Checked worksheets and quiz results