frc1418 / 2024-robot

Our code for the 2024 robot
Other
1 stars 10 forks source link

Move all command declarations to the subsystems, out from RobotContainer #22

Open ArchdukeTim opened 1 month ago

ArchdukeTim commented 1 month ago

It can help to keep your code organized if your commands live within the subsystems and are exposed through methods, rather than being created inside the RobotContainer where they're bound.

For example, in RobotContainer you have

feedSubsystem.setDefaultCommand(new RunCommand(() -> {
      feedSubsystem.feed(0);
    }, feedSubsystem));

    feedInButton.whileTrue(new RunCommand(() -> {
      feedSubsystem.feed(0.25);
    }, feedSubsystem));

    feedOutButton.whileTrue(new RunCommand(() -> {
      feedSubsystem.feed(-0.1);
    }, feedSubsystem));

Instead you can (or even, should) have in FeedSubsystem:

 public FeedSubsystem(CANSparkMax feedMotor) {
        this.feedMotor = feedMotor;
        setDefaultCommand(run(() -> feed(0)); // or runOnce(() -> feed(0)).andThen(Commands.idle());
    }

public Command feedIn() {
  return run(() -> feed(0.25));
}

public Command feedOut() {
  return run(() -> feed(-.1));
}

Then if you want to reuse the feedIn command (which you do multiple times in RobotContainer) you can just call FeedSubsystem.feedIn() instead of having to create a new command each time.

ArchdukeTim commented 1 month ago

This is very good practice for new programmers too