Sonar#
Initializing the Sonar Class#
A Range Finder is created by using the following constructor:
Sonar(port)
This constructor uses one parameter:
Parameter |
Description |
---|---|
|
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. |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Sonar Class constructor.
# 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.
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.
Parameters |
Description |
---|---|
units |
A valid DistanceUnits type. |
Returns: The distance measured by the Range Finder.
# 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")