控制器#

介绍#

V5 控制器包含按钮、两个摇杆、一个震动马达和一个屏幕。控制器方法可用于检测按钮按下、读取摇杆移动、使控制器震动、启用或禁用已配置的控制器操作、在控制器事件发生时运行函数,或在控制器屏幕上显示文本。

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

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

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

操作 — 使控制器震动或启用/禁用已配置的控制器操作。

屏幕——在控制器屏幕上显示文本和数字。

  • print — 在控制器屏幕上显示文本、数字或变量值。

  • set_cursor — 将光标移动到控制器屏幕上的指定行和列。

  • column — 返回控制器屏幕上的当前光标列。

  • row — 返回控制器屏幕上的当前光标行。

  • clear_screen — 清除控制器屏幕。

  • clear_row — 清除控制器屏幕上的一行。

  • next_row — 将光标移动到控制器屏幕上的下一行。

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

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

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

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

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

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

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

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

  • Controller — 创建一个 Controller 对象。

行动#

rumble#

rumble 在控制器上播放震动模式。在模式字符串中,句点代表短震动,连字符代表长震动,空格代表停顿。

Usage:
controller_1.rumble(pattern)

范围

描述

pattern

由句点、连字符和空格组成的字符串,代表隆隆声模式。

# Rumble the Controller with a short-short-long-long pattern
controller_1.rumble("..--")

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_1.axis3.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller_1.axis3.position() < 0:
        drivetrain.drive(REVERSE)
    # Press A to use Controller configured actions again
    elif controller_1.buttonA.pressing():
        break
    else:
        drivetrain.stop()

    wait(20, MSEC)

remote_control_code_enabled = True

屏幕#

V5 控制器屏幕有 3 行 19 列,用于显示文本和数字。

V5 控制器屏幕的屏幕截图,显示了可打印区域中的行和列。第 1 行第 1 列从左上角开始,第 3 行第 19 列位于右下角。

print#

controller_1.screen.print 使用当前光标位置在控制器屏幕上显示文本、数字或变量值。

Usage:
controller_1.screen.print(value, sep, precision)

范围

描述

value

要在控制器屏幕上显示的文本、数字或变量值。可以用逗号分隔多个值来打印它们。

sep

可选。用于分隔值的字符串。必须将其写成关键字参数,例如 sep=”- “默认值为 ” “。

precision

可选。打印简单数字时显示的小数位数。必须以关键字参数的形式指定,例如 precision=2。默认值为 2。

# Print a number on the Controller screen
controller_1.screen.print(1)

# Print multiple numbers separated by hyphens
controller_1.screen.print(1, 2, 3, 4, sep="-")

# Print a number using one decimal place
controller_1.screen.print(1 / 3, precision=1)

set_cursor#

set_cursor 将光标移动到控制器屏幕上的指定行和列。下一个打印值将显示在该位置。

Usage:
controller_1.screen.set_cursor(row, col)

范围

描述

row

光标所在行,从 1 到 3。

col

光标所在列,从 1 到 19。

# Print text at row 2, column 1
controller_1.screen.set_cursor(2, 1)
controller_1.screen.print("Ready")

column#

column 返回当前将在控制器屏幕上打印文本的列。

Usage:
controller_1.screen.column()

参数

描述

此方法没有参数。

# Print the current column number
controller_1.screen.set_cursor(2, 3)
controller_1.screen.print(controller_1.screen.column())

row#

row 返回当前行,文本将打印在控制器屏幕上。

Usage:
controller_1.screen.row()

参数

描述

此方法没有参数。

# Print the current row number
controller_1.screen.set_cursor(2, 3)
controller_1.screen.print(controller_1.screen.row())

clear_screen#

clear_screen 清除控制器屏幕并将光标移回起始位置。

Usage:
controller_1.screen.clear_screen()

参数

描述

此方法没有参数。

# Print text, then clear the Controller screen
controller_1.screen.print("VEX V5")
wait(2, SECONDS)
controller_1.screen.clear_screen()

clear_row#

clear_row 清除控制器屏幕上的一行。

Usage:
controller_1.screen.clear_row(row)

范围

描述

row

可选。要清除的行,从 1 到 3如果没有提供行,则清除当前光标所在行。

# Clear row 2 on the Controller screen
controller_1.screen.clear_row(2)

next_row#

next_row 将光标移动到控制器屏幕下一行的第 1 列。下一个打印值将显示在该行。

Usage:
controller_1.screen.next_row()

