距离传感器#
介绍#
The distance class is used to control and access data from a Distance Sensor.
The Distance Sensor can detect whether an object is in front of the sensor and return how far away that object is. It can also return the detected object’s relative velocity and estimate whether the object is small, medium, or large.
The Distance Sensor operates over a range of 20 mm to 2000 mm.
Below 200mm, accuracy is approximately ±15mm. Above 200mm, accuracy improves to about 5%.
The Distance Sensor uses a Class 1 laser. Detection is directed straight ahead and is classroom-safe.
类构造函数#
distance( int32_t index );
类析构函数#
Destroys the distance object and releases associated resources.
~distance();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The Smart Port that the Distance Sensor is connected to, written as |
示例#
// Create a Distance Sensor instance in Port 1
distance Distance1 = distance(PORT1);
成员功能#
The distance class includes the following member functions:
objectDistance— Returns the distance between the Distance Sensor and the nearest detected object.objectSize— Returns the estimated size of the detected object.objectVelocity— Returns how quickly the detected object is moving toward or away from the Distance Sensor.isObjectDetected— Returns whether the Distance Sensor currently detects an object.changed— Registers a callback function that runs when the detected distance value changes.installed— Returns whether the Distance Sensor is installed and connected.
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 Sensor instance in Port 1
distance Distance1 = distance(PORT1);
objectDistance#
Returns the distance between the Distance Sensor and the nearest detected object.
Available Functionsdouble objectDistance( distanceUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The distance unit:
|
Returns a double representing the distance between the Distance Sensor and the nearest detected object in the specified units.
objectSize#
Returns the estimated size of the object detected by the Distance Sensor.
The Distance Sensor estimates object size based on how much of the sensor’s view is taken up by the detected object.
Available FunctionssizeType objectSize();
此函数不接受任何参数。
Return ValuesReturns a sizeType indicating the estimated size of the detected object:
-
none— No object is detected. -
small— A small object is detected. -
medium— A medium object is detected. -
large— A large object is detected.
objectVelocity#
Returns the relative velocity of the detected object.
Velocity shows how quickly the object is moving toward or away from the Distance Sensor. A value near 0 means the object is not moving much relative to the sensor.
Available Functionsdouble objectVelocity();
此函数不接受任何参数。
Return ValuesReturns a double representing the relative velocity of the detected object in m/s (meters per second).
isObjectDetected#
Returns whether the Distance Sensor currently detects an object.
Available Functionsbool isObjectDetected();
此函数不接受任何参数。
Return ValuesReturns a Boolean indicating whether the Distance Sensor detects an object:
-
true— The Distance Sensor detects an object. -
false— The Distance Sensor does not detect an object.
changed#
注册一个回调函数,当检测到的距离值发生变化时运行该函数。
Available Functionsvoid changed( void (* callback)(void) );
范围 |
类型 |
描述 |
|---|---|---|
|
|
A pointer to a function that will be called when the detected distance value changes. The function must take no parameters and return |
此函数不返回值。
installed#
Returns whether the Distance Sensor is installed and connected.
Available Functionsbool installed();
此函数不接受任何参数。
Return ValuesReturns a Boolean indicating whether the Distance Sensor is installed and connected:
-
true— The Distance Sensor is installed and connected. -
false— The Distance Sensor is not installed or not connected.