Pushing up changes that are related to us trying to fix rotation

This commit is contained in:
2026-01-31 13:29:11 -05:00
parent 4296e2f27f
commit 4666a74877
3 changed files with 23 additions and 16 deletions

View File

@@ -101,9 +101,5 @@ public class ModuleConstants {
.outputRange(-1, 1) .outputRange(-1, 1)
.positionWrappingEnabled(true) .positionWrappingEnabled(true)
.positionWrappingInputRange(0, 2 * Math.PI); .positionWrappingInputRange(0, 2 * Math.PI);
turningConfig.signals
.primaryEncoderVelocityAlwaysOn(false)
.primaryEncoderPositionAlwaysOn(true)
.primaryEncoderPositionPeriodMs(20);
} }
} }

View File

@@ -48,34 +48,34 @@ public class Drivetrain extends SubsystemBase {
frontLeft = new SwerveModule( frontLeft = new SwerveModule(
"FrontLeft", "FrontLeft",
DrivetrainConstants.kFrontLeftDrivingCANID, DrivetrainConstants.kFrontLeftDrivingCANID,
DrivetrainConstants.kFrontLeftTurningCANID, DrivetrainConstants.kFrontLeftTurningCANID);/*,
DrivetrainConstants.kFrontLeftAnalogInPort, DrivetrainConstants.kFrontLeftAnalogInPort,
DrivetrainConstants.kFrontLeftMagEncoderOffset DrivetrainConstants.kFrontLeftMagEncoderOffset
); ); */
frontRight = new SwerveModule( frontRight = new SwerveModule(
"FrontRight", "FrontRight",
DrivetrainConstants.kFrontRightDrivingCANID, DrivetrainConstants.kFrontRightDrivingCANID,
DrivetrainConstants.kFrontRightTurningCANID, DrivetrainConstants.kFrontRightTurningCANID);/*,
DrivetrainConstants.kFrontRightAnalogInPort, DrivetrainConstants.kFrontRightAnalogInPort,
DrivetrainConstants.kFrontRightMagEncoderOffset DrivetrainConstants.kFrontRightMagEncoderOffset
); );*/
rearLeft = new SwerveModule( rearLeft = new SwerveModule(
"RearLeft", "RearLeft",
DrivetrainConstants.kRearLeftDrivingCANID, DrivetrainConstants.kRearLeftDrivingCANID,
DrivetrainConstants.kRearLeftTurningCANID, DrivetrainConstants.kRearLeftTurningCANID);/*,
DrivetrainConstants.kRearLeftAnalogInPort, DrivetrainConstants.kRearLeftAnalogInPort,
DrivetrainConstants.kRearLeftMagEncoderOffset DrivetrainConstants.kRearLeftMagEncoderOffset
); ); */
rearRight = new SwerveModule( rearRight = new SwerveModule(
"RearRight", "RearRight",
DrivetrainConstants.kRearRightDrivingCANID, DrivetrainConstants.kRearRightDrivingCANID,
DrivetrainConstants.kRearRightTurningCANID, DrivetrainConstants.kRearRightTurningCANID);/*,
DrivetrainConstants.kRearRightAnalogInPort, DrivetrainConstants.kRearRightAnalogInPort,
DrivetrainConstants.kRearRightMagEncoderOffset DrivetrainConstants.kRearRightMagEncoderOffset
); ); */
gyro = new AHRS(NavXComType.kMXP_SPI); gyro = new AHRS(NavXComType.kMXP_SPI);

View File

@@ -12,6 +12,7 @@ import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.SparkBase.ControlType; import com.revrobotics.spark.SparkBase.ControlType;
import com.revrobotics.spark.SparkLowLevel.MotorType; import com.revrobotics.spark.SparkLowLevel.MotorType;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.geometry.Rotation2d; import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModulePosition; import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState; import edu.wpi.first.math.kinematics.SwerveModuleState;
@@ -44,6 +45,7 @@ public class SwerveModule {
private String moduleName; private String moduleName;
private SwerveModuleState lastTargetState; private SwerveModuleState lastTargetState;
private SwerveModuleState lastTargetStateOptimized;
private boolean isAbsoluteEncoderDisabled; private boolean isAbsoluteEncoderDisabled;
private boolean turningEncoderAutoRezeroEnabled; private boolean turningEncoderAutoRezeroEnabled;
@@ -125,6 +127,7 @@ public class SwerveModule {
drive.setPosition(0); drive.setPosition(0);
this.lastTargetState = getState(); this.lastTargetState = getState();
this.lastTargetStateOptimized = getState();
this.turningEncoderAutoRezeroEnabled = turningEncoderAutoRezeroEnabled; this.turningEncoderAutoRezeroEnabled = turningEncoderAutoRezeroEnabled;
@@ -136,16 +139,18 @@ public class SwerveModule {
Logger.recordOutput(moduleName + "/AbsoluteEncoder/Position", turningAbsoluteEncoder.get()); Logger.recordOutput(moduleName + "/AbsoluteEncoder/Position", turningAbsoluteEncoder.get());
} }
Logger.recordOutput(moduleName + "ModuleTargetState", lastTargetState); Logger.recordOutput(moduleName + "/ModuleTargetState", lastTargetState);
Logger.recordOutput(moduleName + "/ModuleTargetStateOptimized", lastTargetStateOptimized);
Logger.recordOutput(moduleName + "/SwerveModuleState", getState()); Logger.recordOutput(moduleName + "/SwerveModuleState", getState());
Logger.recordOutput(moduleName + "/SwerveModulePosition", getPosition()); Logger.recordOutput(moduleName + "/SwerveModulePosition", getPosition());
Logger.recordOutput(moduleName + "/RelativeEncoderPosition", getTurningEncoderPosition()); Logger.recordOutput(moduleName + "/RelativeEncoderPosition", getTurningEncoderPosition());
/*
if(!isAbsoluteEncoderDisabled && turningEncoderAutoRezeroEnabled) { if(!isAbsoluteEncoderDisabled && turningEncoderAutoRezeroEnabled) {
if(Math.abs(getState().angle.getRadians() - lastTargetState.angle.getRadians()) <= ModuleConstants.kAutoResetPositionDeadband) { if(Math.abs(getState().angle.getRadians() - lastTargetState.angle.getRadians()) <= ModuleConstants.kAutoResetPositionDeadband) {
resetEncoders(); resetEncoders();
} }
} }*/
} }
public SwerveModuleState getState() { public SwerveModuleState getState() {
@@ -168,12 +173,13 @@ public class SwerveModule {
} }
public void setDesiredState(SwerveModuleState desiredState) { public void setDesiredState(SwerveModuleState desiredState) {
lastTargetState = new SwerveModuleState(desiredState.speedMetersPerSecond, desiredState.angle);
// TODO is this really necessary, the offset is managed by the Absolute Encoder // TODO is this really necessary, the offset is managed by the Absolute Encoder
// and its "source of truth" behavior in relation to the relative encoder // and its "source of truth" behavior in relation to the relative encoder
// Probably doesn't *hurt* that it's here, but it may not be needed // Probably doesn't *hurt* that it's here, but it may not be needed
desiredState.optimize(new Rotation2d(getTurningEncoderPosition())); desiredState.optimize(new Rotation2d(getTurningEncoderPosition()));
lastTargetState = desiredState; lastTargetStateOptimized = desiredState;
drive.setControl( drive.setControl(
driveVelocityRequest.withVelocity( driveVelocityRequest.withVelocity(
@@ -190,7 +196,12 @@ public class SwerveModule {
} }
public double getTurningEncoderPosition() { public double getTurningEncoderPosition() {
return turningRelativeEncoder.getPosition() * (ModuleConstants.kIsEncoderInverted ? -1 : 1); return MathUtil.inputModulus(
turningRelativeEncoder.getPosition(),
0,
2 * Math.PI
);
//return turningRelativeEncoder.getPosition() * (ModuleConstants.kIsEncoderInverted ? -1 : 1);
} }
public void resetEncoders() { public void resetEncoders() {