Simvik

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 1000

Learn it first

  • PID control · Feedback loops, the three terms, anti-windup and tuning.

More PID control problems