声纳#

初始化Sonar类#

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

声纳(端口)

此构造函数使用一个参数:

范围

描述

端口

测距仪连接的三线端口“对”,可以是 Brain 上的端口,也可以是 3 线扩展器。2
注意:**测距仪使用两个相邻的三线端口。这些端口对分别是 a/b、c/d、e/f 和 g/h。输入端口对作为参数时,请使用端口对的首字母,例如:“brain.three_wire_port.a” 指的是 a/b 对。

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

# Create the Brain.
brain = Brain()
# Construct a Sonar "sonar" with the
# Sonar class.
sonar = Sonar(brain.three_wire_port.a)

当引用 Sonar 类方法时,此“sonar”对象将在整个 API 文档的所有后续示例中使用。

类方法#

distance()#

distance(units) 方法返回测距仪当前检测到的物体距离。如果在范围内未检测到任何物体,测距仪将返回一个较大的正数。

参数

描述

单位

有效的 DistanceUnits 类型。

**返回:**测距仪测量的距离。

# Print the current distance detected by the Range Finder
# to the Brain's screen.
brain.screen.print(sonar.distance(MM))

found_object()#

found_object() 方法检查测距仪是否在 0 - 1000 毫米范围内检测到物体。如果检测到的物体距离小于 1000 毫米,测距仪将返回 True

**返回:**如果测距仪检测到物体,则返回 True。如果没有检测到,则返回 False

# Check if an object is 1000mm or closer to the Range Finder.
if sonar.found_object():
    # Print to the Brain's screen that an object was found
    brain.screen.print("object found")