Try Embedded

RTOS: a machine start interlock with an event group

Three FreeRTOS tasks each watch one condition (guard door, temperature, start button) and set a bit in an event group; the motor task waits for all three…

RTOS · Intermediate

In a real product: Machine tool start interlock (Industrial): A machine only starts with its guard closed, its temperature in range and the operator's start command.

A machine tool may only start when several things are true together: the guard door is shut, the spindle isn't overheating, and the operator pressed Start. Each condition is watched by its own task. They need a way to tell the motor task "my condition holds", and the motor task needs to wait for all of them at once.

A FreeRTOS event group is that: a word of flag bits that tasks set and clear, and that a task can block on until any, or all, of a chosen set of bits are 1.

What you'll learn

  • xEventGroupCreate, xEventGroupSetBits, xEventGroupClearBits and xEventGroupWaitBits
  • Waiting for all bits (xWaitForAllBits = pdTRUE) against any bit, and clearing bits on exit
  • Level conditions (door, temperature) against an event (the Start press), and why the start bit must be cleared after use
  • Why a stop condition is checked again while running, not only before starting

How it works

Three bits: DOOR_CLOSED (bit 0), TEMP_OK (bit 1) and START (bit 2).

  • The door task reads the slide switch every 20 ms and sets or clears DOOR_CLOSED. Slide it to the right to close the door.
  • The temperature task reads the potentiometer as 0 to 100 °C every 100 ms, and keeps TEMP_OK set below 70 °C.
  • The button task sets START when the Start button is pressed.

The motor task calls xEventGroupWaitBits(group, ALL, pdFALSE, pdTRUE, ...): don't clear on exit, wait for all. It sleeps until the three bits are 1 together. While it waits, it prints every second which bits are still missing. Then it clears START itself. A press is an event: it must not still count after the motor stops.

While the motor runs (green LED), the task checks the group every 50 ms. If the door opens or the temperature rises, it stops at once and lights the red fault LED. To start again, the condition must come back and the operator must press Start again. A machine that restarts on its own when the door shuts would be dangerous.

Try this

  • Close the door and press Start with the knob low: the motor runs. Open the door while it runs and see the stop.
  • Change the wait to "any bit" (pdFALSE as the last flag) and see why it is wrong for an interlock.
  • Add a fourth condition, an emergency stop button that clears a NO_ESTOP bit, using only one new task.

sketch.ino

// A start interlock on FreeRTOS: three tasks set the bits of an event group (door closed, temperature OK, start
// pressed); the motor task waits for all three, and stops if the door or the temperature bit drops while it runs.
#include <Arduino_FreeRTOS.h>
#include <event_groups.h>

const int DOOR = 4, KNOB = A0, START_BUTTON = 2, MOTOR_LED = 8, FAULT_LED = 9;
const EventBits_t DOOR_CLOSED = 1 << 0, TEMP_OK = 1 << 1, START = 1 << 2;
const EventBits_t ALL = DOOR_CLOSED | TEMP_OK | START;

EventGroupHandle_t machine;

void setOrClear(EventBits_t bit, bool on) {
  if (on) xEventGroupSetBits(machine, bit);
  else xEventGroupClearBits(machine, bit);
}

void doorTask(void *) {
  for (;;) {
    setOrClear(DOOR_CLOSED, digitalRead(DOOR) == HIGH);
    vTaskDelay(pdMS_TO_TICKS(20));
  }
}

void temperatureTask(void *) {
  for (;;) {
    int celsius = analogRead(KNOB) * 100L / 1023;
    setOrClear(TEMP_OK, celsius < 70);
    vTaskDelay(pdMS_TO_TICKS(100));
  }
}

void buttonTask(void *) {
  bool was = false;
  for (;;) {
    bool pressed = digitalRead(START_BUTTON) == LOW;
    if (pressed && !was) xEventGroupSetBits(machine, START);   // the press, not the holding
    was = pressed;
    vTaskDelay(pdMS_TO_TICKS(20));
  }
}

void printMissing(EventBits_t bits) {
  Serial.print(F("waiting for:"));
  if (!(bits & DOOR_CLOSED)) Serial.print(F(" door closed"));
  if (!(bits & TEMP_OK)) Serial.print(F(" temperature OK"));
  if (!(bits & START)) Serial.print(F(" start"));
  Serial.println();
}

void motorTask(void *) {
  for (;;) {
    // All three bits, not cleared on the way out; a second at a time, to say what is missing.
    EventBits_t bits = xEventGroupWaitBits(machine, ALL, pdFALSE, pdTRUE, pdMS_TO_TICKS(1000));
    if ((bits & ALL) != ALL) {
      printMissing(bits);
      continue;
    }
    xEventGroupClearBits(machine, START);
    digitalWrite(FAULT_LED, LOW);
    digitalWrite(MOTOR_LED, HIGH);
    Serial.println(F("motor running"));
    for (;;) {
      vTaskDelay(pdMS_TO_TICKS(50));
      bits = xEventGroupGetBits(machine);
      if ((bits & (DOOR_CLOSED | TEMP_OK)) != (DOOR_CLOSED | TEMP_OK)) break;
    }
    digitalWrite(MOTOR_LED, LOW);
    digitalWrite(FAULT_LED, HIGH);
    Serial.println(bits & DOOR_CLOSED ? F("STOP: over temperature") : F("STOP: door opened"));
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(DOOR, INPUT);
  pinMode(START_BUTTON, INPUT_PULLUP);
  pinMode(MOTOR_LED, OUTPUT);
  pinMode(FAULT_LED, OUTPUT);
  machine = xEventGroupCreate();
  xTaskCreate(doorTask, "door", 90, NULL, 2, NULL);
  xTaskCreate(temperatureTask, "temp", 90, NULL, 2, NULL);
  xTaskCreate(buttonTask, "button", 90, NULL, 2, NULL);
  xTaskCreate(motorTask, "motor", 160, NULL, 3, NULL);
}

void loop() {}

Lessons behind it