Task communication

Is there a way for tasks to communicate with one another? Example: task-A starts task-B. Task-A should go about doing its job while task-B is doing its job. When task-B has finished, it will stop by calling stopTask(task-B) as the last statement of task-B. And… I would like task-A to know when task-B has finished. I know I can use a global variable that task-A can keep checking. Is there a better way? Are there any inter-task communication capabilities built in to the product and the RobotC language?
Thanks in advance for any tips and tricks for inter-task communication.
David

dibl~

I, myself, am a fan of the global variable.

Meanwhile, in modkit I have the ability to use a WHILE loop that can register if another motor is turning or a change in a stick position on the remote, such as WHILE joystick A is >0.

I suspect that RobotC must have a similar feature.
Therefore, if task-B involves (for example) a motor turning and then stopping when completed, couldn’t you just trigger task-B with task-A and then have Task-A carry on within a WHILE loop that automatically ends when the velocity of motor B reaches zero?

Please post your best solution, as I’m sure others will find it useful. As of this reply, almost two dozen people have already read your post!

Best of luck,

~Vexatron

Thanks Vexatron. In general, what I’m asking about is this: is there a better way, other than writing a “busy wait” spinning while loop that chews up CPU cycles? I suspect there isn’t a better way. I also understand that task chaining is one way to accomplish things: task-A fires off task-B and the last thing task-B does is fire off task-C. All good. Since I’m new to this but have a strong comp sci background, I just want to make sure I’m not missing some core concepts. I will write up something for the forum when (if) I arrive at a solution I like.

dibl~
Sounds like you have a very solid grasp. I agree that you’ll need an infinite loop or a task chain. Without it, task-A has no way of listening for task-B to finish.

Unless task-B is disconnecting the power to the brain or something (which would be kind of interesting in itself :slight_smile: the best you can do is keep your code short and shallow - basically avoiding deeply nested IF statements that bog down your response time.

~Vexatron

I use global variables for status of the tasks. So if I just want to wait I go (assuming my global variable is called arm_moving and it is 1 for moving 0 for stopped:

if (arm_move == 1 ) releaseCPU(); // Give up my time slot to other tasks.

The tasking engine in RobotC gives each task time slots, so the releaseCPU would say "I’m done with my slot, let someone else have a term.

while (arm_move == 1) releaseCPU(); // buzz loop version

You can also use the 1 is true form

while(arm_move) releaseCPU(); // wait for the arm to say they are done

Hope this helps.

@dibl I knew I had seen s function to do this in the past but could not find it in the online help so I asked jpearman, here is the info:
Use getTaskState, these are the possible states.

enum TTaskStates
{
taskStateUninitialized = 0,
taskStateStopped = 1,
taskStateRunning = 2,
taskStateWaitingForTimer = 3,
taskStateSuspendedWasRunning = 4,
taskStateSuspendedWasWaiting = 5,
taskStateBreakpointStop = 6,
taskStateWaitingForSemaphore = 7,
taskStateSuspendedWasWaitingForSemaphore = 8
};

if(vexRT[port1Trigger]) {
if( getTaskState( port1Pulse) == taskStateStopped )
startTask(port1Pulse);
}

Thanks TriDragon and Foster. I will look into it and try things out. Much appreciated!