PID Control
Three terms that run almost every machine you've ever touched
Your oven, your car's cruise control, a drone holding altitude, the Mars rover aiming its camera: almost everything that holds a value steady runs the same three-term loop, invented to steer battleships a century ago.
It has a name that sounds like a chemical: PID.
The idea is almost insultingly simple. You have a thing you want at some value, a setpoint. You measure where it actually is. The gap between them is the error. A PID controller turns that error into an action using three terms, each answering a different question.
Drive the loop yourself
Below is the workhorse of every controls course: a springy second-order plant that lags and rings. Tap ▶ play and watch the needle chase the target. Then tune the three gains and see what each one does.
PID step response
Plant G(s) = 1 / (s² + 0.4 s + 1). Controller u = Kp e + Ki ∫e dt + Kd ė. Setpoint steps from 0 → 1 at t = 0.
what to try
- Hit ▶ play with defaults. The needle leaves 0, ramps up, overshoots past the target, then settles. The trace below records that whole story; the dot on it tracks the needle.
- Pure proportional (Ki = Kd = 0): bump Kp. Faster needle, but it parks below the target; the steady-state offset never goes away.
- Add integral: the needle eventually pulls up to the target, but overshoot grows and settling stretches out.
- Add derivative: overshoot calms down. D acts on the rate of change, so it brakes before the needle hits the wall.
- Now turn on noise and replay. The D-term explodes because it amplifies the derivative of the noisy measurement; watch the needle judder. This is why we need observers, later in this chapter.
- Kick the plant. At t = 8 s the needle suddenly drops; only the I-term reliably brings it back to the target.
- Ziegler–Nichols ballpark: Kp ≈ 3.0, Ki ≈ 1.5, Kd ≈ 0.7.
Each term reacts to the error differently:
- P, Proportional ("how far off am I right now?"). Push in proportion to the error. Crank it up and the response gets snappy, but pure P always parks short of the target, leaving a stubborn steady-state offset, and too much makes it oscillate.
- I, Integral ("how long have I been off?"). Accumulate the error over time, so even a tiny lingering gap eventually builds enough push to kill it. I is what erases steady-state error, but it adds lag and overshoot.
- D, Derivative ("how fast am I approaching?"). React to the rate of change and brake early, before you slam into the target. D tames overshoot, but it amplifies sensor noise viciously (turn the noise slider on and watch the needle judder).
The tuning dance
Tuning PID is a balancing act, and the demo lets you feel every trade-off:
- Pure P (set I and D to 0): fast, but it never quite reaches the target.
- Add I: it pulls all the way up, but overshoots and takes longer to settle.
- Add D: the overshoot calms down, because D brakes on approach.
- Turn on noise: the D-term explodes, jittering the output. Real sensors are noisy, which is exactly why the next lessons build observers to estimate a clean signal before differentiating it.
- Arm the load disturbance: at t = 8 s a constant force shoves the plant. Only the integral term can fully shrug it off.
The math: a second-order plant and the three-term law
The plant is the canonical damped second-order system, with natural frequency and damping ratio :
The controller closes the loop on the error (setpoint minus output):
Read it term by term: pushes proportional to the present error; integrates the past to erase any lingering offset; looks at the trend and brakes before overshoot. The demo integrates this by forward-Euler at . Switching on noise adds Gaussian to the measurement before the error is formed. Notice that the derivative reacts to the noisy measurement, not the smooth truth. That single fact is the seed of the entire state-estimation half of robotics.
Key takeaways
- P acts on the present error (snappy, but leaves offset), I on the accumulated past (kills offset, adds lag), D on the predicted future (tames overshoot, amplifies noise).
- Tuning is a trade-off between speed, overshoot, and stability; there's no free lunch.
- Noise breaks the derivative term, which is why robots estimate state before they control it.
Run the controller yourself
This is a full PID loop driving a cold plant up to 100 degrees, and the three gains are yours. Bump Kp for a faster, twitchier response; add Ki to erase steady-state error; add Kd to damp the overshoot. Run it and read the overshoot straight off the numbers.
Three numbers. One loop. It steered a battleship in 1922 and it holds your quadcopter level today.
PID is the plainest possible answer to the deepest question in control: given the gap between where you are and where you want to be, how hard should you push? Everything fancier (pole placement, optimal control, the Kalman-filtered observers a few lessons from now) is just a more careful way of answering that same question.