Sonar#

Initializing the Sonar Class#

The Sonar constructor creates a Sonar object in the specified Smart Port:

This constructor uses one parameter:

Parameter

Description

port

A valid Smart Port that the Range Finder is connected to.

# Create the Brain.
brain = Brain()
# Construct a Range Finder "sonar" with the Sonar class.
sonar = Sonar(Ports.PORT1)

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))

set_maximum()#

The set_maximum(value, units) method sets the maximum working range for the Range Finder.

Objects detected further than this will be ignored by event handlers.

Parameters

Description

value

The new maximum working distance.

units

Optional. A valid DistanceUnit type. The default is MM.

Returns: None.

# Set sonar max distance in mm.
sonar.set_maximum(1200, MM)

set_minimum()#

The set_minimum(value, units) method sets the minimum working range for the Range Finder.

Objects detected closer than this will be ignored by event handlers.

Parameters

Description

value

The new minimum working distance.

units

Optional. A valid DistanceUnit type. The default is MM.

Returns: None.

# Set sonar min distance in mm.
sonar.set_minimum(100, MM)

is_object_detected()#

The is_object_detected() method returns if an object in range.

The Range Finder will return True if an object is detected between minimum and maximum range.

Returns: True if an object is detected in range. False if one is not.

# Is an object detected.
if sonar.is_object_detected():
    print("object found")

object_detected()#

The object_detected(callback, arg) method registers a function to be called when an object is detected by the Range Finder.

Parameters

Description

callback

A function that will be called when an object detected event occurs.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: An instance of the Event class.

# Define a function detected()
def detected():
    # The Brain will print that an object was detected on the Brain Screen.
    brain.screen.print("object detected")

# Run detected when the Range Finder detects an object
sonar.object_detected(detected)

changed()#

The changed(callback, arg) method registers a function to be called when the distance detected by the Range Finder changes.

Parameters

Description

callback

A function that will be called when the distance value changes.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: An instance of the Event class.

# Define a function sonar_changed()
def sonar_changed():
    # The Brain will print that the distance detected by the 
    # Range Finder changed on the Brain Screen.
    brain.screen.print("Sonar distance changed")

# Run sonar_changed when the distance deteced by the 
# Range Finder changes.
sonar.changed(sonar_changed)

installed()#

The installed() method returns if the Range Finder is connected to the Brain.

Returns: True if the Range Finder is connected to the Brian. False if it is not.