Controller#
Introduction#
The VEX AIM One Stick Controller has four buttons and a joystick. The joystick can be moved along two axes and can also be pressed like a button.
Controller methods can be used to check button presses, read joystick movement, check connection and battery status, or run functions when controller events happen.
Below is a list of all methods:
Getters — Read button, joystick, connection, and battery status.
.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.
Callbacks — Run functions when controller input changes.
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.
Usage:
One of five available button objects can be used with this method:
Button |
Command |
|---|---|
|
|
|
|
|
|
|
|
|
|
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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.
Usage:
One of two available axis objects can be used with this method:
Axis |
Command |
|---|---|
|
|
|
|
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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()
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Display the controller's battery level
robot.screen.print(controller.get_battery_level())
Callbacks#
.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.
Usage:
One of five available button objects can be used with this method:
Button |
Command |
|---|---|
|
|
|
|
|
|
|
|
|
|
Parameter |
Description |
|---|---|
|
A previously defined function to run each time the specified button is pressed. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information. |
# 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.
Usage:
One of five available button objects can be used with this method:
Button |
Command |
|---|---|
|
|
|
|
|
|
|
|
|
|
Parameter |
Description |
|---|---|
|
A previously defined function to run each time the specified button is released. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information. |
# 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.
Usage:
One of two available axis objects can be used with this method:
Axis |
Command |
|---|---|
|
|
|
|
Parameter |
Description |
|---|---|
|
A previously defined function to run each time the joystick’s position changes along the specified axis. |
|
Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information. |
# Show an emoji when the joystick moves up or down
def move_joystick():
robot.screen.show_emoji(CONFUSED)
controller.axis1.changed(move_joystick)