Week 8 — Control Systems Implementation
Day 2 of 7 | Difficulty: ⭐⭐⭐
Why PID?
If you can only learn one control algorithm, learn PID. It runs on 90%+ of industrial robots, CNC machines, drones, and 3D printers. It’s simple, robust, and surprisingly effective.
PID stands for:
- Proportional — reacts to present error
- Integral — reacts to past error accumulation
- Derivative — reacts to future error trend
The PID Equation
$$u(t) = K_p \cdot e(t) + K_i \int_0^t e(\tau) d\tau + K_d \frac{de}{dt}$$
Where:
- $u(t)$ = control output (torque, voltage, PWM duty)
- $e(t)$ = error = reference − measured
- $K_p, K_i, K_d$ = tuning gains
In discrete time (what we actually code):
$$u_k = K_p \cdot e_k + K_i \cdot \sum_{j=0}^{k} e_j \cdot T_s + K_d \cdot \frac{e_k - e_{k-1}}{T_s}$$
Where $T_s$ = sampling period.
What Each Term Does
Proportional (P)
Action: Push harder when error is larger.
Effect: Reduces rise time, leaves steady-state error.
Risk: Too high → oscillation; too low → sluggish.
Integral (I)
Action: Accumulate error over time and push until it’s gone.
Effect: Eliminates steady-state error.
Risk: Too high → overshoot, instability; causes integral windup.
Derivative (D)
Action: Brake when approaching the target too fast.
Effect: Reduces overshoot, damps oscillation.
Risk: Too high → amplifies noise; sensitive to sensor jitter.
Tuning Rules
Ziegler-Nichols Method (Classic)
- Set $K_i = 0$, $K_d = 0$.
- Increase $K_p$ until the system oscillates with constant amplitude.
- Record $K_u$ (ultimate gain) and $T_u$ (oscillation period).
- Set gains via table:
| Controller | $K_p$ | $T_i$ | $T_d$ |
|---|---|---|---|
| P | $0.5 K_u$ | — | — |
| PI | $0.45 K_u$ | $T_u / 1.2$ | — |
| PID | $0.6 K_u$ | $T_u / 2$ | $T_u / 8$ |
Then $K_i = K_p / T_i$, $K_d = K_p \cdot T_d$.
Caveat: Z-N gives aggressive tuning. Use for stable processes only.
Manual Tuning Heuristic
- Start with $K_p$ small, $K_i = K_d = 0$.
- Increase $K_p$ until fast response with slight oscillation.
- Add small $K_d$ to kill oscillation.
- Add small $K_i$ to eliminate steady-state offset.
- Iterate.
Anti-Windup: The Integral’s Dark Side
When a motor is saturated (maxed out at voltage/torque limit), the integrator keeps accumulating error. Even after the error flips sign, the integrator output is so large that the system overshoots dramatically.
Solutions:
Conditional Integration
Only integrate when the error is below a threshold:
if |e_k| < threshold:
integral += e_k * Ts
Back-Calculation
When the actuator saturates, reduce the integrator by the difference between unsaturated and saturated output:
integral += (e_k - (u_unsat - u_sat) / Ki) * Ts
Clamping
Hard-limit the integrator:
integral = clamp(integral, -max, +max)
Rule: Always implement anti-windup on real robots. Without it, integral terms will cause crashes.
PID Variants for Robotics
| Variant | When to Use |
|---|---|
| Position PID | Joint angle control (most common) |
| Velocity PID | Speed control, smooth motion |
| Cascade PID | Outer position loop + inner velocity loop |
| Feedforward + PID | Known trajectories: add $u_{ff}$ to reduce PID burden |
Cascade control is the industrial standard: an outer loop commands velocity, an inner loop commands torque. This gives both accuracy and stability.
Practical Considerations
Sampling Rate
- Joint position control: 100 Hz–1 kHz
- Torque control: 1–10 kHz
- Rule of thumb: loop frequency ≥ 20× bandwidth
Filtering
Derivative amplifies noise. Use a low-pass filter on the derivative term:
$$D_{filtered} = \frac{\tau}{\tau + T_s} D_{prev} + \frac{T_s}{\tau + T_s} D_{raw}$$
Where $\tau$ = filter time constant (typically 10–100× $T_s$).
Key Takeaway
PID is the “hello world” of control. Master it before moving to advanced techniques. The secret is not the math—it’s the tuning and anti-windup that separate working robots from broken ones.
Next: Day 3 — State-Space Control and Observers