物体传感器#

介绍#

物体传感器是一种红外 (IR) 接近传感器,它利用反射的红外光来检测物体何时靠近。

VEX物体传感器。

本页面使用 object_sensor 作为示例对象传感器名称。请根据需要将其替换为您自己配置的名称。

以下是可用方法列表:

  • reflectivity – 返回一个 integer,表示物体反射的光量百分比。

  • is_object_detected – 返回对象传感器是否检测到对象。

  • set_threshold – 设置检测到物体之前所需的反射光量。

  • object_detected – 注册一个函数,当物体传感器检测到新物体时,该函数将被调用。

  • object_lost – 注册一个函数,当物体传感器丢失检测到的物体时,该函数将被调用。

  • changed – 注册一个函数,当对象传感器的值发生变化时,该函数将被调用。

构造函数 – 手动初始化对象传感器。

反射率#

reflectivity 返回一个 integer,表示物体反射的光量百分比。

笔记:

  • 0%反射率意味着物体非常暗。

  • 100%反射率意味着物体非常明亮。

Usage:
object_sensor.reflectivity()

参数

描述

此方法没有参数。

# Drive forward when the reflectivity is greater than 50%.
while True:
    if object_sensor.reflectivity() > 50:
        drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(5, MSEC)

检测到物体#

is_object_detected 返回对象传感器是否检测到对象。

  • True – 物体传感器正在检测到物体。

  • False ​​– 物体传感器未检测到物体。

Usage:
object_sensor.is_object_detected()

参数

描述

此方法没有参数。

# Drive forward when the object sensor is detecting an object.
while True:
    if object_sensor.is_object_detected():
        drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(5, MSEC)

设置阈值#

set_threshold 设置物体被检测到之前所需的反射光量(以百分比表示)。

Usage:
object_sensor.set_threshold(value)

参数

描述

value

物体被认为已被检测到之前所需的反射率值,范围从 0 到 100。

# Set the reflectivity threshold to 50%.
object_sensor.set_threshold(50)
# Drive forward when the object sensor is detecting an object.
while True:
    if object_sensor.is_object_detected():
        drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(5, MSEC)

检测到目标#

object_detected 注册一个函数,当物体传感器检测到新物体时,该函数将被调用。

Usage:
object_sensor.object_detected(callback, arg)

参数

描述

callback

先前定义的 函数,当物体传感器检测到新物体时执行。

arg

可选。一个包含要传递给回调函数的参数的tuple。有关更多信息,请参阅带参数的函数。

def my_function():
  brain.screen.print("Object detected.")

# Call my_function whenever object_sensor detects an object
object_sensor.object_detected(my_function)

丢失对象#

object_lost 注册一个函数,当物体传感器丢失检测到的物体时,该函数将被调用。

Usage:
object_sensor.object_lost(callback, arg)

参数

描述

callback

先前定义的 函数,当物体传感器丢失检测到的物体时执行。

arg

可选。一个包含要传递给回调函数的参数的tuple。有关更多信息,请参阅带参数的函数。

def my_function():
  brain.screen.print("Object lost.")

# Call my_function whenever object_sensor loses an object
object_sensor.object_lost(my_function)

已更改#

changed 注册一个函数,当物体传感器的值发生变化时,该函数将被调用。

Usage:
object_sensor.changed(callback, arg)

参数

描述

callback

先前定义的 函数,当对象传感器的值发生变化时执行。

arg

可选。一个包含要传递给回调函数的参数的tuple。有关更多信息,请参阅带参数的函数。

def my_function():
  brain.screen.print("Reflectivity changed.")

# Call my_function whenever object_sensor's value changes
object_sensor.changed(my_function)

构造函数#

构造函数用于手动创建 ObjectDetector 对象,这些对象是配置对象传感器所必需的。

ObjectDetector#

ObjectDetector 创建一个对象传感器。

Usage:
ObjectDetector(port, reverse_det)

范围

描述

port

The 3-Wire Port that the Object Sensor is connected to:

  • On the V5 Brain – brain.three_wire_port.x where x is the number of the port.
  • On a 3-Wire Expander – expander.a where expander is the name of the expander instance.

reverse_det

Optional. Whether to reverse the detection logic:

  • True – When an object is lost, return that an object is detected, and when an object is detected, return that it is lost.
  • False (default) – When an object is lost, return that an object is lost, and when an object is detected, return that it is detected.

# Create an Object Sensor in Port A
object_sensor = ObjectDetector(brain.three_wire_port.a)