Controlador#

Introducción#

El controlador One Stick cuenta con 4 botones y un joystick que funciona como entrada analógica y botón pulsable. Estas entradas permiten al robot detectar las pulsaciones de botones y los movimientos del joystick, lo que permite un control interactivo y sensible. A continuación, se muestra una lista de todos los métodos disponibles:

Getters: botón de lectura, joystick y estado de conexión.

  • .pressing – Returns whether the specified button is being pressed.

  • .position – Returns the position of the joystick’s specified axis.

  • is_connected – Returns whether the controller is connected.

  • get_battery_level – Returns the controller’s battery level as a percentage.

Devoluciones de llamadas: responden a cambios en la entrada del botón o del joystick.

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

Captadores#

.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 button_up (see full list of button objects below).

  • 1 - The specified button is being pressed.

  • 0 - The specified button is not being pressed.

Uso:
Se puede usar uno de los cinco objetos de botón disponibles con este método, como se muestra a continuación:

Botón

Dominio

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

ajustes

Parámetros

Descripción

Este método no tiene parámetros.

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

.position#

.position returns the position of the joystick’s specified axis as an integer from –100 to 100, representing a percentage.

Uso:

Se puede utilizar uno de los dos ejes disponibles con este método, como se muestra a continuación:

Eje

Dominio

axis1

controller.axis1.position() — The vertical axis

axis2

controller.axis2.position() — The horizontal axis

ajustes

Parámetros

Descripción

Este método no tiene parámetros.

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

is_connected#

is_connected returns a Boolean indicating whether the Controller is connected.

  • True - The Controller is connected.

  • False - The Controller is not connected.

Uso:

controller.is_connected()

Parámetros

Descripción

Este método no tiene parámetros.

# Start moving forward.
# When the Controller disconnects, the robot stops.
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 an integer from 0 to 100, representing a percentage.

Uso:

controller.get_battery_level()

Parámetros

Descripción

Este método no tiene parámetros.

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

Devoluciones de llamadas#

.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 button_up – (see full list of button objects below).

Uso:
Se puede usar uno de los cinco objetos de botón disponibles con este método, como se muestra a continuación:

Botón

Dominio

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

ajustes

Parámetros

Descripción

llamar de vuelta

Una función que se define previamente para ejecutarse cuando se presiona el botón especificado.

arg

Opcional. Una tupla que contiene los argumentos que se pasarán a la función de devolución de llamada. Consulte Uso de eventos con parámetros para obtener más información.

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

controller.button_up.pressed(kick_object)

.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 button_up – (see full list of button objects below).

Uso:
Se puede usar uno de los cinco objetos de botón disponibles con este método, como se muestra a continuación:

Botón

Dominio

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

ajustes

Parámetros

Descripción

llamar de vuelta

Una función que se define previamente para ejecutarse cuando se suelta el botón especificado.

arg

Opcional. Una tupla que contiene los argumentos que se pasarán a la función de devolución de llamada. Consulte Uso de eventos con parámetros para obtener más información.

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

controller.button_up.released(clear_screen)

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

.changed#

.changed registers a function to be called when the joystick’s position changes.

Uso:
Se puede utilizar uno de los dos ejes disponibles con este método, como se muestra a continuación:

Eje

Dominio

axis1

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

axis2

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

ajustes

Parámetros

Descripción

llamar de vuelta

Una función que se define previamente para ejecutarse cuando cambia el valor del eje.

arg

Opcional. Una tupla que contiene los argumentos que se pasarán a la función de devolución de llamada. Consulte Uso de eventos con parámetros para obtener más información.

# Function to display an emoji when the joystick is moved
def move_joystick():
    robot.screen.show_emoji(CONFUSED)

# Run the function when the joystick is moved up or down
controller.axis1.changed(move_joystick)