Skip to content
Tactiverge

Control systems

PID controller

Also called: PID · three-term controller · Kp Ki Kd

Definition

A PID controller adds proportional, integral and derivative actions: present error, accumulated past error and predicted future error.

Picture it

P provides the main push, I removes the leftover error, D damps overshoot. Tuning is trading speed against overshoot and robustness. A common hand-tuning order is: raise P until it is lively, add D to calm it, then add just enough I to remove the offset.

The maths

u = Kp e + Ki ∫ e dt + Kd de/dt
discrete: u[k] = Kp e[k] + Ki Σ e Δt + Kd (e[k] − e[k−1]) / Δt
SI units used in this entry
QuantityUnitSymbol
timeseconds

Worked example

At one step, e = 3, the accumulated integral is 4 and de/dt = −1.5. With Kp = 2, Ki = 0.5 and Kd = 0.1, what is u?

  1. P = 2 × 3 = 6
  2. I = 0.5 × 4 = 2
  3. D = 0.1 × (−1.5) = −0.15
  4. u = 6 + 2 − 0.15 = 7.85

Answeru = 7.85.

✓ The number in this answer is recomputed from scratch by an automated test.

In Python

def pid_step(e, state, kp, ki, kd, dt):
    state["i"] += e * dt
    d = (e - state["e_prev"]) / dt
    state["e_prev"] = e
    return kp * e + ki * state["i"] + kd * d

tune it

PID step response

Dashed line: setpoint 1. Solid line: output y over 8 s.
Overshoot 0 % · 2 % settling time 5.17 s · final value 0.994

Simplified model: plant τ·dy/dt = −y + u with τ = 0.5 s and gain 1, forward-Euler steps of 0.01 s, derivative on measurement. Real plants add delay, noise and saturation.

Common misconceptions

  • Misconception: There is no universal 'best' set of gains; they depend on the plant and what you value.
  • Misconception: PID cannot make an actuator stronger; if the motor saturates, the response is limited by the motor.