|
| 1 | +package frc.robot.commands.drive; |
| 2 | + |
| 3 | +import choreo.Choreo; |
| 4 | +import choreo.trajectory.SwerveSample; |
| 5 | +import choreo.trajectory.Trajectory; |
| 6 | +import edu.wpi.first.math.geometry.Pose2d; |
| 7 | +import edu.wpi.first.wpilibj.Timer; |
| 8 | +import edu.wpi.first.wpilibj2.command.Command; |
| 9 | +import frc.robot.commands.auton.AutoCommandInterface; |
| 10 | +import frc.robot.subsystems.drive.DriveSubsystem; |
| 11 | +import java.util.Optional; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +public class DriveAutonCommand extends Command implements AutoCommandInterface { |
| 16 | + private final DriveSubsystem driveSubsystem; |
| 17 | + private Trajectory<SwerveSample> trajectory; |
| 18 | + private final Timer timer = new Timer(); |
| 19 | + private static final Logger logger = LoggerFactory.getLogger(DriveAutonCommand.class); |
| 20 | + private boolean isTherePath = false; |
| 21 | + private String trajectoryName; |
| 22 | + private boolean mirrorTrajectory = false; |
| 23 | + |
| 24 | + private boolean resetOdometry; |
| 25 | + private boolean lastPath; |
| 26 | + |
| 27 | + public DriveAutonCommand( |
| 28 | + DriveSubsystem driveSubsystem, |
| 29 | + String trajectoryName, |
| 30 | + boolean lastPath, |
| 31 | + boolean resetOdometry) { |
| 32 | + |
| 33 | + addRequirements(driveSubsystem); |
| 34 | + this.driveSubsystem = driveSubsystem; |
| 35 | + this.resetOdometry = resetOdometry; |
| 36 | + this.lastPath = lastPath; |
| 37 | + this.trajectoryName = trajectoryName; |
| 38 | + Optional<Trajectory<SwerveSample>> tempTrajectory = Choreo.loadTrajectory(trajectoryName); |
| 39 | + if (tempTrajectory.isPresent()) { |
| 40 | + trajectory = tempTrajectory.get(); |
| 41 | + isTherePath = true; |
| 42 | + } else { |
| 43 | + logger.error("Trajectory {} not found", trajectoryName); |
| 44 | + isTherePath = false; |
| 45 | + } |
| 46 | + timer.start(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void reassignAlliance() { |
| 51 | + mirrorTrajectory = driveSubsystem.shouldFlip(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void initialize() { |
| 56 | + Pose2d initialPose = new Pose2d(); |
| 57 | + if (trajectory.getInitialPose(mirrorTrajectory) != null) { |
| 58 | + initialPose = trajectory.getInitialPose(mirrorTrajectory).get(); |
| 59 | + } |
| 60 | + if (resetOdometry && trajectory.getInitialPose(mirrorTrajectory) != null) { |
| 61 | + driveSubsystem.resetOdometry(initialPose); |
| 62 | + } |
| 63 | + driveSubsystem.setEnableHolo(true); |
| 64 | + // driveSubsystem.recordAutoTrajectory(trajectory); |
| 65 | + driveSubsystem.resetHolonomicController(); |
| 66 | + driveSubsystem.grapherTrajectoryActive(true); |
| 67 | + timer.reset(); |
| 68 | + logger.info("Begin Trajectory: {}", trajectoryName); |
| 69 | + SwerveSample desiredState = trajectory.sampleAt(timer.get(), mirrorTrajectory).get(); |
| 70 | + driveSubsystem.calculateController(desiredState); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void execute() { |
| 75 | + SwerveSample desiredState = trajectory.sampleAt(timer.get(), mirrorTrajectory).get(); |
| 76 | + driveSubsystem.calculateController(desiredState); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean isFinished() { |
| 81 | + if (isTherePath) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + return timer.hasElapsed(trajectory.getTotalTime()); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void end(boolean interrupted) { |
| 89 | + driveSubsystem.setEnableHolo(false); |
| 90 | + // driveSubsystem.recordAutoTrajectory(null); |
| 91 | + |
| 92 | + if (!interrupted && !lastPath) { |
| 93 | + driveSubsystem.calculateController( |
| 94 | + trajectory.sampleAt(trajectory.getTotalTime(), mirrorTrajectory).get()); |
| 95 | + } else { |
| 96 | + driveSubsystem.drive(0, 0, 0); |
| 97 | + } |
| 98 | + |
| 99 | + driveSubsystem.grapherTrajectoryActive(false); |
| 100 | + logger.info("End Trajectory {}: {}", trajectoryName, timer.get()); |
| 101 | + } |
| 102 | +} |
0 commit comments