In this lesson, you will use a continuous decision behavior to detect obstacles.
Obstacle detection
Robot Configuration: TACObot
For this lesson, you will need your SPIKE Prime TACObot completely built. Download the PDF below to view the TACObot building instructions. TACObot
Did You Know? Obstacle Detection Failures
The task of obstacle detection is simple: move a certain distance without hitting anything. There are many ways of thinking about obstacle detection that seem to make sense but actually don’t work. Let's take a look at these common obstacle detection mistakes:
This program clearly doesn’t work. And it shouldn't if you think about it. In this program, we told the robot to go forward for 30cm. And it did. The “move [direction] for [30 cm]” block had the program's full attention for all 30cm, leaving it no way to get to any blocks that would even attempt to check the Distance Sensor.
Obstacle detection failure 1
This program responds to an obstacle! But the robot won’t stop when it’s supposed to… Because the condition of the loop is only checked at the end of the loop, the “wait until” blocks cause the problem by holding program flow. While the program is “waiting” for the Distance Sensor value to drop or rise, it can’t be checking the condition of the loop, and so the robot misses its cue to stop.
Obstacle detection failure 2
In both of these two cases, the problem is the same: some blocks are holding up the program flow, preventing it from reaching other blocks that need to check the other sensor.
Follow the steps below to learn how to construct a program that uses rapid checks, rather than long waits, to ensure continuous program flow!
Step 1: Create a New Program
Create a new program by clicking the [+] New Project button. Name the Project “Obstacle Detection”. Make sure WORD BLOCKS is selected and click the create button. Create a new program
Step 2: Set Movement Motors
Grab a “set movement motors to” block, and drag it under the “when program starts” block. Set the ports in the block to "[D+C]".
Set movement motors
Step 3: Add an "if-then-else" Block
From the orange Control Palette, drag an “if-then-else” block to your program.Add "if-then-else" block
Step 4: Add Distance Sensor Boolean Block
Drag a Distance Sensor Boolean Block and place it inside the “if-then-else” block.
Select the port and then set the threshold to 20cm. Add distance sensor block
Step 5: Add Movement Blocks to Branches
Step 5a) Add the “stop moving” blockto the “if branch." Step 5b) Add the “start moving (straight:0)” block to the “else branch.”Add movement blocks
Step 6: Add Forever Loop
Drag a "forever loop" and wrap it around the "if-then-else" block". Add loop
Step 7: Download and Run Program
Click on the yellow “Download and Run” button in the bottom right corner of the software to run the program immediately. Place and remove objects in front of the robot at any time.
Step 8: Observe
It works! The robot is able to detect when objects are placed in front and removed while responding accordingly.
Try It! Repeat Until Relative Position
Instead of repeating forever, you can program your robot to repeat obstacle detection until the motors have driven a certain amount. Run this program below to detect obstacles until the position of the motors reach 2060 degrees. Relative Position Try It To add the "relative position" block, add the More Motors extension block set to your palette. More Motors
What happens?
Did You Know? How Obstacle Detection Works
Rather than thinking of obstacle detection as one big behavior, it is better to think of it as more smaller behaviors.
One behavior does not work
Repeated decisions allow your robot to check each sensor in turn, without either one of them blocking the other. The program checks, responds, and repeats quickly, as long as no command blocks any other. In each tiny movement, the robot will look in front of it to see whether the way is clear, and based on that decision, run the motors or not.
Smaller behaviors
The robot will repeat that decision as long as its Relative Position has not passed 2060 degrees yet (or 100cm). So, the robot will check its sensor and run its motors if the way is clear. Then loop around, check again, and respond accordingly. It will repeat this until eventually the robot will pass 2060 degrees (100cm), and will then move on to the next behavior which is the end of the program.
How do repeated decisions allow the robot to watch both sensors at once?