Controlador#

Introducción#

El controlador VEX EXP cuenta con botones y dos joysticks. Los métodos del controlador permiten comprobar si se han pulsado los botones, leer el movimiento de los joysticks, activar o desactivar las acciones configuradas o ejecutar funciones cuando se producen eventos en el controlador.

Configured controller actions are controller behaviors set in the Devices menu. Use remote_control_code_enabled to temporarily enable or disable those configured actions during a project.

This page uses controller as the example controller name. Replace it with your own configured name as needed.

A continuación se muestra una lista de todos los métodos disponibles:

Acción: Activa o desactiva las acciones del controlador configuradas.

Funciones de obtención: leen el estado de los botones y la posición del joystick.

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

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

Funciones de devolución de llamada: ejecutan funciones cuando cambia la entrada del controlador.

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

Constructor: crea manualmente un objeto Controller.

Acción#

remote_control_code_enabled#

remote_control_code_enabled is a variable that enables or disables controller actions configured in the Devices menu. Controller configured actions are enabled by default.

Usage:
remote_control_code_enabled = state

Valor

Descripción

True

Habilita las acciones configuradas por el controlador.

False

Deshabilita las acciones configuradas por el controlador.

# Drive forward or backward using the left joystick
remote_control_code_enabled = False

while True:
    if controller.axis3.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller.axis3.position() < 0:
        drivetrain.drive(REVERSE)
    # Press A to use controller configured actions again
    elif controller.buttonA.pressing():
        break
    else:
        drivetrain.stop()

    wait(20, MSEC)

remote_control_code_enabled = True

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

  • True — The specified button is being pressed.

  • False — The specified button is not being pressed.

Uso:
Se puede utilizar uno de los objetos de botón disponibles con este método:

Vista frontal y superior del mando EXP, con los joysticks, las flechas y los botones de acción resaltados en amarillo. En la superficie del mando se encuentran dos joysticks, uno a la izquierda y otro a la derecha, con flechas arriba y abajo a la izquierda y botones A y B a la derecha. En la parte superior del mando se ubican los botones L1 y L2 a la izquierda y los botones R1 y R2 a la derecha.

Botón

Dominio

buttonA

controller.buttonA.pressing() — The A button

buttonB

controller.buttonB.pressing() — The B button

buttonDown

controller.buttonDown.pressing() — The Down button

buttonUp

controller.buttonUp.pressing() — The Up button

buttonL1

controller.buttonL1.pressing() — The L1 button

buttonL2

controller.buttonL2.pressing() — The L2 button

buttonL3

controller.buttonL3.pressing() — The L3 button

buttonR1

controller.buttonR1.pressing() — The R1 button

buttonR2

controller.buttonR2.pressing() — The R2 button

buttonR3

controller.buttonR3.pressing() — The R3 button

Parámetros

Descripción

Este método no tiene parámetros.

# Turn right while L1 is pressed
while True:
    if controller.buttonL1.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()

    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.

Uso:
Se puede utilizar uno de los objetos de eje disponibles con este método:

La parte frontal del controlador EXP muestra los números de los ejes del joystick resaltados en rojo. En la superficie del controlador, el joystick izquierdo está etiquetado como Eje 4 para el movimiento hacia la izquierda y la derecha, y Eje 3 para el movimiento hacia arriba y hacia abajo, mientras que el joystick derecho está etiquetado como Eje 1 para el movimiento hacia la izquierda y la derecha, y Eje 2 para el movimiento hacia arriba y hacia abajo.

Eje

Dominio

axis1

controller.axis1.position() — The right joystick’s horizontal axis

axis2

controller.axis2.position() — The right joystick’s vertical axis

axis3

controller.axis3.position() — The left joystick’s vertical axis

axis4

controller.axis4.position() — The left joystick’s horizontal axis

Parámetros

Descripción

Este método no tiene parámetros.

# Turn with the left joystick
remote_control_code_enabled = False

