Writing the Code for a Command

Subsystem classes get the mechanisms on your robot moving, but to get it to stop at the right time and sequence through more complex operations you write Commands. Previously in writing the code for a subsystem we developed the code for the Claw subsystem on a robot to start the claw opening, closing, or to stop moving. Now we will write the code for a command that will actually run the claw motor for the right time to get the claw to open and close. Our claw example is a very simple mechanism where we run the motor for 1 second to open it or until the limit switch is tripped to close it.

Close Claw Command in RobotBuilder

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

This is the definition of the CloseClaw command in RobotBuilder. Notice that it requires the Claw subsystem. This is explained in the next step.

Generated CloseClaw Class

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.open(); // (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 }

RobotBuilder will generate the class files for the CloseClaw command. The command represents the behavior of the claw, that is the operation over time. To operate this very simple claw mechanism the motor needs to operate in the close direction,. The Claw subsystem has methods to start the motor running in the right direction and to stop it. The commands responsibility is to run the motor for the correct time. The lines of code that are shown in the boxes are added to add this behavior.

  1. Start the claw motor moving in the closing direction by calling the Close() method that was added to the Claw subsystem in the CloseClaw Initialize method.

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

  3. The End() method is called when the command is finished and is a place to clean up. In this case, the motor is stopped since the time has run out.