控制器#
介绍#
V5 主机可以连接到 V5 控制器。V5 控制器有两个模拟摇杆和多个按钮,主机可以通过这些按钮来检测动作和按键操作。
For the examples below, the configured controller will be named controller_1 and will be used in all subsequent examples throughout this API documentation when referring to Controller class methods.
以下是所有可用方法的列表:
操作 – 开启或关闭控制器编程的操作。
rumble– Sends a rumble string to the V5 Controller.print– Prints text on the Controller’s screen.set_cursor– Sets the cursor position used for printing text on the Controller’s screen.column– Returns the current column where text will be printed as an integer.row– Returns the current row where text will be printed as an integer.clear_screen– Clears the whole screen.clear_row– Clears a screen row.next_row– Moves the cursor to the beginning of the next row.remote_control_code_enabled– Enable or disable Controller configured actions.
Getter 函数——读取按钮状态和摇杆位置。
.pressing– Returns whether the specified button is being pressed..position– Returns the position of the joystick’s specified axis.
回调函数——当按钮或摇杆状态改变时运行的代码。
.pressed– Calls a function when the specified button is pressed..released– Calls a function when the specified button is released..changed– Calls a function when the joystick’s axis changes.
构造函数——手动初始化和配置控制器。
Controller– Create a controller.
行动#
rumble#
rumble sends a rumble string to the V5 Controller.
用法:
controller.rumble(pattern)
范围 |
描述 |
|---|---|
|
A pattern using |
# Rumble the V5 Controller to the pattern short-short-long-long.
controller.rumble('..--')
print#
controller.screen.print prints text on the Controller’s screen using the current cursor position.
用法:
controller.screen.print(text, sep, precision)
范围 |
描述 |
|---|---|
|
控制器屏幕上要打印的文本。 |
|
Optional. A string to inset between values. This must be written as a keyword argument (sep=). The default is |
|
Optional. The number of decimal places to display when printing simple numbers. This must be written as a keyword argument ( |
# Print the number 1 on the screen.
controller.screen.print(1)
# Print the numbers 1, 2, 3 and 4 on the screen.
controller.screen.print(1, 2, 3, 4, sep='-')
# Print motor1's velocity on the screen using a format string.
controller.screen.print("motor1 : % 7.2f" %(motor1.velocity()))
set_cursor#
set_cursor sets the cursor to a specific row and column on the controller’s screen. The controller screen has 3 rows and 19 columns.

用法:
controller.screen.set_cursor(row, col)
范围 |
描述 |
|---|---|
|
光标所在行的第 1 行到第 3 行。 |
|
光标所在列,从 1 到 19。 |
column#
column returns the current column where text will be printed as an integer.
用法:
controller.screen.column()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Print the column number on row 2, column 3.
controller.screen.set_cursor(2, 3)
controller.screen.print(controller.screen.column())
row#
row returns the current row where text will be printed as an integer.
用法:
controller.screen.row()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Print the column number on row 2, column 3.
controller.screen.set_cursor(2, 3)
controller.screen.print(controller.screen.row())
clear_screen#
clear_screen clears the whole screen.
用法:
controller.screen.clear_screen()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Print text and then clear the screen.
controller.screen.print("VEX V5")
wait(2, SECONDS)
controller.screen.clear_screen()
clear_row#
clear_row clears a screen row.
用法:
controller.screen.clear_row(row)
参数 |
描述 |
|---|---|
|
可选。要清除的行,范围从 1 到 3。默认值为当前光标所在行。 |
# Clear row 2.
controller.screen.clear_row(2)
next_row#
next_row moves the cursor to the beginning of the next row.
用法:
controller.screen.next_row()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Print text and then clear row 2.
controller.screen.print("VEX V5")
controller.screen.next_row()
controller.screen.print("Controller")
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 = 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 the controller configured actions
elif controller_1.buttonA.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.
用法:
此方法可以使用十二种可用按钮对象之一,如下所示:

按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Turn right while L1 is pressed
while True:
if controller_1.buttonL1.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 axis1 (see full list of axis objects below).
用法:
该方法可以使用以下四个可用坐标轴之一:

轴 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
# 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 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).
用法:
此方法可以使用一个可用的按钮对象,如下所示:

按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
打回来 |
一个预先定义好的函数,当按下指定的按钮时执行。 |
参数 |
可选。包含要传递给回调函数的参数的元组。带参数的函数 提供更多信息。 |
# Drive forward when A is pressed
def drive_forward():
drivetrain.drive_for(FORWARD, 100, MM)
controller_1.buttonA.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 buttonL1 – (see full list of button objects below).
用法:
此方法可以使用一个可用的按钮对象,如下所示:

按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
打回来 |
一个预先定义好的函数,当指定的按钮被释放时执行。 |
参数 |
可选。包含要传递给回调函数的参数的元组。带参数的函数 提供更多信息。 |
# Drive backward when A is released
def back_up():
drivetrain.drive_for(REVERSE, 100, MM)
controller_1.buttonA.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 axis1 (see full list of axis objects below).
用法:
此方法可以使用以下所示的四个可用轴之一:

轴 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
打回来 |
先前定义的当轴的值发生变化时执行的函数。 |
参数 |
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅带参数的函数。 |
# Play a sound when the left joystick moves
def tada_sound():
brain.play_sound(SoundType.TADA)
wait(1, SECONDS)
controller_1.axis4.changed(tada_sound)
构造函数#
Constructors are used to manually create Controller objects, which are necessary for configuring a controller outside of VEXcode. You can only create two controllers in a project.
Controller#
Controller creates a controller.
Usage:
Controller(controller_type)
参数 |
描述 |
|---|---|
|
Optional.The type of controller to create.
|
# Create a Controller
controller_1 = Controller(PRIMARY)
# Create two Controllers
controller_1 = Controller(PRIMARY)
controller_2 = Controller(PARTNER)