Controlador#
Introducción#
El Brain IQ (2.ª generación) se puede conectar a un mando IQ (2.ª generación) o a un mando IQ (1.ª generación). Ambos mandos cuentan con dos joysticks analógicos y varios botones que el Brain puede usar para detectar movimientos y pulsaciones.
For the examples below, the configured controller will be named controller and will be used in all subsequent examples throughout this API documentation when referring to Controller class methods.
A continuación se muestra una lista de todos los métodos disponibles:
Acciones: activa o desactiva las acciones programadas por el controlador.
remote_control_code_enabled– Enable or disable Controller configured actions.
Getters: leen los estados de los botones y las posiciones del joystick.
.pressing– Returns whether the specified button is being pressed..position– Returns the position of the joystick’s specified axis.
Devolución de llamada: ejecuta el código cuando los botones o joysticks cambian de estado.
.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.
Constructores: inicializan y configuran manualmente el controlador.
Controller– Create a controller.
Comportamiento#
remote_control_code_enabled#
remote_control_code_enabled is a variable that can be set to a boolean that enables or disables Controller configured actions from the Devices menu. The Controller is enabled by default. It can be set to either of the following:
True— Enable Controller configured actions.False— Disable Controller configured actions.
Usage:
remote_control_code_enabled = False
# Drive forward or backward using the left joystick
remote_control_code_enabled = False
while True:
if controller.axisA.position() > 0:
drivetrain.drive(FORWARD)
elif controller.axisA.position() < 0:
drivetrain.drive(REVERSE)
# Press E Up to use the controller configured actions
elif controller.buttonEUp.pressing():
break
else:
drivetrain.stop()
wait(20, MSEC)
remote_control_code_enabled = True
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 buttonEDown (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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
# Turn right while E Up is held
while True:
if controller.buttonEUp.pressing():
drivetrain.turn(RIGHT)
else:
drivetrain.stop()
.position#
.position returns the position of the joystick’s specified axis as an integer from –100 to 100, representing a percentage. This method must be called on a specific axis object, such as axisA (see full list of axis objects below).
Uso:
Se puede utilizar uno de los cuatro ejes disponibles con este método, como se muestra a continuación:
Eje |
Dominio |
|---|---|
|
|
|
|
|
|
|
|
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.axisB.position() > 10:
drivetrain.turn(RIGHT)
elif controller.axisB.position() < -10:
drivetrain.turn(LEFT)
else:
drivetrain.stop()
wait(20, MSEC)
Llamar de vuelta#
.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 buttonEDown – (see full list of button objects below).
Uso:
Uno de los objetos de botón disponibles se puede utilizar con este método, como se muestra a continuación:
Botón |
Dominio |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 argumentos para la función de devolución de llamada. Funciones con parámetros para más información. |
# Drive forward when E Up is pressed
def drive_forward():
drivetrain.drive_for(FORWARD, 100, MM)
controller.buttonEUp.pressed(drive_forward)
.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 buttonEDown – (see full list of button objects below).
Uso:
Uno de los objetos de botón disponibles se puede utilizar con este método, como se muestra a continuación:
Botón |
Dominio |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 argumentos para la función de devolución de llamada. Funciones con parámetros para más información. |
# Drive backward when F Down is released
def back_up():
drivetrain.drive_for(REVERSE, 100, MM)
controller.buttonFDown.released(back_up)
.changed#
.changed registers a function to be called when the joystick’s position changes. This method must be called on a specific axis object, such as axisA (see full list of axis objects below).
Uso:
Se puede utilizar uno de los cuatro ejes disponibles con este método, como se muestra a continuación:
Eje |
Dominio |
|---|---|
|
|
|
|
|
|
|
|
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 Funciones con parámetros para obtener más información. |
# Play a sound when the right joystick moves
def beep():
brain.play_sound(SoundType.TADA)
wait(1, SECONDS)
controller.axisD.changed(beep)
Constructores#
Constructors are used to manually create Controller objects, which are necessary for configuring a controller outside of VEXcode.
Controller#
Controller creates a controller.
Usage:
Controller()
Parámetros |
Descripción |
|---|---|
Este constructor no tiene parámetros. |
# Create a Controller object
my_controller = Controller()