控制器#

介绍#

VEX IQ(第二代)控制器配备按钮和两个摇杆。控制器方法可用于检测按钮按下、读取摇杆移动、启用或禁用已配置的控制器操作,或在控制器事件发生时运行相关函数。

第二代 IQ 大脑可以连接到第二代 IQ 控制器或第一代 IQ 控制器。第一代 IQ 控制器必须安装蓝色智能无线电模块。

Configured controller actions are controller behaviors set in the Devices menu. Use remote_control_code_enabled to temporarily enable or disable those configured actions during a project.

This page uses controller as the example controller name. Replace it with your own configured name as needed.

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

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

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

  • .pressing — Returns whether a specified button is being pressed.

  • .position — Returns the position of a specified joystick axis.

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

  • .pressed — Runs a function when a specified button is pressed.

  • .released — Runs a function when a specified button is released.

  • .changed — Runs a function when the joystick’s position changes along a specified axis.

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

行动#

remote_control_code_enabled#

remote_control_code_enabled is a variable that enables or disables controller actions configured in the Devices menu. Controller configured actions are enabled by default.

Usage:
remote_control_code_enabled = state

价值

描述

True

启用控制器配置的操作。

False

禁用控制器配置的操作。

# Use custom controller code, then re-enable configured actions
remote_control_code_enabled = False

while True:
    if controller.axisA.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller.axisA.position() < 0:
        drivetrain.drive(REVERSE)
    elif controller.buttonEUp.pressing():
        break
    else:
        drivetrain.stop()

    wait(20, MSEC)

remote_control_code_enabled = True

吸气剂#

.pressing#

.pressing returns whether a specific button on the controller is currently being pressed. This method must be called on a specific button object, such as controller.buttonEUp.

  • True — The specified button is being pressed.

  • False — The specified button is not being pressed.

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

IQ 第二代控制器的正面和背面视图,所有按钮均以黄色高亮显示。控制器表面左上角和右上角各有两个摇杆按钮,左侧摇杆下方有 E 上下键,右侧摇杆下方有 F 上下键。控制器背面左侧和右侧分别有 L 上下键和 R 上下键。

按钮

命令

buttonEDown

controller.buttonEDown.pressing() — The E ▼ button

buttonEUp

controller.buttonEUp.pressing() — The E ▲ button

buttonFDown

controller.buttonFDown.pressing() — The F ▼ button

buttonFUp

controller.buttonFUp.pressing() — The F ▲ button

buttonL3

controller.buttonL3.pressing() — The Left Joystick button, IQ (2nd gen) Controller only

buttonLDown

controller.buttonLDown.pressing() — The L ▼ button

buttonLUp

controller.buttonLUp.pressing() — The L ▲ button

buttonR3

controller.buttonR3.pressing() — The Right Joystick button, IQ (2nd gen) Controller only

buttonRDown

controller.buttonRDown.pressing() — The R ▼ button

buttonRUp

controller.buttonRUp.pressing() — The R ▲ button

参数

描述

该方法没有参数。

# Turn right while E Up is held
while True:
    if controller.buttonEUp.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

.position#

.position returns the position of a joystick axis as a number from -100 to 100.

This method must be called on a specific axis object, such as controller.axisA.

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

图中高亮显示了第二代 IQ 控制器的四个摇杆​​轴。左侧摇杆的 A 轴为垂直轴,B 轴为水平轴。右侧摇杆的 C 轴为水平轴,D 轴为垂直轴。

命令

axisA

controller.axisA.position() — The left joystick’s vertical axis

axisB

controller.axisB.position() — The left joystick’s horizontal axis

axisC

controller.axisC.position() — The right joystick’s horizontal axis

axisD

controller.axisD.position() — The right joystick’s vertical axis

参数

描述

该方法没有参数。

# Turn with the left joystick
remote_control_code_enabled = False

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

    wait(20, MSEC)

回调函数#

.pressed#

.pressed runs a function when the specified button is pressed. Once it is used, the function will run automatically each time that button is pressed.

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

