1
1
package frc .robot .swerve ;
2
2
3
- import java .util .function .Function ;
4
-
5
3
import edu .wpi .first .math .filter .SlewRateLimiter ;
6
4
import edu .wpi .first .math .geometry .Rotation2d ;
7
5
import edu .wpi .first .math .kinematics .ChassisSpeeds ;
16
14
import frc .lib .DriveRequest ;
17
15
import frc .lib .Subsystem ;
18
16
import frc .lib .Telemetry ;
17
+ import frc .lib .config .FeedbackControllerConfig ;
18
+ import frc .lib .config .FeedforwardControllerConfig ;
19
+ import frc .lib .config .MechanismConfig ;
19
20
import frc .lib .config .MotionProfileConfig ;
21
+ import frc .lib .config .MotorConfig ;
20
22
import frc .lib .controller .SwerveModuleIO ;
21
23
import frc .robot .RobotConstants ;
22
24
import frc .robot .odometry .Odometry ;
25
+ import java .util .function .Function ;
23
26
24
27
/** Swerve subsystem. */
25
28
public class Swerve extends Subsystem {
@@ -33,23 +36,59 @@ public class Swerve extends Subsystem {
33
36
/** Swerve kinematics. */
34
37
private final SwerveDriveKinematics swerveKinematics ;
35
38
39
+ /** Steer motor config. */
40
+ private final MechanismConfig steerConfig =
41
+ new MechanismConfig ()
42
+ .withMotorConfig (
43
+ new MotorConfig ()
44
+ .withCCWPositive (false )
45
+ .withCurrentLimit (20 )
46
+ .withMotorToMechanismRatio (150.0 / 7.0 ))
47
+ .withFeedforwardConfig (
48
+ new FeedforwardControllerConfig ().withStaticFeedforward (0.205 ) // volts
49
+ )
50
+ .withFeedbackConfig (
51
+ new FeedbackControllerConfig ()
52
+ .withContinuousInput (true )
53
+ .withProportionalGain (54.0 ) // volts per rotation
54
+ .withDerivativeGain (0.16 ) // volts per rotation per second
55
+ .withPositionTolerance (Units .degreesToRotations (1 )) // rotations
56
+ );
57
+
58
+ /** Drive motor config. */
59
+ private final MechanismConfig driveConfig =
60
+ new MechanismConfig ()
61
+ .withMotorConfig (
62
+ new MotorConfig ()
63
+ .withCCWPositive (false )
64
+ .withCurrentLimit (40 )
65
+ .withMotorToMechanismRatio (6.75 ))
66
+ .withFeedforwardConfig (
67
+ new FeedforwardControllerConfig ()
68
+ .withStaticFeedforward (0.14 ) // volts
69
+ .withVelocityFeedforward (0.725 ) // volts per rotation per second
70
+ )
71
+ .withFeedbackConfig (
72
+ new FeedbackControllerConfig ()
73
+ .withProportionalGain (0.75 ) // volts per rotation per second
74
+ );
75
+
36
76
/** Translation motion profile config. */
37
77
private final MotionProfileConfig translationMotionProfileConfig =
38
78
new MotionProfileConfig ()
39
- .withMaximumVelocity (4.5 ) // meters per second
40
- .withMaximumAcceleration (18 ); // meters per second per second
79
+ .withMaximumVelocity (4.5 ) // meters per second
80
+ .withMaximumAcceleration (18 ); // meters per second per second
41
81
42
82
/** Rotation motion profile config. */
43
- // TODO Verify
44
83
private final MotionProfileConfig rotationMotionProfileConfig =
45
84
new MotionProfileConfig ().withMaximumVelocity (1 ); // rotations per second
46
85
47
86
/** Initializes the swerve subsystem and configures swerve hardware. */
48
87
private Swerve () {
49
- swerveModules [0 ] = SwerveFactory .createNorthWestModule ();
50
- swerveModules [1 ] = SwerveFactory .createNorthEastModule ();
51
- swerveModules [2 ] = SwerveFactory .createSouthEastModule ();
52
- swerveModules [3 ] = SwerveFactory .createSouthWestModule ();
88
+ swerveModules [0 ] = SwerveFactory .createNorthWestModule (steerConfig , driveConfig );
89
+ swerveModules [1 ] = SwerveFactory .createNorthEastModule (steerConfig , driveConfig );
90
+ swerveModules [2 ] = SwerveFactory .createSouthEastModule (steerConfig , driveConfig );
91
+ swerveModules [3 ] = SwerveFactory .createSouthWestModule (steerConfig , driveConfig );
53
92
54
93
swerveKinematics =
55
94
new SwerveDriveKinematics (
@@ -82,8 +121,6 @@ public void addToShuffleboard(ShuffleboardTab tab) {
82
121
translationConstants .addDouble ("Maximum Velocity (mps)" , this ::maximumTranslationVelocity );
83
122
translationConstants .addDouble (
84
123
"Maximum Accleration (mpsps)" , this ::maximumTranslationAcceleration );
85
- translationConstants .addDouble (
86
- "Wheel Circumference (m)" , () -> SwerveConstants .MK4iConstants .WHEEL_CIRCUMFERENCE );
87
124
88
125
ShuffleboardLayout rotationConstants = Telemetry .addColumn (tab , "Rotation Constants" );
89
126
rotationConstants .addDouble (
@@ -249,30 +286,40 @@ public Rotation2d maximumRotationVelocity() {
249
286
* @return a command that drives the swerve using an Xbox controller.
250
287
*/
251
288
public Command teleopDrive (CommandXboxController controller ) {
252
- final SlewRateLimiter xAccelerationLimiter = translationMotionProfileConfig .createAccelerationLimiter ();
253
- final SlewRateLimiter yAccelerationLimiter = translationMotionProfileConfig .createAccelerationLimiter ();
254
-
255
- final Function <Double , Double > rotationVelocityLimiter = rotationMotionProfileConfig .createVelocityClamper ();
256
-
257
- final Function <ChassisSpeeds , ChassisSpeeds > chassisSpeedsLimiter = chassisSpeeds -> {
258
- return new ChassisSpeeds (
259
- xAccelerationLimiter .calculate (chassisSpeeds .vxMetersPerSecond ),
260
- yAccelerationLimiter .calculate (chassisSpeeds .vyMetersPerSecond ),
261
- Units .rotationsToRadians (rotationVelocityLimiter .apply (Units .radiansToRotations (chassisSpeeds .omegaRadiansPerSecond )))
262
- );
263
- };
264
-
265
- final Function <DriveRequest , ChassisSpeeds > chassisSpeedsGetter = request -> {
266
- return ChassisSpeeds .fromFieldRelativeSpeeds (
267
- request .translationAxis ().getX () * translationMotionProfileConfig .maximumVelocity (),
268
- request .translationAxis ().getY () * translationMotionProfileConfig .maximumVelocity (),
269
- request .rotationVelocityAxis () * Units .rotationsToRadians (rotationMotionProfileConfig .maximumVelocity ()),
270
- Odometry .getInstance ().getDriverRelativeHeading ());
271
- };
272
-
273
- return run (() -> {
274
- setChassisSpeeds (chassisSpeedsLimiter .apply (chassisSpeedsGetter .apply (DriveRequest .fromController (controller ))));
275
- });
289
+ final SlewRateLimiter xAccelerationLimiter =
290
+ translationMotionProfileConfig .createAccelerationLimiter ();
291
+ final SlewRateLimiter yAccelerationLimiter =
292
+ translationMotionProfileConfig .createAccelerationLimiter ();
293
+
294
+ final Function <Double , Double > rotationVelocityLimiter =
295
+ rotationMotionProfileConfig .createVelocityClamper ();
296
+
297
+ final Function <ChassisSpeeds , ChassisSpeeds > chassisSpeedsLimiter =
298
+ chassisSpeeds -> {
299
+ return new ChassisSpeeds (
300
+ xAccelerationLimiter .calculate (chassisSpeeds .vxMetersPerSecond ),
301
+ yAccelerationLimiter .calculate (chassisSpeeds .vyMetersPerSecond ),
302
+ Units .rotationsToRadians (
303
+ rotationVelocityLimiter .apply (
304
+ Units .radiansToRotations (chassisSpeeds .omegaRadiansPerSecond ))));
305
+ };
306
+
307
+ final Function <DriveRequest , ChassisSpeeds > chassisSpeedsGetter =
308
+ request -> {
309
+ return ChassisSpeeds .fromFieldRelativeSpeeds (
310
+ request .translationAxis ().getX () * translationMotionProfileConfig .maximumVelocity (),
311
+ request .translationAxis ().getY () * translationMotionProfileConfig .maximumVelocity (),
312
+ request .rotationVelocityAxis ()
313
+ * Units .rotationsToRadians (rotationMotionProfileConfig .maximumVelocity ()),
314
+ Odometry .getInstance ().getDriverRelativeHeading ());
315
+ };
316
+
317
+ return run (
318
+ () -> {
319
+ setChassisSpeeds (
320
+ chassisSpeedsLimiter .apply (
321
+ chassisSpeedsGetter .apply (DriveRequest .fromController (controller ))));
322
+ });
276
323
}
277
324
278
325
/**
0 commit comments