PID Control
PID control is the most common feedback algorithm in practical mechatronics. It combines proportional, integral, and derivative actions to reduce error, remove steady offset, and add damping.
Learning Objectives
By the end of this lesson, you should be able to explain P, I, and D terms, write a discrete PID loop, recognize windup and derivative noise, and tune a simple axis safely.
PID Equation
Continuous PID is commonly written as:
$$
u(t)=K_p e(t)+K_i\int e(t)dt+K_d\frac{de(t)}{dt}
$$
where u(t) is actuator command and e(t) is setpoint minus measured output.
In firmware, the loop runs at a fixed sample time Ts:
error = setpoint - measurement;
integral += error * Ts;
derivative = (error - previous_error) / Ts;
command = Kp * error + Ki * integral + Kd * derivative;
previous_error = error;
What Each Term Does
| Term | Effect | Risk |
|---|---|---|
| P | reacts to present error | oscillation if too high |
| I | removes steady-state error | windup and slow recovery |
| D | predicts error change and adds damping | amplifies noise |
Saturation and Windup
Actuators have limits. If the command saturates but the integral keeps growing, the loop may overshoot badly when it finally recovers. This is integral windup.
Use anti-windup: clamp the integral, pause integration while saturated, or back-calculate the integral from the saturated command.
Tuning Workflow
- Disable integral and derivative.
- Increase
Kpuntil response is fast but not unstable. - Add
Kdif overshoot or oscillation needs damping. - Add small
Kito remove final error. - Test steps in both directions and with load changes.
- Record plots before and after every change.
Sample Time and Filtering
PID assumes regular timing. Jitter changes derivative and integral behavior. Use a fixed-rate control task. Filter noisy measurements carefully; too much filtering adds delay and can destabilize the loop.
Common Mistakes
- Starting with all three gains nonzero.
- Using derivative directly on noisy error.
- Forgetting actuator saturation and windup.
- Changing sample time without retuning gains.
- Tuning unloaded and then expecting loaded behavior to match.
Summary
PID works because P reacts, I corrects offset, and D damps change. Real PID design is mostly about limits: actuator saturation, sensor noise, timing, delay, friction, and safe tuning steps.
Further Reading
- Astrom and Hagglund, PID Controllers.
- Brett Beauregard, Improving the Beginner's PID.
- Control Engineering, PID tuning fundamentals.