控制器#
简介#
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.
回调函数——当控制器输入发生变化时运行的函数。
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.
用法:
此方法可使用五种可用按钮对象之一:
按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
参数 |
说明 |
|---|---|
该方法没有参数。 |
# 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.
用法:
此方法可以使用以下两个轴对象之一:
轴 |
命令 |
|---|---|
|
|
|
|
参数 |
说明 |
|---|---|
该方法没有参数。 |
# 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.
用法:
此方法可使用五种可用按钮对象之一:
按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
范围 |
说明 |
|---|---|
|
预先定义的 函数,每次按下指定的按钮时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# 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.
用法:
此方法可使用五种可用按钮对象之一:
按钮 |
命令 |
|---|---|
|
|
|
|
|
|
|
|
|
|
范围 |
说明 |
|---|---|
|
预先定义的 函数,每次释放指定的按钮时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# 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.
用法:
此方法可以使用以下两个轴对象之一:
轴 |
命令 |
|---|---|
|
|
|
|
范围 |
说明 |
|---|---|
|
预先定义的 函数,每次操纵杆的位置沿指定轴发生变化时运行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的事件。 |
# Show an emoji when the joystick moves up or down
def move_joystick():
robot.screen.show_emoji(CONFUSED)
controller.axis1.changed(move_joystick)