Limit the rate of change
Move an output towards its target by at most a fixed step per call. An easy embedded C problem (PID control), run and graded in the browser.
easy · PID control · control · motors
Motors, heaters and valves do not like sudden jumps: a motor drive asked to go from 0 to full speed at once draws a current spike and jerks the mechanism. A slew-rate limiter lets the output follow its target, but by at most max_step per call.
Write slew_limit(last, target, max_step): move *last towards target by at most max_step (in either direction, without overshooting), and return the new value.
Starter code
int32_t slew_limit(int32_t *last, int32_t target, int32_t max_step) {
(void)max_step;
*last = target;
return *last;
}Example tests
Ramping up
ramp(0, 1000, 300, 5);Expected output:
300 600 900 1000 1000Learn 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.
- Switch from manual to automatic smoothly · Initialise the integral term so switching to automatic does not jump the output.
- 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.