while True:
    if controller.axis4.position() > 10:
        drivetrain.turn(RIGHT)
    elif controller.axis4.position() < -10:
        drivetrain.turn(LEFT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

Devoluciones de llamada#

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

Uso:
Se puede utilizar uno de los objetos de botón disponibles con este método:

Vista frontal y superior del mando EXP, con los joysticks, las flechas y los botones de acción resaltados en amarillo. En la superficie del mando se encuentran dos joysticks, uno a la izquierda y otro a la derecha, con flechas arriba y abajo a la izquierda y botones A y B a la derecha. En la parte superior del mando se ubican los botones L1 y L2 a la izquierda y los botones R1 y R2 a la derecha.

Botón

Dominio

buttonA

controller.buttonA.pressed(callback, arg) — The A button

buttonB

controller.buttonB.pressed(callback, arg) — The B button

buttonDown

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

buttonUp

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

buttonL1

controller.buttonL1.pressed(callback, arg) — The L1 button

buttonL2

controller.buttonL2.pressed(callback, arg) — The L2 button

buttonL3

controller.buttonL3.pressed(callback, arg) — The L3 button

buttonR1

controller.buttonR1.pressed(callback, arg) — The R1 button

buttonR2

controller.buttonR2.pressed(callback, arg) — The R2 button

buttonR3

controller.buttonR3.pressed(callback, arg) — The R3 button

Parámetro

Descripción

callback

Una función definida previamente para ejecutarse cada vez que se presione 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.

# Drive forward when R1 is pressed
def drive_forward():
    drivetrain.drive_for(FORWARD, 200, MM)

controller.buttonR1.pressed(drive_forward)

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

Uso:
Se puede utilizar uno de los objetos de botón disponibles con este método:

Vista frontal y superior del mando EXP, con los joysticks, las flechas y los botones de acción resaltados en amarillo. En la superficie del mando se encuentran dos joysticks, uno a la izquierda y otro a la derecha, con flechas arriba y abajo a la izquierda y botones A y B a la derecha. En la parte superior del mando se ubican los botones L1 y L2 a la izquierda y los botones R1 y R2 a la derecha.

Botón

Dominio

buttonA

controller.buttonA.released(callback, arg) — The A button

buttonB

controller.buttonB.released(callback, arg) — The B button

buttonDown

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

buttonUp

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

buttonL1

controller.buttonL1.released(callback, arg) — The L1 button

buttonL2

controller.buttonL2.released(callback, arg) — The L2 button

buttonL3

controller.buttonL3.released(callback, arg) — The L3 button

buttonR1

controller.buttonR1.released(callback, arg) — The R1 button

buttonR2

controller.buttonR2.released(callback, arg) — The R2 button

buttonR3

controller.buttonR3.released(callback, arg) — The R3 button

Parámetro

Descripción

callback

Una función definida previamente para ejecutarse cada vez que se suelte 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.

# Stop driving when R1 is released
def stop_driving():
    drivetrain.stop()

controller.buttonR1.released(stop_driving)

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

Uso:
Se puede utilizar uno de los objetos de eje disponibles con este método:

La parte frontal del controlador EXP muestra los números de los ejes del joystick resaltados en rojo. En la superficie del controlador, el joystick izquierdo está etiquetado como Eje 4 para el movimiento hacia la izquierda y la derecha, y Eje 3 para el movimiento hacia arriba y hacia abajo, mientras que el joystick derecho está etiquetado como Eje 1 para el movimiento hacia la izquierda y la derecha, y Eje 2 para el movimiento hacia arriba y hacia abajo.

Eje

Dominio

axis1

controller.axis1.changed(callback, arg) — The right joystick’s horizontal axis

axis2

controller.axis2.changed(callback, arg) — The right joystick’s vertical axis

axis3

controller.axis3.changed(callback, arg) — The left joystick’s vertical axis

axis4

controller.axis4.changed(callback, arg) — The left joystick’s horizontal axis

Parámetro

Descripción

callback

Una función definida previamente para ejecutarse cada vez que la posición del joystick cambie a lo largo del eje 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.

# Play a sound when the left joystick moves
def beep():
    brain.play_sound(SoundType.TADA)

controller.axis3.changed(beep)

Constructores#

Controller#

Controller creates a Controller object. Manually creating a Controller object is only needed when configuring a controller outside of VEXcode.

Usage:
Controller()

Parámetros

Descripción

Este constructor no tiene parámetros.

# Create a Controller object
my_controller = Controller()