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 Class 1 lasers similar to lasers modern cell phones use for head detection. It sends out a tiny pulse of safe laser light, then uses the time it takes for the pulse to reflect to calculate the distance of nearby objects. The sensor calculates the distance based on how long the laser pulse took to make the round trip.
VEXcode VR Commands used with the Distance Sensor
The commands used with the Distance Sensor can be found in the Sensing category.
get_distance Command
Using the get_distance command, the Distance Sensor can report the distance of the nearest object in millimeters (mm) or inches. The get_distance command reports numeric values in either millimeters (mm) or inches.
def main(): |
To display the Distance Sensor values in the Monitor Console, drag in or type the Monitor Sensor command. Then set the parameter to the string identifier for the sensor value to be displayed. In this case the string identifier will be "distance.get_distance". |
The distance will be displayed in millimeters (mm) and inches in the Monitor Console.
found object Command
The Distance Sensor detects if there is an object in front of it using the Distance found object command. The Distance Sensor can detect an object or surface within 3000 mm (~118 inches) of the sensor. The Distance found object command returns a Boolean value as either True or False.
def main(): |
The value of the found_object command returns a Boolean, and reports True when the Distance Sensor is close to an object, and reports False when it is not close enough to an object. To display the Distance Sensor values in the Monitor Console, drag in or type the Monitor Sensor command. Then set the parameter to the string identifier for the sensor value to be displayed. In this case the string identifier will be "distance.found_object". |
True or false will be reported and appear in the Monitor Console.
The laser allows the sensor to have a very narrow field of view, so detection is always directly in front of the sensor.
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.
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.
def main(): |
To use the example above, copy this code into VEXcode VR and run the project on the Wall Maze Playground. |
Distance Sensor Example Project
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.
def main(): |
To use the example above, copy this code into VEXcode VR and run the project on the Castle Crasher or Dynamic Castle Crasher Playground. |