This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update auto chooser to use Trailblazer
- Loading branch information
1 parent
542b4fb
commit 5179f0d
Showing
7 changed files
with
68 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,27 @@ | ||
package frc.robot.autos; | ||
|
||
import com.pathplanner.lib.auto.AutoBuilder; | ||
import dev.doglog.DogLog; | ||
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Commands; | ||
import frc.robot.fms.FmsSubsystem; | ||
import frc.robot.autos.trailblazer.Trailblazer; | ||
import frc.robot.robot_manager.RobotManager; | ||
import java.util.EnumSet; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public class AutoChooser { | ||
private final SendableChooser<AutoSelection> chooser = new SendableChooser<>(); | ||
private final Set<String> brokenAutoNames = new HashSet<>(); | ||
private final Command doNothingAuto; | ||
private Optional<Command> cachedCommand = Optional.empty(); | ||
private String cachedAutoName = ""; | ||
private final SendableChooser<BaseAuto> chooser = new SendableChooser<>(); | ||
|
||
public AutoChooser(AutoCommands commands) { | ||
public AutoChooser(RobotManager robotManager, Trailblazer trailblazer) { | ||
SmartDashboard.putData("Autos/SelectedAuto", chooser); | ||
chooser.setDefaultOption(AutoSelection.DO_NOTHING.toString(), AutoSelection.DO_NOTHING); | ||
|
||
for (AutoSelection selection : EnumSet.allOf(AutoSelection.class)) { | ||
chooser.addOption(selection.toString(), selection); | ||
chooser.addOption(selection.toString(), selection.auto.apply(robotManager, trailblazer)); | ||
} | ||
|
||
this.doNothingAuto = commands.doNothingCommand(); | ||
chooser.setDefaultOption( | ||
AutoSelection.DO_NOTHING.toString(), | ||
AutoSelection.DO_NOTHING.auto.apply(robotManager, trailblazer)); | ||
} | ||
|
||
public Command getAutoCommand() { | ||
var selection = getAutoSelection(); | ||
|
||
String autoName = FmsSubsystem.isRedAlliance() ? selection.redAutoName : selection.blueAutoName; | ||
|
||
// When the name of the auto changes (either from changing alliance color or from making a new | ||
// selection), clear out the cached command | ||
if (!autoName.equals(cachedAutoName)) { | ||
cachedCommand = Optional.empty(); | ||
} | ||
|
||
cachedAutoName = autoName; | ||
|
||
DogLog.log( | ||
"Autos/BrokenAutoNames", brokenAutoNames.toArray(new String[brokenAutoNames.size()])); | ||
DogLog.log("Autos/SelectedAutoName", selection); | ||
DogLog.log("Autos/SelectedAutoFileName", autoName.equals("") ? "(none)" : autoName); | ||
|
||
if (cachedCommand.isPresent()) { | ||
return cachedCommand.get(); | ||
} | ||
|
||
if (autoName.equals("")) { | ||
var command = Commands.print("No auto path provided, running do nothing auto"); | ||
cachedCommand = Optional.of(command.andThen(doNothingAuto)); | ||
return command; | ||
} | ||
|
||
try { | ||
var command = | ||
AutoBuilder.buildAuto(autoName) | ||
.withName("Auto__" + selection.toString() + "__" + autoName); | ||
|
||
cachedCommand = Optional.of(command); | ||
return command; | ||
} catch (Exception e) { | ||
var alliance = FmsSubsystem.isRedAlliance() ? "red" : "blue"; | ||
brokenAutoNames.add(selection.toString() + " - " + alliance + ": " + autoName); | ||
} | ||
|
||
var command = Commands.print("Auto failed to load, check Autos/BrokenAutoNames"); | ||
cachedCommand = Optional.of(command); | ||
return command; | ||
} | ||
|
||
public AutoSelection getAutoSelection() { | ||
var selected = Optional.ofNullable(chooser.getSelected()); | ||
|
||
return selected.orElse(AutoSelection.DO_NOTHING); | ||
public BaseAuto getSelectedAuto() { | ||
return chooser.getSelected(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
package frc.robot.autos; | ||
|
||
import frc.robot.autos.amp.Op345Auto; | ||
import frc.robot.autos.trailblazer.Trailblazer; | ||
import frc.robot.robot_manager.RobotManager; | ||
import java.util.function.BiFunction; | ||
|
||
public enum AutoSelection { | ||
DO_NOTHING("", ""), | ||
DO_NOTHING(DoNothingAuto::new), | ||
|
||
AMP_5_PIECE("Red Amp 5 Piece", "Blue Amp 5 Piece"), | ||
SOURCE_4_PIECE("Red Source 4 Piece", "Blue Source 4 Piece"), | ||
SOURCE_OFFSET("Red Source Offset", "Blue Source Offset"), | ||
MID_5_PIECE("Red 5 Piece 6 to 1", "Blue 5 Piece 6 to 1"), | ||
OP("Red OP", "Blue OP"); | ||
OP(Op345Auto::new); | ||
|
||
public final String redAutoName; | ||
public final String blueAutoName; | ||
public final BiFunction<RobotManager, Trailblazer, BaseAuto> auto; | ||
|
||
private AutoSelection(String redAutoName, String blueAutoName) { | ||
this.redAutoName = redAutoName; | ||
this.blueAutoName = blueAutoName; | ||
private AutoSelection(BiFunction<RobotManager, Trailblazer, BaseAuto> auto) { | ||
this.auto = auto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,21 @@ | ||
package frc.robot.autos; | ||
|
||
import com.pathplanner.lib.auto.AutoBuilder; | ||
import com.pathplanner.lib.auto.NamedCommands; | ||
import com.pathplanner.lib.util.HolonomicPathFollowerConfig; | ||
import com.pathplanner.lib.util.PIDConstants; | ||
import com.pathplanner.lib.util.ReplanningConfig; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Commands; | ||
import frc.robot.localization.LocalizationSubsystem; | ||
import frc.robot.robot_manager.RobotCommands; | ||
import frc.robot.autos.trailblazer.Trailblazer; | ||
import frc.robot.robot_manager.RobotManager; | ||
import frc.robot.swerve.SwerveSubsystem; | ||
import frc.robot.util.scheduling.LifecycleSubsystem; | ||
import frc.robot.util.scheduling.SubsystemPriority; | ||
|
||
public class Autos extends LifecycleSubsystem { | ||
|
||
private static Command wrapAutoEvent(String commandName, Command command) { | ||
return Commands.sequence( | ||
Commands.print("[COMMANDS] Starting auto event " + commandName), | ||
command.deadlineWith( | ||
Commands.waitSeconds(5) | ||
.andThen( | ||
Commands.print( | ||
"[COMMANDS] Auto event " | ||
+ commandName | ||
+ " has been running for 5+ seconds!"))), | ||
Commands.print("[COMMANDS] Finished auto event " + commandName)) | ||
.handleInterrupt(() -> System.out.println("[COMMANDS] Cancelled auto event " + commandName)) | ||
.withName(commandName); | ||
} | ||
|
||
private static void registerCommand(String eventName, Command command) { | ||
NamedCommands.registerCommand(eventName, wrapAutoEvent("Auto_" + eventName, command)); | ||
} | ||
|
||
private final RobotCommands robotCommands; | ||
private final AutoChooser autoChooser; | ||
private final AutoCommands autoCommands; | ||
private final SwerveSubsystem swerve; | ||
private final LocalizationSubsystem localization; | ||
|
||
public Autos( | ||
RobotCommands robotCommands, | ||
RobotManager robotManager, | ||
SwerveSubsystem swerve, | ||
LocalizationSubsystem localization) { | ||
public Autos(RobotManager robotManager, Trailblazer trailblazer) { | ||
super(SubsystemPriority.AUTOS); | ||
|
||
this.robotCommands = robotCommands; | ||
this.swerve = swerve; | ||
this.localization = localization; | ||
|
||
autoCommands = new AutoCommands(robotCommands, robotManager); | ||
|
||
autoChooser = new AutoChooser(autoCommands); | ||
|
||
AutoBuilder.configureHolonomic( | ||
localization::getPose, | ||
localization::resetPose, | ||
swerve::getRobotRelativeSpeeds, | ||
(robotRelativeSpeeds) -> { | ||
swerve.setRobotRelativeAutoSpeeds(robotRelativeSpeeds); | ||
}, | ||
new HolonomicPathFollowerConfig( | ||
new PIDConstants(4.0, 0.0, 0.0), | ||
new PIDConstants(2.5, 0.0, 0.0), | ||
4.4, | ||
0.387, | ||
new ReplanningConfig(true, true)), | ||
() -> false, | ||
swerve); | ||
|
||
registerCommand("speakerShot", autoCommands.speakerShotWithTimeout()); | ||
registerCommand("intakeAssist", robotCommands.intakeAssistCommand()); | ||
registerCommand("dynamic5Piece", autoCommands.dynamicAmp5PieceCommand()); | ||
registerCommand("waitingSpeakerCommand", robotCommands.waitSpeakerCommand()); | ||
autoChooser = new AutoChooser(robotManager, trailblazer); | ||
} | ||
|
||
public Command getAutoCommand() { | ||
return autoChooser.getAutoCommand(); | ||
} | ||
|
||
@Override | ||
public void disabledPeriodic() { | ||
// Constantly load the selected auto to avoid lag on auto init | ||
getAutoCommand(); | ||
return autoChooser.getSelectedAuto().getAutoCommand(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package frc.robot.autos; | ||
|
||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Commands; | ||
import frc.robot.autos.trailblazer.Trailblazer; | ||
import frc.robot.robot_manager.RobotManager; | ||
|
||
public class DoNothingAuto extends BaseAuto { | ||
public DoNothingAuto(RobotManager robotManager, Trailblazer trailblazer) { | ||
super(robotManager, trailblazer); | ||
} | ||
|
||
@Override | ||
protected Command getBlueAutoCommand() { | ||
return Commands.none(); | ||
} | ||
|
||
@Override | ||
protected Command getRedAutoCommand() { | ||
return Commands.none(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters