距離計#

導入#

sonar クラスは、超音波距離計で使用され、音波を用いて最も近い物体までの距離を測定します。超音波パルスを発信し、音が反射して戻ってくるまでの時間を測定することで機能します。

VEX V5 レンジファインダー。

クラスコンストラクタ#

sonar(
  triport::port &port);

クラスデストラクタ#

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

virtual ~sonar();

パラメータ#

パラメータ

タイプ

説明

port

triport::port &

レンジファインダーで使用される必須の 2 ポート グループの最初の 3 線ポート。レンジファインダーは、ブレインまたは 3 線エキスパンダー上の 2 つの隣接するポート (A–B、C–D、E–F、または G–H) を占有します。ペアの最初のポート (A、C、E、または G) を Brain.ThreeWirePort.X または ExpanderName.X のように指定します。ここで X はポート文字です (例: Brain.ThreeWirePort.A または Expander1.C)。

例#

// Create a sonar instance in Ports A and B
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);

メンバーの機能#

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

  • foundObject — レンジファインダーがオブジェクトを検出したかどうかを返します。

  • distance — レンジファインダーと最も近い検出対象物との間の距離を返します。

sonar メンバー関数を呼び出す前に、以下に示すように sonar インスタンスを作成する必要があります。

/* This constructor is required when using VS Code.
Range Finder configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a sonar instance in Ports A and B
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);

foundObject#

レンジファインダーが視野内に物体を検出したかどうかを返します。

Available Functions
bool foundObject();

Parameters

この関数は引数を受け付けません。

Return Values

レンジファインダーがオブジェクトを検出したかどうかを表すブール値 bool を返します。

  • true — 距離計が物体を検出しています。

  • false — 距離計が物体を検出していません。

Examples
// Check if an object is detected.
if (RangeFinderA.foundObject()){
    // Print to screen on the brain that an object was found.
    Brain.Screen.print("object found");
}

distance#

センサーが検出した最も近い物体までの測定距離を返します。

Available Functions
double distance(
  distanceUnits units );

Parameters

パラメータ

タイプ

説明

units

distanceUnits

距離を表す単位: inches または mm — ミリメートル。

Return Values

指定された単位での距離を表す double を返します。

Examples
// Get the Range Finder distance in millimeters.
double value = RangeFinderA.distance(mm);

// Print the current distance detected by the Range Finder to the
// Brain's screen.
Brain.Screen.print(value);