Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
very much tested: cleanup getAutonomousCommand and leave comments for…
Browse files Browse the repository at this point in the history
… future years
  • Loading branch information
Ernie3 committed Apr 2, 2024
1 parent c43db27 commit c9f7a34
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
20 changes: 4 additions & 16 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.ProxyCommand;
import frc.robot.subsystems.drive.SwerveDrive;

/**
Expand Down Expand Up @@ -51,6 +49,9 @@ public void robotInit() {
// Instantiate our RobotContainer. This will perform all our button bindings,
// and put our autonomous chooser on the dashboard.
robotContainer = RobotContainer.getInstance();

// instantiate the autonomous routine
autonomousRoutine = robotContainer.getAutonomousCommand();
}

/**
Expand Down Expand Up @@ -98,20 +99,7 @@ public void disabledPeriodic() {
*/
@Override
public void autonomousInit() {
var selectedAutoRoutine = robotContainer.getAutonomousCommand();

if (selectedAutoRoutine != null) {
// for the end of the auto routine
var stopAllCmd = new InstantCommand(() -> {
RobotContainer.swerveDrive.stop();
RobotContainer.shooter.stopIndexer();
RobotContainer.shooter.stopShooting();
RobotContainer.intake.stop();
}, RobotContainer.swerveDrive, RobotContainer.shooter, RobotContainer.intake);

// get the auto routine as a proxy command so we are free to compose a
// sequential command group using it, run the auto routine then stop all motors
autonomousRoutine = new ProxyCommand(() -> selectedAutoRoutine).andThen(stopAllCmd);
if (autonomousRoutine != null) {
autonomousRoutine.schedule();
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,24 @@ public static RobotContainer getInstance() {

private SendableChooser<Command> autoChooser;

/**
* Gets the selected autonomous routine from SmartDashboard.
*
* @return the selected autonomous routine from the dropdown in SmartDashboard
*/
public Command getAutonomousCommand() {
return autoChooser.getSelected();
// get as a proxy command so we are free to compose a SequentialCommand group
// from it while avoiding a runtime exception
return new ProxyCommand(() -> {
var selected = autoChooser.getSelected();
return selected != null ? selected
: new InstantCommand(() -> DriverStation.reportWarning("Null autonomous was selected.", false));
}).andThen(new InstantCommand(() -> {
RobotContainer.swerveDrive.stop();
RobotContainer.shooter.stopIndexer();
RobotContainer.shooter.stopShooting();
RobotContainer.intake.stop();
}, RobotContainer.swerveDrive, RobotContainer.shooter, RobotContainer.intake));
}

private AutoBuilder autoBuilder;
Expand Down

0 comments on commit c9f7a34

Please sign in to comment.