The Command Framework
Robot code is made up of four main parts: triggers, coroutines, commands and mechanisms.
- Triggers
- A controller button, a sensor reading, a comparison you wrote yourself. Attach a command to a trigger, and when the trigger becomes true or false the command runs automatically.
- Coroutines
- The body of a command. It runs a step each loop and can wait for the next loop before carrying on.
- Commands
- Named actions. A command can require a mechanism, which means it owns it while it runs, or require none.
- Mechanisms
- The arm. The flywheel. The drivetrain. Each one is a class that
implements Mechanism, with its motors and sensors as private fields and its configuration done once, in the constructor.
The scheduler loop
None of the four does anything on its own. robotPeriodic() is called for you for as long as the robot has power. The gap between calls is 20 milliseconds, so it runs 50 times a second.
public class Robot extends OpModeRobot { @Override public void robotPeriodic() { Scheduler.getDefault().run(); }}One call does the whole job. It checks every trigger, starts and cancels commands from what it finds, and runs any background code you register.
Commands in a mechanism
We build most commands inside a mechanism. We will also build commands outside one that require no mechanism, such as a command that runs other commands together.
Take runFast(), a command inside Arm. While runFast runs, it owns the arm. This prevents multiple commands from running on the same motor at one time.
Every command has a priority. By default, a new command takes a mechanism only if its priority is the same or higher than the command already holding it. Every command in this workshop uses the same priority, so the new one always gets to run.
Canceling is not stopping
A mechanism can be given a default command, which the scheduler runs whenever nothing else has claimed it. Set one with setDefaultCommand(...) and give it a low priority, so anything can take the mechanism from it. Set none and an unclaimed mechanism has nothing running on it, which sends nothing at all to the motor.
Nothing commanding the motor is not the same as the motor being off. Phoenix keeps applying the last request it was given, so canceling a command does not stop hardware. Hardware Simulation shows it.
Where bindings live
A mechanism owns hardware: the motors and encoders for that mechanism. We create our mechanisms and the driver's controller in Robot.java, the same file as the scheduler call, so every OpMode can use them.
An OpMode decides what the robot does during one part of the match. That means which buttons do what while a driver is in control, or which routine runs in autonomous.
public class Robot extends OpModeRobot { // The robot's mechanisms. Public so OpModes can use them. public final Arm arm = new Arm(); public final Flywheel flywheel = new Flywheel(); // The driver's controller on port 0. OpModes bind its buttons through robot.driver. public final CommandXboxController driver = new CommandXboxController(0); @Override public void robotPeriodic() { Scheduler.getDefault().run(); }}There are three types of OpMode: Teleop, Autonomous and Utility. Each mode is a separate class marked @Teleop, @Autonomous or @Utility. That marking is how it shows up in the list on the driver station. Every mode is handed the Robot, so it can reach the arm, the flywheel and the controller.
@Teleop(name = "Teleop")public class MyTeleop extends PeriodicOpMode { public MyTeleop(Robot robot) { // Left trigger: push the arm up while held, stop when released. robot.driver.leftTrigger().whileTrue(robot.arm.runFast()).whileFalse(robot.arm.stop()); }}The bindings live in the constructor. The framework builds this class when someone picks Teleop, and throws it away on a mode switch. The bindings go with it, so no binding from auto ever fires during teleop.
Commands with no finish condition
Almost every command in this workshop is built with runRepeatedly(...). The body runs every loop. If you have seen a while loop before, this is one. Each pass runs the body, then waits for the next loop, 20 milliseconds later.
Nothing inside it decides when to stop, so ending it is somebody else's job. A trigger can do it. When the button is released, whileTrue cancels the command. To have something happen on the release, add it with whileFalse.
robot.driver.leftTrigger().whileTrue(robot.arm.runFast()).whileFalse(robot.arm.stop());Check your work
No editor yet. Take the left-trigger binding in MyTeleop above. Write down which command owns the arm before the press, while the trigger is held, and after the release.
You should have
- Before the press: nothing. The arm is unclaimed.
- While held:
runFast, started bywhileTrue. - After the release:
stop, started bywhileFalseasrunFastis canceled.
Check yourself
Robot.java calls Scheduler.getDefault().run() inside robotPeriodic(). What breaks if you delete that line?
One command has the arm. A button fires and a second command that also needs the arm gets scheduled. Both have the ordinary priority. Who ends up with the arm?
A routine has been stuck for eight seconds on runFast, which is built with runRepeatedly. Why?
No command is claiming the flywheel. What is the mechanism doing?