TExcelTAD
Home › Control Systems & Automation › Practical PID: Derivative Filter, Setpoint Weighting and Bumpless Transfer

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 KdsK_d s has gain growing without bound with frequency. Any real implementation limits it with a first-order filter:

D(s)=Kd s1+s Tf,Tf=TdN=KdKpN,N≈5 to 20D(s) = \frac{K_d\,s}{1 + s\,T_f}, \qquad T_f = \frac{T_d}{N} = \frac{K_d}{K_p N}, \quad N \approx 5\ \text{to}\ 20

NN is the maximum derivative gain relative to KpK_p: at high frequency the D term contributes KpNK_p N instead of infinity. For noise nn with standard deviation σn\sigma_n sampled at high bandwidth, the D term's contribution to uu is roughly proportional to KpNσnK_p N \sigma_n, so NN 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 Td/NT_d/N or Ti/NT_i/N.

2. Setpoint weighting: a two-degree-of-freedom PID

The controller does not have to treat the setpoint and the measurement the same way:

u=Kp (b r−y)+Ki∫(r−y) dt+Kd ddt(c r−y)u = K_p\,(b\,r - y) + K_i\int (r - y)\,dt + K_d\,\frac{d}{dt}(c\,r - y)
Weight Typical Effect
bb (proportional) 0 to 1 b<1b < 1 reduces setpoint overshoot and the initial actuator jump; b=0b = 0 is "I-PD" control
cc (derivative) 0 c=0c = 0 is "derivative on measurement": no derivative kick on setpoint steps

The integral term must use the full error r−yr - y, 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 umanu_\text{man}) to automatic, its integral state is whatever it was: usually zero, or a stale value. The output jumps from umanu_\text{man} to Kp(br−y)+IK_p(br - y) + I. Bumpless transfer initialises the integrator at the moment of switching:

I←uman−Kp (b r−y)−DI \leftarrow u_\text{man} - K_p\,(b\,r - y) - D