IQ 第二代控制器的正面和背面视图,所有按钮均以黄色高亮显示。控制器表面左上角和右上角各有两个摇杆按钮,左侧摇杆下方有 E 上下键,右侧摇杆下方有 F 上下键。控制器背面左侧和右侧分别有 L 上下键和 R 上下键。

按钮

命令

buttonEDown

controller.buttonEDown.pressed(callback, arg) — The E ▼ button

buttonEUp

controller.buttonEUp.pressed(callback, arg) — The E ▲ button

buttonFDown

controller.buttonFDown.pressed(callback, arg) — The F ▼ button

buttonFUp

controller.buttonFUp.pressed(callback, arg) — The F ▲ button

buttonL3

controller.buttonL3.pressed(callback, arg) — The Left Joystick button, IQ (2nd gen) Controller only

buttonLDown

controller.buttonLDown.pressed(callback, arg) — The L ▼ button

buttonLUp

controller.buttonLUp.pressed(callback, arg) — The L ▲ button

buttonR3

controller.buttonR3.pressed(callback, arg) — The Right Joystick button, IQ (2nd gen) Controller only

buttonRDown

controller.buttonRDown.pressed(callback, arg) — The R ▼ button

buttonRUp

controller.buttonRUp.pressed(callback, arg) — The R ▲ button

范围

描述

callback

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

arg

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

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

controller.buttonEUp.pressed(drive_forward)

.released#

.released runs a function when the specified button is released. Once it is used, the function will run automatically each time that button is released.

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

IQ 第二代控制器的正面和背面视图,所有按钮均以黄色高亮显示。控制器表面左上角和右上角各有两个摇杆按钮,左侧摇杆下方有 E 上下键,右侧摇杆下方有 F 上下键。控制器背面左侧和右侧分别有 L 上下键和 R 上下键。

按钮

命令

buttonEDown

controller.buttonEDown.released(callback, arg) — The E ▼ button

buttonEUp

controller.buttonEUp.released(callback, arg) — The E ▲ button

buttonFDown

controller.buttonFDown.released(callback, arg) — The F ▼ button

buttonFUp

controller.buttonFUp.released(callback, arg) — The F ▲ button

buttonL3

controller.buttonL3.released(callback, arg) — The Left Joystick button, IQ (2nd gen) Controller only

buttonLDown

controller.buttonLDown.released(callback, arg) — The L ▼ button

buttonLUp

controller.buttonLUp.released(callback, arg) — The L ▲ button

buttonR3

controller.buttonR3.released(callback, arg) — The Right Joystick button, IQ (2nd gen) Controller only

buttonRDown

controller.buttonRDown.released(callback, arg) — The R ▼ button

buttonRUp

controller.buttonRUp.released(callback, arg) — The R ▲ button

范围

描述

callback

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

arg

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

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

controller.buttonEUp.released(stop_driving)

.changed#

.changed runs a function when the joystick’s position changes along the specified axis. Once it is used, the function will run automatically each time the joystick’s position changes along that axis.

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

图中高亮显示了第二代 IQ 控制器的四个摇杆​​轴。左侧摇杆的 A 轴为垂直轴,B 轴为水平轴。右侧摇杆的 C 轴为水平轴,D 轴为垂直轴。

命令

axisA

controller.axisA.changed(callback, arg) — The left joystick’s vertical axis

axisB

controller.axisB.changed(callback, arg) — The left joystick’s horizontal axis

axisC

controller.axisC.changed(callback, arg) — The right joystick’s horizontal axis

axisD

controller.axisD.changed(callback, arg) — The right joystick’s vertical axis

范围

描述

callback

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

arg

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

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

controller.axisD.changed(beep)

构造函数#

Controller#

Controller creates a Controller object. Manually creating a Controller object is only needed when configuring a controller outside of VEXcode.

Usage:
Controller()

参数

描述

此构造函数没有参数。

# Create a Controller object
my_controller = Controller()