物体传感器#

介绍#

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

VEX物体传感器。

This page uses object_sensor as the example Object Sensor name. Replace it with your own configured name as needed.

以下是可用方法列表:

  • reflectivity – Returns the amount of light reflected from the object.

  • set_threshold – Sets the amount of reflected light needed before an object is considered to be detected.

  • is_object_detected – Returns whether the Object Sensor is detecting an object.

  • changed – Registers a function to be called whenever the Object Sensor’s value changes.

  • object_detected – Registers a function to be called whenever the Object Sensor detects a new object.

  • object_lost – Registers a function to be called whenever the Object Sensor loses a detected object.

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

反射率#

reflectivity returns the amount of light reflected from the object as a percent.

笔记:

  • 0%反射率表示物体非常暗。传感器检测到的原始值为3000或更高。

  • 100%反射率意味着物体非常明亮。传感器检测到的原始值为0。

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)

设置阈值#

set_threshold sets the amount of reflected light needed as a percent before an object is considered to be detected.

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)

检测到物体#

is_object_detected returns whether the Object Sensor is detecting an object.

  • True – The Object Sensor is detecting an object.

  • False – The Object Sensor is not detecting an object.

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)

已更改#

changed registers a function to be called whenever the Object Sensor’s value changes.

Usage:
object_sensor.changed(callback, arg)

参数

描述

callback

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

arg

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

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

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

检测到目标#

object_detected registers a function to be called whenever the Object Sensor detects a new object.

Usage:
object_sensor.object_detected(callback, arg)

参数

描述

callback

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

arg

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

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 registers a function to be called whenever the Object Sensor loses a detected object.

Usage:
object_sensor.object_lost(callback, arg)

参数

描述

callback

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

arg

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

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

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

构造函数#

Constructors are used to manually create ObjectDetector objects, which are necessary for configuring Object Sensors.

ObjectDetector#

ObjectDetector creates an Object Sensor.

Usage:
ObjectDetector(port, reverse_det)

范围

描述

port

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

  • On the V5 Brainbrain.three_wire_port.x where x is the number of the port.
  • On a 3-Wire Expanderexpander.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)