Go Down

Topic: leOS - a simple taskmanager/scheduler (Read 9 times) previous topic - next topic

leo72

Jun 27, 2012, 12:19 pm Last Edit: Sep 16, 2012, 11:44 am by leo72 Reason: 1
leOS, standing for little embedded Operating System, is a scheduler/taskmanager (a prototype of an OS) for Arduino boards and Atmel microcontrollers that can manage background tasks. leOS can start new tasks, pause and resume them and delete them too. All is done using a scheduler that can start tasks without the partecipation of the user so that the tasks will appear trasparents at the main loop. To be honest, it should more correct to say that leOS is actually something that is halfway a simple prototype of an real-time operating system (RTOS) and a scheduler. In fact it doesnt have the methods to give a task a tick of time to run itself and a preemptive sistem to stop them, freeze their state and resume them in a later moment; but it's something that is more complex and efficient of several other schedulers  that just use millis() to schedule when launch a task because i.e. leOS isn't affected by the use of delay() in the main loop.

It uses an interrupt driven scheduler (based on an 8-bit timer of the micro, usually Timer 2) that checks is time has come to start an active task. The user can choose the interval time from 1 to 3600000 ms (1 hour at the moment, the max value can be changed). The library offers 4 methods to manage tasks:
addTask(function, interval)
pauseTask(function)
restartTask(function)
removeTask(function)

addTasK add the "function" in the scheduler with interval "interval". pauseTask and restartTask halts and resume the execution of a task, removeTask removes a task from the scheduler.

The library should run on several micros albeit it has been tested only on Arduino/Atmega328 at the moment.

More info and the code are available here:
http://www.leonardomiliani.com/?p=516&lang=en

leOS is at a very early stage, comments, suggestions and critics are welcome  :smiley-sweat:

EDIT:
a stable version is attached at this post

EDIT2:
if someone has found this lib usefull,I publish for him the new version 0.1.1, that let the user chooses between 32-bit & 64-bit counters (the first ones consume much less Flash memory), it's compatible with Atmega32U4 (Arduino Leonardo), can manage one-time tasks, can modify a running task.

EDIT3:
leOS is now updated to version 0.1.3.
I've introduced the ability to add a paused task at the scheduler, so that it won't start running immediately after adding. This is useful i.e. if your task drives something like a LED or a transistor and you want to choose the right moment to start the action. So the new sintax of the method addTask is now as follow:
Code: [Select]
addTask(function, interval[, status]);
with status that can be:
PAUSED: to add a task in paused mode, so that it won't start until the user won't use the restartTask() method;
SCHEDULED (default option): a task that start running immediately
ONETIME: a task that will have to be run only once

Thank you for your feedback.

N.B.:
starting from v. 0.1.3 the library will be available only from my web site, due to the fact that in this manner I can keep track of the whole number of downloads

CrossRoads

So one would do things like read buttons, watch the state of data,  etc. and then call one of the functions to make some recurring task (or one-time tasks too I suppose) occur/stop occurring, etc?
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

leo72

Yes, I've designed it to be indipendent so it can run tasks in the background without interfering with the main loop.
If you want to see more clearly what leOS can do, try the sketch with 3 LEDs.
You'll see that a LED is driven by the main loop, the other 2 are flashed by 2 indipendent tasks. In the main loop there's also a piece of code that every 10 seconds pauses a task and then resumes it again.
Code: [Select]
//include the OS
#include "leOS.h"

leOS myOS; //create a new istance of the class leOS

//variabiles to control the LEDs
byte led1Status = 0;
byte led2Status = 0;
byte led3Status = 0;
const byte LED1 = 7;
const byte LED2 = 8;
const byte LED3 = 9;
byte counter = 10;
char direction = -1;


//program setup
void setup() {
    myOS.begin(); //initialize the scheduler
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
   
    //add the tasks at the scheduler
    myOS.addTask(flashLed1, 600);
    myOS.addTask(flashLed2, 350);
}


//main loop
void loop() {
    digitalWrite(LED2, led2Status);
    led2Status ^= 1;   
    delay(1000);
   
    //every 10 secs pause/restart the second task
    counter += direction;
    if (counter == 0) {
        myOS.pauseTask(flashLed1); //pause the task
        direction = 1;
    }
    if (counter == 10) {
        myOS.restartTask(flashLed1); //restart the task
        direction = -1;
    }
}


//first task
void flashLed1() {
    led1Status^=1;
    digitalWrite(LED1, led1Status);
}


//second task
void flashLed2() {
    led3Status^=1;
    digitalWrite(LED3, led3Status);
}




Let me show you another example.
This is the sketch BlinkWithoutMillis (funny name, isn't it  ;)):
Code: [Select]

//include the OS
#include "leOS.h"

leOS myOS; //create a new istance of the class leOS

//variabiles to control the LEDs
byte led1Status = 0;
const byte LED1 = 13;

//program setup
void setup() {
    myOS.begin(); //initialize the scheduler
    pinMode(LED1, OUTPUT);
   
    //add the tasks at the scheduler
    myOS.addTask(flashLed1, 1000);
}


//main loop
void loop() {
    //empty
}


//flashing task
void flashLed1() {
    led1Status^=1;
    digitalWrite(LED1, led1Status);
}


You can see that the main loop doesn't contain ANY code, by the way the led on the thirtheen pin will flash every 1 second.

Riemer

#3
Jul 03, 2012, 05:50 pm Last Edit: Jul 03, 2012, 06:14 pm by Riemer Reason: 1
Really looks useful to me. It might be better to rename it to a taskmanager? Or can we expect more functionality soon? :)

edit: What I'm trying to say is, an abstract layer like this for timer interrupts can be quite useful for the beginners. Will definetly check it out myself as well.

leo72

Thanks, Riemer, for your appreciation   :smiley-sweat:

It isn't a real OS, I know... But more sophisticated than other schedulers or taskmanagers that use millis(). And it's simpler than an [RT]OS that store and restore the task's status into the stack. I chose leOS because it's a pun with my nick, leo  ;)
And maybe there could be some improvements, who knows about that?

BTW, here is version 0.1.0. It's fully compatible with Arduino Leonardo and its Atmega32U4. Moreover, leOS 0.1.0 supports one-time tasks (a task that should be executed only 1 time and then removed from the scheduler) and the ability to modify the interval and the type of the task. Info on the README file, and more examples in the package.


Go Up