- FRC Game Manual
- FRC Game Q&A

# 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

Java

    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}

C++

    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 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 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.