距離センサー#

導入#

sonar クラスは、VEX IQ距離センサー(第1世代)で使用されます。2距離 distance VEX IQ距離センサー(第2世代)で使用されます。

どちらの距離センサーも、センサーの前に物体があるかどうかを検知し、その物体までの距離を返します。VEX IQ距離センサー(第2世代)は、検出された物体の相対速度も表示し、物体が小さい、中くらい、大きいのいずれであるかを推定することもできます。

クラスコンストラクタ#

1 — このコンストラクターは、VEX IQ 距離センサー (第 1 世代) を作成します。

sonar(
    int32_t index);

2 — このコンストラクターは、VEX IQ 距離センサー (第 2 世代) を作成します。

distance(
    int32_t index);

クラスデストラクタ#

距離 distance オブジェクトを破壊し、関連するリソースを解放します。

1 — VEX IQ距離センサー(第1世代)を破壊します。

~sonar();

2 — VEX IQ距離センサー(第2世代)を破壊します。

~distance();

パラメータ#

パラメータ

タイプ

説明

index

int32_t

距離センサーが接続されているスマートポートは、 PORTxと表記され、 x はポート番号です (例: PORT1)。

例#

1 — ポート 1 に VEX IQ 距離センサー (第 1 世代) インスタンスを作成します。

sonar Distance1 = sonar(PORT1);

2 — ポート 1 に VEX IQ 距離センサー (第 2 世代) インスタンスを作成します。

distance Distance1 = distance(PORT1);

メンバーの機能#

sonar クラスには、以下のメンバ関数が含まれています。

  • foundObject — 距離センサーが現在オブジェクトを検出しているかどうかを返します。

  • distance — 距離センサーと最も近い検出されたオブジェクト間の距離を返します。

距離 distance には、以下のメンバ関数が含まれています。

  • objectDistance — 距離センサーと最も近い検出されたオブジェクト間の距離を返します。

  • objectVelocity — 検出されたオブジェクトが距離センサーに向かって、または遠ざかる速度を返します。

  • objectSize — 検出されたオブジェクトの推定サイズを返します。

  • isObjectDetected — 距離センサーが現在オブジェクトを検出しているかどうかを返します。

  • changed — 検出された距離値が変化したときに実行されるコールバック関数を登録します。

  • installed — 距離センサーがインストールされ、接続されているかどうかを返します。

distance または sonar メンバー関数を呼び出す前に、以下に示すように distance または 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 mm ~ 1000 mm)
  • cm — センチメートル (2.4 cm ~ 100 cm)
  • inches — インチ (1 インチ ~ 40 インチ)

Return Values

距離センサーと検出された最も近い物体との間の距離を、指定された単位で表す double のdouble値を返します。

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

objectDistance#

距離センサーと検出された最も近い物体との間の距離を返します。

Available Functions
double objectDistance( distanceUnits units );

Parameters

パラメータ

タイプ

説明

units

distanceUnits

距離の単位:

  • mm — ミリメートル (20 mm ~ 2000 mm)
  • cm — センチメートル (2 cm ~ 200 cm)
  • inches — インチ (0.78 インチ ~ 78 インチ)

Return Values

距離センサーと検出された最も近い物体との間の距離を、指定された単位で表す double のdouble値を返します。

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

objectVelocity#

検出された物体の相対速度を返します。

速度は、物体が距離センサーに向かって、または遠ざかる速度を示します。値が0に近い場合は、物体がセンサーに対してほとんど動いていないことを意味します。

Available Functions
double objectVelocity();

Parameters

この関数は引数を取りません。

Return Values

検出された物体の相対速度をm/s(メートル毎秒)で表す double を返します。

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

objectSize#

距離センサーによって検出された物体の推定サイズを返します。

距離センサーは、検出された物体がセンサーの視野のどれだけを占めているかに基づいて、物体のサイズを推定します。

Available Functions
sizeType objectSize();

Parameters

この関数は引数を取りません。

Return Values

検出されたオブジェクトの推定サイズを示す 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.