Using the Location Sensor in VEXcode VR

The VR Robot has a multitude of sensors, including a Location Sensor.


Location Sensor on the VR Robot

VEXcode VR Playground displaying the robot’s X and Y coordinates from the Location Sensor

The VR Robot has a Location Sensor built in that reports the (X, Y) position of the VR Robot. The location of the VR Robot is determined by the center turning point. This is also the location of the pen on the VR Robot.

Location Sensor displaying the VR Robot’s X and Y coordinates in the Playground

The Location Sensor also reports the location angle which ranges from 0 degrees to 359.9 degrees following a compass heading style.

Location Sensor on the front of the VEXcode VR robot

Each Playground ranges from -1000mm to 1000mm for the X and Y positions. The starting location of the VR Robot depends on the Playground selected. For more information about location details on a Playground and the coordinate system, view the following articles:

Location Sensor highlighted on the VR robot in the Playground

The Location Sensor values can be displayed on the Dashboard in VEXcode VR. To learn more about the Dashboard, view the Dashboard - Playground Features - VEXcode VR article.

Location Sensor button in the VEXcode VR toolbar

The Location Sensor values can be displayed on the Monitor Console in VEXcode VR.

  • To learn more about using the Monitor Console with VEXcode VR Blocks, see this article.
  • To learn more about using the Monitor Console with VEXcode VR Python, see this article.

Common Uses of the Location Sensor

The Location Sensor on the VR Robot can be used in many ways.

VEXcode VR Blocks Castle Crasher Playground
Location Sensor displays the VR robot’s X and Y position on the Playground grid Location Sensor position on the VR robot used to measure x- and y-coordinates
VEXcode VR Python
def main():
while not location.position(Y, MM) > -300:
drivetrain.drive(FORWARD)
wait(20, MSEC)
drivetrain.stop()

The Location Sensor can be used to navigate the coordinates of any Playground. Each Playground ranges from -1000mm to 1000mm for the X and Y positions.

For example, if you wanted the VR Robot to stop once it crossed a certain value on the Y-axis, you could set a threshold on the Y-axis to be greater than that value. This center castle in the Castle Crasher Playground is close to a Y-value of -250. So the example project sets the VR Robot to stop driving after it passes a Y-value greater than -300, and will not crash into the castle.

VEXcode VR Blocks Disk Mover Playground
Location Sensor displays the VR robot’s X and Y coordinates on the Playground. Location Sensor reporting the VR robot’s X and Y coordinates on the Playground grid
VEXcode VR Python
def main():
drivetrain.turn_for(RIGHT, 90, DEGREES)
while not location.position(X, MM) > 800:
drivetrain.drive(FORWARD)
wait(20, MSEC)
drivetrain.turn_for(LEFT, 90, DEGREES)
while not location.position(Y, MM) > -100:
drivetrain.drive(FORWARD)
wait(20, MSEC)
drivetrain.stop()
magnet.energize(BOOST)

The Location Sensor can also be used to move the VR Robot to a known location. In this project, the first green disk appears to be at the location (800, -200). To reach that point, the VR Robot can drive along the X-axis until it reaches 800 and then along the Y-axis until it reaches -200 to pick up the disk.

VEXcode VR Blocks Art Canvas Playground
Location Sensor displaying the VR robot’s X and Y coordinates on the Playground grid Location Sensor button in the Playground toolbar
VEXcode VR Python
def main():
pen.move(DOWN)
drivetrain.drive_for(FORWARD, 400, MM)
while location.position_angle(DEGREES) < 89:
drivetrain.turn(RIGHT)
wait(20, MSEC)
pen.set_pen_color(RED)
drivetrain.drive_for(FORWARD, 400, MM)

The Location Sensor can also be used to determine the current angle of the VR Robot. In this project, the VR Robot will drive forward, and then turn right. It will wait until the VR Robot's angle is greater than 89 degrees and then change the pen color to red and drive forward another 400mm.

Note that the location angle in the Dashboard is 92 instead of exactly 90 or even 89.1. This is because it takes time for the flow of the project to proceed and process each command as they are executed. Thus, the 92 degrees is not a mistake, it is intended due to processing time and the speed of the VR Robot.


Using the Location Sensor in VEXcode VR Blocks

(Position of robot) block

Location Sensor displaying the VR robot’s X and Y coordinates on the Playground grid

The (Position of robot) block reports the X or Y coordinate position of the VR Robot.

Location Sensor button in the VEXcode VR toolbar

The X or Y coordinate position can be selected from the drop-down menu on the block.

Location Sensor position on the VR robot’s front bumper

The mm or inches units can be selected from the drop-down menu on the block.

(Position angle in degrees) block

VEXcode VR Playground displaying the robot's X position, Y position, and heading values from the Location Sensor

The (Position angle in degrees) block reports the angle of the VR Robot in degrees to the nearest tenths place. The values range from 0.0 to 359.9 degrees.


Using the Location Sensor in VEXcode VR Python

Location Sensor position on the VR robot used to track X and Y coordinates

To begin programming the Location Sensor with Python, you will need to first open a new text project in VEXcode VR. For more information, see this article.

Location Sensor X and Y coordinates for the VR robot on the Playground grid

The position command reports the X or Y coordinate position of the VR Robot.

The position angle command reports the angle of the VR Robot in degrees.

To add either of these commands to your project, you can drag the command in from the Toolbox, or type out the command in the workspace using the Autocomplete function. To learn more about Autocomplete in VEXcode VR with Python, see this article.

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: