---
title: PID control and tuning
heading: The proportional, integral and derivative terms and what each does, a drone holding altitude as a mass under PD control with its natural frequency and damping, overshoot and settling, the integral term that removes steady droop, and Ziegler–Nichols tuning
eyebrow: D3 · Lesson 3 · PID control and tuning
xp: 40
---

::: hook
More than nine in ten industrial control loops are PID controllers: three terms, three knobs. The proportional term pushes in proportion to the error, the integral term keeps pushing harder for as long as any error remains, and the derivative term brakes when the error is changing fast. A quadcopter holding its altitude is a good place to see them work, because the drone is a mass with almost no friction: thrust accelerates it, and without the derivative term it would bounce up and down forever around its setpoint.
:::

::: play
- A 1.2 kg drone told to climb 1 m. Its altitude controller adds thrust $K_p e - K_d\dot h$ to the hover thrust: proportional to the height error, minus a damping term proportional to the climb rate.
- The drone behaves as a mass on a spring ($K_p$) with a damper ($K_d$): natural frequency $\omega_n = \sqrt{K_p/m}$, damping ratio $\zeta = K_d/(2\sqrt{K_pm})$.
- Too little $K_d$ and it overshoots and bounces; too much and it creeps up slowly. ζ around 0.7 is a common compromise.
:::

```sim plot
view: graph
x: t = 0..6
y: h = 0..1.8
slider: P = 1..30 step 0.2 = 4.8
slider: D = 0.2..10 step 0.1 = 2.4
plot: 1 + 0*t | dashed | color: ch3 | label: setpoint, m
plot: 1 - Re(exp(-D/(2*sqrt(P*1.2))*sqrt(P/1.2)*t)*(cosh(sqrt((D/(2*sqrt(P*1.2)))^2 - 1 + 1e-9)*sqrt(P/1.2)*t) + D/(2*sqrt(P*1.2))*sinh(sqrt((D/(2*sqrt(P*1.2)))^2 - 1 + 1e-9)*sqrt(P/1.2)*t)/sqrt((D/(2*sqrt(P*1.2)))^2 - 1 + 1e-9))) | samples: 400 | label: altitude, m (Kp in N/m, Kd in N·s/m)
readout: natural frequency, rad/s = sqrt(P/1.2) | digits: 3
readout: damping ratio = D/(2*sqrt(P*1.2)) | digits: 3
readout: overshoot, % = 100*Re(exp(-pi*D/(2*sqrt(P*1.2))/sqrt(1 - (D/(2*sqrt(P*1.2)))^2 + 1e-9)))*step(1 - D/(2*sqrt(P*1.2))) | digits: 3
```

::: predict
## With $K_p$ = 4.8 N/m and $K_d$ = 2.4 N·s/m, the 1.2 kg drone has ζ = 0.5. By how much does it overshoot a 1 m climb?

- [ ] Not at all
- [ ] About 5 cm
- [x] About 16 cm
- [ ] About 50 cm

::: reveal
A second-order system overshoots by $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$ of the step: $e^{-\pi \times 0.5/0.866}$ = 0.163, or 16 cm. Raising $K_d$ to 3.36 N·s/m gives ζ = 0.7 and an overshoot of only 4.6%.
:::
:::

::: nameit
## The three terms

$$u(t) = K_pe(t) + K_i\int_0^t e\,d\tau + K_d\frac{de}{dt}$$

- **Proportional**: reacts to the present error. More $K_p$ responds faster but, on a plant like the drone, oscillates more.
- **Integral**: reacts to accumulated past error, so it keeps growing until the error is zero. It removes steady-state error (lesson 2) but adds overshoot and can **wind up** when the actuator saturates.
- **Derivative**: reacts to the error's rate of change, a prediction of where it's heading, which adds damping. It amplifies sensor noise, so it's filtered, and it's often applied to the measurement instead of the error to avoid a jolt when the setpoint jumps.

## The drone

With mass m, PD control on the altitude gives $m\ddot h + K_d\dot h + K_ph = K_ph_\text{ref}$, a second-order system with

$$\omega_n = \sqrt{K_p/m}, \qquad \zeta = \frac{K_d}{2\sqrt{K_pm}}$$

