Real-Time Multitasking in
Arduino
Pasquale Buonocunto, Alessandro Biondi, Pietro Lorefice
1
Who I am
Pasquale Buonocunto
PhD Fellow at Scuola Superiore Sant'Anna
Graduated cum Laude (Oct 2012) in
Computer Engineering at University of Pisa
Nov 2012 , now PhD Fellow on Embedded
Systems at TeCIP Institute, Scuola Superiore
Sant'Anna, Pisa.
Research
Interests
Real-time operating systems: design and
implementation;
Real-time wireless communication
Wearable, low power e-Health devices
Who I am
Alessandro Biondi
PhD Fellow at Scuola Superiore Sant'Anna
Graduated cum Laude in 2013 in Computer
Engineering at University of Pisa, within the
excellence program;
Aug 2011, Oct 2011 Visiting student @ San
Diego State University, California.
Research
Interests
Real-time operating systems: design and
implementation;
Real-time schedulability analysis;
Hierarchical systems;
Synchronization protocols.
5
Arduino Framework
Arduino is a tool for making computers that can sense
and control more of the physical world than your
desktop computer.
Very popular
2013: 700,000+ official boards were in users' hands
(not counting unofficial clones)
Arduino Framework
Very simple!
Simple programming interface;
Easy firmware loading;
Arduino Framework
Low cost
Arduino Uno ~20;
Arduino Due ~35.
Arduino Framework
Very popular
2013: 700,000+ official boards were in users' hands
Very simple!
Embedded system
Simple programming
interface;
programming
for
everyone
Easy firmware loading.
Low cost
~20$ official
9
Arduino Framework
Very simple!
10
Arduino Framework
void setup() {
<instructions here>
One-shot execution
at startup
void loop() {
<instructions here>
delay(1000);
Cyclically executed
until power off!
}
11
Arduino Framework
A lot of libraries
Standard Libraries
EEPROM
Ethernet
GSM
LiquidCrystal
SD
Servo
SPI
SoftwareSerial
Stepper
TFT
WiFi
Wire
Contributed Libraries
Messenger
NewSoftSerial
OneWire
PS2Keyboard
Simple Message System
SSerial2Mobile
Webduino
X10
XBee
SerialControl
Capacitive Sensing
Debounce
GLCD
Improved LCD
LedControl
LedDisplay
Matrix
PCD8544
Sprite
ST7735
FFT
Tone
TLC5940
DateTime
Metro
MsTimer2
PString
Streaming
12
Limitation of the Arduino Framework
No support for concurrency;
Execution limited to a single loop of
instructions;
No period can be expressed.
void loop() {
<instructions here>
delay(1000);
}
13
Existing Solutions
Scheduler Library
Support for multiple concurrent loops;
Cooperative Scheduler: each task is
responsible to pass the baton;
No periodic activities can be expressed;
Experimental Library.
14
Existing Solutions
Scheduler Library
void setup() {
Scheduler.startLoop(func1);
Scheduler.startLoop(func2);
}
void func1() {
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);
}
void func2() {
<instructions here>
yield(); // Pass control to other tasks.
}
Pass the control to
func2()
Pass the control to
func1()
15
Existing Solutions
Scheduler Library
void setup() {
Scheduler.startLoop(func1);
Scheduler.startLoop(func2);
}
void func1() {
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);
}
void func2() {
<instructions here>
yield(); // Pass control to other tasks.
}
No Scheduling
Policy
The scheduling
pattern is established
by explicit call to
yield() or delay()
16
Existing Solutions
Scheduler Library
void setup() {
Scheduler.startLoop(func1);
Scheduler.startLoop(func2);
}
void func1() {
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);
}
void func2() {
<instructions here>
yield(); // Pass control to other tasks.
}
No Real-Time
No Periodic Activities
No Preemption
17
Existing Solutions
Other Solutions:
Arduino Simple Task Scheduler;
Looper;
WrapOS;
18
Existing Solutions
scheduler.createSchedule(7*20*4, -1, false, put_leds_into_cycle);
Main(void) // entry point
uint32_t pid = scheduler.createSchedule(50, -1, false, void
callback_function);
{
scheduler.beginProfiling(pid);
SEM_ID sem1;
scheduler.stopProfiling(pid);
MSGQ_ID msgQ1;
scheduler.clearProfilingData(pid);
char * temp = dumpProfilingData(void);
if (temp != NULL) {
Serial.println(temp);
free(temp);
}
semCreate(sem1); // initial it.
msgQCreate(msgQ1, 100, 10);
// create and start tasks.
taskCreate("task1", 240, 0x7fff, (FUNCPTR)task1);
taskCreate("task2", 250, 0x7fff, (FUNCPTR)task2);
} itself up. */
/* Fires callback_function() after 790ms, and then cleans
scheduler.createSchedule(790, 0, true, callback_function);
void task1(void)
{
/* Blinks an LED for (20*9)ms. */
scheduler.createSchedule(20, 9, true, toggle_led);
while (!done) { // main loop
semTake(sem1, 10); // wait for the semaphore
/* Makes a 500Hz beep that lasts for 0.5 seconds. */
scheduler.createSchedule(1, 1000, true, toggle_speaker_pin);
// Do task1 stuff here
}
void put_leds_into_cycle() {
}
// Pulse all the LEDs in sequence...
scheduler.createSchedule(20, 7, true, toggle_02_oclock_led_r);
void7,task2(void)
scheduler.delaySchedule(scheduler.createSchedule(20,
true,
{
toggle_04_oclock_led_r), 7*20);
while(!done)
scheduler.delaySchedule(scheduler.createSchedule(20,
7, true, { // main loop
// wait for a message to come.
toggle_08_oclock_led_r), 7*20*2);
buf, size, WAIT_FOREVER);
scheduler.delaySchedule(scheduler.createSchedule(20,msgQReceive(msgQ1,
7, true,
toggle_10_oclock_led_r), 7*20*3);
// Do task2 stuff here.
}
}
}
19
Existing Solutions
scheduler.createSchedule(7*20*4, -1, false, put_leds_into_cycle);
Main(void) // entry point
uint32_t pid = scheduler.createSchedule(50, -1, false, void
callback_function);
{
scheduler.beginProfiling(pid);
SEM_ID sem1;
scheduler.stopProfiling(pid);
MSGQ_ID msgQ1;
scheduler.clearProfilingData(pid);
char * temp = dumpProfilingData(void);
if (temp != NULL) {
Serial.println(temp);
free(temp);
}
semCreate(sem1); // initial it.
msgQCreate(msgQ1, 100, 10);
// create and start tasks.
taskCreate("task1", 240, 0x7fff, (FUNCPTR)task1);
taskCreate("task2", 250, 0x7fff, (FUNCPTR)task2);
} itself up. */
/* Fires callback_function() after 790ms, and then cleans
scheduler.createSchedule(790, 0, true, callback_function);
void task1(void)
{
/* Blinks an LED for (20*9)ms. */
scheduler.createSchedule(20, 9, true, toggle_led);
while (!done) { // main loop
semTake(sem1, 10); // wait for the semaphore
/* Makes a 500Hz beep that lasts for 0.5 seconds. */
scheduler.createSchedule(1, 1000, true, toggle_speaker_pin);
// Do task1 stuff here
}
void put_leds_into_cycle() {
}
// Pulse all the LEDs in sequence...
scheduler.createSchedule(20, 7, true, toggle_02_oclock_led_r);
void7,task2(void)
scheduler.delaySchedule(scheduler.createSchedule(20,
true,
{
toggle_04_oclock_led_r), 7*20);
while(!done)
scheduler.delaySchedule(scheduler.createSchedule(20,
7, true, { // main loop
// wait for a message to come.
toggle_08_oclock_led_r), 7*20*2);
buf, size, WAIT_FOREVER);
scheduler.delaySchedule(scheduler.createSchedule(20,msgQReceive(msgQ1,
7, true,
toggle_10_oclock_led_r), 7*20*3);
// Do task2 stuff here.
}
}
}
20
ARTE: Arduino Real-Time Extension
ARTE
21
ARTE: Arduino Real-Time Extension
Support for real-time multitasking and periodic
activities;
Maintain a very simple programming interface
compliant with the Arduino philosophy;
Minimum amount of differences with respect to the
original Arduino programming model.
22
What is Real-Time Multitasking?
Scheduling Algorithm: Rules dictating the
task that needs to be executed on the CPU
at each time instant
23
What is Real-Time Multitasking?
Preemptive & Priority driven scheduling
Task have priorities (static);
At each time, the highest priority task is
executed ;
Preemption: the execution of the running task
can be interrupted by an higher priority task,
and the CPU given to the new task.
24
What is Real-Time Multitasking?
Periodic Activities
time
Standard Real-time programming model:
Periodic activities without explicit
delay/suspensions;
Be predictable: polling preferred to event
reaction.
25
What is Real-Time Multitasking?
Periodic Activities
time
Typical Real-Time Applications:
Sampling sensors and actuating (e.g., control
loop,);
Multimedia and transmissions;
26
What is Real-Time Multitasking?
Periodic Activities
time
Deadline: we want the task finishing before the
next activation.
27
What is Real-Time Multitasking?
Preemption
1)
2)
3)
28
Real-Time Multitasking and Arduino
Idea: Exploit the power of Real-Time
Multitasking with the simple and effective
Arduino Programming Model;
Extension to the Arduino Framework
allowing concurrent, multiple, loops each
one scheduled with a Real-Time OS
29
Erika Enterprise
ERIKA Enterprise is an OSEK/VDX certified RTOS;
ERIKA Enterprise implements an API inspired by the
AUTOSAR OS API;
Offers a suitable open-source license allowing the static
linking of closed source code;
Typical footprint around 2-4KB Flash;
Used by several automotive/white goods companies.
30
Example
Make blinking three different leds, each one at a
different frequency.
Led1: 3s
Led2: 7s
Led3: 11s
Robotmill.com
31
Example
With classical
Arduino
programming
model
Led1
3s
Led2
7s
int led1 = 13;
int led2 = 14;
int led3 = 15;
int count = 0;
void loop() {
if (count%3 == 0)
digitalToggle(led1);
if (count%7 == 0)
digitalToggle(led2);
Led2
11s
if (count%11 == 0)
digitalToggle(led3);
if (count == 3*7*11)
count = 0;
count++;
delay(1000);
}
32
Example
ARTE
With Arduino
Real-Time
Extension
Led1
3s
Led2
7s
Led2
11s
int led1 = 13;
int led2 = 14;
int led3 = 15;
void loop1(3000) {
digitalToggle(led1);
}
void loop2(7000) {
digitalToggle(led2);
}
void loop3(11000) {
digitalToggle(led3);
}
33
Example
34
Arduino Real-Time Extension
ARTE is a real extension to the Arduino IDE, not
an external library!
It provides a code pre/post-processor that
avoids tiring and error-prone user
configurations;
Automatic generation of the application
configuration and skeleton code;
The user is focused on the application logic.
35
Arduino Real-Time Extension
ARTE has a real RTOS under the hood
ARTE
36
Example
int led1 = 13;
int led2 = 14;
int led3 = 15;
void loop1(3000) {
digitalToggle(led1);
}
RT
Task 1
RT
Task 2
RT
Task 3
void loop2(7000) {
digitalToggle(led2);
}
void loop3(11000) {
digitalToggle(led3);
}
37
Erika Enterprise
ERIKA Enterprise is a static RTOS:
All the RTOS configuration (tasks, resources, counters,
hardware configuration) is decided at compiling time;
Minimal impact on the RAM;
OIL language is used to configure the RTOS.
38
Example
Mapping to an OSEK application
int led1 = 13;
int led2 = 14;
int led3 = 15;
void loop1(3000) {
digitalToggle(led1);
}
void loop2(7000) {
digitalToggle(led2);
}
void loop3(11000) {
digitalToggle(led3);
}
OIL
TASK loop1 {
PRIORITY = 0x01;
SCHEDULE = FULL;
STACK = SHARED;
};
ALARM Alarmloop1 {
COUNTER = TaskCounter;
ACTION = ACTIVATETASK {
TASK = loop1;
};};
TASK loop2 {
PRIORITY = 0x02;
SCHEDULE = FULL;
STACK = SHARED;
};
ALARM Alarmloop2 {
COUNTER = TaskCounter;
ACTION = ACTIVATETASK {
TASK = loop2;
};};
TASK loop3 {
PRIORITY = 0x03;
SCHEDULE = FULL;
STACK = SHARED;
};
ALARM Alarmloop3 {
COUNTER = TaskCounter;
ACTION = ACTIVATETASK {
TASK = loop3;
};};
39
Build Process
ARTE application
Core of the Arduino
Real-Time Extension
Generates an ERIKA
application code
starting from the
Arduino code
Parse the sketch an
automatically generates
the RTOS configuration
40
Build Process
OIL (OSEK
Implementation
Language)
41
Build Process
Arduino build process
as ERIKA application
Link all together!
42
Build Process
Flashing of the binary
on the Arduino board
43
DEMO
44
Arduino Real-Time Extension
Additional possible usage:
Fast prototyping: an ERIKA application is
automatically generated from the sketch; it
can be used for extending the development
by directly working with the RTOS.
45
Conclusion
ARTE is an extension for the Arduino
framework to support Real-Time Multitasking;
Simple programming interface with minimal
impact on the Arduino programming model;
Relies on an OSEK/VDX RTOS (ERIKA Enterprise);
46
Thank you!
Alessandro Biondi
alessandro.biondi@sssup.it
Pasquale Buonocunto
p.buonocunto@sssup.it
47