測距儀#
介紹#
sonar 級聲吶與超音波測距儀配合使用,後者利用聲波測量到最近物體的距離。其工作原理是發射超音波脈衝,並測量聲波反射回來所需的時間。

類別建構函數#
sonar(
triport::port &port);
類別析構函數#
摧毀 sonar 目標並釋放相關資源。
virtual ~sonar();
參數#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
測距儀使用的所需雙埠組中的第一個三線連接埠。測距儀佔用主機或三線擴展器上的兩個相鄰連接埠(A-B、C-D、E-F 或 G-H)。指定該對連接埠中的第一個連接埠(A、C、E 或 G),格式為 |
範例#
// 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 Functionsbool foundObject();
此函數不接受任何參數。
Return Values傳回一個布林值 bool ,表示測距儀是否偵測到物體:
true— 測距儀正在偵測物體。false— 測距儀未偵測到物體。
// 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 Functionsdouble distance(
distanceUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
表示距離的單位: |
傳回一個 double 之間的雙精度浮點數,表示以指定單位表示的距離。
// 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);