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

12public class CloseClaw extends CommandBase {
13
14    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
15        private final Claw m_claw;
16
17    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_DECLARATIONS
18
19    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
20
21
22    public CloseClaw(Claw subsystem) {
23
24
25    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=CONSTRUCTORS
26        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
27
28    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=VARIABLE_SETTING
29        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
30
31        m_claw = subsystem;
32        addRequirements(m_claw);
33
34    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
35    }
36
37    // Called when the command is initially scheduled.
38    @Override
39    public void initialize() {
40        m_claw.open(); // (1)
41    }
42
43    // Called every time the scheduler runs while the command is scheduled.
44    @Override
45    public void execute() {
46    }
47
48    // Called once the command ends or is interrupted.
49    @Override
50    public void end(boolean interrupted) {
51        m_claw.stop(); // (3)
52    }
53
54    // Returns true when the command should end.
55    @Override
56    public boolean isFinished() {
57        return m_claw.isGripping(); // (2)
58    }
59
60    @Override
61    public boolean runsWhenDisabled() {
62        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
63        return false;
64
65    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DISABLED
66    }
67}

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.