Skip to content
This repository was archived by the owner on May 19, 2024. It is now read-only.

Commit 8c91868

Browse files
committed
fix: Use driver-relative heading for driving.
1 parent 55bfc14 commit 8c91868

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

Diff for: src/main/java/frc/robot/odometry/Odometry.java

+11-20
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class Odometry extends Subsystem {
4747
private final Field2d field;
4848

4949
/** List of functions to be called when pose is manually updated. */
50-
private final List<Consumer<Pose2d>> poseUpdateConsumers;
50+
private final List<Consumer<Rotation2d>> yawUpdateConsumers;
5151

5252
/** Creates a new instance of the odometry subsystem. */
5353
private Odometry() {
@@ -73,7 +73,7 @@ private Odometry() {
7373

7474
field = new Field2d();
7575

76-
poseUpdateConsumers = new ArrayList<Consumer<Pose2d>>();
76+
yawUpdateConsumers= new ArrayList<Consumer<Rotation2d>>();
7777
}
7878

7979
/**
@@ -110,7 +110,8 @@ public void addToShuffleboard(ShuffleboardTab tab) {
110110

111111
position.addDouble("X (m)", () -> getPosition().getX());
112112
position.addDouble("Y (m)", () -> getPosition().getY());
113-
position.addDouble("Rotation (deg)", () -> getPosition().getRotation().getDegrees());
113+
position.addDouble("Field Rotation (deg)", () -> getFieldRelativeHeading().getDegrees());
114+
position.addDouble("Driver Rotation (deg)", () -> getDriverRelativeHeading().getDegrees());
114115

115116
ShuffleboardLayout velocity = Telemetry.addColumn(tab, "Velocity");
116117

@@ -150,13 +151,7 @@ public Rotation2d getFieldRelativeHeading() {
150151
* alliance.
151152
*/
152153
public Rotation2d getDriverRelativeHeading() {
153-
Rotation2d fieldRelativeHeading = getFieldRelativeHeading();
154-
155-
if (AllianceFlipHelper.shouldFlip()) {
156-
return fieldRelativeHeading.plus(Rotation2d.fromDegrees(180));
157-
}
158-
159-
return fieldRelativeHeading;
154+
return Rotation2d.fromRotations(gyroscopeValues.yawRotations);
160155
}
161156

162157
/**
@@ -187,25 +182,21 @@ public void setRotation(Rotation2d rotation) {
187182
*
188183
* @param consumer consumer for when pose is manually updated.
189184
*/
190-
public void onPoseUpdate(Consumer<Pose2d> consumer) {
191-
poseUpdateConsumers.add(consumer);
185+
public void onYawUpdate(Consumer<Rotation2d> consumer) {
186+
yawUpdateConsumers.add(consumer);
192187
}
193188

194189
/**
195-
* Tares the rotation of the robot.
190+
* Zeroes the driver-relative rotation of the robot.
196191
*
197-
* @return a command that zeroes the rotation of the robot.
192+
* @return a command that zeroes the driver-relative rotation of the robot.
198193
*/
199194
public Command tare() {
200195
return Commands.runOnce(
201196
() -> {
202-
if (AllianceFlipHelper.shouldFlip()) {
203-
setRotation(Rotation2d.fromDegrees(180));
204-
} else {
205-
setRotation(Rotation2d.fromDegrees(0));
206-
}
197+
gyroscope.setYaw(0.0);
207198

208-
poseUpdateConsumers.forEach(consumer -> consumer.accept(getPosition()));
199+
yawUpdateConsumers.forEach(consumer -> consumer.accept(Rotation2d.fromDegrees(0)));
209200
});
210201
}
211202

Diff for: src/main/java/frc/robot/swerve/DriveCommand.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public DriveCommand(CommandXboxController driverController) {
5656

5757
headingSnapper = SnapRotation.to(Rotation2d.fromDegrees(90));
5858

59-
odometry.onPoseUpdate(newPose -> resetHeadingGoal());
59+
odometry.onYawUpdate(newPose -> resetHeadingGoal());
6060
}
6161

6262
@Override
@@ -78,7 +78,7 @@ public void execute() {
7878
}
7979

8080
if (request.isSnapping()) {
81-
setPositionHeadingGoal(headingSnapper.snap(request.fieldHeading()));
81+
setPositionHeadingGoal(headingSnapper.snap(request.driverHeading()));
8282
}
8383

8484
Rotation2d omega;
@@ -152,7 +152,7 @@ private ChassisSpeeds clampChassisSpeeds(ChassisSpeeds desiredChassisSpeeds) {
152152
* @return the reference heading to use with the heading motion profile.
153153
*/
154154
private Rotation2d getReferenceHeading() {
155-
return odometry.getFieldRelativeHeading();
155+
return odometry.getDriverRelativeHeading();
156156
}
157157

158158
/**

Diff for: src/main/java/frc/robot/swerve/DriveRequest.java

-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import edu.wpi.first.math.geometry.Rotation2d;
55
import edu.wpi.first.math.geometry.Translation2d;
66
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
7-
import frc.lib.AllianceFlipHelper;
87

98
public record DriveRequest(
109
DriveRequest.TranslationMode translationMode,
@@ -103,14 +102,4 @@ public Rotation2d omega() {
103102
public Rotation2d driverHeading() {
104103
return rotation().getAngle();
105104
}
106-
107-
public Rotation2d fieldHeading() {
108-
Rotation2d heading = driverHeading();
109-
110-
if (AllianceFlipHelper.shouldFlip()) {
111-
heading = heading.plus(Rotation2d.fromDegrees(180));
112-
}
113-
114-
return heading;
115-
}
116105
}

0 commit comments

Comments
 (0)