Using the Distance Sensor in VEXcode VR

The Distance Sensor on the VR Robot reports the distance between the VR Robot and the nearest object. The sensor calculates distance by using the time it takes for laser light to bounce off of an object and return to the sensor.


Distance Sensor on the VR Robot

front eye down

The Distance Sensor is a sensor that reports the distance between the VR Robot and the nearest solid object.

VR top down

The Distance Sensor uses the same principles as sonar devices to measure sound. It sends out a tiny pulse of laser light with its emitter at one side of the sensor, and then times how long it takes for the light to reflect back to the receiver at the other side of the sensor. The sensor calculates the distance based on how long the pulse took to make the round trip. Then, the Distance Sensor reports to the VR Robot how far the nearest object is.

Screen_Shot_2021-12-10_at_2.45.39_PM.png

The width of the Distance Sensors range of vision changes as it looks further away from the front of the VR Robot:

  • The sensor can detect objects within a 10 degree field of view when looking for objects less than 1000 millimeters (~ 39 inches) away.
  • The sensor can detect objects within a 5 degree field of view when looking for objects between 1000 millimeters (~ 39 inches) and 2000 millimeters (~ 78 inches) away.
  • The sensor can detect objects within a 2 degree field of view when looking for objects greater than 2000 millimeters (~ 78 inches) away.

Common Uses of the Distance Sensor

VR topdown

The Distance Sensor can detect if there is an object in front of the VR Robot. This can be used to avoid an obstacle.

VEXcode VR Blocks Wall Maze Playground
Front distance example.png playground distance 1.jpeg
VEXcode VR Python
def main():
while not front_distance.get_distance(MM) < 50:
drivetrain.drive(FORWARD)
wait(20, MSEC)
drivetrain.stop()

The Distance Sensor can measure the distance between the front of the Distance Sensor and the front of the object. This can be helpful to complete a maze without continuously bumping into the walls or to avoid an object.

VEXcode VR Blocks Dynamic Castle Crasher Playground
Distance forever example.png

playground distance 2.jpeg

VEXcode VR Python
def main():
while True:
if front_distance.found_object():
drivetrain.drive(FORWARD)
else:
drivetrain.turn(RIGHT)
wait(20,MSEC)

In this example, the VR Robot will drive forward if the Distance Sensor finds an object. If it does not detect an object, the VR Robot will turn right and keep checking for an object.

Note: This example will find the first building, but needs improvements to avoid falling off the table.


Using the Distance Sensor with VEXcode VR Blocks

Distance Sensor.png

The blocks used with the Distance Sensor can be found in the Sensing category.

<Distance found object> Block

Distance found object block.png

The Distance Sensor detects if there is an object in front of it using the <Distance found object> block. The Distance Sensor can detect an object or surface within 3000 mm (~118 inches) of the sensor.

Wait until distance found object.png

The <Distance found object> block is a Boolean block that returns a condition as either true or false and fits inside any blocks with hexagonal (six-sided) spaces for other blocks.

For more information on boolean blocks, view this article.

Distance found object.png

The value of the <Distance found object> block is a Boolean block that reports true when the Distance Sensor is close to an object, and reports false when it is not close enough to an object.

True or false will be reported and appear in the Monitor Console. 

To learn more about the Monitor Console, view this article.

(Distance from) Block

Distance dropdown.png

Using the (Distance from) block, the Distance Sensor can report the distance of the nearest object in millimeters (mm) or inches.

Distance greater than.png

The (Distance from)  block reports number values and fits inside any blocks with oval spaces.

For more information on reporter blocks, view this article

Front distance in mm or in.png

The value of the (Distance from) block in the units of millimeters (mm) or inches can be displayed on the Monitor Console in VEXcode VR. 

To learn more about the Monitor Console, view this article.


Using the Distance Sensor in VEXcode VR Python

Screen_Shot_2021-11-11_at_9.49.14_AM.png

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

Screen_Shot_2021-12-10_at_2.26.12_PM.png

Next, locate the Sensing category in the Toolbox and find found_object and get_distance commands. These are functions that report a Boolean value or numerical value about the sensor.

Screen_Shot_2021-12-10_at_2.38.31_PM.png

To add the command 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.

def main(): 
while not front_distance.get_distance(MM) < 50:
drivetrain.drive(FORWARD)
wait(20, MSEC)
drivetrain.stop()

A while loop can be used with sensors on your robot when you want your robot to do something like drive until the Distance Sensor is 50mm or less from a wall, then stop or turn.

In this project, the robot will drive forward while the Distance Sensor reports a value greater than 50mm, and will stop driving when the Distance Sensor is less than 50mm away from a wall.

def main():
monitor_sensor("front_distance.get_distance", "front_distance.found_object")

The value of the get_distance and found_object commands can be displayed on the Monitor Console in VEXcode VR. To learn more about using the Monitor Console in VEXcode VR Python, see this article.

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

Last Updated: