Distancia#

Inicializando la clase de distancia#

Un sensor de distancia se crea utilizando el siguiente constructor:

Distance(port)

Este constructor utiliza un parámetro:

Parámetro

Descripción

port

Un Puerto inteligente válido al que está conectado el sensor de distancia.

# Construct a Distance Sensor "distance" with the
# Distance class.
distance = Distance(Ports.PORT1)

This distance object will be used in all subsequent examples throughout this API documentation when referring to Distance class methods.

Métodos de clase#

object_distance()#

The object_distance(units) method returns the distance the Distance Sensor is currently reading. The Distance Sensor will return a large positive number if no object is detected.

Parámetros

Descripción

unidades

Optional. A valid DistanceUnits type. The default unit is MM.

Devuelve: La distancia al objeto detectado en las unidades especificadas.

# Get the distance detected by the Distance Sensor in mm.
value = distance.object_distance()

# Get the distance detected by the Distance Sensor in inches.
value = distance.object_distance(INCHES)

object_size()#

The object_size() method returns an estimate of the size of the detected object.

Devuelve: El tamaño estimado del objeto detectado.

# Get the object size detected by the Distance Sensor.
size = distance.object_size()

object_velocity()#

The object_velocity() method returns the velocity of the detected object. The velocity is calculated by the change in distance over the change in time.

Devuelve: La velocidad del objeto detectado en metros por segundo.

is_object_detected()#

The is_object_detected() method checks if an object is detected.

Returns: True if an object is detected. False if an object is not detected.

changed()#

The changed(callback, arg) method registers a callback function for when the detected object changes.

Parámetros

Descripción

llamar de vuelta

Una función que se llamará cuando el objeto detectado cambie.

arg

Opcional. Una tupla que se utiliza para pasar argumentos a la función de devolución de llamada.

Devuelve: Una instancia de la clase Event.

# Define function distance_changed().
def distance_changed():
    # The Brain will print that the distance changed on
    # the Brain's screen.
    brain.screen.print("distance changed ")
# Run distance_changed() when the value detected by the 
# Distance Sensor changes.
distance.changed(distance_changed)

object_rawsize()#

The object_rawsize() method returns the raw value of object size the sensor is detecting.

Devuelve: El tamaño sin procesar del objeto detectado.

# Get the raw size of the object detected by the
# Distance Sensor.
size = distance.object_rawsize()

installed()#

The installed() method checks if the Distance Sensor is connected.

Returns: True if the Distance Sensor is connected. False if it is not.

timestamp()#

The timestamp() method returns the timestamp of the last received status packet from the Distance Sensor.

Devuelve: La marca de tiempo del último paquete de estado recibido en milisegundos.