---
title: PID control
heading: What the proportional, integral and derivative terms each do, the ideal and parallel forms, PID as a pair of zeros, filtering the derivative, avoiding derivative kick, and integrator windup with its cures
eyebrow: S9 · Lesson 15 · PID control
xp: 50
---

::: hook
Somewhere around nine control loops in ten in industry run a PID controller: three terms, acting on the error now, on its accumulated past and on its trend. Thermostats, motor drives, power supplies, chemical plants and quadcopters all use it, because three knobs are enough to make most plants behave and because each knob has a clear physical meaning. This lesson takes the controller apart term by term and covers the practical details that separate a textbook PID from one that works on real hardware.
:::

::: play
- PI control of a first-order plant (DC gain 1, time constant 1 s), $C = K_p(1 + \frac{1}{T_is})$, against proportional control alone (dashed).
- Proportional alone settles short of 1 by $1/(1 + K_p)$. The integral term removes that error, more quickly the smaller $T_i$ is.
- Too small a $T_i$ and the loop overshoots and rings: the integrator keeps pushing because it remembers the error that built up on the way.
:::

```sim plot
view: graph
x: t = 0..8 s
y: y = 0..1.6
slider: K = 0.5..10 = 2 log
slider: T = 0.1..3 = 0.5 log
plot: Re(1 - exp(-(1 + K)/2*t)*(cos(sqrt(K/T - (1 + K)^2/4 + 0*j)*t) + (1 + K)/2*sin(sqrt(K/T - (1 + K)^2/4 + 0*j)*t)/sqrt(K/T - (1 + K)^2/4 + 0*j)) + K*exp(-(1 + K)/2*t)*sin(sqrt(K/T - (1 + K)^2/4 + 0*j)*t)/sqrt(K/T - (1 + K)^2/4 + 0*j)) | samples: 600 | label: PI
plot: K/(1 + K)*(1 - exp(-(1 + K)*t)) | dashed | color: ch3 | label: P only
readout: integral gain Ki = K/T | digits: 3
readout: damping ratio of the closed loop = (1 + K)/2/sqrt(K/T) | digits: 3
```

::: predict
## A PI controller's integral term is $K_i\int e\,dt$. While the actuator is saturated, the error stays large for several seconds. What happens when the output finally reaches the setpoint?

- [ ] It stops cleanly, because the error is zero
- [x] It overshoots badly, because the integral has grown huge and must be unwound by negative error
- [ ] The integral resets itself to zero
- [ ] The loop goes permanently unstable

::: reveal
That's **integrator windup**. While the actuator was pinned at its limit, the integral kept accumulating error that the actuator couldn't act on. At the setpoint the error is zero but the integral term is still large, holding the actuator at full drive, so the output sails past and stays past until enough negative error has accumulated to bring the integral back down. Every practical PID includes anti-windup.
:::
:::

::: nameit
## The three terms

$$u = K_p\left(e + \frac{1}{T_i}\int_0^t e\,d\tau + T_d\frac{de}{dt}\right) = K_pe + K_i\int e + K_d\dot{e}$$

with $K_i = K_p/T_i$ and $K_d = K_pT_d$ in the **parallel form**.

- **P** reacts to the present error. Alone, it leaves a steady error on type-0 plants.
- **I** reacts to accumulated error and removes steady error (lesson 10), at the cost of phase lag.
- **D** reacts to the error's slope, predicting it about $T_d$ ahead, which adds damping.

## PID as zeros

$$C(s) = K_p\frac{T_iT_ds^2 + T_is + 1}{T_is}$$

a pole at the origin and two zeros, real when $T_i \ge 4T_d$. A common design puts the zeros on the plant's slow poles.

## Practical details

- **Derivative filter:** a pure derivative amplifies noise without limit. Use $\frac{T_ds}{1 + T_ds/N}$ with N between 5 and 20; the filter pole is at $N/T_d$, and the D term's gain stops rising at $K_pN$.
- **Derivative on measurement:** differentiate $-y$ instead of $e$, so a step in the setpoint doesn't produce an impulse ("derivative kick").
- **Anti-windup:** stop integrating while the actuator is saturated (clamping), or feed the difference between the commanded and actual actuator signal back into the integrator through a gain $1/T_t$ (back-calculation), with $T_t$ around $\sqrt{T_iT_d}$.
- **Bumpless transfer:** when switching from manual to automatic, initialize the integrator so the output doesn't jump.
:::

::: example
## The motor of lesson 2, $P = 20/(0.016s + 1)$, needs a speed loop with a 2 ms closed-loop time constant and no steady error. Design a PI controller.

::: solution
1. **Put the integral zero on the plant pole.**
   $$T_i = 16\ \text{ms}$$
2. **The loop.** It becomes a pure integrator:
   $$L = K_p \cdot \frac{20}{0.016s}$$
