Distance#
Introduction#
The Distance Sensor can detect objects directly in front of it and measure their distance. The 1st Gen sensor uses sonar for detection, while the 2nd Gen sensor uses a classroom-safe laser and can also estimate an object’s size and velocity.
For the examples below, the configured Distance Sensors will be named distance_1
and will be used in all subsequent examples throughout this API documentation when referring to Distance
and Sonar
class methods.
Below is a list of all available methods:
Getters – Return data from the Distance Sensor.
is_object_detected – (1st and 2nd Gen) Returns whether an object is within range.
distance – (1st Gen) Returns the distance to the nearest object.
object_distance – (2nd Gen) Returns the distance to the nearest object in inches, millimeters, or centimeters.
object_velocity – (2nd Gen) Returns the speed of a detected object in meters per second.
object_size – (2nd Gen) Returns the estimated size of the detected object (small, medium, large).
installed – (1st and 2nd Gen) Returns whether the Distance Sensor is connected to the Brain.
Constructors – Manually initialize and configure a Distance Sensor.
Getters#
is_object_detected#
is_object_detected
returns a Boolean indicating whether the Distance Sensor detects an object within range.
True
- The Distance Sensor detects an object.False
- The Distance Sensor does not detect an object.
Usage:
distance_1.is_object_detected()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
distance#
distance
returns the current distance from the Distance Sensor and a detected object as a float.
Note: This is a (1st Gen) Distance Sensor method.
Usage:
distance_1.distance(units)
Parameters |
Description |
---|---|
|
Optional. The unit that represents the distance:
|
# Example coming soon
object_distance#
object_distance
returns the distance between the Distance Sensor and the nearest object as a float.
Note: This is a (2nd Gen) Distance Sensor method.
Usage:
distance_1.object_distance(units)
Parameters |
Description |
---|---|
|
Optional. The unit that represents the distance:
|
# Example coming soon
object_velocity#
object_velocity
returns the velocity of a detected object as a float, measured in meters per second. A positive value indicates the object is moving toward the Distance Sensor (2nd gen), while a negative value indicates it is moving away.
Note: This is a (2nd Gen) Distance Sensor method.
Usage:
distance_1.object_velocity()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
object_size#
object_size
returns an ObjectSizeType
, which represents the estimated size of the detected object based on how much of the Distance Sensor (2nd gen)’s field of view it occupies:
ObjectSizeType.NONE
– No object is detected, or the object is so close that it fills the entire field of view.ObjectSizeType.SMALL
– The object occupies a small portion of the field of view.ObjectSizeType.MEDIUM
– The object occupies roughly half of the field of view.ObjectSizeType.LARGE
– The object occupies most of the field of view.
Note: This is a (2nd Gen) Distance Sensor method.
Usage:
distance_1.object_size()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
installed#
installed
returns a Boolean indicating whether the Distance Sensor is connected to the Brain.
True
- The Distance Sensor is connected to the Brain.False
- The Distance Sensor is not connected to the Brain.
Usage:
distance_1.installed()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
Constructors#
Constructors are used to manually create Distance
and Sonar
objects, which are necessary for configuring a Distance Sensor (2nd gen) or (1st gen) outside of VEXcode.
Sonar#
Sonar
creates a Distance Sensor (1st gen).
Usage:
distance_1 = Sonar(port)
Parameter |
Description |
---|---|
|
Which Smart Port that the Distance Sensor (1st gen) is connected to as |
# Example coming soon
Distance#
Distance
creates a Distance Sensor (2nd gen).
Usage:
distance_1 = Distance(port)
Parameter |
Description |
---|---|
|
Which Smart Port that the Distance Sensor (2nd gen) is connected to as |
# Example coming soon