Skip to content

ControlPID Control

One Step of a PID Controller

u = Kp e + Ki (sum of e Δt) + Kd (Δe / Δt), computed fresh every loop.

Not started Jump to practice

Two more terms fix P control's weaknesses: one remembers the past, one looks at the trend.

Key idea

PID: $u = K_p e + K_i \sum e\,\Delta t + K_d \dfrac{e - e_{\text{prev}}}{\Delta t}$. The integral term adds up error over time and removes steady-state error. The derivative term reacts to how fast the error changes and damps overshoot.
Turn each term on and off. Watch the graph: I removes the gap, D calms the wobble.
  • Each pass: measure, compute $e$.
  • Integral: $I \leftarrow I + e\,\Delta t$.
  • Derivative: $D = (e - e_{\text{prev}})/\Delta t$.
  • Command: $u = K_p e + K_i I + K_d D$; clip it; send it.
  • Save $e$ as $e_{\text{prev}}$.

Worked example: One step

$K_p = 2$, $K_i = 1$, $K_d = 0.1$, $\Delta t = 0.05$. Previous error 10, integral 3. Now $e = 8$.
  1. $I = 3 + 8 \times 0.05 = 3.4$.
  2. $D = (8 - 10)/0.05 = -40$.
  3. $u = 16 + 3.4 - 4 = 15.4$.

Answer: 15.4.

Why it works

The derivative term is negative here because the error is shrinking: it eases off before the cart overshoots. The integral term keeps pushing a little as long as any error is left.

Common mistake

Forgetting $\Delta t$, or letting the integral grow without bound while the motor is saturated (called windup). Real code limits the integral.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.

Loading practice…

Builds on

Leads to

Sources and further reading