A monolithic TeleOp loop works until three mechanisms need to move at once. Command-based programming separates what a mechanism can do (subsystem) from what the robot is doing right now (command).
The three conceptsLink to this section
- Subsystem — owns hardware, exposes intent, runs periodic().
- Command — a small unit of behaviour with initialize, execute, end, isFinished.
- Scheduler — runs commands, enforcing that two commands never own the same subsystem.
A commandLink to this section
public class RaiseLift extends CommandBase {
private final Lift lift;
private final int target;
public RaiseLift(Lift lift, int target) {
this.lift = lift;
this.target = target;
addRequirements(lift);
}
@Override public void initialize() { lift.setTarget(target); }
@Override public boolean isFinished() { return lift.atTarget(); }
@Override public void end(boolean interrupted) { lift.hold(); }
}Composing autonomousLink to this section
new SequentialCommandGroup(
new FollowPath(drive, toBackboard),
new ParallelCommandGroup(new RaiseLift(lift, HIGH), new AimArm(arm)),
new ScoreSample(claw),
new FollowPath(drive, toPark)
);