26
26
import com .revrobotics .CANSparkLowLevel .PeriodicFrame ;
27
27
28
28
import edu .wpi .first .math .MathUtil ;
29
- import edu .wpi .first .math .Pair ;
30
- import edu .wpi .first .math .geometry .Pose2d ;
31
29
import edu .wpi .first .math .geometry .Rotation2d ;
32
30
import edu .wpi .first .math .geometry .Translation2d ;
33
31
import edu .wpi .first .math .kinematics .SwerveModulePosition ;
34
32
import edu .wpi .first .math .kinematics .SwerveModuleState ;
33
+ import edu .wpi .first .units .Angle ;
35
34
import edu .wpi .first .units .Current ;
36
35
import edu .wpi .first .units .Dimensionless ;
37
36
import edu .wpi .first .units .Distance ;
@@ -132,14 +131,14 @@ private GearRatio(double value) {
132
131
private GearRatio m_driveGearRatio ;
133
132
private double m_driveConversionFactor ;
134
133
private double m_rotateConversionFactor ;
134
+ private double m_radius ;
135
135
private double m_autoLockTime ;
136
136
private boolean m_autoLock ;
137
137
private double m_runningOdometer ;
138
138
private String m_odometerOutputPath ;
139
139
140
140
private TractionControlController m_tractionControlController ;
141
141
private Instant m_autoLockTimer ;
142
- private Pair <Pose2d , Instant > m_previousPose ;
143
142
144
143
/**
145
144
* Create an instance of a MAXSwerveModule
@@ -169,6 +168,7 @@ public MAXSwerveModule(Hardware swerveHardware, ModuleLocation location, GearRat
169
168
DRIVE_METERS_PER_ROTATION = DRIVE_METERS_PER_TICK * encoderTicksPerRotation ;
170
169
DRIVE_MAX_LINEAR_SPEED = (swerveHardware .driveMotor .getKind ().getMaxRPM () / 60 ) * DRIVE_METERS_PER_ROTATION * DRIVETRAIN_EFFICIENCY ;
171
170
COSINE_CORRECTION = RobotBase .isReal () ? 1 : 0 ;
171
+
172
172
this .m_driveMotor = swerveHardware .driveMotor ;
173
173
this .m_rotateMotor = swerveHardware .rotateMotor ;
174
174
this .m_location = location ;
@@ -181,7 +181,6 @@ public MAXSwerveModule(Hardware swerveHardware, ModuleLocation location, GearRat
181
181
this .m_tractionControlController = new TractionControlController (driveWheel , slipRatio , mass , Units .MetersPerSecond .of (DRIVE_MAX_LINEAR_SPEED ));
182
182
this .m_autoLockTimer = Instant .now ();
183
183
this .m_runningOdometer = 0.0 ;
184
- this .m_previousPose = new Pair <Pose2d , Instant >(new Pose2d (), Instant .now ());
185
184
186
185
// Set drive encoder conversion factor
187
186
m_driveConversionFactor = driveWheel .diameter .in (Units .Meters ) * Math .PI / m_driveGearRatio .value ;
@@ -268,6 +267,9 @@ public MAXSwerveModule(Hardware swerveHardware, ModuleLocation location, GearRat
268
267
RobotModeTriggers .disabled ().onTrue (Commands .runOnce (() -> disabledInit ()).ignoringDisable (true ));
269
268
RobotModeTriggers .disabled ().onFalse (Commands .runOnce (() -> disabledExit ()).ignoringDisable (true ));
270
269
270
+ // Get distance from center of robot
271
+ m_radius = m_moduleCoordinate .getNorm ();
272
+
271
273
// Make sure settings are burned to flash
272
274
m_driveMotor .burnFlash ();
273
275
m_rotateMotor .burnFlash ();
@@ -338,20 +340,12 @@ private SwerveModuleState getDesiredState(SwerveModuleState requestedState) {
338
340
339
341
/**
340
342
* 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)
342
345
* @return Speed of module (m/s)
343
346
*/
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 );
355
349
}
356
350
357
351
/**
@@ -421,13 +415,14 @@ public void set(SwerveModuleState state) {
421
415
/**
422
416
* Set swerve module direction and speed, automatically applying traction control
423
417
* @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
425
420
*/
426
- public void set (SwerveModuleState state , Pose2d currentPose ) {
421
+ public void set (SwerveModuleState state , Measure < Velocity < Distance >> inertialVelocity , Measure < Velocity < Angle >> rotateRate ) {
427
422
// Apply traction control
428
423
state .speedMetersPerSecond = m_tractionControlController .calculate (
429
424
Units .MetersPerSecond .of (state .speedMetersPerSecond ),
430
- calculateRealSpeed (currentPose ),
425
+ calculateRealSpeed (inertialVelocity . in ( Units . MetersPerSecond ), rotateRate . in ( Units . DegreesPerSecond ) ),
431
426
getDriveVelocity ()
432
427
).in (Units .MetersPerSecond );
433
428
@@ -446,10 +441,11 @@ public void set(SwerveModuleState[] states) {
446
441
/**
447
442
* Set swerve module direction and speed, automatically applying traction control
448
443
* @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
450
446
*/
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 );
453
449
}
454
450
455
451
/**
0 commit comments