Skip to content

Commit 2592a4e

Browse files
committed
fix face in same direction paths
1 parent 58112c9 commit 2592a4e

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/main/java/frc/robot/lib/path/RobotPath.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.ArrayList;
99
import java.util.Collections;
1010
import java.util.List;
11+
import java.util.concurrent.atomic.AtomicReference;
1112
import java.util.function.Supplier;
1213

1314
import org.apache.commons.csv.CSVFormat;
@@ -16,6 +17,7 @@
1617

1718
import edu.wpi.first.wpilibj.Filesystem;
1819
import edu.wpi.first.wpilibj.Timer;
20+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1921
import edu.wpi.first.math.geometry.Pose2d;
2022
import edu.wpi.first.math.geometry.Rotation2d;
2123
import edu.wpi.first.math.geometry.Translation2d;
@@ -83,13 +85,17 @@ public Command getPathCommand(boolean faceInPathDirection, boolean stopAtEnd) {
8385
}
8486
hs.reset();
8587
// 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();
8991
Command command = dt.createAutoCommand(trajectory, desiredHeading);
9092
if (stopAtEnd) {
9193
command = command.andThen(new InstantCommand(dt::stop, dt));
9294
}
95+
if (!faceInPathDirection) {
96+
command = new InstantCommand(() -> headingRef.set(getRotationOfDrivetrain(dt))).andThen(command);
97+
SmartDashboard.putNumber("Desired Path Heading", headingRef.get().getDegrees());
98+
}
9399
return command;
94100
}
95101

@@ -263,6 +269,15 @@ public static File getPathFile(String pathName) {
263269
.toFile();
264270
}
265271

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+
266281
private static class HeadingSupplier {
267282
private Trajectory trajectory;
268283
private Timer timer;

0 commit comments

Comments
 (0)