Skip to main content

Lesson 2: Drive to the Next Disks

Drive to the Blue Disks

The previous project has the VR Robot turn right when the Front Eye Sensor detects a green disk, which took the VR Robot through the first step in the maze. Now, we will continue to build onto the project so the VR Robot drives to the first four disks on the Disk Maze Playground.

Looking at where the VR Robot is facing after turning right at the green disk, we can see that to navigate to the next three blue disks, the VR Robot should drive forward and turn left at each blue disk. We can use the same logic of the while loop with the not condition to have the VR Robot continue to drive through the maze, and turn left when it detects a blue disk.Top down view of the disk maze playground. The robot at the first green disk facing to the right towards the first blue disk. Black arrows are shown for how the robot will need to move. This indicates the robot will be driving to the blue disk, turning left, driving to a second blue disk, turning left, and stopping at a third blue disk.

  • Add another comment beneath the turn_for in the existing project to describe the next behaviors of the VR Robot. Your project should look like this:
def main():
    # Drive to 1st disk (green), turn right
    while not front_eye.detect(GREEN):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(RIGHT, 90, DEGREES)

    # Drive to 2nd disk (blue), turn left
  • Beneath the new comment, add another while loop with a not condition. Set the condition to the detect command and set the parameters to "BLUE", since the next disk that the VR Robot will encounter is blue.
    # Drive to 2nd disk (blue), turn left
    while not front_eye.detect(BLUE):
        wait(5, MSEC)
  • Inside the while loop, drag or type the non-waiting drive command. 
    # Drive to 2nd disk (blue), turn left
    while not front_eye.detect(BLUE):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)
  • Drag in or type a turn_for command, and set the parameters to turn left 90 degrees.
    # Drive to 2nd disk (blue), turn left
    while not front_eye.detect(BLUE):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(LEFT, 90, DEGREES)
  • Run the project to see how the VR Robot moves through the Disk Maze Playground.Top down view of the VR Robot on the Disk Maze Playground with a black arrow indicating the robot will drive to the green disk directly across from the starting location then turn right, drive forward to the blue disk, turn left then stop driving.
    • Notice that the VR Robot turns right when the Front Eye Sensor detects a green disk, and turns left when the Front Eye Sensor detects a blue disk.
  • Continue to build this project to drive the VR Robot to two more blue disks on the Disk Maze Playground using the Front Eye Sensor. Add commands to the project navigate the VR Robot to the next two disks then stop. Your full project should now look like this:
def main():
    # Drive to 1st disk (green), turn right
    while not front_eye.detect(GREEN):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(RIGHT, 90, DEGREES)

    # Drive to 2nd disk (blue), turn left
    while not front_eye.detect(BLUE):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Drive to 3rd disk (blue), turn left
    while not front_eye.detect(BLUE):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Drive to 4th disk (blue), turn left
    while not front_eye.detect(BLUE):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(LEFT, 90, DEGREES)

Identifying a Pattern in the VR Robot's Movements

The goal of successfully navigating the Disk Maze with the Front Eye Sensor is to create a consistent pattern where the VR Robot turns right when the Front Eye Sensor detects a green disk, and left when the Front Eye Sensor detects a blue disk. Identifying patterns like this can help you simplify your projects as they grow in complexity.

Top down view of the disk maze playground. The robot is on the starting location. Arrows indicate the path the Vr robot will take showing drive forward to the first green disk, turning right, driving to the blue disk, turning left, driving to a second blue disk, turning left, and stopping at a third blue disk.

For Your Information

Notice that there is a pattern in the sequence of commands in the project as well. There is a series of commands (for the blue disks) that are repeated three times in the project. This section of code can be condensed by using a for loop. The project shown here combines those three while loops related to blue disks into a single for loop.

def main():
    # Drive to 1st disk (green), turn right
    while not front_eye.detect(GREEN):
        drivetrain.drive(FORWARD)
        wait(5, MSEC)

    drivetrain.turn_for(RIGHT, 90, DEGREES)

    # Drive to the next 3 disks (blue), turn left
    for value in range(3): 
        while not front_eye.detect(BLUE):
            drivetrain.drive(FORWARD)
            wait(5, MSEC)

        drivetrain.turn_for(LEFT, 90, DEGREES)

Using a for loop can make your project simpler and more easily readable, but containing the repetitive commands inside one loop.

  • Open the Disk Maze Playground if it is not already open, and run the project.
  • Watch the VR Robot navigate to four disks on the Disk Maze Playground.

    Top down view of the disk maze playground. The robot is on the starting location. Arrows indicate the path the VR robot will take showing drive forward to the first green disk, turning right, driving to the blue disk, turning left, driving to a second blue disk, turning left, and stopping at a third blue disk. A red box is on the final blue disk indicating the end location.
  • In this project, the VR Robot drives forward until the Front Eye Sensor detects the color green.
    • The VR Robot turns right 90 degrees and drives forward again until the color blue is detected.
    • The VR Robot then turns left and drives forward until the Front Eye Sensor detects blue. Once the color blue is detected, the VR Robot turns left again.
    • Finally, the VR Robot drives forward until the Front Eye Sensor detects blue, then stops.
  • Notice the pattern that when the Front Eye Sensor detects green, the VR Robot turns right. When the Front Eye Sensor detects blue, the VR Robot turns left in this project.

Select the Next button to continue with the rest of this Lesson.