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

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
13package frc.robot.commands;
14import edu.wpi.first.wpilibj2.command.Command;
15import java.util.function.DoubleSupplier;
16
17// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
18import frc.robot.subsystems.Claw;
19
20 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
21
22/**
23 *
24 */
25public class CloseClaw extends Command {
26
27 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
28 private final Claw m_claw;
29
30 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
31
32 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
33
34
35 public CloseClaw(Claw subsystem) {
36
37
38 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
39 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
40
41 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
42 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
43
44 m_claw = subsystem;
45 addRequirements(m_claw);
46
47 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
48 }
49
50 // Called when the command is initially scheduled.
51 @Override
52 public void initialize() {
53 m_claw.close();
54 }
55
56 // Called every time the scheduler runs while the command is scheduled.
57 @Override
58 public void execute() {
59 }
60
61 // Called once the command ends or is interrupted.
62 @Override
63 public void end(boolean interrupted) {
64 m_claw.stop();
65 }
66
67 // Returns true when the command should end.
68 @Override
69 public boolean isFinished() {
70 return m_claw.isGripping();
71 }
72
73 @Override
74 public boolean runsWhenDisabled() {
75 // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
76 return false;
77
78 // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
79 }
80}
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();
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();
42}
43
44// Called once after isFinished returns true
45void CloseClaw::End(bool interrupted) {
46 m_claw->Stop();
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.
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.This command is finished when the limit switch in the Claw subsystem is tripped.
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.