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
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.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 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.
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.This command is finished when the limit switch in the Claw subsystem is tripped.
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.