ObjectDetector#
Initializing the ObjectDetector Class#
An Object Sensor is created by using the following constructor:
ObjectDetector(port, reverse)
This constructor uses two parameters:
Parameter |
Description |
---|---|
|
The 3-Wire Port that the Object Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander. |
|
Optional. Set this to reverse the detection logic. The default is |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the ObjectDetector Class constructor.
# 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.
Class Methods#
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.
A reflectivity of 0% is a raw value of 3000 or greater. A reflectivity of 100% is a raw value of 0.
Parameters |
Description |
---|---|
units |
Optional. The only valid unit for reflectivity is |
Returns: The reflectivity measured by the Object Sensor in the range 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.
Parameters |
Description |
---|---|
value |
The reflectivity threshold for object detection. In the range 0 - 100. |
Returns: None.
# 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.
Parameters |
Description |
---|---|
callback |
The callback function to be called when the value of an Object Sensor changes. |
arg |
Optional. A tuple of arguments to pass to the callback function. |
Returns: An instance of the Event class.
# 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.
Parameters |
Description |
---|---|
callback |
The callback function to be called when an object is detected. |
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'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.
Parameters |
Description |
---|---|
callback |
The callback function to be called when an object is lost. |
arg |
Optional. A tuple of arguments to pass to the callback function. |
Returns: An instance of the Event class.
# 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)