Crear comandos simples

Importante

Esta documentación describe el uso de la biblioteca heredada basada en comandos. Si bien esta documentación se ha conservado para ayudar a los equipos que aún no lo han hecho, se recomienda a los equipos encarecidamente migrar a la nueva librería basada en comandos <docs/software/commandbased/index:Command-Based Programming>`.

Este artículo describe el formato básico de un comando y muestra un ejemplo de cómo crear un comando para manejar su robot con Joysticks.

Formato de comando básico

Para implementar un comando, se anulan varios métodos de la clase del comando WPILib. La mayoría de los métodos son de placa de caldera y, a menudo, pueden ignorarse, pero existen para una máxima flexibilidad cuando la necesita. Hay varias partes en esta clase de comando básico:

public class MyCommandName extends Command {

   /*
    * 1.   Constructor - Might have parameters for this command such as target positions of devices. Should also set the name of the command for debugging purposes.
    *  This will be used if the status is viewed in the dashboard. And the command should require (reserve) any devices is might use.
    */
    public MyCommandName() {
       super("MyCommandName");
        requires(elevator);
    }

    //    initialize() - This method sets up the command and is called immediately before the command is executed for the first time and every subsequent time it is started .
    //  Any initialization code should be here.
    protected void initialize() {
    }

    /*
     *   execute() - This method is called periodically (about every 20ms) and does the work of the command. Sometimes, if there is a position a
     *  subsystem is moving to, the command might set the target position for the subsystem in initialize() and have an empty execute() method.
     */
    protected void execute() {
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return false;
    }
}

Ejemplo de comando simple

Este ejemplo ilustra un comando simple que conducirá el robot usando la unidad de tanque con valores proporcionados por los joysticks.

public class DriveWithJoysticks extends Command {

    public DriveWithJoysticks() {
       requires(drivetrain);// drivetrain is an instance of our Drivetrain subsystem
    }

    protected void initialize() {
    }

    /*
     * execute() - In our execute method we call a tankDrive method we have created in our subsystem. This method takes two speeds as a parameter which we get from methods in the OI class.
     * These methods abstract the joystick objects so that if we want to change how we get the speed later we can do so without modifying our commands
     * (for example, if we want the joysticks to be less sensitive, we can multiply them by .5 in the getLeftSpeed method and leave our command the same).
     */
    protected void execute() {
       drivetrain.tankDrive(oi.getLeftSpeed(), oi.getRightSpeed());
    }

    /*
     * isFinished - Our isFinished method always returns false meaning this command never completes on it's own. The reason we do this is that this command will be set as the default command for the subsystem. This means that whenever the subsystem is not running another command, it will run this command. If any other command is scheduled it will interrupt this command, then return to this command when the other command completes.
     */
    protected boolean isFinished() {
        return false;
    }

    protected void end() {
    }

    protected void interrupted() {
    }
}