物体探测器#

初始化 ObjectDetector 类#

使用以下构造函数创建对象传感器:

ObjectDetector(端口,反向)

此构造函数使用两个参数:

范围

描述

端口

物体传感器连接到的 3 线端口,无论它是 大脑 上的端口,还是 3 线扩展器 上的端口。

反向

**可选。**设置此项可反转检测逻辑。默认值为“False”。

必须先创建 Brain3-Wire Expander,然后才能使用 ObjectDetector 类构造函数创建对象。

# Create the Brain.
brain = Brain()
# Construct an ObjectSensor "objdet" with the
# ObjectDetector class.
objdet = ObjectDetector(brain.three_wire_port.a)

当引用 ObjectDetector 类方法时,此 objdet 对象将在整个 API 文档的所有后续示例中使用。

类方法#

reflectivity()#

reflectivity(units) 方法返回物体传感器测量的反射率。该反射率是基于物体传感器原始值的估算值。

反射率为 0% 时,原始值等于或大于 3000。反射率为 100% 时,原始值等于 0。

参数

描述

单位

**可选。**反射率的唯一有效单位是“百分比”。

**返回:**物体传感器测量的反射率在 0% - 100% 范围内。

# Print the Object Sensor's current reflectivity to the
# Brain's screen.
brain.screen.print(objdet.reflectivity())

set_threshold()#

set_threshold(value, units) 方法将对象传感器的检测阈值设置为特定值。

参数

描述

价值

物体检测的反射率阈值。范围为 0 - 100。

**返回:**无。

# Set the Object Sensor's reflectivity threhold to 50%.
objdet.set_threshold(50)

is_object_detected()#

如果检测到物体,则 is_object_detected() 方法返回。

返回: 如果检测到物体,则返回 True。如果没有检测到,则返回 False

changed()#

changed(callback, arg) 方法注册一个回调函数,用于当对象传感器的值发生变化时调用。

参数

描述

打回来

当对象传感器的值发生变化时调用的回调函数。

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

object_detected(callback, arg) 方法注册一个在检测到对象时回调的函数。

参数

描述

打回来

当检测到物体时调用的回调函数。

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

object_lost(callback, arg) 方法注册一个当对象丢失时的回调函数。

参数

描述

打回来

当对象丢失时调用的回调函数。

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)