距离传感器#

介绍#

sonar 级与 VEX IQ 距离传感器(第一代)配合使用。2 距离 distance 与 VEX IQ 距离传感器(第二代)配合使用。

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

类构造函数#

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

sonar(
    int32_t index);

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

distance(
    int32_t index);

类析构函数#

销毁距离为 distance 对象并释放相关资源。

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

~sonar();

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

~distance();

参数#

范围

类型

描述

index

int32_t

距离传感器连接的智能端口,记为 PORTx,其中 x 是端口号(例如, PORT1)。

示例#

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

sonar Distance1 = sonar(PORT1);

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

distance Distance1 = distance(PORT1);

成员功能#

sonar 类包含以下成员函数:

  • foundObject — 返回距离传感器当前是否检测到对象。

  • distance — 返回距离传感器与最近检测到的对象之间的距离。

距离 distance 包含以下成员函数:

  • objectDistance — 返回距离传感器与最近检测到的对象之间的距离。

  • objectVelocity — 返回检测到的对象向距离传感器移动或远离距离传感器的速度。

  • objectSize — 返回检测到的对象的大小估计值。

  • isObjectDetected — 返回距离传感器当前是否检测到对象。

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

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

在调用 distance 或 distance 成员函数之前,必须创建 sonar 或 sonar 实例,如下所示:

/* 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

距离单位:

  • mm — 毫米(24 毫米至 1000 毫米)
  • cm — 厘米(2.4 厘米至 100 厘米)
  • inches — 英寸(1 英寸至 40 英寸)

Return Values

返回一个 double ,表示距离传感器与最近检测到的物体之间的距离,单位为指定单位。

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

objectDistance#

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

Available Functions
double objectDistance( distanceUnits units );

Parameters

范围

类型

描述

units

distanceUnits

距离单位:

  • mm — 毫米(20 毫米至 2000 毫米)
  • cm — 厘米(2 厘米至 200 厘米)
  • inches — 英寸(0.78 英寸至 78 英寸)

Return Values

返回一个 double ,表示距离传感器与最近检测到的物体之间的距离,单位为指定单位。

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

objectVelocity#

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

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

Available Functions
double objectVelocity();

Parameters

此函数不接受任何参数。

Return Values

返回一个 double 表示检测到的物体的相对速度,单位为 m/s(米每秒)。

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

objectSize#

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

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

Available Functions
sizeType objectSize();

Parameters

此函数不接受任何参数。

Return Values

返回 sizeType 为 sizeType ,表示检测到的对象的大小估计值:

  • 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)

指向一个函数的指针,该函数将在检测到的距离值发生变化时被调用。该函数必须不接受任何参数,并且返回 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.