声纳#
初始化Sonar类#
使用以下构造函数创建 Range Finder:
声纳(端口)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
测距仪连接的三线端口“对”,可以是 Brain 上的端口,也可以是 3 线扩展器。2 |
必须先创建 Brain 或 3-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")