Scheduler

Intro

This scheduler is a simple way to fire functions asynchronously on a regular basis. Each function can be scheduled at its own period separate from other functions.

Since no interrupts are used and many functions can fire during a single pass, it is possible for functions to not be run at exactly the period requested. They will never be run more often than the requested period, but could be run less often. It should only be used for asynchronous functions that can accept some amount of delay.

I use this for the following types of functions:

  • Read sensors and move sensor servos to the next position
  • Update global variables based on map data
  • Update calculations for R/C inputs to offload the calculations from the interrupt routine.
  • Print out debugging info on a regular basis
  • Fade map data over time

API

Below is a description of the API. You can download the code at the bottom of this page.

  • schedInit() - Must be called at the start of the program to set up the scheduler stack and itialize the entries to empty
  • schedAdd(proc,period) - Add a process (function) to the scheduler. Takes two variables: the address of the function to run and the time between runs.
  • schedDel(proc) - Remove a process from the scheduler.
  • schedRun() - Should be called every time through your main loop. This is the heart of the scheduler that loops through all the processes and sees if any should be run at this time.
  • schedPrintTimes() - Print the max time each process has taken to run. This can be used for debugging starvation issues or to see which code you may need to optimize for better speed.

Notes

  • The scheduler is based on TIMER2 which is configured by default on the Axon for /64 from the system clock. I then just use the overflow counter. Since this is an 8-bit counter, the overflow is effectively a /256. This results in each unit on the overflow counter to be approx 1.024 mS on a 16Mhz Axon. This is close enough for this purpose. You must do a timer2Init() somewhere before calling this API. If you use timer2 for anything other than running free at the default rate without resets, then you will need to modify the getNow() and getTimeDiff(...) functions.

Download

Download Axon Code