Escribiendo el Código para un Comando

Las clases de subsistemas hacen que los mecanismos de su robot se muevan, pero para que se detenga en el momento adecuado y realice operaciones más complejas, debe escribir Comandos. Previamente en: writing the code for a subsystem 1 desarrollamos el código para el subsistema Claw en un robot para iniciar la apertura, cierre o parada de la garra. Ahora escribiremos el código para un comando que realmente hará funcionar el motor de la garra en el momento adecuado para que la garra se abra y se cierre. Nuestro ejemplo de garra es un mecanismo muy simple en el que hacemos funcionar el motor durante 1 segundo para abrirlo o hasta que se dispara el interruptor de límite para cerrarlo.

Comando de cierre de garras en RobotBuilder

../../../../../_images/writing-command-code-1.png

Esta es la definición del comando CloseClaw en RobotBuilder. Observa que requiere el subsistema “claw”. Esto se explica en el siguiente paso.

Clase CloseClaw generada

11 // ROBOTBUILDER TYPE: Command.
12
13 package frc.robot.commands;
14 import edu.wpi.first.wpilibj2.command.CommandBase;
15
16 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
17 import frc.robot.subsystems.Claw;
18
19     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
20
21 /**
22 *
23 */
24 public class CloseClaw extends CommandBase {
25
26     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
27         private final Claw m_claw;
28
29     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
30
31     // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
32
33
34     public CloseClaw(Claw subsystem) {
35
36
37     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
38         // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
39
40     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
41         // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
42
43         m_claw = subsystem;
44         addRequirements(m_claw);
45
46     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
47     }
48
49     // Called when the command is initially scheduled.
50     @Override
51     public void initialize() {
52         m_claw.close(); // (1)
53     }
54
55     // Called every time the scheduler runs while the command is scheduled.
56     @Override
57     public void execute() {
58     }
59
60     // Called once the command ends or is interrupted.
61     @Override
62     public void end(boolean interrupted) {
63         m_claw.stop(); // (3)
64     }
65
66     // Returns true when the command should end.
67     @Override
68     public boolean isFinished() {
69         return m_claw.isGripping(); // (2)
70     }
71
72     @Override
73     public boolean runsWhenDisabled() {
74         // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
75         return false;
76
77     // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
78     }
79 }
11// ROBOTBUILDER TYPE: Command.
12
13// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
14
15#include "commands/CloseClaw.h"
16
17CloseClaw::CloseClaw(Claw* m_claw)
18:m_claw(m_claw){
19
20    // Use AddRequirements() here to declare subsystem dependencies
21    // eg. AddRequirements(m_Subsystem);
22    SetName("CloseClaw");
23    AddRequirements({m_claw});
24
25// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTOR
26
27}
28
29// Called just before this Command runs the first time
30void CloseClaw::Initialize() {
31    m_claw->Close(); // (1)
32}
33
34// Called repeatedly when this Command is scheduled to run
35void CloseClaw::Execute() {
36
37}
38
39// Make this return true when this Command no longer needs to run execute()
40bool CloseClaw::IsFinished() {
41    return m_claw->IsGripping(); // (2)
42}
43
44// Called once after isFinished returns true
45void CloseClaw::End(bool interrupted) {
46    m_claw->Stop(); // (3)
47}
48
49bool CloseClaw::RunsWhenDisabled() const {
50    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
51    return false;
52
53    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
54}

RobotBuilder generará los archivos de clase para el comando CloseClaw. El comando representa el comportamiento de la garra, es decir el funcionamiento en el tiempo. Para hacer funcionar este mecanismo de garra tan simple, el motor necesita funcionar en la dirección de cierre,. El subsistema Claw tiene métodos para poner en marcha el motor en la dirección correcta y para pararlo. La responsabilidad de los comandos es hacer funcionar el motor durante el tiempo correcto. Las líneas de código que se muestran en los recuadros se añaden para añadir este comportamiento.

  1. Inicia el movimiento del motor de la garra en la dirección de cierre llamando al método Close() que fue añadido al subsistema Claw en el método CloseClaw Initialize.

  2. This command is finished when the limit switch in the Claw subsystem is tripped.

  3. El método End() se llama cuando el comando ha terminado. En este caso, el motor está parado desde que el tiempo se ha agotado.