参数

描述

此方法没有参数。

# Print on two rows
controller_1.screen.print("VEX V5")
controller_1.screen.next_row()
controller_1.screen.print("Controller")

获取器#

.pressing#

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

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

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

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

V5 控制器的正面和背面,按钮以黄色高亮显示。控制器正面左侧有上、下、左、右方向键,右侧从 12 点钟方向顺时针方向依次为 X、A、B、Y 键。控制器背面左侧和右侧分别是 L1、L2、R1 和 R2 键。

按钮

命令

buttonA

controller_1.buttonA.pressing() — A 按钮

buttonB

controller_1.buttonB.pressing() — B 按钮

buttonX

controller_1.buttonX.pressing() — X 按钮

buttonY

controller_1.buttonY.pressing() — Y 按钮

buttonDown

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

buttonUp

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

buttonLeft

controller_1.buttonLeft.pressing() — 左按钮

buttonRight

controller_1.buttonRight.pressing() — 右按钮

buttonL1

controller_1.buttonL1.pressing() — L1 按钮

buttonL2

controller_1.buttonL2.pressing() — L2 按钮

buttonR1

controller_1.buttonR1.pressing() — R1 按钮

buttonR2

controller_1.buttonR2.pressing() — R2 按钮

参数

描述

此方法没有参数。

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

    wait(20, MSEC)

.position#

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

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

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

一个 VEX V5 控制器,摇杆周围的轴已标注。轴 1 和轴 2 位于右侧摇杆周围,轴 3 和轴 4 位于左侧摇杆周围。

轴

命令

axis1

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

axis2

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

axis3

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

axis4

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

参数

描述

此方法没有参数。

# Turn with the left joystick
remote_control_code_enabled = False

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

    wait(20, MSEC)

回调函数#

.pressed#

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

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

V5 控制器的正面和背面,按钮以黄色高亮显示。控制器正面左侧有上、下、左、右方向键,右侧从 12 点钟方向顺时针方向依次为 X、A、B、Y 键。控制器背面左侧和右侧分别是 L1、L2、R1 和 R2 键。

按钮

命令

buttonA

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

buttonB

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

buttonX

controller_1.buttonX.pressed(callback, arg) — X 按钮

buttonY

controller_1.buttonY.pressed(callback, arg) — Y 按钮

buttonDown

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

buttonUp

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

buttonLeft

controller_1.buttonLeft.pressed(callback, arg) — 左按钮

buttonRight

controller_1.buttonRight.pressed(callback, arg) — 右按钮

buttonL1

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

buttonL2

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

buttonR1

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

buttonR2

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

范围

描述

callback

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

arg

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

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

controller_1.buttonA.pressed(drive_forward)

.released#

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

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

V5 控制器的正面和背面,按钮以黄色高亮显示。控制器正面左侧有上、下、左、右方向键,右侧从 12 点钟方向顺时针方向依次为 X、A、B、Y 键。控制器背面左侧和右侧分别是 L1、L2、R1 和 R2 键。

按钮

命令

buttonA

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

buttonB

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

buttonX

controller_1.buttonX.released(callback, arg) — X 按钮

buttonY

controller_1.buttonY.released(callback, arg) — Y 按钮

buttonDown

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

buttonUp

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

buttonLeft

controller_1.buttonLeft.released(callback, arg) — 左按钮

buttonRight

controller_1.buttonRight.released(callback, arg) — 右按钮

buttonL1

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

buttonL2

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

buttonR1

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

buttonR2

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

范围

描述

callback

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

arg

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

# Drive backward when A is released
def back_up():
    drivetrain.drive_for(REVERSE, 100, MM)

controller_1.buttonA.released(back_up)

.changed#

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

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

一个 VEX V5 控制器,摇杆周围的轴已标注。轴 1 和轴 2 位于右侧摇杆周围,轴 3 和轴 4 位于左侧摇杆周围。

轴

命令

axis1

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

axis2

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

axis3

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

axis4

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

范围

描述

callback

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

arg

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

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

controller_1.axis4.changed(tada_sound)

构造函数#

Controller#

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

Usage:
Controller(controller_type)

范围

描述

controller_type

可选。要创建的控制器类型: PRIMARY 表示连接到大脑的主控制器,或 PARTNER 表示连接到大脑的第二个控制器。

# Create the primary Controller
controller_1 = Controller(PRIMARY)

# Create a partner Controller
controller_2 = Controller(PARTNER)