Simvik

PI controller with anti-windup

Implement an integer PI control step with clamped integral and output. A hard embedded C problem (Fixed-point and signal processing), run and graded in the…

hard · Fixed-point and signal processing · control · pid

A PI controller drives its output from the current error and from the accumulated error. Implement one control step:

  1. Add error to integral, then clamp integral to the range -i_limit to +i_limit.
  2. Compute (kp * error + ki * integral) / 256, with the division truncating toward zero.
  3. Clamp that to out_min .. out_max and return it.

The gains are fixed-point values scaled by 256, so kp = 512 means 2.0.

Starter code

int32_t pi_update(pi_t *c, int32_t error) {
  // integrate, clamp the integral, combine, clamp the output
  return 0;
}

Example tests

Proportional term only

pi_t c = { 512, 0, 0, 1000, -100, 100 };
printf("%d ", pi_update(&c, 10));
printf("%d\n", pi_update(&c, -30));

Expected output:

20 -60

Integral term accumulates

pi_t c = { 0, 128, 0, 1000, -100, 100 };
for (int i = 0; i < 3; i++) printf("%d ", pi_update(&c, 10));
printf("\n");

Expected output:

5 10 15

Learn it first

More Fixed-point and signal processing problems