测距仪#

介绍#

sonar 级声呐与超声波测距仪配合使用,后者利用声波测量到最近物体的距离。其工作原理是发射超声波脉冲,并测量声波反射回来所需的时间。

VEX V5测距仪。

类构造函数#

sonar(
  triport::port &port);

类析构函数#

摧毁 sonar 目标并释放相关资源。

virtual ~sonar();

参数#

范围

类型

描述

port

triport::port &

测距仪使用的所需双端口组中的第一个三线端口。测距仪占用主机或三线扩展器上的两个相邻端口(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);