控制器#

介绍#

VEX EXP 控制器配有按钮和两个操纵杆。控制器方法可用于检测按钮按下、读取操纵杆移动、启用或禁用已配置的控制器操作,或在控制器事件发生时运行函数。

已配置的控制器操作是指在“设备”菜单中设置的控制器行为。使用 remote_control_code_enabled 可在项目进行期间临时启用或禁用这些已配置的操作。

本页面使用 controller 作为示例控制器名称。请根据需要将其替换为您自己配置的名称。

以下是所有可用方法的列表:

操作 — 启用或禁用已配置的控制器操作。

Getter 函数——读取按钮状态和摇杆位置。

  • .pressing — 返回指定按钮是否被按下。

  • .position — 返回指定操纵杆轴的位置。

回调函数——当控制器输入发生变化时运行的函数。

  • .pressed — 当按下指定的按钮时运行一个函数。

  • .released — 当指定的按钮被释放时运行一个函数。

  • .changed — 当操纵杆的位置沿指定轴发生变化时运行函数。

构造函数 — 手动创建 Controller 对象。

  • Controller — 创建一个 Controller 对象。

行动#

remote_control_code_enabled#

remote_control_code_enabled 变量用于启用或禁用在“设备”菜单中配置的控制器操作。默认情况下,已配置的控制器操作处于启用状态。

Usage:
remote_control_code_enabled = state

价值

描述

True

启用控制器配置的操作。

False

禁用控制器配置的操作。

# Drive forward or backward using the left joystick
remote_control_code_enabled = False

while True:
    if controller.axis3.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller.axis3.position() < 0:
        drivetrain.drive(REVERSE)
    # Press A to use controller configured actions again
    elif controller.buttonA.pressing():
        break
    else:
        drivetrain.stop()

    wait(20, MSEC)

remote_control_code_enabled = True

获取器#

.pressing#

.pressing 返回控制器上的特定按钮当前是否被按下。此方法必须在特定的按钮对象上调用,例如 controller.buttonA。

  • True — 指定的按钮正在被按下。

  • False ​​— 指定的按钮未被按下。

用法:
此方法可以使用其中一个可用的按钮对象:

EXP 控制器的正面和顶部,摇杆、方向键和动作键以黄色高亮显示。控制器表面左右两侧各有一个摇杆,左侧是上下方向键,右侧是 A 键和 B 键。控制器顶部左侧是 L1 键和 L2 键,右侧是 R1 键和 R2 键。

按钮

命令

buttonA

controller.buttonA.pressing() — A 按钮

buttonB

controller.buttonB.pressing() — B 按钮

buttonDown

controller.buttonDown.pressing() — 向下 按钮

buttonUp

controller.buttonUp.pressing() — 向上 按钮

buttonL1

controller.buttonL1.pressing() — L1 按钮

buttonL2

controller.buttonL2.pressing() — L2 按钮

buttonL3

controller.buttonL3.pressing() — L3 按钮

buttonR1

controller.buttonR1.pressing() — R1 按钮

buttonR2

controller.buttonR2.pressing() — R2 按钮

buttonR3

controller.buttonR3.pressing() — R3 按钮

参数

描述

此方法没有参数。

# Turn right while L1 is pressed
while True:
    if controller.buttonL1.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

.position#

.position 返回操纵杆轴的位置,数值范围为 -100 到 100。

必须在特定的轴对象上调用此方法,例如 controller.axis1。

用法:
此方法可以使用其中一个可用的轴对象:

EXP 控制器的正面,摇杆轴编号以红色突出显示。控制器表面显示,左侧摇杆标记为轴 4(左右移动)和轴 3(上下移动),而右侧摇杆标记为轴 1(左右移动)和轴 2(上下移动)。

轴

命令

axis1

controller.axis1.position() — 右摇杆的水平轴

axis2

controller.axis2.position() — 右摇杆的垂直轴

axis3

controller.axis3.position() — 左摇杆的垂直轴

axis4

controller.axis4.position() — 左摇杆的水平轴

参数

描述

此方法没有参数。

# Turn with the left joystick
remote_control_code_enabled = False

