Distance Sensor#
Introduction#
The distance class is used to control and access data from the V5 Distance Sensor. This sensor measures the distance between the sensor and nearby objects, allowing your robot to detect obstacles and determine proximity.
Class Constructors#
distance( int32_t index );
Class Destructor#
Destroys the distance object and releases associated resources.
~distance();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The Smart Port that the Distance Sensor is connected to, written as |
Examples#
// Create a distance instance in Port 1
distance Distance1 = distance(PORT1);
Member Actions#
The distance class includes the following member functions:
objectDistance— Returns the distance currently detected by the sensor.objectSize— Returns an estimate of the detected object’s size.objectVelocity— Returns the velocity of the detected object.isObjectDetected— Returns whether an object is currently detected.changed— Registers a callback function that runs when the detected object changes.installed— Returns whether the sensor is installed.
Before calling any distance member functions, a distance instance must be created, as shown below:
/* This constructor is required when using VS Code.
Distance Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create a distance instance in Port 1
distance Distance1 = distance(PORT1);
objectDistance#
Returns the distance currently detected by the Distance Sensor.
Available Functionsdouble objectDistance( distanceUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit of measurement for the returned distance value:
|
Returns a double representing the distance to the detected object in the specified units.
objectSize#
Returns an estimate of the detected object’s size.
Available FunctionssizeType objectSize();
This function does not take any parameters.
Return ValuesReturns a sizeType indicating the estimated size of the detected object:
-
none— No object is detected. -
small— The detected object is small. -
medium— The detected object is medium in size. -
large— The detected object is large.
objectVelocity#
Returns an estimate of the detected object’s velocity.
Available Functionsdouble objectVelocity();
This function does not take any parameters.
Return ValuesReturns a double representing the velocity of the detected object in meters per second.
isObjectDetected#
Returns whether an object is currently detected by the Distance Sensor.
Available Functionsbool isObjectDetected();
This function does not take any parameters.
Return ValuesReturns a Boolean indicating whether an object is detected:
-
true— An object is detected. -
false— No object is detected.
changed#
Registers a callback function that runs when the detected distance value changes.
Available Functionsvoid changed( void (* callback)(void) );
Parameter |
Type |
Description |
|---|---|---|
|
|
A pointer to a function that will be called when the detected distance value changes. The function must take no parameters and return |
This function does not return a value.
installed#
Returns whether the Distance Sensor is connected.
Available Functionsbool installed();
This function does not take any parameters.
Return ValuesReturns a Boolean indicating whether the Distance Sensor is connected:
-
true— The sensor is connected and responding. -
false— The sensor is not connected or not detected.