|
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.Collections; |
10 | 10 | import java.util.List; |
| 11 | +import java.util.concurrent.atomic.AtomicReference; |
11 | 12 | import java.util.function.Supplier; |
12 | 13 |
|
13 | 14 | import org.apache.commons.csv.CSVFormat; |
|
16 | 17 |
|
17 | 18 | import edu.wpi.first.wpilibj.Filesystem; |
18 | 19 | import edu.wpi.first.wpilibj.Timer; |
| 20 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
19 | 21 | import edu.wpi.first.math.geometry.Pose2d; |
20 | 22 | import edu.wpi.first.math.geometry.Rotation2d; |
21 | 23 | import edu.wpi.first.math.geometry.Translation2d; |
@@ -83,13 +85,17 @@ public Command getPathCommand(boolean faceInPathDirection, boolean stopAtEnd) { |
83 | 85 | } |
84 | 86 | hs.reset(); |
85 | 87 | // We want the robot to stay facing the same direction (in this case), so save |
86 | | - // the current heading |
87 | | - Rotation2d heading = Rotation2d.fromDegrees(dt.getHeadingDeg()); |
88 | | - Supplier<Rotation2d> desiredHeading = (!faceInPathDirection) ? () -> heading : () -> hs.sample(); |
| 88 | + // the current heading (make sure to update at the start of the command) |
| 89 | + AtomicReference<Rotation2d> headingRef = new AtomicReference<>(getRotationOfDrivetrain(dt)); |
| 90 | + Supplier<Rotation2d> desiredHeading = (!faceInPathDirection) ? () -> headingRef.get() : () -> hs.sample(); |
89 | 91 | Command command = dt.createAutoCommand(trajectory, desiredHeading); |
90 | 92 | if (stopAtEnd) { |
91 | 93 | command = command.andThen(new InstantCommand(dt::stop, dt)); |
92 | 94 | } |
| 95 | + if (!faceInPathDirection) { |
| 96 | + command = new InstantCommand(() -> headingRef.set(getRotationOfDrivetrain(dt))).andThen(command); |
| 97 | + SmartDashboard.putNumber("Desired Path Heading", headingRef.get().getDegrees()); |
| 98 | + } |
93 | 99 | return command; |
94 | 100 | } |
95 | 101 |
|
@@ -263,6 +269,15 @@ public static File getPathFile(String pathName) { |
263 | 269 | .toFile(); |
264 | 270 | } |
265 | 271 |
|
| 272 | + private static final Rotation2d getRotationOfDrivetrain(DrivetrainInterface dt) { |
| 273 | + return |
| 274 | + dt instanceof SwerveDriveInterface ? |
| 275 | + ((SwerveDriveInterface)dt).getOdometry().getPoseMeters().getRotation() : |
| 276 | + dt instanceof DifferentialDriveInterface ? |
| 277 | + ((DifferentialDriveInterface)dt).getOdometry().getPoseMeters().getRotation() : |
| 278 | + Rotation2d.fromDegrees(dt.getHeadingDeg()); |
| 279 | + } |
| 280 | + |
266 | 281 | private static class HeadingSupplier { |
267 | 282 | private Trajectory trajectory; |
268 | 283 | private Timer timer; |
|
0 commit comments