声纳#
初始化Sonar类#
使用以下构造函数创建 Range Finder:
Sonar(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. |
必须先创建 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)
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 object at. The Range Finder will return a large positive number if no object is detected in range.
参数 |
描述 |
---|---|
单位 |
有效的 DistanceUnits 类型。 |
**返回:**测距仪测量的距离。
# Print the current distance detected by the Range Finder
# to the Brain's screen.
brain.screen.print(sonar.distance(MM))
found_object()#
The found_object()
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.found_object():
# Print to the Brain's screen that an object was found
brain.screen.print("object found")