Skip to content

Commit

Permalink
Fix zero gyro
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenTomato5 committed Oct 7, 2024
1 parent 70e1184 commit f74061b
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/main/java/frc/robot/subsystems/drive/GyroIONavx2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kauailabs.navx.frc.AHRS;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.SerialPort;
import java.util.OptionalDouble;
Expand All @@ -14,15 +15,18 @@ public class GyroIONavx2 implements GyroIO {
private final Queue<Double> yawPositionQueue;
private final Queue<Double> yawTimestampQueue;

private Rotation3d offset;

public GyroIONavx2() {
navx = new AHRS(SerialPort.Port.kUSB1);
navx.enableBoardlevelYawReset(true);
navx.reset();
offset = new Rotation3d();

yawTimestampQueue = HybridOdometryThread.getInstance().makeTimestampQueue();
yawPositionQueue = HybridOdometryThread.getInstance()
.registerSignal(() -> {
if (navx.isConnected()) {
return OptionalDouble.of(navx.getYaw());
return OptionalDouble.of(navx.getRotation3d().minus(offset).getAngle());
} else {
return OptionalDouble.empty();
}
Expand All @@ -31,18 +35,15 @@ public GyroIONavx2() {

@Override
public void zero() {
System.out.println("ahhaha");
navx.reset();
navx.zeroYaw();
System.out.println(navx.getYaw());
this.offset = navx.getRotation3d();
}

@Override
public void updateInputs(GyroIOInputs inputs) {
inputs.connected = navx.isConnected();
inputs.yawPosition = Rotation2d.fromDegrees(navx.getYaw());
inputs.yawPosition = Rotation2d.fromRadians(navx.getRotation3d().minus(offset).getAngle());
inputs.yawVelocityRadPerSec = Units.degreesToRadians(navx.getRate());
inputs.yawPosDeg = navx.getYaw();
inputs.yawPosDeg = Units.radiansToDegrees(navx.getRotation3d().minus(offset).getAngle());

if (yawTimestampQueue != null && yawPositionQueue != null) {
inputs.odometryYawTimestamps = yawTimestampQueue
Expand Down

0 comments on commit f74061b

Please sign in to comment.