Control Systems
PID control explained
A PID controller works out how hard to push a system toward a target from three terms: P, in proportion to the error now; I, in proportion to the error added up over time; and D, against how fast the error is changing. P does most of the work, D stops it overshooting, and I removes the last bit of error that P leaves behind.
P: push in proportion
Take a drone that should hover at 10 m. The error is the target minus its height. A proportional controller adds a push in proportion to it, on top of the thrust that just balances gravity:
Far away, a big push; close in, a gentle one; on target, none beyond hover. A small Kp crawls. A large one gets there fast but arrives with speed, flies past and swings back: it overshoots and rings.
D: brakes
Overshoot is a speed problem, and P can't see speed. So add a term that pushes against it:
Here v is the drone's vertical speed. Climbing fast, the Kd × v term cuts the thrust; falling fast, it adds. Because the target stays put, the speed is exactly how fast the error is shrinking, its derivative: this is the D term. Acting on the measured speed, rather than the error itself, also stops the controller lurching when the target suddenly changes.
With brakes, P can be bold. Kp 16 alone rings; Kp 16 with Kd 7 arrives in about three seconds and stops dead.
Live experiment
A bold push, then brakes
The drone climbs to 10 m with thrust = 9.8 + 16 × error − Kd × v. With no damping it flies past and rings (the dashed line). Pick how hard to brake against its speed, from Kd 0 to Kd 15, and find the fast, clean stop at 10 m.
“Brakes” is lesson 4 of 6 in Feedback Falls, Control Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.
I: the last inch
Load the drone with a parcel the controller doesn't know about. With Kp 4, it stops about 0.25 m short: to make the missing push of 1.0 it needs an error, and 4 × 0.25 is exactly 1.0. That standing gap is droop, or steady-state error. A bigger Kp shrinks it but never removes it.
The integral term keeps a running total of the error and pushes in proportion to it:
integral = integral + error * 0.01; // 0.01 s per step
thrust = 9.8 + 4.0 * error + 0.5 * integral - 3.0 * v;
While the drone hangs low, the total grows, and so does the push, until the drone sits exactly on 10 m. Only then does the error, and the total's growth, stop.
Tuning, roughly
Start with P alone and raise it until the response is quick. Add D until the overshoot goes. Add a small I only if a steady error remains. Each gain has a sweet spot: too much D creeps in slowly, too much I overshoots.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Brakes” is lesson 4 of 6 in Feedback Falls, Control Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Arrives faster but overshoots and swings. A bigger push means more speed on the way in, and the drone can't lose that speed instantly. It flies past, the push reverses, and it swings back. Fast, but not clean.
- Why not “Arrives slower”? More gain means a stronger push per metre of error, so the drone gets there sooner, not later.
- Why not “Never moves”? A bigger gain makes the push stronger, not weaker. With any error at all, the drone is pushed toward the target.
- Why not “Arrives faster and stops dead”? A bigger gain pushes harder, so the drone arrives with more speed. Proportional control alone has nothing to brake that speed, so it overshoots.
Show the answer
Answer: Reduces it: it pushes against the climb. v is positive on the way up, so −Kd × v is negative: less thrust. The faster it climbs, the more the brakes come on, and it arrives with little speed to lose.
- Why not “Increases it”? The damping term pushes against the speed. Climbing is positive speed, so −Kd × v is negative and takes thrust away.
- Why not “Nothing until the target”? The damping term acts on speed, not position, so it works all the way in, not only at the target.
- Why not “Flips it negative”? The term takes some thrust away, it doesn't reverse it. The hover thrust and the P term are still there, so the drone eases off rather than being pulled down.
Show the answer
Answer: Grows as long as any error remains, until the error is gone. Each step it adds the current error to a running total. A small error that persists keeps adding up, so the push keeps growing until the error reaches zero and the total stops changing.
- Why not “Depends on speed”? Pushing against speed is the D term's job. The integral term adds up the error over time.
- Why not “Is fixed”? The integral term is a running total of the error, so it keeps changing while any error remains. Only once the error is gone does it hold steady.
- Why not “Only acts far from the target”? It acts on any error that lasts, however small. A small error that persists near the target is exactly what it's for.
“Brakes” is lesson 4 of 6 in Feedback Falls, Control Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 3 before it first.
Questions people ask
What does PID stand for?
Proportional, integral, derivative: the three terms a PID controller adds up. P pushes in proportion to the error now, I pushes in proportion to the error added up over time, and D pushes against how fast the error is changing.
What does the D term do in PID?
It acts like brakes. It pushes against the rate of change, so the faster the system approaches its target, the harder D holds it back. That lets you use a bolder P gain without overshooting.
Why does a P controller never quite reach the target?
When the system needs a steady push to hold its position, like a drone carrying an unexpected load, P can only make that push from a steady error. The gap that remains is called droop, or steady-state error. The I term removes it.
What is integral windup?
While the error is large for a long time, such as during a long climb, the integral builds up a big total. That stored push can carry the system past its target. Capping the total, called anti-windup, keeps it in check.
Related topics
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.
- millis() vs delay() on ArduinoWhy delay() freezes your sketch, and how millis() runs several jobs at once.
- Low-pass filter explainedA resistor and a capacitor that pass the bass and fade the treble, and where the cutoff sits.