Detector de objetos#
Inicialización de la clase ObjectDetector#
Un sensor de objetos se crea utilizando el siguiente constructor:
ObjectDetector(port, reverse)
Este constructor utiliza dos parámetros:
Parámetro |
Descripción |
---|---|
|
El puerto de 3 cables al que está conectado el sensor de objetos, ya sea un puerto en el Cerebro o un Expandedor de 3 cables. |
|
Optional. Set this to reverse the detection logic. The default is |
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 ObjectDetector.
# Create the Brain.
brain = Brain()
# Construct an ObjectSensor "objdet" with the
# ObjectDetector class.
objdet = ObjectDetector(brain.three_wire_port.a)
This objdet
object will be used in all subsequent examples throughout this API documentation when referring to ObjectDetector class methods.
Métodos de clase#
reflectivity()#
The reflectivity(units)
method returns the reflectivity measured by the Object Sensor. The reflectivity is an estimation based on the raw value of the Object Sensor.
Una reflectividad del 0 % es un valor bruto de 3000 o superior. Una reflectividad del 100 % es un valor bruto de 0.
Parámetros |
Descripción |
---|---|
unidades |
Optional. The only valid unit for reflectivity is |
Devuelve: La reflectividad medida por el sensor de objetos en el rango 0% - 100%.
# Print the Object Sensor's current reflectivity to the
# Brain's screen.
brain.screen.print(objdet.reflectivity())
set_threshold()#
The set_threshold(value, units)
method sets the detection threshold for the Object Sensor to a specific value.
Parámetros |
Descripción |
---|---|
valor |
Umbral de reflectividad para la detección de objetos. Rango de 0 a 100. |
Devoluciones: Ninguna.
# Set the Object Sensor's reflectivity threhold to 50%.
objdet.set_threshold(50)
is_object_detected()#
The is_object_detected()
method returns if an object is detected.
Returns: True
if an object is detected. False
if one is not.
changed()#
The changed(callback, arg)
method registers a callback function for when the value of an Object Sensor changes.
Parámetros |
Descripción |
---|---|
llamar de vuelta |
La función de devolución de llamada que se llamará cuando cambie el valor de un sensor de objetos. |
arg |
Opcional. Una tupla de argumentos para pasar a la función de devolución de llamada. |
Devuelve: Una instancia de la clase Event.
# Define a function detector_changed().
def detector_changed():
# The brain will print that the Object Sensor value
# changed on the Brain's screen.
brain.screen.print("Object Sensor value changed")
# Run detector_changed when the value of the
# Object Sensor changes.
objdet.changed(detector_changed)
object_detected()#
The object_detected(callback, arg)
method registers a callback function for when an object is detected.
Parámetros |
Descripción |
---|---|
llamar de vuelta |
La función de devolución de llamada que se llamará cuando se detecte un objeto. |
arg |
Opcional. Una tupla de argumentos para pasar a la función de devolución de llamada. |
Devuelve: Una instancia de la clase Event.
# Define a function detected().
def detected():
# The Brain will print that an object was detected
# on the Brain's screen.
brain.screen.print("object detected")
# Run detected when the Object Sensor detects an object.
objdet.object_detected(detected)
object_lost()#
The object_lost(callback, arg)
method registers a callback function for when an object is lost.
Parámetros |
Descripción |
---|---|
llamar de vuelta |
La función de devolución de llamada que se llamará cuando se pierda un objeto. |
arg |
Opcional. Una tupla de argumentos para pasar a la función de devolución de llamada. |
Devuelve: Una instancia de la clase Event.
# Define a function lost().
def lost():
# The Brain will print that an object was lost on the
# Brain's screen.
brain.screen.print("object lost")
# Run lost() when the Object Sensor loses an object.
objdet.object_lost(lost)