while True:
    if controller.axis4.position() > 10:
        drivetrain.turn(RIGHT)
    elif controller.axis4.position() < -10:
        drivetrain.turn(LEFT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

回调函数#

.pressed#

.pressed 表示当指定的按钮被按下时运行一个函数。一旦使用,每次按下该按钮时,该函数都会自动运行。

用法:
此方法可以使用其中一个可用的按钮对象:

EXP 控制器的正面和顶部,摇杆、方向键和动作键以黄色高亮显示。控制器表面左右两侧各有一个摇杆,左侧是上下方向键,右侧是 A 键和 B 键。控制器顶部左侧是 L1 键和 L2 键,右侧是 R1 键和 R2 键。

按钮

命令

buttonA

controller.buttonA.pressed(callback, arg) — A 按钮

buttonB

controller.buttonB.pressed(callback, arg) — B 按钮

buttonDown

controller.buttonDown.pressed(callback, arg) — 向下 按钮

buttonUp

controller.buttonUp.pressed(callback, arg) — 向上 按钮

buttonL1

controller.buttonL1.pressed(callback, arg) — L1 按钮

buttonL2

controller.buttonL2.pressed(callback, arg) — L2 按钮

buttonL3

controller.buttonL3.pressed(callback, arg) — L3 按钮

buttonR1

controller.buttonR1.pressed(callback, arg) — R1 按钮

buttonR2

controller.buttonR2.pressed(callback, arg) — R2 按钮

buttonR3

controller.buttonR3.pressed(callback, arg) — R3 按钮

范围

描述

callback

预先定义的 函数,每次按下指定的按钮时运行。

arg

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

# Drive forward when R1 is pressed
def drive_forward():
    drivetrain.drive_for(FORWARD, 200, MM)

controller.buttonR1.pressed(drive_forward)

.released#

.released 会在指定的按钮被释放时运行一个函数。一旦使用,每次释放该按钮时,该函数都会自动运行。

用法:
此方法可以使用其中一个可用的按钮对象:

EXP 控制器的正面和顶部,摇杆、方向键和动作键以黄色高亮显示。控制器表面左右两侧各有一个摇杆,左侧是上下方向键,右侧是 A 键和 B 键。控制器顶部左侧是 L1 键和 L2 键,右侧是 R1 键和 R2 键。

按钮

命令

buttonA

controller.buttonA.released(callback, arg) — A 按钮

buttonB

controller.buttonB.released(callback, arg) — B 按钮

buttonDown

controller.buttonDown.released(callback, arg) — 向下按钮

buttonUp

controller.buttonUp.released(callback, arg) — 向上 按钮

buttonL1

controller.buttonL1.released(callback, arg) — L1 按钮

buttonL2

controller.buttonL2.released(callback, arg) — L2 按钮

buttonL3

controller.buttonL3.released(callback, arg) — L3 按钮

buttonR1

controller.buttonR1.released(callback, arg) — R1 按钮

buttonR2

controller.buttonR2.released(callback, arg) — R2 按钮

buttonR3

controller.buttonR3.released(callback, arg) — R3 按钮

范围

描述

callback

预先定义的 函数,每次释放指定的按钮时运行。

arg

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

# Stop driving when R1 is released
def stop_driving():
    drivetrain.stop()

controller.buttonR1.released(stop_driving)

.changed#

.changed 会在操纵杆沿指定轴位置发生变化时运行一个函数。一旦启用,该函数会在每次操纵杆沿该轴位置发生变化时自动运行。

用法:
此方法可以使用其中一个可用的轴对象:

EXP 控制器的正面,摇杆轴编号以红色突出显示。控制器表面显示,左侧摇杆标记为轴 4(左右移动)和轴 3(上下移动),而右侧摇杆标记为轴 1(左右移动)和轴 2(上下移动)。

轴

命令

axis1

controller.axis1.changed(callback, arg) — 右摇杆的水平轴

axis2

controller.axis2.changed(callback, arg) — 右摇杆的垂直轴

axis3

controller.axis3.changed(callback, arg) — 左摇杆的垂直轴

axis4

controller.axis4.changed(callback, arg) — 左摇杆的水平轴

范围

描述

callback

预先定义的 函数,每次操纵杆的位置沿指定轴发生变化时运行。

arg

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

# Play a sound when the left joystick moves
def beep():
    brain.play_sound(SoundType.TADA)

controller.axis3.changed(beep)

构造函数#

Controller#

Controller 创建一个控制器对象。只有在 VEXcode 之外配置控制器时才需要手动创建控制器对象。

Usage:
Controller()

参数

描述

此构造函数没有参数。

# Create a Controller object
my_controller = Controller()