2626import com .revrobotics .CANSparkLowLevel .PeriodicFrame ;
2727
2828import edu .wpi .first .math .MathUtil ;
29- import edu .wpi .first .math .Pair ;
30- import edu .wpi .first .math .geometry .Pose2d ;
3129import edu .wpi .first .math .geometry .Rotation2d ;
3230import edu .wpi .first .math .geometry .Translation2d ;
3331import edu .wpi .first .math .kinematics .SwerveModulePosition ;
3432import edu .wpi .first .math .kinematics .SwerveModuleState ;
33+ import edu .wpi .first .units .Angle ;
3534import edu .wpi .first .units .Current ;
3635import edu .wpi .first .units .Dimensionless ;
3736import 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 /**
0 commit comments