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

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

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.

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

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
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

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

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

<Distance found object> Block

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.

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.

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.

(Distance from) Block

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

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

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.

Using the Distance Sensor in VEXcode VR Python

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.

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.

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.

 `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.