PID Control
You've got a closed-loop system with a sensor, a controller, and an actuator. The sensor tells you the robot is 0.5 meters away from the target. Now what? How do you decide how fast to drive?
This is where PID control comes in. PID stands for Proportional-Integral-Derivative — three terms that work together to calculate the right control output based on the error.
It's the most popular control algorithm in robotics, used everywhere from motor speed control to drone stabilization to temperature regulation. And once you understand it, you'll see it everywhere.
The Goal
The goal of a PID controller is simple: drive the error to zero as quickly and smoothly as possible.
- Error =
desired value - actual value - Control output = PID computes this based on the error
Let's say you want your robot to drive at 1.0 m/s, but it's currently going 0.7 m/s. The error is 1.0 - 0.7 = 0.3. The PID controller takes that error and calculates how much motor power to apply to fix it.
The P Term: Proportional
The P term (Proportional) is the simplest. It says:
"The control output should be proportional to the error."
In code:
The bigger the error, the bigger the correction. Simple and effective.
P Term Alone: Problems
If you only use P control, you run into two issues:
- Overshoot — If
Kpis too high, the robot accelerates too aggressively, overshoots the target, and oscillates back and forth. - Steady-State Error — If there's constant resistance (friction, gravity, wind), the error never quite reaches zero. The robot might settle at 0.95 m/s instead of 1.0 m/s because the P term gets so small that it can't overcome the drag.
To fix these, we add the I and D terms.
The I Term: Integral
The I term (Integral) accumulates the error over time. It says:
"If the error persists, ramp up the correction."
This eliminates steady-state error. If the robot is consistently 0.05 m/s slow, the integral term builds up and adds extra power until the error is gone.
The integral term grows as long as there's any error, no matter how small. Eventually, it overpowers the resistance and drives the error to exactly zero.
I Term Alone: Problems
- Windup — If the error is large for a long time (e.g., the robot is blocked), the integral term can grow huge. When the blockage is removed, the robot shoots forward uncontrollably. This is called integral windup.
- Slow Response — The I term takes time to accumulate, so it doesn't react quickly to sudden changes.
To fix this, we add the D term.
The D Term: Derivative
The D term (Derivative) looks at the rate of change of the error. It says:
"If the error is decreasing quickly, ease off the correction to avoid overshoot."
This acts like a damper — it slows down aggressive corrections and smooths out oscillations.
If the error is shrinking fast (derivative is negative), the D term reduces the output, preventing overshoot. If the error is growing fast (derivative is positive), the D term boosts the output, speeding up the response.
D Term Alone: Problems
- Noise Sensitivity — The derivative amplifies noise. If your sensor jitters even slightly, the derivative will spike and cause erratic behavior. You often need to low-pass filter the sensor data before computing the derivative.
- No Correction at Steady State — If the error is constant, the derivative is zero, so the D term does nothing. It only helps during transitions.
PID Together: The Full Algorithm
Now combine all three terms:
This is the standard PID formula:
output = Kp * error + Ki * ∫error dt + Kd * (d(error)/dt)
In practice, it's usually written as:
output = Kp * e(t) + Ki * ∫e(τ)dτ + Kd * (de(t)/dt)
But the code version is easier to understand.
What Each Term Does
| Term | What it does | When it helps | Problem it causes |
|---|---|---|---|
| P | Reacts proportionally to current error | Always — the workhorse of PID | Overshoot, oscillation, steady-state error |
| I | Eliminates steady-state error by accumulating past error | When there's constant drag or bias | Integral windup, slow response |
| D | Damps oscillations by predicting future error | When the system overshoots or oscillates | Noise amplification, requires smooth sensor data |
A good mental model:
- P = "How far off am I right now?"
- I = "How far off have I been over time?"
- D = "How fast am I approaching the target?"
Many real-world systems use just PI control (no D term) because sensor noise makes the derivative unreliable. Start with PI, and add D only if you see persistent oscillations.
Interactive Tuning Intuition
Imagine you're tuning a PID controller for a drone's altitude. You want it to hover at 10 meters.
- Too much P: The drone shoots up to 10m, overshoots to 12m, drops to 8m, overshoots again — oscillates forever.
- Too much I: The drone slowly drifts upward even when at the target because the integral term has accumulated. Then it shoots way past and takes a long time to settle.
- Too much D: The drone reacts to every tiny gust of wind (noise), causing jittery, erratic movements.
- Just right: The drone smoothly ascends to 10m, gently settles without overshoot, and holds position despite wind.
Tuning PID is an art — we'll cover practical tuning methods in Lesson 5.
What's Next?
PID is the algorithm, but what are you controlling? In the next lesson, we'll dive into motor control modes — velocity control, position control, and torque control — and how PID fits into each one.