距离传感器#

介绍#

The sonar class is used with the VEX IQ Distance Sensor (1st gen). The distance class is used with the VEX IQ Distance Sensor (2nd gen).

两款距离传感器都能检测前方是否有物体,并返回物体与传感器的距离。VEX IQ 距离传感器(第二代)还能返回被检测物体的相对速度,并估计物体的大小(小、中、大)。

类构造函数#

1 此构造函数创建一个 VEX IQ 距离传感器(第一代)。

sonar(
    int32_t index);

2 此构造函数创建一个 VEX IQ 距离传感器(第二代)。

distance(
    int32_t index);

类析构函数#

Destroys the distance object and releases associated resources.

1 摧毁一个 VEX IQ 距离传感器(第一代)。

~sonar();

2 摧毁一个 VEX IQ 距离传感器(第二代)。

~distance();

参数#

范围

类型

描述

index

int32_t

The Smart Port that the Distance Sensor is connected to, written as PORTx, where x is the port number (for example, PORT1).

示例#

1 在端口 1 中创建一个 VEX IQ 距离传感器(第一代)实例。

sonar Distance1 = sonar(PORT1);

2 在端口 1 中创建一个 VEX IQ 距离传感器(第二代)实例。

distance Distance1 = distance(PORT1);

成员功能#

The sonar class includes the following member functions:

  • foundObject — Returns whether the Distance Sensor currently detects an object.

  • distance — Returns the distance between the Distance Sensor and the nearest detected object.

The distance class includes the following member functions:

  • objectDistance — Returns the distance between the Distance Sensor and the nearest detected object.

  • objectVelocity — Returns how quickly the detected object is moving toward or away from the Distance Sensor.

  • objectSize — Returns the estimated size of the detected object.

  • 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 or sonar member functions, a distance or sonar 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 VEX IQ Distance Sensor (2nd gen) instance in Port 1
distance Distance1 = distance(PORT1);

foundObject#

返回距离传感器当前是否检测到物体。

Available Functions
bool foundObject();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示距离传感器是否检测到物体:

  • true — The Distance Sensor detects an object.
  • false — The Distance Sensor does not detect an object.
Notes - This is a VEX IQ Distance Sensor (1st gen) function.

distance#

返回距离传感器与最近检测到的物体之间的距离。

Available Functions
double distance( distanceUnits units );

Parameters

范围

类型

描述

units

distanceUnits

The distance unit:

  • mm — millimeters (24 mm to 1000 mm)
  • cm — centimeters (2.4 cm to 100 cm)
  • inches — inches (1 inch to 40 inches)

Return Values

Returns a double representing the distance between the Distance Sensor and the nearest detected object in the specified units.

Notes - This is a VEX IQ Distance Sensor (1st gen) function.

objectDistance#

返回距离传感器与最近检测到的物体之间的距离。

Available Functions
double objectDistance( distanceUnits units );

Parameters

范围

类型

描述

units

distanceUnits

The distance unit:

  • mm — millimeters (20 mm to 2000 mm)
  • cm — centimeters (2 cm to 200 cm)
  • inches — inches (0.78 inches to 78 inches)

Return Values

Returns a double representing the distance between the Distance Sensor and the nearest detected object in the specified units.

Notes - This is a VEX IQ Distance Sensor (2nd gen) function.

objectVelocity#

返回检测到的物体的相对速度。

速度表示物体相对于距离传感器移动或远离的快慢。接近 0 的值表示物体相对于传感器移动的幅度很小。

Available Functions
double objectVelocity();

Parameters

此函数不接受任何参数。

Return Values

Returns a double representing the relative velocity of the detected object in m/s (meters per second).

Notes - This is a VEX IQ Distance Sensor (2nd gen) function.

objectSize#

返回距离传感器检测到的物体的估计尺寸。

距离传感器根据被检测到的物体在传感器视野中所占的比例来估计物体的大小。

Available Functions
sizeType objectSize();

Parameters

此函数不接受任何参数。

Return Values

Returns 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.
Notes - This is a VEX IQ Distance Sensor (2nd gen) function.

isObjectDetected#

返回距离传感器当前是否检测到物体。

Available Functions
bool isObjectDetected();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示距离传感器是否检测到物体:

  • true — The Distance Sensor detects an object.
  • false — The Distance Sensor does not detect an object.
Notes - This is a VEX IQ Distance Sensor (2nd gen) function.

changed#

注册一个回调函数,当检测到的距离值发生变化时运行该函数。

Available Functions
void changed( void (* callback)(void) );

Parameters

范围

类型

描述

callback

void (*)(void)

A pointer to a function that will be called when the detected distance value changes. The function must take no parameters and return void.

Return Values

此函数不返回值。

Notes - This is a VEX IQ Distance Sensor (1st gen) or (2nd gen) function.

installed#

返回距离传感器是否已安装并连接。

Available Functions
bool installed();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示距离传感器是否已安装并连接:

  • true — The Distance Sensor is installed and connected.
  • false — The Distance Sensor is not installed or not connected.
Notes - This is a VEX IQ Distance Sensor (1st gen) or (2nd gen) function.