WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

scheduler.h

Provides a scheduler mechanism whereby code can be queued up to be called at a later date. This is currently used to flash the status LED when an error occurs. NB the scheduler timer cannot be used to time/measure fast events of less than 1ms ie certainly not to send pulses to a servo or for PWM to servos. However it is fine for less time critical applications.
Since the status LED is flashed using this code to signify an error then this code is always automatically included.
The scheduler can only queue up a maximum number of jobs. The default is currently 4 jobs but you can change this by defining a value for SCHEDULER_MAX_JOBS prior to including your system file.
Example: if we are using the Axon but want the scheduler to cope with 8 jobs then:-
#define SCHEDULER_MAX_JOBS 8
#include "sys/axon.h"

 

Function

 


scheduleJob(SchedulerCallback callback, SchedulerData data, TICK_COUNT start, TICK_COUNT delay)

Queue up a function to be called at a later date. The scheduler only calls the function once - so if you want it to keep running then it will need to keep calling this function to queue itself up again.
This function takes the following parameters:-
callback - The function to be called at a later date. This method must have the following parameters (void * data, TICK_COUNT lasttime, TICK_COUNT overflow).
data - the data structure to be passed back into the data parameter of the callback function
start - The TICK_COUNT, in microseconds, when the current delay starts from. See examples below.
delay - The number of microseconds after 'start' when the callback routine should be called.
Lets assume we only want to callback a function once, after 1000 microseconds from now, and that function is called myCallback then here is the code:-
void myCallback(MyData * data, TICK_COUNT lasttime, TICK_COUNT overflow){
    // lasttime is the microsecond time of when I should have been called
    // overflow is the number of microseconds of error between lasttime
    // and when actually called
    .... do what you want to do ....
}
// Then in some other code you can queue up the callback using
scheduleJob(&myCallback, someData, clockGetus(),1000);
If you want the code to keep being called back then you will need to change the callback function to keep re-scheduling the job. For example:
void myCallback(MyData * data, TICK_COUNT lasttime, TICK_COUNT overflow){
    // lasttime is the microsecond time of when I should have been called
    // overflow is the number of microseconds of error between lasttime
    // and when actually called
    .... do what you want to do ....
    ... and queue up the job again 1000 microseconds after it should have been called ...
    scheduleJob(&myCallback, someData, lasttime,1000);
}
or
void myCallback(MyData * data, TICK_COUNT lasttime, TICK_COUNT overflow){
    // lasttime is the microsecond time of when I should have been called
    // overflow is the number of microseconds of error between lasttime
    // and when actually called
    .... do what you want to do ....
    ... and queue up the job again 1000 microseconds from now ...
    scheduleJob(&myCallback, someData, clockGetus(),1000);
}
The difference between these last two examples is that the first will start the callback routine 1000ms after the previous call back started whereas the second will start the callback routine 1000ms after the previous call back has finished.
By default the scheduler can cope with a maximum of 4 queued jobs. However you can change this by defining SCHEDULER_MAX_JOBS at the top of your program eg.
// Allow up to 16 scheduled jobs
#define SCHEDULER_MAX_JOBS 16

Valid XHTML 1.0 Transitional