声纳#
初始化声纳类#
使用以下构造函数创建 Range Finder:
sonar
构造函数在指定的三线端口中创建一个 sonar 对象。
范围 |
描述 |
---|---|
|
测距仪连接到的有效 智能端口。 |
// Create the Brain.
brain Brain;
// Construct a Range Finder "Sonar" with the
// sonar class.
sonar Sonar = sonar(PORT1);
当引用 sonar 类方法时,此 API 文档中的所有后续示例中都将使用此 Sonar
对象。
类方法#
距离()#
distance(units)
方法返回测距仪当前探测到的物体距离。如果在探测范围内未探测到任何物体,测距仪将返回一个较大的正数。
参数 |
描述 |
---|---|
单位 |
有效的 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()
方法检查测距仪是否在 0 - 1000 毫米范围内检测到物体。如果检测到的物体距离小于 1000 毫米,测距仪将返回 true。
**返回:**如果测距仪检测到物体,则返回 true
。如果没有检测到,则返回 false
。
// 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(distance, units)
方法设置 foundObject()
方法将返回 true
的最大距离。
参数 |
描述 |
---|---|
距离 |
表示测距仪能够找到物体的最大距离的双精度数。 |
单位 |
有效的 distanceUnit。 |
**返回:**无。
检测到物体()#
当测距仪检测到物体时,objectDetected(callback)
命令会调用一个函数。
参数 |
描述 |
---|---|
打回来 |
当检测到对象时调用的函数的引用。 |
**返回:**无。
// Define the detected function with a void return type,
// showing it doesn't return a value.
void detected() {
// The Brain will print that the Range Finder detected
// an object on the Brain's screen.
Brain.Screen.print("Range Finder Detected Object");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Run detected when the Range Finder detect an object.
Sonar.objectDetected(detected);
}
已更改()#
参数 |
描述 |
---|---|
打回来 |
当 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);
}
时间戳()#
timestamp()
方法从 Range Finder 请求最后接收到的状态包的时间戳。
**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。
安装()#
如果测距仪已安装,则 installed()
命令返回。
**返回:**如果安装了 Range Finder,则返回 true
。如果没有安装,则返回 false
。