so uu is continuous. The same idea applies when gains are changed on line (in the parallel form, changing KiK_i while the integral of the error is stored would jump the output; storing the integral term I=Ki∫eI = K_i\int e 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: rr follows yy, so the switch-over starts with zero error.

Worked example: the three fixes on the lab plant

Given: the plant G(s)=1(s+1)(0.5s+1)G(s) = \frac{1}{(s + 1)(0.5 s + 1)}, PID Kp=3K_p = 3, Ki=2 s−1K_i = 2\ \text{s}^{-1}, Kd=0.5 sK_d = 0.5\ \text{s} (Td=0.167 sT_d = 0.167\ \text{s}), a controller running every 1 ms1\ \text{ms}.

Find: the peak actuator command for a unit setpoint step with a nearly ideal derivative (N=100N = 100), with N=10N = 10, with derivative on measurement (c=0c = 0) and with b=0.5b = 0.5; the actuator noise caused by sensor noise of σ=0.01\sigma = 0.01 for N=100N = 100 and N=10N = 10; the output jump when switching from manual (u=0.4u = 0.4) to automatic with and without bumpless initialisation.

Solve: with N=10N = 10 the derivative kick at the first sample is about Kd/Tf=KpN=30K_d/T_f = K_p N = 30 (a little less, Kd/(Tf+Ts)=28.3K_d/(T_f + T_s) = 28.3, with the backward-Euler filter at Ts=1 msT_s = 1\ \text{ms}), plus Kpb=3K_p b = 3 from the proportional term: u(0)≈31u(0) \approx 31. With c=0c = 0 the kick disappears and u(0)=Kpbu(0) = K_p b: 3, or 1.5 with b=0.5b = 0.5. The program checks these and the rest:

python · try it yourself
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 (u(0)=Kp=3u(0) = K_p = 3), but the overshoot rises from about 1.8 %1.8\,\% to 7.42 %7.42\,\%: 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 bb to 0.50.5 halves the initial command and removes the overshoot completely, while the disturbance response (not shown) is identical in all four cases. Sensor noise of 0.010.01 produces an actuator noise of 2.12.1 with N=100N = 100 and 0.320.32 with N=10N = 10, a factor of 6.7. And without bumpless initialisation the switch to automatic makes the command jump from 0.4000.400 to 1.8361.836; with it, the command continues at 0.4000.400.

Lab: derivative on the measurement

The PID lab has the cc switch ("D term acts"). With Kp=3K_p = 3, Ki=2K_i = 2, Kd=0.5K_d = 0.5 on the error, the readouts are an overshoot of 1.94 %1.94\,\% and a settling time of 1.212 s1.212\ \text{s}; on the measurement, 7.72 %7.72\,\% and 2.808 s2.808\ \text{s}. 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 uu. The closed-loop poles printed in the lab are the same in both cases.

r = 1+C(s): Kp 3, Ki 2, Kd 0.5P(s) = 1/((1s+1)(0.5s+1))y-T(s) = (3.000 s + 2.000) / (0.500 s^3 + 2.000 s^2 + 4.000 s + 2.000)closed-loop poles: -0.70, -1.65+j1.72, -1.65-j1.720.00.51.001 s2 s3 s4 sty(t)t_s 2.81 s+7.7 %y(t)setpoint10-90 % rise

Guided lab: see derivative kick and noise on a motor speed loop

Hand calculation. For your speed loop's KpK_p and TdT_d, estimate the kick KpNΔrK_p N \Delta r for a setpoint step Δr\Delta r and compare with the driver's limit; estimate the actuator noise from the encoder's speed quantisation (Δω=2π/(counts per rev⋅Ts)\Delta\omega = 2\pi/(\text{counts per rev} \cdot T_s)).

Simulation. Adapt the program's plant to your motor model and sample time; try N=5N = 5, 1010, 2020.

What you would measure. The small motor on a current-limited 12 V12\ \text{V} supply with an encoder and an H-bridge driven by a microcontroller PID at 1 kHz1\ \text{kHz}. Log the PWM duty cycle. With D on the error, each speed step produces a full-scale duty spike for a few samples; with c=0c = 0 it disappears. With N=20N = 20 the duty cycle visibly chatters at constant speed, with N=5N = 5 much less; expect the duty-cycle standard deviation to scale roughly with NN (within a factor of 2: quantisation noise is not white).

Hazards and safe setup. Current limit about 1 A1\ \text{A}, 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 NN so large that the derivative filter is faster than the sample rate (Tf<TsT_f < T_s): 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 KiK_i on line in a controller that stores ∫e\int e rather than the integral term.

Practice

  1. Kp=2K_p = 2, Td=0.4 sT_d = 0.4\ \text{s}, N=8N = 8. What is TfT_f, and what is the high-frequency gain of the D term?
  2. A loop with b=1b = 1 overshoots by 25 % on setpoint steps but rejects loads well. What do you change, and what stays the same?
  3. In manual mode u=35 %u = 35\,\%, r=60r = 60, y=58y = 58, Kp=1.5 %K_p = 1.5\ \% per unit, b=1b = 1, D=0D = 0. What must the integral term be at the switch?
Answer
  1. Tf=0.4/8=0.05 sT_f = 0.4/8 = 0.05\ \text{s}; high-frequency gain KpN=16K_p N = 16.
  2. Reduce bb (e.g. 0.5) and use c=0c = 0; poles, margins and load response stay the same.
  3. I=35−1.5×(60−58)=32 %I = 35 - 1.5 \times (60 - 58) = 32\,\%.

Test yourself

With a derivative filter Tf=Td/NT_f = T_d/N, the D term's gain at high frequency is limited to

  1. KdK_d
  2. KpNK_p N
  3. NN
  4. KiTfK_i T_f

Setpoint weighting with b<1b < 1 changes

  1. the closed-loop poles
  2. the load-disturbance response
  3. only the setpoint-to-output zeros (setpoint response)
  4. the steady-state error

Why must the integral term use the full error r−yr - y?

  1. for noise reasons
  2. otherwise the steady-state error to setpoint changes returns
  3. to avoid windup
  4. for bumpless transfer

Bumpless transfer from manual to automatic sets

  1. r=0r = 0
  2. the integrator so that the automatic output equals the manual output at the switch
  3. Kp=0K_p = 0 for one sample
  4. the derivative filter to zero