Practical PID: Derivative Filter, Setpoint Weighting and Bumpless Transfer
The PID of the structure lesson works perfectly in a simulation and badly on a bench. The derivative term turns every millikelvin of sensor noise into actuator chatter and answers a setpoint step with a spike the actuator cannot deliver. Setpoint steps overshoot even when load disturbances are rejected nicely. And when the operator switches from manual to automatic, the valve or the heater jumps. Industrial controllers solve all three with a few lines of code: a derivative filter, setpoint weighting (a two-degree-of-freedom PID) and bumpless transfer. The fourth practical issue, integrator windup, has its own lesson (Anti-Windup).
1. The derivative filter
An ideal derivative has gain growing without bound with frequency. Any real implementation limits it with a first-order filter:
is the maximum derivative gain relative to : at high frequency the D term contributes instead of infinity. For noise with standard deviation sampled at high bandwidth, the D term's contribution to is roughly proportional to , so trades phase lead (better damping) against actuator noise. Many designs also filter the measurement for the whole PID with a second-order filter whose time constant is or .
2. Setpoint weighting: a two-degree-of-freedom PID
The controller does not have to treat the setpoint and the measurement the same way:
| Weight | Typical | Effect |
|---|---|---|
| (proportional) | 0 to 1 | reduces setpoint overshoot and the initial actuator jump; is "I-PD" control |
| (derivative) | 0 | is "derivative on measurement": no derivative kick on setpoint steps |
The integral term must use the full error , or the steady-state error returns. The weights change the zeros of the setpoint-to-output transfer function only: the closed-loop poles, the load-disturbance response and the stability margins stay exactly as they were. That is why a loop can be tuned hard for disturbances (SIMC, Ziegler–Nichols) and still have a gentle setpoint response.
3. Bumpless transfer
When a controller switches from manual (operator sets ) to automatic, its integral state is whatever it was: usually zero, or a stale value. The output jumps from to . Bumpless transfer initialises the integrator at the moment of switching:
so is continuous. The same idea applies when gains are changed on line (in the parallel form, changing while the integral of the error is stored would jump the output; storing the integral term instead avoids it) and when a loop is handed from one controller to another in a cascade or a selector. A common trick in manual mode is setpoint tracking: follows , so the switch-over starts with zero error.
Worked example: the three fixes on the lab plant
Given: the plant , PID , , (), a controller running every .
Find: the peak actuator command for a unit setpoint step with a nearly ideal derivative (), with , with derivative on measurement () and with ; the actuator noise caused by sensor noise of for and ; the output jump when switching from manual () to automatic with and without bumpless initialisation.
Solve: with the derivative kick at the first sample is about (a little less, , with the backward-Euler filter at ), plus from the proportional term: . With the kick disappears and : 3, or 1.5 with . The program checks these and the rest:
import numpy as np
Kp, Ki, Kd = 3.0, 2.0, 0.5
dt, tend = 1e-3, 8.0
def run(b=1.0, c=1.0, N=10.0, noise=0.0, manual_until=None, u_man=0.4, bumpless=True, seed=1):
rng = np.random.default_rng(seed)
x1 = x2 = 0.0 # plant states: (s+1) then (0.5s+1)
I = D = 0.0
Tf = Kd / (Kp * N)
v_prev = 0.0 # at rest before t = 0: r = 0, y = 0
us, ys = [], []
for k in range(int(tend / dt)):
t = k * dt
r = 1.0
ym = x2 + noise * rng.standard_normal()
v = c * r - ym
D = (Tf * D + Kd * (v - v_prev)) / (Tf + dt) # filtered derivative, backward Euler
v_prev = v
if manual_until is not None and t < manual_until:
u = u_man
if bumpless:
I = u_man - Kp * (b * r - ym) - D # keep the integral ready for the switch
else:
u = Kp * (b * r - ym) + I + D
I += Ki * (r - ym) * dt
x1 += dt * (u - x1)
x2 += dt * (x1 - x2) / 0.5
us.append(u); ys.append(x2)
return np.array(us), np.array(ys)
for name, kw in (("D on error, N=100", dict(N=100)), ("D on error, N=10", dict(N=10)),
("D on measurement (c=0)", dict(c=0.0)), ("c=0 and b=0.5", dict(c=0.0, b=0.5))):
u, y = run(**kw)
print(f"{name:23s}: u(0) = {u[0]:6.2f}, max u = {u.max():6.2f}, overshoot {max(0.0, 100 * (y.max() - 1)):5.2f} %")
for N in (100, 10):
u, _ = run(N=N, noise=0.01)
print(f"sensor noise 0.01, N={N:3d}: std of u in the last 3 s = {u[-3000:].std():.3f}")
for bl in (False, True):
u, y = run(c=0.0, manual_until=4.0, bumpless=bl)
k = int(4.0 / dt)
print(f"manual -> auto at 4 s, bumpless={bl!s:5s}: u jumps from {u[k - 1]:.3f} to {u[k]:.3f}")D on error, N=100 : u(0) = 190.50, max u = 190.50, overshoot 1.89 %
D on error, N=10 : u(0) = 31.30, max u = 31.30, overshoot 1.73 %
D on measurement (c=0) : u(0) = 3.00, max u = 3.01, overshoot 7.42 %
c=0 and b=0.5 : u(0) = 1.50, max u = 1.54, overshoot 0.00 %
sensor noise 0.01, N=100: std of u in the last 3 s = 2.099
sensor noise 0.01, N= 10: std of u in the last 3 s = 0.315
manual -> auto at 4 s, bumpless=False: u jumps from 0.400 to 1.836
manual -> auto at 4 s, bumpless=True : u jumps from 0.400 to 0.400
The nearly ideal derivative hits the actuator with a command of 190 for one sample, the filtered one with 31: both far beyond any real driver, which would saturate. Derivative on measurement removes the spike altogether (), but the overshoot rises from about to : the textbook form owed its small overshoot to the zero that the derivative puts in the setpoint path, i.e. to the spike itself. Lowering to halves the initial command and removes the overshoot completely, while the disturbance response (not shown) is identical in all four cases. Sensor noise of produces an actuator noise of with and with , a factor of 6.7. And without bumpless initialisation the switch to automatic makes the command jump from to ; with it, the command continues at .
Lab: derivative on the measurement
The PID lab has the switch ("D term acts"). With , , on the error, the readouts are an overshoot of and a settling time of ; on the measurement, and . Here the textbook form looks better, because its zero speeds up the response, but only because the lab's derivative is ideal and its actuator unlimited: the "better" response is bought with an infinite spike in . The closed-loop poles printed in the lab are the same in both cases.
Guided lab: see derivative kick and noise on a motor speed loop
Hand calculation. For your speed loop's and , estimate the kick for a setpoint step and compare with the driver's limit; estimate the actuator noise from the encoder's speed quantisation ().
Simulation. Adapt the program's plant to your motor model and sample time; try , , .
What you would measure. The small motor on a current-limited supply with an encoder and an H-bridge driven by a microcontroller PID at . Log the PWM duty cycle. With D on the error, each speed step produces a full-scale duty spike for a few samples; with it disappears. With the duty cycle visibly chatters at constant speed, with much less; expect the duty-cycle standard deviation to scale roughly with (within a factor of 2: quantisation noise is not white).
Hazards and safe setup. Current limit about , motor clamped, nothing on the shaft, enable switch within reach. Duty spikes can trip the supply's current limit; that is the expected protection, not a fault.
Pitfalls
- Applying setpoint weighting to the integral term: the steady-state error returns.
- Choosing so large that the derivative filter is faster than the sample rate (): the filter does nothing.
- Implementing bumpless transfer only in one direction: auto-to-manual needs the manual output to start from the last automatic output.
- Changing on line in a controller that stores rather than the integral term.
Practice
- , , . What is , and what is the high-frequency gain of the D term?
- A loop with overshoots by 25 % on setpoint steps but rejects loads well. What do you change, and what stays the same?
- In manual mode , , , per unit, , . What must the integral term be at the switch?
Answer
- ; high-frequency gain .
- Reduce (e.g. 0.5) and use ; poles, margins and load response stay the same.
- .
Test yourself
With a derivative filter , the D term's gain at high frequency is limited to
Setpoint weighting with changes
- the closed-loop poles
- the load-disturbance response
- only the setpoint-to-output zeros (setpoint response)
- the steady-state error
Why must the integral term use the full error ?
- for noise reasons
- otherwise the steady-state error to setpoint changes returns
- to avoid windup
- for bumpless transfer
Bumpless transfer from manual to automatic sets
- the integrator so that the automatic output equals the manual output at the switch
- for one sample
- the derivative filter to zero