Skip to content

Commit c187143

Browse files
committed
Revert "Fix real module velocity calculation"
This reverts commit ee906a0.
1 parent 523611c commit c187143

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/main/java/org/lasarobotics/drive/MAXSwerveModule.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
import com.revrobotics.CANSparkLowLevel.PeriodicFrame;
2727

2828
import edu.wpi.first.math.MathUtil;
29-
import edu.wpi.first.math.Pair;
30-
import edu.wpi.first.math.geometry.Pose2d;
3129
import edu.wpi.first.math.geometry.Rotation2d;
3230
import edu.wpi.first.math.geometry.Translation2d;
3331
import edu.wpi.first.math.kinematics.SwerveModulePosition;
3432
import edu.wpi.first.math.kinematics.SwerveModuleState;
33+
import edu.wpi.first.units.Angle;
3534
import edu.wpi.first.units.Current;
3635
import edu.wpi.first.units.Dimensionless;
3736
import edu.wpi.first.units.Distance;
@@ -132,14 +131,14 @@ private GearRatio(double value) {
132131
private GearRatio m_driveGearRatio;
133132
private double m_driveConversionFactor;
134133
private double m_rotateConversionFactor;
134+
private double m_radius;
135135
private double m_autoLockTime;
136136
private boolean m_autoLock;
137137
private double m_runningOdometer;
138138
private String m_odometerOutputPath;
139139

140140
private TractionControlController m_tractionControlController;
141141
private Instant m_autoLockTimer;
142-
private Pair<Pose2d, Instant> m_previousPose;
143142

144143
/**
145144
* Create an instance of a MAXSwerveModule
@@ -169,6 +168,7 @@ public MAXSwerveModule(Hardware swerveHardware, ModuleLocation location, GearRat
169168
DRIVE_METERS_PER_ROTATION = DRIVE_METERS_PER_TICK * encoderTicksPerRotation;
170169
DRIVE_MAX_LINEAR_SPEED = (swerveHardware.driveMotor.getKind().getMaxRPM() / 60) * DRIVE_METERS_PER_ROTATION * DRIVETRAIN_EFFICIENCY;
171170
COSINE_CORRECTION = RobotBase.isReal() ? 1 : 0;
171+
172172
this.m_driveMotor = swerveHardware.driveMotor;
173173
this.m_rotateMotor = swerveHardware.rotateMotor;
174174
this.m_location = location;
@@ -181,7 +181,6 @@ public MAXSwerveModule(Hardware swerveHardware, ModuleLocation location, GearRat
181181
this.m_tractionControlController = new TractionControlController(driveWheel, slipRatio, mass, Units.MetersPerSecond.of(DRIVE_MAX_LINEAR_SPEED));
182182
this.m_autoLockTimer = Instant.now();
183183
this.m_runningOdometer = 0.0;
184-
this.m_previousPose = new Pair<Pose2d, Instant>(new Pose2d(), Instant.now());
185184

186185
// Set drive encoder conversion factor
187186
m_driveConversionFactor = driveWheel.diameter.in(Units.Meters) * Math.PI / m_driveGearRatio.value;
@@ -268,6 +267,9 @@ public MAXSwerveModule(Hardware swerveHardware, ModuleLocation location, GearRat
268267
RobotModeTriggers.disabled().onTrue(Commands.runOnce(() -> disabledInit()).ignoringDisable(true));
269268
RobotModeTriggers.disabled().onFalse(Commands.runOnce(() -> disabledExit()).ignoringDisable(true));
270269

270+
// Get distance from center of robot
271+
m_radius = m_moduleCoordinate.getNorm();
272+
271273
// Make sure settings are burned to flash
272274
m_driveMotor.burnFlash();
273275
m_rotateMotor.burnFlash();
@@ -338,20 +340,12 @@ private SwerveModuleState getDesiredState(SwerveModuleState requestedState) {
338340

339341
/**
340342
* Get real speed of module
341-
* @param currentPose Current pose of robot
343+
* @param inertialVelocity Inertial velocity of robot (m/s)
344+
* @param rotateRate Rotate rate of robot (degrees/s)
342345
* @return Speed of module (m/s)
343346
*/
344-
private Measure<Velocity<Distance>> calculateRealSpeed(Pose2d currentPose) {
345-
var previousPose = m_previousPose.getFirst();
346-
var now = Instant.now();
347-
var delta = Units.Milliseconds.of(Duration.between(m_previousPose.getSecond(), now).toMillis());
348-
var velocity = Units.Meters.of(
349-
currentPose.getTranslation().plus(m_moduleCoordinate).getDistance(previousPose.getTranslation())
350-
).per(delta);
351-
352-
m_previousPose = new Pair<Pose2d, Instant>(currentPose, now);
353-
354-
return velocity;
347+
private Measure<Velocity<Distance>> calculateRealSpeed(double inertialVelocity, double rotateRate) {
348+
return Units.MetersPerSecond.of(inertialVelocity + Math.toRadians(rotateRate) * m_radius);
355349
}
356350

357351
/**
@@ -421,13 +415,14 @@ public void set(SwerveModuleState state) {
421415
/**
422416
* Set swerve module direction and speed, automatically applying traction control
423417
* @param state Desired swerve module state
424-
* @param currentPose Current robot pose
418+
* @param inertialVelocity Current inertial velocity
419+
* @param rotateRate Current robot rotate rate
425420
*/
426-
public void set(SwerveModuleState state, Pose2d currentPose) {
421+
public void set(SwerveModuleState state, Measure<Velocity<Distance>> inertialVelocity, Measure<Velocity<Angle>> rotateRate) {
427422
// Apply traction control
428423
state.speedMetersPerSecond = m_tractionControlController.calculate(
429424
Units.MetersPerSecond.of(state.speedMetersPerSecond),
430-
calculateRealSpeed(currentPose),
425+
calculateRealSpeed(inertialVelocity.in(Units.MetersPerSecond), rotateRate.in(Units.DegreesPerSecond)),
431426
getDriveVelocity()
432427
).in(Units.MetersPerSecond);
433428

@@ -446,10 +441,11 @@ public void set(SwerveModuleState[] states) {
446441
/**
447442
* Set swerve module direction and speed, automatically applying traction control
448443
* @param states Array of states for all swerve modules
449-
* @param currentPose Current robot pose
444+
* @param inertialVelocity Current inertial velocity
445+
* @param rotateRate Current rotate rate
450446
*/
451-
public void set(SwerveModuleState[] states, Pose2d currentPose) {
452-
set(states[m_location.index], currentPose);
447+
public void set(SwerveModuleState[] states, Measure<Velocity<Distance>> inertialVelocity, Measure<Velocity<Angle>> rotateRate) {
448+
set(states[m_location.index], inertialVelocity, rotateRate);
453449
}
454450

455451
/**

src/main/java/org/lasarobotics/drive/TractionControlController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public TractionControlController(Measure<Dimensionless> staticCoF,
8686
this.m_mass = mass.divide(4).in(Units.Kilograms);
8787
this.m_maxLinearSpeed = Math.floor(maxLinearSpeed.in(Units.MetersPerSecond) * 1000) / 1000;
8888
this.m_maxAcceleration = m_staticCoF * GlobalConstants.GRAVITATIONAL_ACCELERATION.in(Units.MetersPerSecondPerSecond);
89-
this.m_maxPredictedSlipRatio = (m_maxAcceleration * GlobalConstants.ROBOT_LOOP_HZ / 2)
89+
this.m_maxPredictedSlipRatio = (m_maxAcceleration * GlobalConstants.ROBOT_LOOP_HZ)
9090
/ (m_staticCoF * m_mass * GlobalConstants.GRAVITATIONAL_ACCELERATION.in(Units.MetersPerSecondPerSecond));
9191
this.m_isSlipping = false;
9292
this.m_slippingDebouncer = new Debouncer(MIN_SLIPPING_TIME.in(Units.Seconds), DebounceType.kRising);

src/test/java/org/lasarobotics/drive/TractionControlTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2727
public class TractionControlTest {
28-
private final Measure<Dimensionless> SLIP_RATIO = Units.Percent.of(.0);
28+
private final Measure<Dimensionless> SLIP_RATIO = Units.Percent.of(8.0);
2929
private final Measure<Dimensionless> STATIC_FRICTION_COEFFICIENT = Units.Value.of(1.1);
3030
private final Measure<Dimensionless> DYNAMIC_FRICTION_COEFFICIENT = Units.Value.of(0.8);
3131
private final Measure<Mass> MASS = Units.Pounds.of(110.0);

0 commit comments

Comments
 (0)