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. 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 motors. However it is fine for less demanding applications - such as regular sampling in a Kalman filter.
The scheduler can only queue up a maximum number of jobs. The default is 0 jobs so that this code is only included if needed. To change the setting then, in Project Designer, edit the 'clock' and enter a number in the 'Scheduled jobs' box. This number represents the maximum number of jobs that can be scheduled at any one time. If this number is too small then you will get a 'Scheduler Exhausted' error at run time.

 

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 your 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 1000 microseconds after the previous call back started whereas the second will start the callback routine 1000 microseconds after the previous call back has finished.

Valid XHTML 1.0 Transitional