This article describes a method for “reconnecting” the separate paths of an EV3 program after a task split. It’s usually best to avoid task splits where possible, especially because of the risk of them leading to a “race condition” (more of this later in a separate post) but sometimes they’re hard to avoid.
Let’s say we want to run two motors independently and then stop each one in response to a particular EV3 button. In this example, Motor A will run until the Left EV3 button is pressed and, at the same time, Motor D will run until the Right EV3 button is pressed. The task split provides a simple way of setting this up so that the motors can be turned off in either order.
What if we want to continue the program after both motors have been turned off, but still allow for them to be turned off in either order? For example, how could we modify the previous example so that the cheering sound is played only after both task have completed?
Maybe it would be nice if we could somehow reconnect the tasks with something like the opposite of a task split, but the EV3 Software doesn’t allow us to do that…. or at least not directly!
A relatively simple workaround is to use a “flag”. That is, we create a logic variable and set it to false prior to the task split, then set it to true at the end of the forked task. Then have the main task wait until the flag is set to true before continuing.
Furthermore, by creating additional logic variables to serve as additional flags and the use of a Logic Operations block in the wait loop, this idea could be extended to accommodate multiple task splits.
Have fun!
Rob Torok
Latest posts by Rob Torok (see all)
- Obstacle Course - 26 August 2020
- Crash Test Dummy - 26 August 2020
- The Wave - 21 May 2020
- Build X - 20 May 2020
- Build a Duck - 20 May 2020
You do not need the flag.
Delete the ‘Read Flag1 variable’ block inside the ‘loop 01’ block.
Replace the ‘Write Flag1 variable’ block in the second task by a ‘Loop interrupt’ block.
Then the ‘Write Flag1 variable’ block for initialization can also be deleted.
This simplifies the program considerably.
Both variants have one big issue: they busy wait (burning precious cycles and battery power).
It would be better to actually block the task (like any wait block presumably does).
In my variant this can be partially achieved by adding a ‘Wait 0.2s’ inside the ‘loop 01’ block.
I also experimented by using ‘Reset timer’ in the second task and ‘Wait for timer <1' in the first but that has not yet lead to a nice solution.
Hi Rob!
Hope in the meantime you have found the solution.
ev3lessons have a great article on parallel beams sync http://ev3lessons.com/en/ProgrammingLessons/advanced/SyncBeams.pdf
Wishing you a great day!