测距仪#
介绍#
The sonar class is used with the Ultrasonic Range Finder, which measures the distance to the nearest object using sound waves. It works by sending out an ultrasonic pulse and measuring how long it takes for the sound to bounce back.

类构造函数#
sonar(
triport::port &port);
类析构函数#
Destroys the sonar object and releases associated resources.
virtual ~sonar();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The first 3-Wire Port of a required two-port group used by the Range Finder. The Range Finder occupies two adjacent ports (A–B, C–D, E–F, or G–H) on either the Brain or a 3-Wire Expander. Specify the first port in the pair (A, C, E, or G), written as |
示例#
// Create a sonar instance in Ports A and B
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);
成员功能#
The sonar class includes the following member functions:
foundObject— Returns whether the Range Finder detects an object.distance— Returns the distance between the Range Finder and the nearest detected object.
Before calling any sonar member functions, a sonar instance must be created, as shown below:
/* 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 ValuesReturns a bool representing whether the Range Finder detects an object:
true— The Range Finder is detecting an object.false— The Range Finder is not detecting an object.
// 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 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
The unit that represents the distance: |
Returns a double representing the distance in the specified units.
// 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);