3. **The closed loop.** It's $1/(\tau_cs + 1)$ with
   $$\tau_c = \frac{0.016}{20K_p}$$
4. **Gains for 2 ms.**
   $$\begin{aligned} K_p &= \frac{0.016}{20 \times 0.002} \\ &= 0.4 \\ K_i &= 0.4/0.016 \\ &= 25\ \text{s}^{-1} \end{aligned}$$
5. **Check the response.** The step response has no overshoot and settles within 2% in 7.8 ms (3.9τ).
6. **When cancelling is safe.** Cancelling a stable, well-known plant pole like this is safe; lesson 4 explains why an unstable one must never be cancelled.

**Answer:** $K_p$ = 0.4 and $T_i$ = 16 ms ($K_i$ = 25 per second), which settles within 2% in 7.8 ms with no overshoot.
:::
:::

::: example
## A plant $P = 5/((s + 1)(s + 5))$ needs a 0.1 s closed-loop time constant. Design a PID that cancels both poles.

::: solution
1. **Match the numerator to the plant's poles.**
   $$\begin{aligned} &T_iT_ds^2 + T_is + 1 \\ &= (s + 1)(s + 5)/5 \\ &= 0.2s^2 + 1.2s + 1 \end{aligned}$$
2. **Read off the times.**
   $$\begin{aligned} T_i &= 1.2\ \text{s} \\ T_d &= 0.2/1.2 \\ &= 0.167\ \text{s} \end{aligned}$$
3. **The loop.**
   $$\begin{aligned} L &= K_p \cdot \frac{5}{5 \times 1.2s} \\ &= \frac{K_p}{1.2s} \end{aligned}$$
   giving $\tau_c = 1.2/K_p$.
4. **Gains for 0.1 s.**
   $$\begin{aligned} K_p &= 12 \\ K_i &= 10 \\ K_d &= 2 \end{aligned}$$
5. **The derivative filter.** In practice the derivative needs a filter, say N = 10 (a pole at 60 rad/s), which slightly changes the result.

**Answer:** $K_p$ = 12, $T_i$ = 1.2 s and $T_d$ = 0.167 s, so $K_i$ = 10 and $K_d$ = 2.
:::
:::

::: example
## A PID with $K_p$ = 2, $T_d$ = 0.1 s and a derivative filter with N = 10 acts on the error. The setpoint steps by 1. What does the derivative term do?

::: solution
1. **High-frequency gain.** The filtered derivative
   $$\frac{K_pT_ds}{1 + T_ds/N}$$
   has high-frequency gain $K_pN = 20$.
2. **The kick.** A unit step in the error produces an instant kick of 20 in the output, decaying with time constant
   $$T_d/N = 10\ \text{ms}$$
3. **Why it matters.** That spike can saturate the actuator and jolt the mechanism.
4. **The fix.** Taking the derivative of the measurement instead of the error removes the kick, since the measurement can't step; disturbances still get the full derivative action.

**Answer:** it kicks the output by 20 at the instant of the step, and the kick decays with a 10 ms time constant.
:::
:::

::: check
given: K = 0.5 | 2 | 8; T = 0.05 | 0.4 | 2
answer: K/T
unit: s^-1
tolerance: 0.5%

## A PID has $K_p$ = {K} and $T_i$ = {T} s. What's its integral gain $K_i$?

::: right
Right. $K_i = K_p/T_i$ = {answer}.
:::

::: misses
- [K*T] $T_i$ divides: a shorter integral time means stronger integral action.
- [any] Not yet. Compare the ideal and parallel forms.
:::
:::

::: check
given: t = 10 | 50 | 200; P = 2 | 20; c = 2 | 5
answer: t/(P*c)
tolerance: 0.5%

## A plant $P = \given{P}/(\tau s + 1)$ with τ = {t} ms gets PI control with $T_i$ = τ. What $K_p$ gives a closed-loop time constant of {c} ms?

::: right
Right. $L = K_pK_0/(\tau s)$, so $\tau_c = \tau/(K_pK_0)$ and $K_p$ = {answer}.
:::

::: misses
- [c/(P*t)] Invert it: $K_p = \tau/(K_0\tau_c)$.
- [t/c] Include the plant's gain.
- [any] Not yet. Write the loop gain with the zero cancelling the pole.
:::
:::

::: check
given: K = 0.3 | 1 | 5; P = 4 | 25; t = 20 | 100
answer: t/(K*P)
unit: ms
tolerance: 0.5%

## PI control with $T_i$ = τ = {t} ms and $K_p$ = {K} on a plant with DC gain {P}. What's the closed-loop time constant?

::: right
Right. $\tau/(K_pK_0)$ = {answer} ms.
:::

::: misses
- [t/(1 + K*P)] That's for proportional control alone. With the zero on the pole, the loop is a pure integrator.
- [any] Not yet. $L = K_pK_0/(\tau s)$.
:::
:::

