控制器#

介绍#

第二代 IQ 大脑可以连接到第二代 IQ 控制器或第一代 IQ 控制器。两个控制器都配有两个模拟摇杆和多个按钮Sensing/Brain.md大脑可以利用这些按钮来检测动作和按压。

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

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

操作 — 开启或关闭控制器配置的操作。

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

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

  • .position — Returns the position of the joystick’s specified axis.

回调函数——当按钮或摇杆状态改变时运行代码。

  • .pressed — Registers a function to be called when the specified button is pressed.

  • .released — Registers a function to be called when the specified button is released.

  • .changed — Registers a function to be called when the joystick’s axis changes.

构造函数——手动初始化和配置控制器。

行动#

remote_control_code_enabled#

remote_control_code_enabled is a variable that can be set to a boolean that enables or disables controller configured actions from the Devices menu. The controller is enabled by default. It can be set to either of the following:

  • True — Enable controller configured actions.

  • False — Disable controller configured actions.

Usage:
remote_control_code_enabled

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

while True:
    if controller.axisA.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller.axisA.position() < 0:
        drivetrain.drive(REVERSE)
    # Press E Up to use the controller configured actions
    elif controller.buttonEUp.pressing():
        break
    else:
        drivetrain.stop()
    wait(20, MSEC)

remote_control_code_enabled = True

吸气剂#

.pressing#

.pressing returns an integer indicating whether a specific button on the controller is currently being pressed. This method must be called on a specific button object, such as buttonEDown (see full list of button objects below).

  • 1 — The specified button is being pressed.

  • 0 — 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()

.position#

.position returns the position of the joystick’s specified axis as an integer from -100 to 100, representing a percentage. This method must be called on a specific axis object, such as axisA (see full list of axis objects below).

用法:

此方法可使用四个可用轴之一,如下所示:

图中高亮显示了第二代 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 registers a function to be called when a specific button on the controller is pressed. This method must be called on a specific button object, such as buttonEDown (see full list of button objects below).

用法:
可以使用此方法使用其中一个可用的按钮对象,如下所示:

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) 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) 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 registers a function to be called when a specific button on the controller is released. This method must be called on a specific button object, such as buttonEDown (see full list of button objects below).

用法:
可以使用此方法使用其中一个可用的按钮对象,如下所示:

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

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

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

controller.buttonFDown.released(back_up)

.changed#

.changed registers a function to be called when the joystick’s position changes. This method must be called on a specific axis object, such as axisA (see full list of axis objects below).

用法:
可以使用此方法的四个可用轴之一,如下所示:

图中高亮显示了第二代 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)
    wait(1, SECONDS)

controller.axisD.changed(beep)

构造函数#

Constructors are used to manually create Controller objects, which are necessary for configuring a controller outside of VEXcode.

Controller#

Controller creates a controller.

Usage:
Controller()

参数

描述

此构造函数没有参数。

# Create a Controller object
my_controller = Controller()