Skip to main content

Lesson 2: Driving for Distance - Moving Forward and in Reverse

In this Lesson, you will create a project that uses Drivetrain commands to knock down the first castle in the Castle Crasher Playground!

Top down view of the Castle Crasher playground with the center castle highlighted in a red box, indicating which structure will be knocked over in this lesson.

Learning Outcomes

  • Identify how to use the drive_for command to drive the VR Robot a specific distance.
  • Identify how to change the parameter in the drive_for command to move the VR Robot forward and reverse.
  • Identify how to use the set_drive_velocity command to set the speed of the drivetrain.
  • Identify the range of values used in the set_drive_velocity command.

Create a New Project

A new text project must be created in order for the VR Robot to move forward in order to collide with the first castle in the Castle Crasher Playground, then drive in reverse to return to the starting position.

Follow these steps to create a new Python text project.

  • Launch VEXcode VR at vr.vex.com.
  • Once you launch VEXcode VR, select "File", then "New Text Project."
    The VEXcode VR interface showing the File menu. The New Text Project button is highlighted with a red box.
  • You will be prompted to Choose a Python Playground. Select the Castle Crasher Playground. The thumbnail for the Castle Crasher selection in the VEXcode VR Open Playground window.

Name Your Project

  • To name your project, select the project name box.
    The VEXcode VR interface. At the top of the interface, the project title is highlighted with a red box. The project title currently reads VEXcode Project.
  • Enter the new project name Unit2Lesson2, and select “Save."

The VEXcode VR interface. The Project Name window is open, and with the project name having been changed from VEXcode Project to Unit2Lesson2. The Save button is highlighted with a red box.

Drive Forward and in Reverse

Use the drive_for command to program the VR Robot to move forward to knock over the first castle on the Castle Crasher Playground.

  • Notice, the drive_for command is already in the project, as part of the new project template. If it is not already in your project, drag the drive_for command into the workspace and place under the main definition.

    # Add project code in "main"
    def main():
        drivetrain.drive_for(FORWARD, 200, MM)
  • Or, begin typing the command in the workspace. Autocomplete functionality will help prevent errors while typing commands. 
    The VEXcode VR Python coding interface. drivetrain dot has already been written, and a box appears, showing available methods and their parameters that can be used in the drivetrain command, such as drive, drive for, heading, etc.
  • Select the “Open Playground” button to open the Playground Window if it is not already open.The top of the VEXcode VR interface. The Open Playground button is highlighted with a red box.
  • Check to make sure that the Castle Crasher Playground is open.
    Top down view of the Castle Crasher playground showing the full Playground interface with play, reset, and camera buttons.
  • If a different Playground opens, change the Playground to Castle Crasher. To change Playgrounds, select the 'Select Playground' button.
    The top of the VEXcode VR interface. The Select Playground button is highlighted with a red box.
  • Select the Castle Crasher Playground.
     The thumbnail for the Castle Crasher selection in the VEXcode VR Open Playground window.
  • Select the “Start” button to test the project.
    The top of the VEXcode VR interface. The Start button is highlighted with a red box.
  • The VR Robot will drive forward for 200 millimeters (mm) on the Castle Crasher Playground. Note that the VR Robot did not touch the center castle. The parameters in the drive_for command need to be adjusted so the VR Robot can drive further.
    Top down view of the Castle Crasher playground.
  • Select the “Reset” button to reset the Playground and move the VR Robot back to the starting position.Top down view of the Castle Crasher playground showing the full Playground interface. The Reset button is highlighted with a red box.

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