by badcommandorfilename » Fri Jan 29, 2010 12:30 am
by badcommandorfilename
Fri Jan 29, 2010 12:30 am
I want to write an embedded system (AVR) task scheduler in C to handle the operation of a variety of non-critical tasks which my
robobuilder needs to perform. I'm looking for some advice on how to implement it so that it adds the lowest amount of overhead while giving it the most flexibility.
I'm also not trying to write a real time OS or anything - it just needs to be simple enough to keep a few core functions balanced. None of the tasks are time critical (I'm using interrupts for those), but I need a way to make sure that nothing is neglected for too long.
1) It needs to be able to schedule tasks for execution at a specific time.
This is mainly for scripted actions (it's a humanoid robot) like walking, etc as well as recharging the battery (which is trickle charged). Right now, I'm thinking of using an array of counters, which are decremented periodically, combined with a state machine in the main loop. This will simply switch the state to the overdue task when the current state expires.
2) It would be nice to have a method of forcing the execution of WAAAY overdue tasks.
Although it's no biggie if some actions are a few tens of milliseconds late, it would be nice if I could guarantee that a task would be executed in a reasonable time. I'm thinking of adding a watchdog interrupt which will perform the task if it's overdue.
3) Some tasks might take a long time to finish.
I'm trying to make this an extensible system, which allows for many tasks. However, if I use the state-machine system above I risk having long running tasks (like path planning/IK) postponing shorter and perhaps more critical tasks (like movement). Including the watchdog override would require the entire task to be run inside the interrupt, which is even worse.
4) A priority system would also be desirable.
In the case that a long task finishes and there are several overdue tasks to perform, the program should run the most critical ones first. Creative use of priorities could also help solve the problem of having a mix of long and short running functions.
5) I'd like a way to pass arguments to functions when the task is scheduled (not executed).
This is the one I really need help with. I'm almost certainly going to end up using function pointers, but unless all the functions take the same arguments I will have to have different data-types and handler functions. Also, how can I copy the argument(s) in their current state for execution later (in the generic case). I think I could do this in C using a sort of pseudo-stack if I can guarantee that each function will only end up in the task queue once, but I'm hoping a generic solution can be implemented if I am creative with assembly.
Has anyone had to do something like this before? I'm looking for a balance between execution overhead, complexity and versatility, so it's OK if there's no good way to get all these features working together. Also, I'm using C now but C++ is also an option.
Any input is appreciated. Thanks for your help!
More detailed information is available at my
blog.
--------------
http://buildtherobot.blogspot.com - for robot builders and enthusiasts
I want to write an embedded system (AVR) task scheduler in C to handle the operation of a variety of non-critical tasks which my
robobuilder needs to perform. I'm looking for some advice on how to implement it so that it adds the lowest amount of overhead while giving it the most flexibility.
I'm also not trying to write a real time OS or anything - it just needs to be simple enough to keep a few core functions balanced. None of the tasks are time critical (I'm using interrupts for those), but I need a way to make sure that nothing is neglected for too long.
1) It needs to be able to schedule tasks for execution at a specific time.
This is mainly for scripted actions (it's a humanoid robot) like walking, etc as well as recharging the battery (which is trickle charged). Right now, I'm thinking of using an array of counters, which are decremented periodically, combined with a state machine in the main loop. This will simply switch the state to the overdue task when the current state expires.
2) It would be nice to have a method of forcing the execution of WAAAY overdue tasks.
Although it's no biggie if some actions are a few tens of milliseconds late, it would be nice if I could guarantee that a task would be executed in a reasonable time. I'm thinking of adding a watchdog interrupt which will perform the task if it's overdue.
3) Some tasks might take a long time to finish.
I'm trying to make this an extensible system, which allows for many tasks. However, if I use the state-machine system above I risk having long running tasks (like path planning/IK) postponing shorter and perhaps more critical tasks (like movement). Including the watchdog override would require the entire task to be run inside the interrupt, which is even worse.
4) A priority system would also be desirable.
In the case that a long task finishes and there are several overdue tasks to perform, the program should run the most critical ones first. Creative use of priorities could also help solve the problem of having a mix of long and short running functions.
5) I'd like a way to pass arguments to functions when the task is scheduled (not executed).
This is the one I really need help with. I'm almost certainly going to end up using function pointers, but unless all the functions take the same arguments I will have to have different data-types and handler functions. Also, how can I copy the argument(s) in their current state for execution later (in the generic case). I think I could do this in C using a sort of pseudo-stack if I can guarantee that each function will only end up in the task queue once, but I'm hoping a generic solution can be implemented if I am creative with assembly.
Has anyone had to do something like this before? I'm looking for a balance between execution overhead, complexity and versatility, so it's OK if there's no good way to get all these features working together. Also, I'm using C now but C++ is also an option.
Any input is appreciated. Thanks for your help!
More detailed information is available at my
blog.
--------------
http://buildtherobot.blogspot.com - for robot builders and enthusiasts