光传感器#

介绍#

光传感器测量物体反射或照射到物体上的光的亮度。它输出一个模拟值,表示检测到的光量。

VEX V5 光传感器。

本页面使用 light_sensor 作为示例光传感器名称。您可以根据需要将其替换为您自己配置的名称。

以下是可用方法列表:

  • brightness – 返回光传感器检测到的光量百分比。

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

构造函数 - 手动初始化光传感器。

  • Light – 创建一个光传感器。

亮度#

brightness 返回光传感器检测到的光量,类型为 integer。这可用于检查物体是否足够明亮,以便准确地报告其他视觉数据。

Usage:
light_sensor.brightness(units)

范围

描述

units

可选。返回亮度的单位: PERCENT (默认值)。

已更改#

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

Usage:
light_sensor.changed(callback, arg)

参数

描述

callback

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

arg

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

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

# Call my_function whenever the brightness changes
light_sensor.changed(my_function)

构造函数#

构造函数用于手动创建 Light 对象,这是在 VEXcode 之外配置光传感器所必需的。

Light#

Light 创建一个光传感器。

Usage:
Light(port)

范围

描述

port

The 3-Wire Port that the Light 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.

# Create a Light Sensor in Port A
light_sensor = Light(brain.three_wire_port.a)