Lesson 2: Drive to the Letter 'A'
Using the Distance Sensor to Avoid Walls
Now that you have learned about what comparison operators are and how they can be used in a project, you will apply that learning to drive the VR Robot to the letter 'A' in the Wall Maze Playground. This project will use the Distance Sensor to navigate through the Wall Maze Playground without bumping into walls.
Name and Save the Project
- Start a new text project and select the Wall Maze Playground when prompted.
- Name the project Unit5Lesson2.
-
Drag in, type, or copy a while loop and remove the Drivetrain command that populates as part of the new Text project template, so your project looks like this. Notice the command will populate a while loop with a wait command inside. Do not remove the wait command, it will ensure the VR Robot executes the project correctly.
def main(): while condition: wait(5, MSEC)
-
Drag, type, or copy the non-waiting drive command inside the loop, so your project looks like this.
def main(): while condition: drivetrain.drive(FORWARD) wait(5, MSEC)
- Check that the indentation is correct and the drive command is aligned with the wait command.
Add the get_distance function and the greater than operator
- Drag or type the get_distance command in the while loop's 'condition', so your project looks like this. Type the greater than operator and the second operand as '50.
-
def main(): while front_distance.get_distance(MM) > 50: drivetrain.drive(FORWARD) wait(5, MSEC)
-
Add a stop command outside the while loop, so your project looks like this. A stop command is needed after the while loop because the VR Robot will continue to drive until it is instructed to stop.
def main(): while front_distance.get_distance(MM) > 50: drivetrain.drive(FORWARD) wait(5, MSEC) drivetrain.stop()
- Open the Playground Window if it is not already open, be sure the Wall Maze Playground opens, and run the project.
- Watch the VR Robot drive forward until the Distance Sensor reports the distance between the VR Robot and the wall is not greater than 50 millimeters (mm).
- Reset the Playground to move the VR Robot back to the starting position.
Continue the Project to Drive to the Letter ‘A’
Using the greater than (>) operator with the get_distance command as the condition of the while loop instructs the VR Robot to drive forward as long as the distance between the VR Robot and the wall is greater than 50 millimeters (mm). Once the Distance Sensor reports a distance from the VR Robot to the wall that is not greater than 50 millimeters, the VR Robot will stop driving. To continue on to the letter 'A', the VR Robot needs to turn when it approaches the wall.
To drive to the letter ‘A’, the VR Robot will need to detect two walls, and turn left when these walls are detected. Then, the VR Robot will stop when it detects the third wall, located behind the letter ‘A.’
-
Replace the stop command with a turn_for command, so your project looks like this. The turn_for command is needed because once the Distance Sensor is within 50 millimeters (mm) of a wall, the VR Robot will turn left to continue toward the letter ‘A.’
- Set the parameters of the turn to "LEFT, 90, DEGREES."
def main(): while front_distance.get_distance(MM) > 50: drivetrain.drive(FORWARD) wait(5, MSEC) drivetrain.turn_for(LEFT, 90, DEGREES)
- Add another while loop with a drive command inside below the turn_for command. Set the condition using the get_distance command as the first operand, the greater than operator and the constant '50' as the second operand.
while front_distance.get_distance(MM) > 50: drivetrain.drive(FORWARD) wait(5, MSEC)
- Finally, add the following commands below the second while loop to instruct the VR Robot to detect the final wall behind the letter ‘A’ then stop driving. Be sure that the turn_for command and the stop command are outside their respective while loops.
drivetrain.turn_for(LEFT, 90, DEGREES) while front_distance.get_distance(MM) > 50: drivetrain.drive(FORWARD) wait(5, MSEC) drivetrain.stop()
- Launch the Wall Maze Playground if it is not already open and run the project.
- Watch the VR Robot drive from the start of the Wall Maze to the letter ‘A,’ and then stop.
- Notice that the VR Robot does not bump into any walls as it drives to the letter ‘A.’ When the Distance Sensor value reports that the VR Robot is not greater than 50 millimeters (mm) from the wall, the VR Robot is instructed to turn or stop in the project.
Select the Next button to continue with the rest of this Lesson.