overshoot $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$ and a 2% settling time of about $4/(\zeta\omega_n)$.

A constant disturbance force $F_d$, such as thrust lost as the battery sags, leaves a steady altitude error of $F_d/K_p$ under PD control. Adding the integral term drives it to zero.

## Ziegler–Nichols tuning

Raise $K_p$ alone until the loop oscillates steadily. That **ultimate gain** $K_u$ and the oscillation's period $P_u$ give a starting point:

$$K_p = 0.6K_u, \qquad T_i = P_u/2, \qquad T_d = P_u/8$$

with $K_i = K_p/T_i$ and $K_d = K_pT_d$. The result is aggressive, with about 25% overshoot, and is usually softened by hand.
:::

::: example
## The drone's battery sags and it loses 0.24 N of thrust. Under PD control with $K_p$ = 4.8 N/m, how far does it sink? What fixes it?

::: solution
1. **How far it sinks.**
   $$\begin{aligned} F_d/K_p &= 0.24/4.8 \\ &= 0.05\ \text{m} \end{aligned}$$
2. **The fix.** An integral term keeps adding thrust while any error remains, so it settles back at the setpoint.

**Answer:** it sinks 0.05 m, and an integral term brings it back to the setpoint.
:::
:::

::: example
## A loop oscillates steadily at $K_u$ = 10 with period 2 s. What PID gains does Ziegler–Nichols suggest?

::: solution
1. **Proportional gain.**
   $$K_p = 6$$
2. **Integral gain.**
   $$T_i = 1\ \text{s}$$
   so $K_i = 6$ s⁻¹.
3. **Derivative gain.**
   $$T_d = 0.25\ \text{s}$$
   so $K_d = 1.5$ s.

**Answer:** $K_p$ = 6, $K_i$ = 6 s⁻¹ and $K_d$ = 1.5 s.
:::
:::

::: example
## The sagging drone sits 0.05 m low when an integral term with $K_i$ = 6 N/(m·s) is switched on. At first, how fast does the integral's force grow, and roughly how long until it makes up the lost 0.24 N?

::: solution
1. **How fast the force grows.**
   $$\begin{aligned} K_i e &= 6 \times 0.05 \\ &= 0.3\ \text{N/s} \end{aligned}$$
2. **Time to make up 0.24 N.** If the error stayed put:
   $$0.24/0.3 = 0.8\ \text{s}$$
3. **What really happens.** The drone rises as the force builds, the error shrinks, and the last of the correction comes more slowly.

**Answer:** the integral's force grows at 0.3 N/s at first, so it makes up the 0.24 N in roughly 0.8 s.
:::
:::

::: check
given: m = 0.5 | 1.2 | 2.5; P = 2 | 5 | 12
answer: sqrt(P/m)
unit: rad/s
tolerance: 0.5%

## A {m} kg drone's altitude loop has $K_p$ = {P} N/m. What's its natural frequency?

::: right
Right. $\sqrt{K_p/m}$ = {answer} rad/s.
:::

::: misses
- [P/m] Take the square root, as for a mass on a spring.
- [any] Not yet. Spring constant over mass.
:::
:::

::: check
given: m = 0.5 | 1.2 | 2.5; P = 2 | 5 | 12; D = 0.8 | 1.5 | 2.5
answer: D/(2*sqrt(P*m))
tolerance: 0.5%

## The same drone ({m} kg, $K_p$ = {P} N/m) has $K_d$ = {D} N·s/m. What's its damping ratio?

::: right
Right. $K_d/(2\sqrt{K_pm})$ = {answer}.
:::

::: misses
- [any] Not yet. As for a mass, spring and damper.
:::
:::

::: check
given: z = 0.2 | 0.4 | 0.6 | 0.8
answer: 100*exp(-pi*z/sqrt(1 - z^2))
tolerance: 0.5%

## A second-order loop has damping ratio {z}. By what percentage does it overshoot a step?

::: right
Right. $100e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$ = {answer}%.
:::

::: misses
- [100*exp(-pi*z)] Divide by $\sqrt{1 - \zeta^2}$ in the exponent.
- [any] Not yet. The second-order overshoot formula.
:::
:::

