控制器#

简介#

VEX AIM 单摇杆控制器有四个按钮和一个摇杆。摇杆可以沿两个轴向移动,也可以像按钮一样按下。

控制器方法可用于检查按钮按下、读取操纵杆移动、检查连接和电池状态,或在控制器事件发生时运行功能。

以下是所有方法的列表:

读取按钮、操纵杆、连接和电池状态。

  • [.pressing] (#pressing) —返回是否正在按下指定按钮。

  • [.position] (#position) —返回沿指定轴的操纵杆位置。

  • [is_connected] (#is_connected) —返回控制器是否已连接。

  • [get_battery_level] (#get_battery_level) —返回控制器的电池电量。

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

  • [.pressed] (#pressed) —在按下指定按钮时运行函数。

  • [.released] (#released) —在释放指定按钮时运行函数。

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

Getters#

.pressing#

.pressing 返回控制器上某个特定按钮当前是否被按住。此方法必须在特定的按钮对象上调用,如 controller.button_up

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

  • False —未按下指定按钮。

用法:
此方法可使用五种可用按钮对象之一:

按钮

命令

button_up

controller.button_up.pressing() — * *按钮

button_down

controller.button_down.pressing() — * *按钮

button_left

controller.button_left.pressing() — * *按钮

button_right

controller.button_right.pressing() — * *按钮

button_stick

controller.button_stick.pressing() — * 操纵杆 *按钮

VEX AIM 单摇杆控制器,左侧为摇杆,右侧为四个按钮。摇杆和四个按钮已高亮显示。

参数

说明

该方法没有参数。

# Move forward while the Up button is being pressed
while True:
    if controller.button_up.pressing():
        robot.move_at(0)
    else:
        robot.stop_all_movement()

    wait(20, MSEC)

.position#

.position 返回摇杆轴的位置,值为从 -100100 的数字。

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

用法:
此方法可以使用以下两个轴对象之一:

命令

axis1

controller.axis1.position() —垂直轴

axis2

controller.axis2.position() —水平轴

VEX AIM 控制器左侧有一个摇杆,右侧有四个按钮。摇杆的 1 轴和 2 轴已高亮显示。

参数

说明

该方法没有参数。

# Move forward when the joystick is moved up
while True:
    if controller.axis1.position() > 0:
        robot.move_at(0)
    else:
        robot.stop_all_movement()

    wait(20, MSEC)

is_connected#

is_connected 返回控制器是否已连接。

  • True — 控制器已连接。

  • False —控制器未连接。

Usage:
controller.is_connected()

参数

说明

该方法没有参数。

# Stop the robot if the controller disconnects
robot.move_at(0)

while True:
    if not controller.is_connected():
        robot.stop_all_movement()

    wait(100, MSEC)

get_battery_level#

get_battery_level 返回控制器的电池电量,值为从 0100 的数字。

Usage:
controller.get_battery_level()

参数

说明

该方法没有参数。

# Display the controller's battery level
robot.screen.print(controller.get_battery_level())

回调#

.pressed#

.pressed 在按下指定按钮时运行一个函数。使用后,该功能将在每次按下该按钮时自动运行。

用法:
此方法可使用五种可用按钮对象之一:

按钮

命令

button_up

controller.button_up.pressed(callback, arg) — * *按钮

button_down

controller.button_down.pressed(callback, arg) — * *按钮

button_left

controller.button_left.pressed(callback, arg) — * *按钮

button_right

controller.button_right.pressed(callback, arg) — * *按钮

button_stick

controller.button_stick.pressed(callback, arg) — * 操纵杆 *按钮

VEX AIM 单摇杆控制器,左侧为摇杆,右侧为四个按钮。摇杆和四个按钮已高亮显示。

参数

说明

callback

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

arg

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

# Kick hard when the Up button is pressed
def kick_object():
    robot.kicker.kick(HARD)

controller.button_up.pressed(kick_object)

.released#

.released 运行一个函数当指定的按钮被放开。 一旦它被使用,每次按键被释放时,该函数将自动运行。

用法:
此方法可使用五种可用按钮对象之一:

按钮

命令

button_up

controller.button_up.released(callback, arg) — * *按钮

button_down

controller.button_down.released(callback, arg) — * *按钮

button_left

controller.button_left.released(callback, arg) — * *按钮

button_right

controller.button_right.released(callback, arg) — * *按钮

button_stick

controller.button_stick.released(callback, arg) — * 操纵杆 *按钮

VEX AIM 单摇杆控制器,左侧为摇杆,右侧为四个按钮。摇杆和四个按钮已高亮显示。

参数

说明

callback

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

arg

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

# Clear the screen after the Up button is released
def clear_screen():
    robot.screen.clear_screen()

controller.button_up.released(clear_screen)

robot.screen.print("Press Up, then")
robot.screen.next_row()
robot.screen.print("Release Up!")

.changed#

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

用法:
此方法可以使用以下两个轴对象之一:

命令

axis1

controller.axis1.changed(callback, arg) —垂直轴

axis2

controller.axis2.changed(callback, arg) —水平轴

VEX AIM 控制器左侧有一个摇杆,右侧有四个按钮。摇杆的 1 轴和 2 轴已高亮显示。

参数

说明

callback

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

arg

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

# Show an emoji when the joystick moves up or down
def move_joystick():
    robot.screen.show_emoji(CONFUSED)

controller.axis1.changed(move_joystick)