The Proportional-Integral-Derivative (PID) controller is the workhorse of industrial feedback control. It continuously computes an error
Despite its simplicity, PID handles a remarkably wide range of plants — from temperature regulation to motor speed control — because each term addresses a different aspect of the transient response:
- P reacts immediately to the current error.
- I eliminates steady-state offset by integrating past errors.
- D anticipates future error by acting on the rate of change.
This library implements a discrete recursive form suitable for fixed-sample-rate embedded loops, where the output is computed incrementally rather than from scratch at every step.
Discretizing with sampling period
where:
This incremental (velocity) form has two key advantages:
- No integral accumulator — the integral action is implicit in the recursion, reducing overflow risk.
- Bumpless mode switching — toggling between auto/manual mode does not cause output jumps because the output is updated incrementally.
The output is clamped to
| Case | Time | Space | Notes |
|---|---|---|---|
| Per sample | 3 multiplications, 3 additions, 1 clamp |
The algorithm uses a fixed-size recursive buffer storing
Parameters:
Coefficients:
$a_0 = 2 + 0.5 + 0.1 = 2.6$ $a_1 = -(2 + 0.2) = -2.2$ $a_2 = 0.1$
| Step | Measured |
|
||||
|---|---|---|---|---|---|---|
| 0 | 0.0 | 5.0 | 0 | 0 | 10.0 (clamped) | |
| 1 | 2.0 | 3.0 | 5.0 | 0 | 6.8 | |
| 2 | 4.0 | 1.0 | 3.0 | 5.0 | 3.3 | |
| 3 | 4.8 | 0.2 | 1.0 | 3.0 | 1.92 |
The output converges toward the steady-state value needed to maintain the setpoint.
-
Derivative kick. A sudden change in setpoint causes a spike in
$e[n] - e[n-1]$ . Mitigate by differentiating the process variable instead of the error, or by filtering the derivative term. -
Integral windup. Although the recursive form provides implicit anti-windup through output clamping, very aggressive
$K_i$ values can still cause sluggish recovery from saturation. Monitor the output rail time. -
Sample rate dependency. The gains
$K_i$ and$K_d$ are implicitly scaled by$T_s$ . Changing the control loop rate without retuning will alter the effective integral and derivative contributions. -
Fixed-point saturation. For Q15/Q31 types, the intermediate products
$a_0 \cdot e[n]$ etc. can overflow. Ensure gains and error ranges are scaled to stay within the representable range. -
Setpoint jumps. Call
Reset()after large setpoint changes to clear stale error history.
| Variant | Key Difference |
|---|---|
| Standard (positional) PID | Computes |
| PI controller |
|
| PD controller |
|
| PID with derivative filter | Low-passes the derivative term to reject high-frequency noise |
| Gain-scheduled PID | Tuning parameters vary as a function of operating point |
| Cascade PID | Inner and outer loops, each with its own PID — common in motor control |
- Temperature control — Maintaining oven, room, or process temperatures via heater/cooler actuation.
- Motor speed/position control — Servo drives, robotics joints, CNC machines.
- Flow and pressure regulation — Industrial process control (chemical plants, water treatment).
- Voltage/current regulation — Power supply output regulation, battery charging.
- Attitude control — Drone stabilization, satellite pointing.
graph LR
PID["PID Controller"]
LQR["LQR Controller"]
KF["Kalman Filter"]
PID -.->|"alternative"| LQR
KF -->|"state estimate"| LQR
KF -->|"filtered measurement"| PID
| Algorithm | Relationship |
|---|---|
| LQR Controller | Optimal alternative when a state-space model is available; PID is model-free. |
| Kalman Filter | Can provide filtered state estimates as input to either PID or LQR |
- Åström, K.J. and Murray, R.M., Feedback Systems: An Introduction for Scientists and Engineers, Princeton University Press, 2008 — Chapter 10.
- Åström, K.J. and Hägglund, T., Advanced PID Control, ISA, 2006.
- Ziegler, J.G. and Nichols, N.B., "Optimum settings for automatic controllers", Transactions of the ASME, 64, 1942.