保险杠开关#

介绍#

碰撞开关是一种机械开关,按下时会接通电路,使机器人能够检测到按下和释放动作。

VEX V5 碰撞开关是一款三线设备 带有一个宽大的黑色底座,用于将其固定在机器人上,顶部有一个红色可按压按钮。

本页面使用 bumper_switch 作为示例保险杠开关名称。您可以根据需要将其替换为您自己配置的名称。

以下是可用方法列表:

  • pressing – 返回是否按下了保险杠开关。

  • pressed – 注册一个函数,当按下保险杠开关时调用该函数。

  • released – 注册一个函数,当 Bumper 开关被释放时,该函数将被调用。

构造函数 – 手动初始化和配置碰撞开关。

  • Bumper – 创建一个保险杠开关。

紧迫#

pressing 返回 Boolean,指示碰撞开关当前是否被按下。这可用于检查机器人是否撞到其他物体。

  • True – 保险杠开关被按下。

  • False ​​– 保险杠开关未被按下。

Usage:
bumper_switch.pressing()

参数

描述

此方法没有参数。

# Display a message after bumper_switch is pressed
while not bumper_switch.pressing():
  pass

brain.screen.print("Bumper Switch pressed!")

按下#

pressed 注册一个函数,该函数会在每次按下保险杠开关时调用。

Usage:
bumper_switch.pressed(callback, arg)

参数

描述

callback

一个先前定义的函数,当按下保险杠开关时执行。

arg

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

def my_function():
  brain.screen.print("Pressed")

# Call my_function whenever bumper_switch is pressed
bumper_switch.pressed(my_function)

发布#

released 注册一个函数,该函数将在 Bumps Switch 释放时调用。

Usage:
bumper_switch.released(callback, arg)

参数

描述

callback

先前定义的 函数,当 Bumper 开关释放时执行。

arg

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

def my_function():
  brain.screen.print("Released")

# Call my_function whenever bumper_switch is released
bumper_switch.released(my_function)

构造函数#

构造函数用于手动创建 Bumper 对象,这是在 VEXcode 之外配置 Bumper Switch 所必需的。

Bumper#

Bumper 创建一个保险杠开关。

Usage:
Bumper(port)

范围

描述

port

The 3-Wire Port that the Bumper Switch 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 Bumper Switch in Port A
bumper_switch = Bumper(brain.three_wire_port.a)