限位开关#

介绍#

限位开关是一种小型数字传感器,用于检测与物体的物理接触。

VEX V5 限位开关。

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

以下是可用方法列表:

  • pressing – 返回指定的限位开关是否被按下。

  • pressed – 注册一个函数,当限位开关被按下时调用该函数。

  • released – 注册一个函数,当限位开关释放时要调用该函数。

构造函数 - 手动初始化限位开关。

  • Limit – 创建一个限位开关。

紧迫#

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

  • True – 限位开关被按下。

  • False ​​– 限位开关未被按下。

Usage:
limit_switch.pressing()

参数

描述

此方法没有参数。

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

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

按下#

pressed 注册一个函数,该函数会在限位开关被按下时调用。

Usage:
limit_switch.pressed(callback, arg)

参数

描述

callback

一个先前定义的函数,当限位开关被按下时执行。

arg

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

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

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

发布#

released 注册一个函数,该函数将在限位开关释放时调用。

Usage:
limit_switch.released(callback, arg)

参数

描述

callback

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

arg

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

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

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

构造函数#

构造函数用于手动创建 Limit 对象,这是在 VEXcode 之外配置限位开关所必需的。

Limit#

Limit 创建限位开关。

Usage:
Limit(port)

范围

描述

port

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