Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
first implement of swerve optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen756 committed Oct 18, 2023
1 parent 85234a3 commit ae51fbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 65 deletions.
20 changes: 17 additions & 3 deletions src/main/java/frc/robot/swerve/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import edu.wpi.first.math.util.Units;
import frc.robot.config.Config;
import frc.robot.util.CircleConverter;
import frc.robot.util.CtreModuleState;
import frc.robot.util.GearingConverter;
import org.littletonrobotics.junction.Logger;

Expand Down Expand Up @@ -126,8 +125,7 @@ public void log() {

public void setDesiredState(
SwerveModuleState state, boolean openLoop, boolean skipJitterOptimization) {
final var steerMotorPosition = getSteerMotorPosition();
state = CtreModuleState.optimize(state, steerMotorPosition);
state = optimize(state);

double commandedSteerPosition =
STEER_MOTOR_GEARING_CONVERTER.gearingToMotor(state.angle.getRotations());
Expand Down Expand Up @@ -210,4 +208,20 @@ private final Rotation2d getCancoderPosition() {
private Rotation2d getRawCancoderPosition() {
return Rotation2d.fromDegrees(encoder.getAbsolutePosition());
}

private SwerveModuleState optimize(SwerveModuleState desiredState) {
SwerveModuleState currentState = getState();
var delta = desiredState.angle.minus(currentState.angle);

// if velocity is greater than 0.2mps then do not invert drive motor else
// if delta > 90 then invert drive velocity

if (Math.abs(delta.getDegrees()) > 90.0 && currentState.speedMetersPerSecond < 0.2) {
return new SwerveModuleState(
-desiredState.speedMetersPerSecond,
desiredState.angle.rotateBy(Rotation2d.fromDegrees(180.0)));
} else {
return new SwerveModuleState(desiredState.speedMetersPerSecond, desiredState.angle);
}
}
}
62 changes: 0 additions & 62 deletions src/main/java/frc/robot/util/CtreModuleState.java

This file was deleted.

0 comments on commit ae51fbd

Please sign in to comment.