Skip to main content

Lesson 3: Turn to Heading

The turn_to_heading command can also be used to turn the VR Robot. This command turns the VR Robot to a given clockwise heading between 0 and 359.99 degrees. When a VR Robot collides with a castle, it may be knocked off course. The turn_to_heading command ensures the VR Robot will turn to face the next castle no matter what previously happened.

A top-down view of the VR robot with a compass-like circle around it, showing orientation angles. The circle marks 0° at the top (forward), 90° to the right, 180° at the bottom (backward), and 270° to the left. The orange arrow at 0° indicates the robot’s forward-facing direction.

This example will have the VR Robot move to knock over the center building, then turn and knock over the building in the bottom left corner on the Castle Crasher Playground.

Top down view of the Castle Crasher playground with the center castle and bottom left castle highlighted in a red boxes, indicating which structures will be knocked over in this lesson.
  • Remove the bottom two commands from the project. Your project should now look like this:

    # Add project code in "main"
    def main():
        drivetrain.set_drive_velocity(100, PERCENT)
        drivetrain.set_turn_velocity(100, PERCENT)
        drivetrain.drive_for(FORWARD, 800, MM)
        drivetrain.drive_for(REVERSE, 800, MM)
  • Drag in or type the turn_to_heading command after the drive_for command. Set the parameters of the turn_to_heading command to 270 degrees. Your project should now look like this:

    # Add project code in "main"
    def main():
        drivetrain.set_drive_velocity(100, PERCENT)
        drivetrain.set_turn_velocity(100, PERCENT)
        drivetrain.drive_for(FORWARD, 800, MM)
        drivetrain.drive_for(REVERSE, 800, MM)
        drivetrain.turn_to_heading(270, DEGREES)
  • Drag in or type the drive_for command and attach it below the turn_to_heading command. Set the parameters of the drive_for command to drive forward 700 millimeters (mm). Your project should now look like this:

    # Add project code in "main"
    def main():
        drivetrain.set_drive_velocity(100, PERCENT)
        drivetrain.set_turn_velocity(100, PERCENT)
        drivetrain.drive_for(FORWARD, 800, MM)
        drivetrain.drive_for(REVERSE, 800, MM)
        drivetrain.turn_to_heading(270, DEGREES)
        drivetrain.drive_for(FORWARD, 700, MM)
  • Select the “Open Playground” button to launch the Castle Crasher Playground if it is not already open.
    The VEXcode VR Toolbar. The Open Playground button at the top right of the Toolbar is highlighted with a red box.
  • Select the “Start” button to test the project.
    The VEXcode VR Toolbar. The Start button at the top right of the Toolbar is highlighted with a red box.
  • Watch the VR Robot move forward and collide with the center building before reversing back to the start. The VR Robot will then turn to a heading of 270 degrees and drive forward to collide with the building at the bottom left corner of the Castle Crasher Playground.

    Top down view of the Castle Crasher Playground. The castles in the middle of the playground and the bottom left of the playground have been toppled over. The VR Robot is at the bottom left of the playground and is facing the playground's left side.

Mini Challenge

For this mini challenge, the VR Robot should collide with the middle building and the building on the top right of the Castle Crasher Playground. You will be given a project, but there is an error. Edit the project to solve the mini challenge!

Top down view of the Castle Crasher playground with the center castle and top right castle highlighted in a red boxes, indicating which structures will be knocked over in this challenge.

Follow these steps to complete the mini challenge:

  • The video clip below shows how the VR Robot should move to topple both sets of blocks. The robot should drive straight ahead through the middle set of blocks to knock them over, then turn right and drive forward into the top right set of blocks to topple them as well.

    Video file
  • Create this project by adding or removing the necessary commands to the Unit2Lesson3 project.

    # Add project code in "main"
    def main():
        drivetrain.set_drive_velocity(100, PERCENT)
        drivetrain.set_turn_velocity(100, PERCENT)
        drivetrain.drive_for(FORWARD, 1550, MM)
        drivetrain.turn_for(RIGHT, 180, DEGREES)
        drivetrain.drive_for(FORWARD, 700, MM)
        drivetrain.turn_for(RIGHT, 90, DEGREES)
  • Start the project to see where the error is in the code.
  • Edit the code and try again. Continue this process until the mini challenge is complete.
  • Once the VR Robot successfully collides with the middle building and the building on the top right of the Castle Crasher Playground, save the project.
  • Save the project before moving on.

Congratulations! You solved the mini challenge!

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf