As I am working on the robot software. You make changes here and there, and you start to think about how to make things easier/better etc.
My main PID loop has been rewritten to be more of a time slice mechanism for various periodic systems. (I'll post new code on
Baically, it has been redesigned to accept task objects and a run priority. The PID object runs every time, but things like the keyboard scanner runs much less often, the report object runs MUCH less often.
I typically would have the OS do this sort of thing, but there are so many periodic tasks, I would have so many small tasks calling usleep(). This way, I can have a bit more control when certain functions are run in relation to others. I'm not convinced that this is any more efficient, but it does provide a flexable system I could move to threads or processes if I want.
I know this is not rocket science, and there are probably many implementations of it out there, but I started thinking...
The task objects have only a few virtul API entry points, these could easily be wrapped in a UNIX shared library or Windows DLL. Outside a few control API functions, there are three operational funtions:
Boolean Init() Boolean Handler(int usec) Boolean Shutdown()
Init is called to initialze the object "in system" but outside timing loop. By returning FALSE, the object can indicate it could not be initialized and should not run.
Handler is called periodically according to system load and priority. The "usec" variable is a precision measurement of elapsed time since its last run. If the funtion return FALSE, it is removed from the task list and its Shutdown function is called.
Shutdown is called when the object is not longer being used and no longer scheduled. If the object is shutdown successfully, the function should return TRUE, otherwise FALSE. Currently the return value is ignored.
My thought is that functionality is added to the robot via this API and end users (developer/users) simply make a shared library (or DLL or COM object for Windos) that provides these entry points.
What do you think?