控制器#
介绍#
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.
以下是所有可用方法的列表:
操作 — 启用或禁用已配置的控制器操作。
remote_control_code_enabled— Enables or disables controller actions configured in the Devices menu.
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 对象。
Controller— Creates a Controller object.
行动#
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
价值 |
描述 |
|---|---|
|
启用控制器配置的操作。 |
|
禁用控制器配置的操作。 |
# 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.
用法:
此方法可以使用其中一个可用的按钮对象:

按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
该方法没有参数。 |
# 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.
用法:
此方法可以使用其中一个可用的轴对象:

轴 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
该方法没有参数。 |
# 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.
用法:
此方法可以使用其中一个可用的按钮对象:

按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
范围 |
描述 |
|---|---|
|
预先定义的 函数,每次按下指定的按钮时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# 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.
用法:
此方法可以使用其中一个可用的按钮对象:

按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
范围 |
描述 |
|---|---|
|
预先定义的 函数,每次释放指定的按钮时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# 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.
用法:
此方法可以使用其中一个可用的轴对象:

轴 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
范围 |
描述 |
|---|---|
|
预先定义的 函数,每次操纵杆的位置沿指定轴发生变化时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# 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()