距離感測器#

介紹#

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.