Step 1: Recognize whether the code is blocking or non-blocking
- Look at the top two Motor.spin commands next to the green arrows. Green arrows indicate non-blocking commands.
- Notice that there is no goal state included in those action commands - only to spin the Right Motor forward and the Left Motor in reverse. They are non-blocking commands which will continue to run as the program continues on with the next commands.
- Look at the Motor.rotateFor and Motor.rotateTo commands next to the red arrows. Red arrows indicate blocking commands.
- Notice that there is a goal state included in those action commands - to rotate for 270 degrees or to rotate to 270 degrees. They are blocking commands because the program will do this until finished before continuing on to the next command.
- Look at the Motor.rotateFor and Motor.rotateTo commands next to the green arrows.
- Notice that there is a waitForCompletion Boolean parameter included in those commands, and that it is set to false. That means the default setting of having the command wait until done (as a blocking command) is turned off (set to false) and so they are now non-blocking. The program will start them and continue to the next command.
NOTE: Again, the Left Motor's commands (rotateTo and rotateFor) next to the bottom green arrows are set to be non-blocking by setting the waitForCompletion parameter to false. But, you could also use a startRotateTo or startRotateFor instead of those. Both startRotate commands are non-blocking and let the program continue on to the next command before completing.
NOTE: There are many other examples of blocking vs. non-blocking commands in VCS. These examples were chosen for their simplicity.
NOTE: Some commands are easy to recognize as blocking or non-blocking based on their code. For example, Motor.startRotateTo and Motor.startRotateFor are obviously non-blocking commands by design because the action is started but not necessarily completed before the program moves on to the next command.
Step 2: Use non-blocking code to initiate commands and continue
- Look at the two non-blocking Motor.spin commands next to the upper green arrows.
- Notice that the LeftMotor.spin command is initiated and then the program continues immediately to the RightMotor.spin command. This is important so that both motors are spinning at the same time.
NOTE: If they were blocking commands, first the Left Motor would spin forward until done. Then the Right Motor would spin reverse until done. Then the program would continue.
- Look at the Motor.rotateFor and Motor.rotateTo commands next to the lower green arrows.
- Notice that the waitForCompletion Boolean parameter included in those commands is set to false and therefore makes them both non-blocking.
Step 3: Use non-blocking and blocking code together in order to have parallel processes
- Look again at the Motor.rotateFor and Motor.rotateTo commands now next to the blue arrows. Blue arrows are used to indicate parallel commands.
- Notice that because the Left Motor commands' waitForCompletion parameters are set to false, the Right Motor begins to rotate immediately after the Left Motor. This is important for having the robot move forward or backward in a straight line.
NOTE: The reason the Left Motor's commands were set to be non-blocking is because its commands were before the Right Motor's commands. In order for a pair of motor commands to run at the same time, the first one should be non-blocking and the second one should be blocking.
Step 4: Use blocking code to carry out commands until done
- Notice that in order for the program to finish before moving on, the Right Motor's commands are blocking.
NOTE: It's only the Right Motor's blocking commands that have the program wait until that command is complete before continuing. It's that blocking that lets the programmer see the robot stop before proceeding to the next commands.
Step 5: Understand how the non-blocking and blocking commands work with other commands
NOTE: The Motor.spin commands start the Left Motor and the Right Motor spinning in opposite directions. The sleep task lets the motors spin that way for 3 seconds before the Left and Right Motors are both stopped by the Motor.stop commands. Then the Left Motor and the Right Motor rotate forward for 270 degrees and brake before moving on because the Right Motor's command is blocking. Then the Left Motor and the Right Motor rotate in reverse to 270 degrees completely and brake before ending the program because the Right Motor's command is blocking.