Skip to main content

Lesson 3: Adding Drivetrain Commands to Solve the Wall Maze

Solving the Wall Maze Problem

Now that the VR Robot has driven to the letter ‘A’ as well as to the number ‘2’ on the Maze Wall Playground, how would you create a project where the VR Robot drives to other locations, such as to the letter ‘B?’ So far, we have used data from the Bumper Sensor to drive to a wall, then turn to move to the next wall. To navigate to the letter 'B', we need to be able to move the VR Robot to a point in the middle of the maze surface, so that it can get on a different path towards our destination.

If the code only contained drive and turn_for commands, the VR Robot could get “stuck” in the number ‘2’ section of the Wall Maze, because it would continue to turn left or right without ever backing up.A top down view of the Wall Maze playground with the VR robot positioned over the 2 marker. It is angled up and to the left, approximately facing the B marker.

Instead, we can estimate the distance from the last wall the VR Robot touched to where it needs to turn. We can then use Drivetrain commands, like drive_for and turn_for, with the while loops and Bumper Sensor data in our project to solve the maze!

  • Begin by modifying your previous project, create a new project, or select “Copy” and paste this code into VEXcode VR to match this base project.

    def main():
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.turn_for(RIGHT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    	
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.stop()
  • The project above begins to drive the VR Robot to the letter ‘B,’ however, in order for the VR Robot to do so, it would first need to back up and then turn left.A top down view of the Wall Maze playground with the VR robot positioned above the 2 marker.The VR Robot is pressed against and facing the left wall, with green arrows pointing right, and then down, showing the path it will need to take.
  • Edit the code by removing the stop command and replacing it with drive_for and turn_for commands.
    • We can estimate that the VR Robot needs to drive in reverse for approximately 300 millimeters (mm). First, set the parameters of the drive_for command to “reverse” for 300 millimeters (mm).
    • Then, set the parameters of the turn_for command to turn left 90 degrees. Your project should now look like this:

      def main():
      	while not left_bumper.pressed():
      		drivetrain.drive(FORWARD)
      		wait(5, MSEC)
      		
      	drivetrain.turn_for(LEFT, 90, DEGREES)
      
      	while not left_bumper.pressed():
      		drivetrain.drive(FORWARD)
      		wait(5, MSEC)
      		
      	drivetrain.turn_for(RIGHT, 90, DEGREES)
      
      	while not left_bumper.pressed():
      		drivetrain.drive(FORWARD)
      		wait(5, MSEC)
      		
      	drivetrain.turn_for(LEFT, 90, DEGREES)
      	
      	while not left_bumper.pressed():
      		drivetrain.drive(FORWARD)
      		wait(5, MSEC)
      		
      	drivetrain.drive_for(REVERSE, 300, MM)
      	drivetrain.turn_for(LEFT, 90, DEGREES)
  • Launch the Wall Maze Playground if it is not already open and run the project.
  • Adding these commands will orient the VR Robot in the right direction to drive the letter ‘B.’To the left, the Python code project that was shown before, ending with the drivetrain.turn_for(LEFT, 90, DEGREES). On the right, a top down view of the Wall Maze playground showing the VR Robot has moved according to the new code, positioned in the hall between the A and 2 markers and is now facing the bottom wall of the playground.

     

  • Select the “Reset” button to reset the Playground and move the VR Robot back to the starting position.
  • Now that the VR Robot is oriented in the correct direction, add the following commands below the final turn_for command, to drive the VR Robot to the letter 'B' on the Wall Maze Playground. Be sure that the commands are correctly indented in your project.

    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.turn_for(RIGHT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.turn_for(RIGHT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    		
    	drivetrain.stop()
  • Launch the Wall Maze Playground if it is not already open and run the project.
  • The VR Robot will now drive to the letter ‘B’ on the Wall Maze Playground.

    A top down view of the Wall Maze playground with the VR Robot positioned over the letter B, in the center of the left wall of the Playground. It is pressed against a wall, facing the top of the playground.

Mini Challenge

In this challenge, the VR Robot should navigate to the number ‘3’ in the Wall Maze Playground using multiple while loops, Drivetrain commands, and the Bumper Sensor.

A top down view of the Wall Maze playground with the VR robot positioned over the number 3. The number 3 is located in the upper left quadrant of the maze, above B and to the right, within a corner created by 3 surrounding walls. The VR Robot is pressed against the wall below the 3 marker, facing the bottom of the playground.

Follow these steps to complete the mini challenge:

  • Watch the solution video below and review how the VR Robot should drive in order to complete the mini challenge. In the video clip below, the VR Robot begins in the starting location and follows the same pattern of driving to a wall until the bumper is pressed then turning. The robot drives the same path to start, that it did to drive to the letter B. At the turning point away from the wall, the robot turns right to continue on to number 3. From that point it drives forward and turns left twice to get around the next set of walls, past the letter B. Finally, the robot drives forward and turns right twice to get around the final walls to reach number 3.

  • Create a project by adding or removing the necessary commands to the Unit4Lesson3 project to drive the VR Robot to the number ‘3’ on the Wall Maze Playground.
  • Start the project to test if it works.
  • If the project is not successful, edit and try again. Continue this process until the challenge is complete.
  • Once the VR Robot successfully drives to the number ‘3’ on the Wall Maze Playground, save the project.

Congratulations! You have successfully completed the Wall Maze Challenge!

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf