Task Scheduler For Arduino

For their recent high altitude balloon project LVL1 member [Brad] programmed a pretty complicated brain based on an Arduino. It was responsible for collecting data from all of the sensors, and reporting back in a few different ways. One of the things he did to simplify the project was develop a task scheduler for the Arduino board. It lets you add functions to a queue of jobs, along with data about when they should be run.

The task scheduler does make coding a bit easier, but where it really shines is in situations like this where you don’t have access to the hardware if there’s a problem. In his description of the scheduler [Brad] mentions the possibility that one of the sensors could fail as the cold of the upper atmosphere takes its toll. This could leave the whole system stuck in a subroutine, and therefore it will stop sending reports back to the team on the ground. Since he was using the task scheduler it was a snap to add watchdog timer servicing to the mix. Now if program execution gets stuck the watchdog will reset the chip and all is not lost.

20 thoughts on “Task Scheduler For Arduino”

Now if program execution gets stuck the watchdog will reset the chip and all is not lost.

If some sensor is causing program to malfunction, probably it will malfunction again a moment after restart. Nice scheduler though, have you considered using linked list with sorting by insertion? Worked for me, I could use more than 15 events but when less, it didn’t use so much memory. Such task scheduler helps with debouncing and interactivity. When you detect key press just schedule checking if its still pressed after 5ms and you still can do other things in the meantime.

The watchdog wasn’t so much to avoid failing sensors (the sensors were very reliable!), but to avoid failures in other systems from affecting the flight computer. The ballast controller, sensor controller and communications controller all had their own microcontrollers communicating over the I2C bus. Due to the craptacular nature of the Arduino Wire library, they would fail with more frequency than we wanted for our transatlantic flight.
With the watchdog, the flight computer could survive a failure ultimately caused by an outside system, and carry on.
Since we wouldn’t be allocating memory dynamically, we went with a ring buffer instead of a linked list. The ring buffer is slightly smaller and speedier with that constraint.

Cute design, but many of us that have been writing embedded code have been doing this for decades. In areospace a failed sensor reports a failed response and after 3 fails it is not read again until a commande from ground control to attempt again.

And, yes, schedulers like these are pretty straightforward to implement, and saves a lot of time if you need to run multiple things in different frequencies. My robot uses that to run the PID controller at 100Hz, receive radio commands (at some other frequency I forgot), and, of course, blink an LED (at 1Hz). These things wouldn’t been difficult to write without a task manager, but hey — it was fun to write.

How does this differ from DuinoOS, the Arduino RTOS? I wrote all my code like the millis() checking in the BlinkWithoutDelay sketch, but I don’t think that is the best way. What is the best way to schedule things?

A task scheduler is a simple time management software. Simple forms can be used w/o having the additional library code required. It is as simple as requesting the “time”, comparing it to an old time and see if enough time has passed. It is really efficient and allows some blocks of code to run faster than other blocks of code. etc…

Yes, but there is a correct and incorrect way to do this. If you just check the time over and over with no gaps, it actually disables the millis value increment since millis() disables the interrupt while fetching the value.

I believe the correct way to switch between tasks is to do what the example sketch does above and use delay() for the GCD of your task intervals so that your microcontroller can sleep and not keep checking the time.

If you check the interval at thge top of your loop it is not “disables the millis value increment”. I run serial retrieval and frame recognition code in my “off time”. It is not Costently checking the millis fcn…