控制器#

简介#

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

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

以下是所有方法的列表:

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

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

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

  • is_connected — Returns whether the controller is connected.

  • get_battery_level — Returns the controller’s battery level.

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

  • .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.

Getters#

.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.button_up.

  • True — The specified button is being pressed.

  • False — The specified button is not being pressed.

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

按钮

命令

button_up

controller.button_up.pressing() — The Up button

button_down

controller.button_down.pressing() — The Down button

button_left

controller.button_left.pressing() — The Left button

button_right

controller.button_right.pressing() — The Right button

button_stick

controller.button_stick.pressing() — The Joystick button

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 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.axis1.

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

命令

axis1

controller.axis1.position() — The vertical axis

axis2

controller.axis2.position() — The horizontal axis

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 returns whether the controller is connected.

  • True — The controller is connected.

  • False — The controller is not connected.

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 returns the controller’s battery level as a number from 0 to 100.

Usage:
controller.get_battery_level()

参数

说明

该方法没有参数。

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

回调#

.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.

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

按钮

命令

button_up

controller.button_up.pressed(callback, arg) — The Up button

button_down

controller.button_down.pressed(callback, arg) — The Down button

button_left

controller.button_left.pressed(callback, arg) — The Left button

button_right

controller.button_right.pressed(callback, arg) — The Right button

button_stick

controller.button_stick.pressed(callback, arg) — The Joystick button

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 runs a function when the specified button is released. Once it is used, the function will run automatically each time that button is released.

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

按钮

命令

button_up

controller.button_up.released(callback, arg) — The Up button

button_down

controller.button_down.released(callback, arg) — The Down button

button_left

controller.button_left.released(callback, arg) — The Left button

button_right

controller.button_right.released(callback, arg) — The Right button

button_stick

controller.button_stick.released(callback, arg) — The Joystick button

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 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.

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

命令

axis1

controller.axis1.changed(callback, arg) — The vertical axis

axis2

controller.axis2.changed(callback, arg) — The horizontal axis

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)