Commands Package

Purpose

To have the Robot perform actions, we create commands using the subsystems and sensors we have created. Each command extends the FRC created abstract class, Command. The necessary methods to override are: initialize() execute() isFinished() end() interrupted() and the constructor is also important. Most commands are simple, I will walk through how to make an Intake command.

Constructor

The constructor of a command is like any other constructor for a class, it should take in any necessary infomation for the command to run (E.X. amount of time to run). But, an important part of the constructor for commands is that it should call the method requires() method which intakes a subsystem that this command is going to use. Example:

private double runTime;
public Intake(double runTime) {
     this.runTime = runTime;
     requires(Robot.gripper);
}

initialize()

The initialize method only runs once and should set any sensors or robot state info to the values they should be at the start of the method. Example:

@Override
protected void initialize() {
     Robot.isIntaking = true;
}

execute()

The execute method runs repeatedly with the robot, every 20 milliseconds to be precise, and should contain the code for what the robot should be doing, using the methods we write for that subsystem. Example:

@Override
protected void execute() {
     Robot.gripper.intake();
}

isFinished()

The isFinished method also runs repeatedly and returns a boolean, true if the command should stop, false otherwise. Commands which are supposed to run while a button is held down, should return false so that they continue. Autonomous commands and commands which are expected to run after a click of a button should return some kind of boolean which changes based of time or a sensor. If a command is meant to only run once, it should isFinished should simply return true. In this intake command, it should finish when either the runTime is complete or perhaps when a limitswitch tells us the gripper has a ball. The method timeSinceInitalized() is a built in method in the Command class which returns how long it has been since the initalize() method was called. Example:

@Override
protected boolean isFinished() {
     return timeSinceInitalized() > this.runTime || Robot.limitSwitches.isBallIn();
}

end()

The end method runs once and is called directly after isFinished() returns false. This method should do whats needed to stop the command, such as stopping motors or subsystems, and change any needed Robot states. Example:

@Override
protected void end() {
     Robot.gripper.stop();
     Robot.isIntaking = false;
}

interrupted()

The interrupted method runs if cancel() is called on the command or another command is called which requires the same subsystem as the current. The code for this method should be the exact same for every single command of ours, it should simply call end(). Example:

@Override
protected void interrupted() {
     end();
}


© All Rights Reserved.