Try Embedded

RTOS: catch a periodic task missing its deadline

A FreeRTOS control task runs every 128 ms with vTaskDelayUntil and measures its own release jitter, run time and missed deadlines, while a knob adds work…

RTOS · Advanced

In a real product: Motor drive control loop (Robotics): A drive's control loop must finish inside its period every time, so its firmware watches for overruns.

A control loop is only correct if it runs on time, every time. If a step takes longer than its period, the next one starts late, the loop's timing drifts and the controller can go unstable. Real-time firmware therefore measures itself. It checks when each run started compared with when it should have (the jitter), how long it ran, and whether it finished before the next period began (the deadline).

Here a FreeRTOS task runs every 8 ticks (128 ms). The knob sets how much "control work" each run does, from 0 to 180 ms. Every second a report task prints the worst figures and the task's stack margin. The red LED lights on each missed deadline.

What you'll learn

  • vTaskDelayUntil, which keeps a fixed period, against vTaskDelay, which drifts by the run time each time
  • Measuring release time, run time and response time inside a task
  • What FreeRTOS does when a task overruns: vTaskDelayUntil returns at once, and the task falls behind
  • uxTaskGetStackHighWaterMark: how close a task came to overflowing its stack

How it works

vTaskDelayUntil(&wake, PERIOD) blocks until wake + PERIOD, then adds PERIOD to wake. The releases stay on a fixed grid (0, 8, 16, … ticks), however long each run took. With vTaskDelay(PERIOD) instead, the next run would start PERIOD after this one ended, and the run time would add to the period every time.

Each run notes micros() when it starts and when it ends. The start minus the ideal release time is the release jitter. The end minus the release is the response time. If the response time is longer than the period, the deadline was missed. vTaskDelayUntil then finds the next wake time already in the past and returns at once, so the task runs back to back until it has caught up, or never does.

The report task has a lower priority, so its printing never delays the control task. It prints the worst jitter, the worst response time, the run count and the misses. uxTaskGetStackHighWaterMark gives the fewest free stack bytes the control task has ever had: a figure that should never get near zero.

Try this

  • Turn the knob up until the response time passes 128 ms: misses start counting and the red LED flickers.
  • Replace vTaskDelayUntil with vTaskDelay(PERIOD) and count the runs in 10 s: fewer than 78.
  • Add a watchdog: reset the board if the control task hasn't run for 500 ms (wdt_enable can't be used, because this FreeRTOS port uses the watchdog for its tick, so count it in the report task instead).

sketch.ino

// A periodic FreeRTOS task (every 8 ticks) measures its release jitter, response time and missed deadlines; a
// potentiometer adds 0-180 ms of work per run. A lower-priority task reports once a second.
#include <Arduino_FreeRTOS.h>

const int KNOB = A0, MISS_LED = 9;
const TickType_t PERIOD = 8;                    // ticks
TaskHandle_t control;

volatile unsigned long worstJitter, worstResponse, runs, misses;

// Busy work standing in for the control law: ms of this task's own CPU time.
void work(unsigned long ms) {
  long left = ms * 1000L;
  unsigned long t = micros();
  while (left > 0) {
    unsigned long now = micros(), d = now - t;
    t = now;
    if (d < 200) left -= d;                      // a longer gap is time another task ran
  }
}

void controlTask(void *) {
  const unsigned long periodUs = PERIOD * portTICK_PERIOD_MS * 1000UL;
  TickType_t wake = xTaskGetTickCount();
  unsigned long release = micros();             // when this run should start, in microseconds
  for (;;) {
    vTaskDelayUntil(&wake, PERIOD);
    release += periodUs;
    unsigned long start = micros();
    unsigned long jitter = start - release;
    if ((long)jitter < 0) jitter = 0;
    work(analogRead(KNOB) * 180UL / 1023);
    unsigned long response = micros() - release;
    runs++;
    if (jitter > worstJitter) worstJitter = jitter;
    if (response > worstResponse) worstResponse = response;
    bool missed = response > periodUs;
    if (missed) misses++;
    digitalWrite(MISS_LED, missed);
  }
}

void reportTask(void *) {
  for (;;) {
    vTaskDelay(pdMS_TO_TICKS(1000));
    Serial.print(F("runs "));
    Serial.print(runs);
    Serial.print(F(", missed "));
    Serial.print(misses);
    Serial.print(F(", worst jitter "));
    Serial.print(worstJitter / 1000.0, 1);
    Serial.print(F(" ms, worst response "));
    Serial.print(worstResponse / 1000.0, 1);
    Serial.print(F(" ms of "));
    Serial.print(PERIOD * portTICK_PERIOD_MS);
    Serial.print(F(", stack left "));
    Serial.print(uxTaskGetStackHighWaterMark(control));
    Serial.println(F(" bytes"));
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(MISS_LED, OUTPUT);
  xTaskCreate(controlTask, "control", 110, NULL, 3, &control);
  xTaskCreate(reportTask, "report", 180, NULL, 1, NULL);
}

void loop() {}

Lessons behind it