::: check
given: T = 1 | 2 | 4; D = 0.05 | 0.2
answer: (-T + sqrt(T^2 - 4*T*D))/(2*T*D)
unit: rad/s
tolerance: 0.5%

## A PID has $T_i$ = {T} s and $T_d$ = {D} s. Where is its zero nearer the origin?

::: right
Right. The roots of $T_iT_ds^2 + T_is + 1$: the slower one is {answer}.
:::

::: misses
- [-1/T] That's the zero of a PI controller. The derivative term moves it.
- [any] Not yet. Solve $T_iT_ds^2 + T_is + 1 = 0$.
:::
:::

::: check
given: D = 0.01 | 0.1 | 0.5; N = 5 | 10 | 20
answer: N/D
unit: rad/s
tolerance: 0.5%

## A derivative term $T_ds/(1 + T_ds/N)$ has $T_d$ = {D} s and N = {N}. Where's the filter pole?

::: right
Right. $N/T_d$ = {answer} rad/s; above it the D term's gain levels off at $K_pN$.
:::

::: misses
- [1/D] The filter pole is N times beyond $1/T_d$.
- [any] Not yet. Set $1 + T_ds/N = 0$.
:::
:::

::: check
given: K = 1 | 4; T = 0.2 | 0.5; e = 2 | 10; s = 3 | 8
answer: K/T*e*s
tolerance: 0.5%

## A PI controller ($K_p$ = {K}, $T_i$ = {T} s) is saturated for {s} s with an error of {e} that it can't act on. Without anti-windup, how much does its integral term grow?

::: right
Right. $K_i \cdot e \cdot t$ = ({K}/{T}) × {e} × {s} = {answer}, all of which has to be unwound later.
:::

::: misses
- [K*e*s] The integral gain is $K_p/T_i$.
- [any] Not yet. $\Delta u = K_i\int e\,dt$.
:::
:::

::: check
kind: parts
given: K = 0.5 | 2 | 8; D = 0.05 | 0.2 | 1; N = 5 | 10 | 20

## A PID with $K_p$ = {K} and $T_d$ = {D} s has a derivative filter with N = {N}, and its derivative acts on the error. The setpoint steps by 1.

::: part
answer: K*N
tolerance: 0.5%

### How big is the derivative term's initial kick?

::: misses
- [K*D] The filtered derivative's high-frequency gain is $K_pN$.
- [any] $K_pN$.
:::
:::

::: part
answer: D/N*1000
unit: ms
tolerance: 0.5%

### With what time constant does it decay?

::: misses
- [any] The filter pole at $N/T_d$.
:::
:::
:::

::: check
kind: derivation
vars: K = 0.1..100, T = 0.01..100, D = 0.001..10

## A PID in standard form has gain K, integral time T and derivative time D.

::: step
answer: K/T

### What is $K_i$?
:::

::: step
answer: K*D

### What is $K_d$?
:::

::: step
answer: 1/(T*D)

### What is the product of its two zeros?
:::

::: right
Right. The numerator $T_iT_ds^2 + T_is + 1$ fixes the zeros: their product is $1/(T_iT_d)$ and their sum $-1/T_d$.
:::
:::

::: explain
## Why does a PID's derivative term need a filter?

::: model
A pure derivative has gain proportional to frequency, so it amplifies fast changes without limit. Measurement noise and quantization steps are fast, so an unfiltered D term turns them into large, jittery actuator commands that waste energy, heat motors and can saturate the actuator. The useful part of the derivative action is at frequencies around the loop's crossover, where it adds phase lead and damping. A first-order low-pass on the derivative, with its pole N times above $1/T_d$, keeps the lead where it's needed and caps the gain at high frequencies at N times the proportional gain.
:::
:::

::: practice

::: check
given: K = 0.2 | 3; D = 0.02 | 0.5
answer: K*D
unit: s
tolerance: 0.5%

## $K_d$ for $K_p$ = {K}, $T_d$ = {D} s?

::: misses
- [any] $K_d = K_pT_d$.
:::
:::

::: check
given: t = 5 | 80; P = 1 | 10; c = 1 | 4
answer: t/(P*c)
tolerance: 0.5%

## $K_p$ for a {c} ms closed loop on $\given{P}/(\tau s + 1)$, τ = {t} ms, $T_i$ = τ?

::: misses
- [any] $\tau/(K_0\tau_c)$.
:::
:::

::: check
given: T = 0.5 | 3; D = 0.02 | 0.1
answer: (-T + sqrt(T^2 - 4*T*D))/(2*T*D)
unit: rad/s
tolerance: 0.5%

## Slower PID zero for $T_i$ = {T}, $T_d$ = {D}?

::: misses
- [any] Roots of $T_iT_ds^2 + T_is + 1$.
:::
:::
:::
