distance#
Initializing the distance Class#
A Distance Sensor is created by using the following constructor:
The distance constructor creates an object of the distance Class in the specified port.
Parameter |
Description |
|---|---|
|
A valid |
// Construct a Distance Sensor "Distance" with the
// distance class.
distance Distance1 = distance(PORT1);
This Distance1 object will be used in all subsequent examples throughout this API documentation when referring to distance class methods.
Class Methods#
objectDistance()#
The objectDistance(units) method returns the distance the Distance Sensor is currently reading. The Distance Sensor will return a large positive number if no object is detected.
Parameters |
Description |
|---|---|
|
A valid |
Returns: A double representing the distance to the detected object in the specified units.
// Get the distance detected by the Distance Sensor in inches.
double value = Distance1.objectDistance(inches);
objectSize()#
The objectSize() method returns an estimate of the size of the detected object.
Returns: A sizeType representing the estimated size of the detected object.
// Get the object size detected by the Distance Sensor.
sizeType size = Distance1.objectSize();
objectVelocity()#
The objectVelocity() method returns the velocity of the detected object. The velocity is calculated by the change in distance over the change in time.
Returns: A double representing velocity of the detected object in meters per second.
isObjectDetected()#
The isObjectDetected() method returns if an object is detected.
Returns: true if an object is detected. false if an object is not detected.
changed()#
The changed(callback) method registers a callback function for when the detected object changes.
Parameters |
Description |
|---|---|
|
A function that will be called when the detected object changes. |
Returns: None.
// Define the distanceChanged function with a void return
// type, showing it doesn't return a value.
void distanceChanged() {
// The Brain will print that the distance changed on the
// Brain's screen.
Brain.Screen.print("distance changed");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Drive the robot forward.
Drivetrain.drive(forward);
// Run distanceChanged when the value of the distance
// detected by the Distance Sensor changes.
Distance1.changed(distanceChanged);
}
objectRawSize()#
The objectRawSize() method returns the raw value of object size the Distance Sensor is detecting.
Returns: A 32 bit signed integer representing the raw size of the detected object.
// Get the raw size of the object detected by the distance sensor.
int32_t size = Distance1.objectRawSize();
timestamp()#
The timestamp() method requests the timestamp of the last received status packet from the Distance Sensor.
Returns: Timestamp of the last status packet as an unsigned 32-bit integer in milliseconds.
installed()#
The installed() method returns if the Distance Sensor is connected.
Returns: true if the Distance Sensor is connected. false if it is not.