Computing stuff tied to the physical world

It’s time for real-time

In Software on May 23, 2013 at 00:01

For some time, I’ve been doodling around with various open-source Real-time operating system (RTOS) options out there. There are quite a few out there to get lost in…

But first, what is an RTOS, and why would you want one?

The RTOS is code which can manage multiple tasks in a computer. You can see what it does by considering what sort of code you’d write if you wanted to periodically read out some sensors, not necessarily all at the same time or equally often. Then, perhaps you want to respond to external events such as a button press of a PIR sensor firing, and let’s also try and report this on the serial port and throw in a command-line configuration interface on that same serial port…

Oh, and in between, let’s go into a low-power mode to save energy.

Such code can be written without RTOS, in fact that’s what I did with a (simpler) example for the roomNode sketch. But it gets tricky, and everything can become a huge tangle of variables, loops, conditions, and before you know it … you end up with spaghetti!

In short, the problem is blocking code – when you write something like this, for example:

void setup () {}

void loop () {
  digitalWrite(LED, HIGH);
  digitalWrite(LED, LOW);

The delay() calls will put the processor into a busy loop for as long as needed to make the requested number of milliseconds pass. And while this is the case, nothing else can be done by the processor, other than handling hardware interrupts (such as timer ticks).

What if you wanted to respond to button presses? Or make a second LED blink at a different rate at the same time? Or respond to commands on the serial port?

This is why I added a MilliTimer class to JeeLib early on. Let’s rewrite the code:

MilliTimer ledTimer;
bool ledOn;;

void setup () {
  ledTimer.set(1); // start the timer

void loop () {
  if (ledTimer.poll()) {
    if (ledOn) {
      digitalWrite(LED, LOW);
    } else {
      digitalWrite(LED, HIGH);
    ledOn = ! ledOn;
  // anything ...

It’s a bit more code, but the point is that this implementation is no longer blocking: instead of stopping on a delay() call, we now track the progress of time through the MilliTimer, we keep track of the LED state, and we adjust the time to wait for the next change.

As a result, the comment line at the end gets “executed” all the time, and this is where we can now perform other tasks while the LED is blinking in the background, so to speak.

You can get a lot done this way, but things do tend to become more complicated. The simple flow of each separate activity starts to become a mix of convoluted flows.

With a RTOS, you can create several tasks which appear to all run in parallel. You don’t call delay(), but you tell the RTOS to suspend your task for a certain amount of time (or until a certain event happens, which is the real magic sauce of RTOS’es, actually).

So in pseudo code, we can now write our app as:

  TASK 1:
    turn LED on
    wait 100 ms
    turn LED off
    wait 400 ms

    start task 1
    do other stuff (including starting more tasks)

All the logic related to making the LED blink has been moved “out of the way”.

Tomorrow I’ll expand on this, using an RTOS which works fine in the Arduino IDE.

  1. in the setup() it should be :

    ledTimer.set(1) ; instead of leadTimer.set(1) ;

  2. I do like both ChibiOS and NilRTOS, which work very good on these little AVR’s. NilRTOS is currently only 600 bytes in size, and very, very, very fast!

    Curious about tomorrow to find out where you are playing with…

  3. Remember that using an RTOS is just a way of organising your code to appear to do multiple things at once: it doesn’t magically add any capabilities to the processor that it didn’t have before.

    After JCW has shown us what he is using on the Arduino, I’ll explain how I do it without using an RTOS (and in assembler) on a PIC…

    • Yes, but this is exactly the point:

      you CAN do everything in assembler and without an RTOS, but it is usually much easier (faster, less errorprone, ….) to use a high level language like C and an RTOS for programming.

    • In certain circumstances, an RTOS does add capabilities in the sense that wasted time (delay()) can now be used to do something usefull!

  4. A real-time system, almost by definition, is one where it is necessary to decide how to waste time…

Comments are closed.