Skip to main content

Lesson 1: What is a Distance Sensor?

In this Lesson, you will learn what the Distance Sensor is, and why you would use the Distance Sensor with a VR Robot. You will also learn how the Distance Sensor works and how to use the Distance Sensor in a VEXcode VR Python project.

Learning Outcomes

  • Identify that the Distance Sensor reports the distance between the sensor itself and the nearest object.
  • Identify that the Distance Sensor determines distance by using the time it takes for a pulse of laser light to reflect off an object and return to the sensor.
  • Identify that the found_object command reports a Boolean value if the Distance Sensor has found an object or not.
  • Identify that the get_distance command reports the distance of the nearest object from the sensor in millimeters (mm) or inches.
  • Describe why the Distance Sensor would be used in a project.

Distance Sensor

The Distance Sensor can detect if there is an object in front of a VR Robot, and if so, how far away that object is from the Distance Sensor. This can be helpful in navigating a maze without continuously bumping into the walls, to avoid an object, or to drive to an object or wall in the Playgrounds.

In this Unit, you will use the Distance Sensor, a sensor that uses a pulse of laser light to detect objects. The Distance Sensor reports the distance between the front of the Distance Sensor on the VR Robot and the nearest object. The Distance Sensor is located in the Front Eye on the VR Robot. Just like the Bumper Sensor, the VR Robot can use the data from the Distance Sensor to make decisions. 

A front view of the VR Robot with the Distance Sensor on the front center of the robot highlighted with a red box.

The Distance Sensor calculates distance by using the time it takes for a pulse of laser light to reflect off of an object and return to the sensor. The Distance Sensor can be used to detect how far away VR Robot is from the walls in the Wall Maze Playground.

A top down image of the VR Robot opposite the center stack of blocks as on the Castle Crasher Playground. There are a series of red and green curved lines between the front of the robot and the blocks. The red ones show the signal emitted from the sensor to the object, and the green ones show the signal bouncing back from the object to the sensor, to enable it to calculate the distance.

Distance Found Object Command

The Distance Sensor can be used to detect if there is an object (like a wall) in front of it. This data can then be used in a project so the VR Robot can make a decision when the Distance Sensor detects an object.

Use the found_object command in a Python project to check the condition of the Distance Sensor. The found_object command returns Boolean values of True or False, depending on whether an object is detected by the sensor.

VEXcode VR Python Distance found object command reads distance dot found underscore object ( ).

  • found_object returns True when the Distance Sensor detects an object or surface within its field of view, and within 3000 mm of the sensor.
  • found_object returns False when the Distance Sensor does not detect an object or surface within 3000 mm.

The found_object command is typically used with a Control structure, like a while loop, so that the VR Robot can use the data from the Distance Sensor to make a decision.

When the found_object command is used in a project, the Distance Sensor being used should be specified. In the Wall Maze Playground, only the Front Distance Sensor is available on the VR Robot. When the found_object command is dragged from the Toolbox into a project, the found_object command will automatically specify the Distance Sensor in use, as shown here. In the video clip below, the found_object command is selected in the Toolbox and dragged into the Workspace. When it appears in the project, the command appears with the Device written as “front_distance” automatically.

Video file

Get Distance Command

The Distance Sensor can also report the distance of an object, and use that value to make a decision. The get_distance command returns the numeric distance of the nearest object from the front of a VR Robot.

Choose whether the distance is reported in millimeters (mm) or inches by setting the parameter to "MM" or "INCHES."

The VEXcode VR Python Get distance commands written twice - the first line shows the command with the parameter set to millimeters, the second line shows the command with the parameter set to inches.

The get_distance command can be used with other commands, like a while loop, in a project to instruct a VR Robot to drive a certain distance away from (or toward) an object, or to complete an action when a specific distance is reported.

When the get_distance command is used in a project, the Distance Sensor being used should be specified. In the Wall Maze Playground, only the Front Distance Sensor is available on the VR Robot. When the get_distance command is dragged from the Toolbox into a project, the get_distance command will automatically specify the Distance Sensor in use, as shown here. In the video clip below, the get_distance command is selected in the Toolbox and dragged into the Workspace. When it appears in the project, the command appears with the Device written as “front_distance” automatically.

Video file

The Distance Sensor and the Wall Maze Problem

Thinking about the Wall Maze Problem, the goal is to have the VR Robot drive while it is far away from a wall. When the VR Robot gets close to a wall, it then needs to stop or turn without touching the wall, in order to continue through the maze. Using the Distance Sensor will allow a VR Robot to drive up to an object without touching it.

In the following video clip example, the VR Robot will drive forward while the Distance Sensor reports that it is greater than 50 millimeters away from an object. Once it is less than 50 millimeters away, the VR Robot will stop driving. Notice that the VR Robot does not touch the wall.

Video file

You will notice that the Dashboard displays the value the Distance Sensor is reporting in real time as the project is run. When the value is greater than 50 millimeters, the VR Robot will continue to drive forward.VEXcode VR with the Workspace to the left and the Playground window to the right. In the Playground window, the VR Robot is in the start location, not near a wall. Across the top of the Playground Window the dashboard is open, and to the far right, the Distance is reported as 287mm, and is highlighted in a red box.

When the Distance Sensor reports a value of less than 50 millimeters, the VR Robot is near the wall without touching it, and stops driving. VEXcode VR with the Workspace to the left and the Playground window to the right. In the Playground window, the VR Robot has driven forward and is now stopped near the first wall. Across the top of the Playground Window the dashboard is open, and to the far right, the Distance is reported as 27mm, and is highlighted in a red box.

In this example, the get_distance command is used with a comparison operator to return a Boolean value in a loop. You will learn more about the greater than (>) and less than (>) operators in this Unit.

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf