距离传感器#
介绍#
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.
类构造函数#
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 instance in Port 1
distance Distance1 = distance(PORT1);
成员功能#
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#
返回距离传感器当前检测到的距离。
Available Functionsdouble objectDistance( distanceUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit of measurement for the returned distance value:
|
Returns a double representing the distance to the detected object in the specified units.
objectSize#
返回检测到的对象的大小估计值。
Available FunctionssizeType objectSize();
此函数不接受任何参数。
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#
返回检测到的物体的速度估计值。
Available Functionsdouble objectVelocity();
此函数不接受任何参数。
Return ValuesReturns a double representing the velocity of the detected object in meters per second.
isObjectDetected#
返回距离传感器当前是否检测到物体。
Available Functionsbool isObjectDetected();
此函数不接受任何参数。
Return Values返回一个布尔值,指示是否检测到对象:
-
true— An object is detected. -
false— No object is detected.
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#
返回距离传感器是否已连接。
Available Functionsbool installed();
此函数不接受任何参数。
Return Values返回一个布尔值,指示距离传感器是否已连接:
-
true— The sensor is connected and responding. -
false— The sensor is not connected or not detected.