声纳#

初始化声纳类#

使用以下构造函数创建 Range Finder:

The sonar constructor creates a sonar object in the specified Three Wire Port.

范围

描述

port

The 3-Wire Port “pair” that the Range Finder is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.
Note: A Range Finder uses two adjacent 3-Wire ports. These pairs are A/B, C/D, E/F, and G/H. When entering the pair as an argument, use the first letter of the pair, ie: Brain.ThreeWirePort.A refers to the a/b pair.

必须先创建 Brain3-Wire Expander,然后才能使用 sonar Class 构造函数创建对象。

// Create the Brain.
brain Brain;
// Construct a Range Finder "Sonar" with the
// sonar class.
sonar Sonar = sonar(Brain.ThreeWirePort.A);

This Sonar object will be used in all subsequent examples throughout this API documentation when referring to sonar class methods.

类方法#

distance()#

The distance(units) method returns the current distance the Range Finder is detecting on abject at. The Range Finder will return a large positive number if no object is detected in range.

参数

描述

单位

有效的 distanceUnit

**返回:**表示测距仪测量的距离的双精度数。

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

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

foundObject()#

The foundObject() method checks if an object is detected by the Range Finder in the range 0 - 1000 millimeters. The Range Finder will return true if an object is detected closer than 1000mm.

Returns: true if an object is detected by the Range Finder. false if one is not.

// Check if an object is 1000mm or closer to the Range Finder.
if (Sonar.foundObject()){
    // Print to screen on the brain that an object was found.
    Brain.Screen.print("object found");
}

setMaximum()#

The setMaximum(distance, units) method sets the maximum distance the foundObject() method will return true for.

参数

描述

距离

表示测距仪能够找到物体的最大距离的双精度数。

单位

有效的 distanceUnit

**返回:**无。

changed()#

The changed(callback) method runs a callback function when the Range Finder’s value changes.

参数

描述

打回来

当 Range Finder 的值发生变化时运行的回调函数。

**返回:**无。

// Define the sonarChanged function with a void return type,
// showing it doesn't return a value.
void sonarChanged() {
  // The Brain will print that the value detected by the
  // Range Finder changed on the Brain's screen.
  Brain.Screen.print("Range Finder value changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run sonarChanged when the value of the
  // Range Finder changes.
  Sonar.changed(sonarChanged);
}