PID Controller: From Drone to Thermostat
The PID controller — Proportional-Integral-Derivative — is the most widely deployed feedback control algorithm in existence, estimated to run in 90% of industrial control loops and in every quadcopter drone. Three terms, each fixing a different failure mode of naive feedback control.
1. Feedback Control Basics
A feedback control system compares a measured output (process variable, PV) to a desired target (setpoint, SP). The difference is the error e(t) = SP − PV. The controller uses this error to compute a control signal (output) that drives an actuator to reduce the error.
Example: a thermostat. Setpoint = 22°C. Room temperature = 18°C. Error = +4°C. Heater switches on. Temperature rises. Error shrinks. Heater reduces output. At steady state, temperature ≈ setpoint.
Naive proportional control alone has problems: overshoot, oscillation, and persistent steady-state error. The I and D terms fix these.
2. The Three PID Terms
3. The PID Equation
where:
u(t) = controller output (0–100%)
e(t) = setpoint − measured_value
Kp = proportional gain
Ki = integral gain
Kd = derivative gain
Alternatively expressed using Ti (integral time) and Td (derivative time):
4. What Each Term Does
Proportional (P)
Produces an output proportional to the current error. If Kp is too low, response is sluggish. If Kp is too high, the system oscillates. On its own, P-only control always has a residual steady-state error: to maintain heat output, the temperature must stay below setpoint ("offset").
Integral (I)
Accumulates error over time. Even a small persistent error grows in the integral term, increasing control action until the error reaches zero. This eliminates steady-state offset. Downside: integral windup — if the actuator saturates (e.g., heater at 100%), the integral keeps growing. When the setpoint is reached, the accumulated integral overshoots badly. Anti-windup clamps the integral.
Derivative (D)
Responds to the rate of change of error, acting like a "brake" that anticipates overshoot. If the error is decreasing rapidly, D reduces the control action. This dramatically improves settling time. However, D amplifies high-frequency noise in the measurement — in practice, differentiate only the process variable (not the error) to avoid derivative kick on setpoint changes, and apply a low-pass filter.
5. Digital Implementation
class PIDController {
constructor(kp, ki, kd, dt) {
this.kp = kp; this.ki = ki; this.kd = kd; this.dt = dt;
this.integral = 0;
this.prevError = 0;
this.outMin = -Infinity;
this.outMax = Infinity;
}
update(setpoint, measurement) {
const error = setpoint - measurement;
// Proportional
const P = this.kp * error;
// Integral with anti-windup clamping
this.integral += error * this.dt;
const I = this.ki * this.integral;
// Derivative on measurement (avoids derivative kick)
const D = this.kd * (this.prevError - error) / this.dt;
this.prevError = error;
const output = Math.min(this.outMax,
Math.max(this.outMin, P + I + D));
// Back-calculate anti-windup: remove excess from integral
if (output !== P + I + D) {
this.integral -= error * this.dt; // revert last integration step
}
return output;
}
}6. Tuning Methods
Ziegler-Nichols
Classic empirical method: Set Ki = Kd = 0, increase Kp until the system oscillates with constant amplitude (ultimate gain Ku, ultimate period Tu). Then set: Kp = 0.6Ku, Ki = 2Kp/Tu, Kd = KpTu/8. Often gives aggressive tuning — fine-tune from there.
Manual Tuning
- Set Ki = Kd = 0; increase Kp until fast response with acceptable overshoot (~10–20%).
- Increase Ki slowly until steady-state error is eliminated without excessive oscillation.
- Add Kd to reduce overshoot; keep small to avoid noise amplification.
7. Real-World Applications
- Drone (quadcopter): 4 independent PID loops control roll, pitch, yaw, and altitude. Update rate 500–2000 Hz using IMU feedback. Typical Kp for angle: 4–8, Ki: 0.02, Kd: 0.1.
- Temperature control: Industrial furnaces, 3D printer hot ends, brewing fermenters. Often PI only, update rate 0.1–10 Hz.
- Cruise control: Measures actual vehicle speed (from wheel encoders), controls throttle and brakes to maintain setpoint. Modern ACC adds distance-following outer loop.
- CNC machines: Each axis has a PID servo loop running at 1–10 kHz to track position commands precisely.
- Rocket guidance: TVC (thrust vector control) adjusts engine nozzle angle using PID-like controllers to maintain attitude.