HeapCrash / Challenges / Periodic control loop

Periodic control loop

A 100 Hz temperature control loop. Testers report the loop runs slower than expected and the output stays saturated long after the setpoint is reached.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

static float integral;
static float prev_err;

float pid_step(float err, float dt)
{
    integral += err * dt;
    float out = KP * err + KI * integral
              + KD * (err - prev_err) / dt;
    return out;
}

void control_task(void *p)
{
    for (;;) {
        float err = setpoint - read_temp();
        set_output(pid_step(err, 0.01f));
        vTaskDelay(pdMS_TO_TICKS(10));
    }
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs