Computing Library › Real Time Systems
Real Time Systems

PID Control

The PID controller combines proportional, integral, and derivative action; it is the most widely used feedback law and a template for real-time control code.

Three Terms

A proportional-integral-derivative (PID) controller computes its output from three responses to the error between setpoint and measurement. The proportional term reacts to the present error, the integral term to the accumulated past error, and the derivative term to the predicted future trend. Their weighted sum is the control command. Despite its simplicity, PID handles a large fraction of real-world control tasks well.

What Each Term Does

python

# discrete PID step, fixed sample period dt
def pid_step(setpoint, meas, state, kp, ki, kd, dt):
    err = setpoint - meas
    state['integ'] += err * dt
    deriv = (err - state['prev']) / dt
    state['prev'] = err
    return kp*err + ki*state['integ'] + kd*deriv

Real-World Refinements

Practical PID needs care beyond the textbook formula. Integral windup, where the accumulator grows while the actuator is saturated, must be limited by clamping or back-calculation. The derivative term is usually filtered because differentiating noisy measurements amplifies noise; it is often applied to the measurement rather than the error to avoid a spike when the setpoint changes. A fixed, jitter-free sample period matters because both the integral and derivative terms depend on dt.

Tuning

Tuning sets the three gains to balance speed, overshoot, and stability. Methods range from heuristic rules to model-based design. The gains interact: raising proportional gain speeds response but reduces damping; adding integral action removes offset but can destabilize; derivative adds damping but is limited by noise. Good tuning respects the loop's timing, because the effective delay of the digital implementation reduces the stability margin the gains can safely use.

When PID Is Not Enough

PID assumes a single input, single output, and relatively benign dynamics. Coupled multivariable systems, significant delays, or tight optimality requirements call for state-space or model-predictive methods. Even then, PID often survives as an inner loop, valued for its transparency and its ease of implementation in bounded real-time code.