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:


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:

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)

  1. Set $K_i = 0$, $K_d = 0$.
  2. Increase $K_p$ until the system oscillates with constant amplitude.
  3. Record $K_u$ (ultimate gain) and $T_u$ (oscillation period).
  4. 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

  1. Start with $K_p$ small, $K_i = K_d = 0$.
  2. Increase $K_p$ until fast response with slight oscillation.
  3. Add small $K_d$ to kill oscillation.
  4. Add small $K_i$ to eliminate steady-state offset.
  5. 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

VariantWhen to Use
Position PIDJoint angle control (most common)
Velocity PIDSpeed control, smooth motion
Cascade PIDOuter position loop + inner velocity loop
Feedforward + PIDKnown 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

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