::: check
given: F = 0.1 | 0.3 | 0.8; P = 2 | 5 | 12
answer: F/P*100
unit: cm
tolerance: 0.5%

## A disturbance force of {F} N acts on a drone under PD altitude control with $K_p$ = {P} N/m. How far does it settle from its setpoint?

::: right
Right. $F_d/K_p$ = {answer} cm. An integral term would remove it.
:::

::: misses
- [any] Not yet. The proportional "spring" must balance the force.
:::
:::

::: check
given: K = 2 | 10 | 40; T = 0.5 | 2 | 8
answer: 0.6*K*T/8
unit: s
tolerance: 0.5%

## A loop oscillates steadily at an ultimate gain of {K} with period {T} s. What derivative gain $K_d$ does Ziegler–Nichols suggest?

::: right
Right. $K_pT_d = 0.6K_u \times P_u/8$ = {answer} s.
:::

::: misses
- [K*T/8] $K_p$ is 0.6 of the ultimate gain.
- [any] Not yet. $K_d = K_pT_d$.
:::
:::

::: check
kind: parts
given: F = 0.1 | 0.24 | 0.5; Kp = 2 | 4.8 | 10; Ki = 2 | 6

## A PD-controlled drone with $K_p$ = {Kp} N/m loses {F} N of thrust; then an integral term with $K_i$ = {Ki} N/(m·s) is added.

::: part
answer: F/Kp
unit: m
tolerance: 0.5%

### How far does it sink under PD alone?

::: misses
- [any] $F/K_p$.
:::
:::

::: part
answer: Ki*F/Kp
unit: N/s
tolerance: 0.5%

### At what rate does the integral's force grow at first?

::: misses
- [any] $K_ie$.
:::
:::
:::

::: check
kind: parts
given: Ku = 5 | 10 | 40; Tu = 0.5 | 2 | 4

## A loop oscillates steadily at gain {Ku} with period {Tu} s. Ziegler–Nichols PID sets $K_p = 0.6K_u$, $T_i = T_u/2$ and $T_d = T_u/8$.

::: part
answer: 0.6*Ku
tolerance: 0.5%

### What is $K_p$?

::: misses
- [any] $0.6K_u$.
:::
:::

::: part
answer: 0.6*Ku/(Tu/2)
tolerance: 0.5%

### What is $K_i = K_p/T_i$?

::: misses
- [any] $K_p/T_i$.
:::
:::

::: part
answer: 0.6*Ku*Tu/8
tolerance: 0.5%

### What is $K_d = K_pT_d$?

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

::: explain
## What does each term of a PID controller contribute?

::: model
The proportional term pushes in proportion to the present error, giving a fast response, but on its own it leaves a steady error whenever a constant effort is needed and can make a lightly damped plant oscillate. The integral term accumulates the error over time and keeps increasing its push for as long as any error persists, which drives the steady-state error to zero but tends to add overshoot. The derivative term responds to how fast the error is changing, anticipating where it's heading and braking the motion before it overshoots, which adds damping; because it amplifies rapid changes, it's filtered and often applied to the measurement to ignore sudden setpoint changes.
:::
:::

::: practice

::: check
given: m = 0.1 | 50; P = 0.5 | 500
answer: sqrt(P/m)
unit: rad/s
tolerance: 0.5%

## $\omega_n$: m = {m} kg, $K_p$ = {P} N/m?

::: misses
- [any] $\sqrt{K_p/m}$.
:::
:::

::: check
given: m = 0.1 | 50; P = 0.5 | 500; D = 0.1 | 100
answer: D/(2*sqrt(P*m))
tolerance: 0.5%

## ζ: m = {m}, $K_p$ = {P}, $K_d$ = {D}?

::: misses
- [any] $K_d/(2\sqrt{K_pm})$.
:::
:::

::: check
given: z = 0.05 | 0.1 | 0.2 | 0.3 | 0.5 | 0.7 | 0.95
answer: 100*exp(-pi*z/sqrt(1 - z^2))
tolerance: 0.5%

## Overshoot, %, at ζ = {z}?

::: misses
- [any] $100e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$.
:::
:::
:::
