One of the things that is necessary when programming an autonomous robot for competitions is that it can Multitask.
That is, do something eg Line Follow, while waiting for something to happen eg a Touch Sensor to detect an obstacle.
When the obstacle is detected, the program needs to stop line following and change programs to navigate around the obstacle. Once it has done this, the program needs to be able to change to something else, eg back to line following.
I have been programming robots in Lego EV3-G as well as RobotC etc for some time but have only recently looked at Arduino, which in reality is what the mBot and Orion boards are based upon.
I have found that it is possible to MultiTask within the mBlock/Arduino environment using a couple of techniques adapted from other languages.
The base requirement is an understanding of Variables, Case Where (Switch) control structures and Functions.
The mBlock environment can handle Variables and Funtions well, however Case is not a Control option, so we will have to use IF decision controls.
The image above shows the basics of the unfinished program.
What is interesting is that I can put multiple ‘mBot Program’ blocks and in the Arduino code they become Tasks which is what we want.
Now to be able to have one task run and have the others also running and checking if that event has taken place requires the Main mBot Program to be constantly looping through each task checking if it is running OR if an event has triggered a change in the TaskNo (task number) which is controlled by the Variable taskNo.
Now in this case the main program is running, taskNo is set to zero (0) and the Line Follower function is running (see light blue function). The main program sweeps though the IF’s checking to see if taskNo has changed. If so, then through the next sweep it will execute the code in that IF and not the others.
The other mBot Porgram blocks are constantly running in Forever loops, always checking to see if the event takes place. When one does, eg touches an obstacle the variable taskNo is changed to 1. Note: a wait may need to be placed here so that the main program has time to reach the loop iterating through the IF’s.
Now taskNo 1 will execute and the robot will navigate around the obstacle.
Note: that once it has navigated around the obstacle the last thing that happens is taskNo variable is set back to zero (0) so that the robot will go back to Line Following.
The light blue Function can be made under the Data&Blocks pallet. You simply need to define an meaningful name to the Function/sub-program/sub-routine and then you can call it from within another program in this instance the main program.
Note: None of the code is finished in this example, it is only an example.
The Main Program uses IF’s rather than IF ELSE (however, I have included an example that could be substituted. Why, because in this instance I do not want each task to be mutually exclusive. I want the program to run through each running process checking if it has been met.
Using the IF ELSE is a classic substitute for the CaseWhere control, and is in fact the way it is handled by the CPU.
If you need mutually exclusive CaseWhere then use the IF ELSE instead.
MultiTasking Example.sb2 (75.1 KB)
I’ve also included the sb2 code so if your keen you can adapt it to your project.
NOTE: This type of program is what teams use when program autonomous robots in RoboCup Junior Rescue events. RoboCup Junior Rescue
Hope this helps everyone who is trying to make autonomous robots do more than one thing at once, sort of!