物体探测器#
初始化 ObjectDetector 类#
使用以下构造函数创建对象传感器:
ObjectDetector(port, reverse)
此构造函数使用两个参数:
范围 |
描述 |
---|---|
|
|
|
Optional. Set this to reverse the detection logic. The default is |
必须先创建 Brain 或 3-Wire Expander,然后才能使用 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.
类方法#
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.
反射率为 0% 时,原始值等于或大于 3000。反射率为 100% 时,原始值等于 0。
参数 |
描述 |
---|---|
单位 |
Optional. The only valid unit for reflectivity is |
**返回:**物体传感器测量的反射率在 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.
参数 |
描述 |
---|---|
价值 |
物体检测的反射率阈值。范围为 0 - 100。 |
**返回:**无。
# 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.
参数 |
描述 |
---|---|
打回来 |
当对象传感器的值发生变化时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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.
参数 |
描述 |
---|---|
打回来 |
当检测到物体时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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.
参数 |
描述 |
---|---|
打回来 |
当对象丢失时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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)