I am using pros, and have all the different subsystems of our robot broken down in to different tasks with varying priority. I would like to add a button where the robot automatically begins a routine using more than one subsystem at once. I have considered suspending all threads and restarting them after the routine but I am worried about performance. I think the way to do that is with a mutex, but I do not know how to use it in the way that I want to wait until it is relinquished and not take the mutex.
A mutex is a good way to protect a resource if more than one task want’s access to that resource. I’m not sure I completely understand what you are trying to do, so you have different tasks for perhaps drive, lift and claw? Is this during driver control? Can you explain little more and perhaps post a simple example of some of your code?
I think you may be confused as to what a mutex is.
Mutual exclusion (usually shortened to “mutex”) is instituted when multiple, concurrent processes (in this case, this means the tasks) need to access a variable that is shared across all tasks in order to prevent unpredictable results (for more, see this stackoverflow question and then this one for a typical use for mutexes).
That being said, I can’t see any problem with suspending all your tasks after the routine is finished to restart them at a later time. Can you be any more specific with regard to the structure of your program?
void operatorControl() {
extendStabalizerNonBlocking();
TaskHandle wristThread = taskCreate(wrist, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_LOWEST+1);
TaskHandle liftThread = taskCreate(lift, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_LOWEST+2);
TaskHandle clawThread = taskCreate(claw, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_LOWEST+3);
while (true)
{
normOpControl();
}
}
Each task has its own domain managing different parts of the robot they all can coexist and run together. The wrist, lift, and claw functions all are loops that manage themselves based on input from the controller. I want to add a button that takes over every system temporarily. The button will lift up a star, flip it over the wall, and then retract the mechanism automatically.
So you have three sets of resources, the motors for each sub-system. You could protect each set of motors with a mutex, every time you want to control motors grab the mutex associated with them, send new values then release it. In the special button (presumably) task, you will have to grab all mutexes that are needed, do what you need and give them up again. The normal sub-system tasks will block when trying to acquire the mutex if the button task has taken over.