Sonar#

Inicializando la clase Sonar#

Un telémetro se crea utilizando el siguiente constructor:

Sonar(port)

Este constructor utiliza un parámetro:

Parámetro

Descripción

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.
Note: A Range Finder uses two adjacent 3-Wire ports. These pairs are a/b, c/d, e/f, and g/h. When entering the pair as an argument, use the first letter of the pair, ie: brain.three_wire_port.a refers to the a/b pair.

Primero se debe crear un Brain o un 3-Wire Expander antes de poder usarlos para crear un objeto con el constructor de la clase 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.

Métodos de clase#

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.

Parámetros

Descripción

unidades

Un tipo DistanceUnits válido.

Devuelve: La distancia medida por el telémetro.

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