Switch from manual to automatic smoothly
Initialise the integral term so switching to automatic does not jump the output. A medium embedded C problem (PID control), run and graded in the browser.
medium · PID control · pid · control
Industrial controllers have a manual mode, where an operator sets the output directly, and an automatic mode, where the PI controller does. A naive switch to automatic jumps the output to whatever the controller computes from a stale integral, which can slam a valve.
Bumpless transfer sets the integral at the moment of switching so that the first automatic output equals the last manual one.
Write pi_step(c, setpoint, measured):
- in manual mode, return
manual_out, - in automatic mode: error = setpoint − measured;
i_term += ki × error; return (kp × error + i_term) / 100,
And write pi_set_auto(c, setpoint, measured), called when the operator switches over: set i_term so that the next pi_step with the same setpoint and measurement, which first adds ki × error to i_term, returns exactly manual_out; then enter automatic mode. Solve the output equation for i_term.
Starter code
int32_t pi_step(pi_t *c, int32_t setpoint, int32_t measured) {
if (!c->automatic) return c->manual_out;
int32_t error = setpoint - measured;
c->i_term += c->ki * error;
return (c->kp * error + c->i_term) / 100;
}
void pi_set_auto(pi_t *c, int32_t setpoint, int32_t measured) {
(void)setpoint; (void)measured;
c->automatic = true;
}Example tests
No bump at the switch
pi_t c = { 150, 20, 0, false, 40 };
int32_t m[] = { 70, 70 };
show(&c, 80, m, 2);
pi_set_auto(&c, 80, 70);
int32_t m2[] = { 70, 72, 75, 78 };
show(&c, 80, m2, 4);Expected output:
40 40
40 38 35 31Learn it first
- PID control · Feedback loops, the three terms, anti-windup and tuning.
More PID control problems
- Derivative without the kick · Take the derivative of the measurement, not the error, so setpoint steps do not spike the output.
- Limit the rate of change · Move an output towards its target by at most a fixed step per call.
- Reflow oven: a PI loop that does not wind up · Run one step of a PI temperature loop whose integral stops growing while the heater is saturated.