5
5
import com .ctre .phoenix6 .configs .TorqueCurrentConfigs ;
6
6
import com .ctre .phoenix6 .hardware .Pigeon2 ;
7
7
import com .ctre .phoenix6 .mechanisms .swerve .SwerveDrivetrain ;
8
+ import com .ctre .phoenix6 .mechanisms .swerve .SwerveDrivetrain .SwerveDriveState ;
8
9
import com .ctre .phoenix6 .mechanisms .swerve .SwerveModule ;
9
10
import com .ctre .phoenix6 .mechanisms .swerve .SwerveModule .DriveRequestType ;
10
11
import com .ctre .phoenix6 .mechanisms .swerve .SwerveModuleConstants ;
13
14
import dev .doglog .DogLog ;
14
15
import edu .wpi .first .math .geometry .Rotation2d ;
15
16
import edu .wpi .first .math .kinematics .ChassisSpeeds ;
16
- import edu .wpi .first .math .kinematics .SwerveModulePosition ;
17
17
import edu .wpi .first .math .util .Units ;
18
18
import edu .wpi .first .wpilibj .DriverStation ;
19
19
import edu .wpi .first .wpilibj .Notifier ;
26
26
import frc .robot .util .scheduling .SubsystemPriority ;
27
27
import frc .robot .util .state_machines .StateMachine ;
28
28
import java .security .InvalidParameterException ;
29
- import java .util .List ;
30
29
31
30
public class SwerveSubsystem extends StateMachine <SwerveState > {
32
31
/** Max speed allowed to make a speaker shot and feeding. */
@@ -84,12 +83,11 @@ private static SwerveModuleConstants constantsForModuleNumber(int moduleNumber)
84
83
private Notifier simNotifier = null ;
85
84
86
85
private boolean slowEnoughToShoot = false ;
87
- private List < SwerveModulePosition > modulePositions ;
88
- private ChassisSpeeds robotRelativeSpeeds ;
89
- private ChassisSpeeds fieldRelativeSpeeds ;
86
+ private SwerveDriveState drivetrainState = new SwerveDriveState () ;
87
+ private ChassisSpeeds robotRelativeSpeeds = new ChassisSpeeds () ;
88
+ private ChassisSpeeds fieldRelativeSpeeds = new ChassisSpeeds () ;
90
89
private boolean slowEnoughToFeed ;
91
90
private double goalSnapAngle = 0 ;
92
- boolean closedLoop = false ;
93
91
94
92
/** The latest requested teleop speeds. */
95
93
private ChassisSpeeds teleopSpeeds = new ChassisSpeeds ();
@@ -112,8 +110,8 @@ public boolean isSlowEnoughToFeed() {
112
110
return slowEnoughToFeed ;
113
111
}
114
112
115
- public List < SwerveModulePosition > getModulePositions () {
116
- return modulePositions ;
113
+ public SwerveDriveState getDrivetrainState () {
114
+ return drivetrainState ;
117
115
}
118
116
119
117
public void setSnapToAngle (double angle ) {
@@ -134,7 +132,6 @@ public SwerveSubsystem() {
134
132
driveToAngle .HeadingController = RobotConfig .get ().swerve ().snapController ();
135
133
driveToAngle .HeadingController .enableContinuousInput (-Math .PI , Math .PI );
136
134
driveToAngle .HeadingController .setTolerance (0.02 );
137
- modulePositions = calculateModulePositions ();
138
135
139
136
// The CTR SwerveModule class will overwrite your torque current limits and the stator current
140
137
// limit with the configured slip current. This logic allows us to exercise more precise control
@@ -240,28 +237,16 @@ public void driveTeleop(double x, double y, double theta) {
240
237
241
238
@ Override
242
239
protected void collectInputs () {
243
- modulePositions = calculateModulePositions ();
244
- robotRelativeSpeeds = calculateRobotRelativeSpeeds () ;
240
+ drivetrainState = drivetrain . getState ();
241
+ robotRelativeSpeeds = drivetrainState . speeds ;
245
242
fieldRelativeSpeeds = calculateFieldRelativeSpeeds ();
246
243
slowEnoughToShoot = calculateMovingSlowEnoughForSpeakerShot (robotRelativeSpeeds );
247
244
slowEnoughToFeed = calculateMovingSlowEnoughForFloorShot (robotRelativeSpeeds );
248
245
}
249
246
250
- private List <SwerveModulePosition > calculateModulePositions () {
251
- return List .of (
252
- frontLeft .getPosition (true ),
253
- frontRight .getPosition (true ),
254
- backLeft .getPosition (true ),
255
- backRight .getPosition (true ));
256
- }
257
-
258
- private ChassisSpeeds calculateRobotRelativeSpeeds () {
259
- return drivetrain .getState ().speeds ;
260
- }
261
-
262
247
private ChassisSpeeds calculateFieldRelativeSpeeds () {
263
248
return ChassisSpeeds .fromRobotRelativeSpeeds (
264
- robotRelativeSpeeds , drivetrain . getState () .Pose .getRotation ());
249
+ robotRelativeSpeeds , drivetrainState .Pose .getRotation ());
265
250
}
266
251
267
252
private static boolean calculateMovingSlowEnoughForSpeakerShot (ChassisSpeeds speeds ) {
@@ -364,6 +349,9 @@ public void robotPeriodic() {
364
349
super .robotPeriodic ();
365
350
366
351
DogLog .log ("Swerve/SnapAngle" , goalSnapAngle );
352
+ DogLog .log ("Swerve/ModuleStates" , drivetrainState .ModuleStates );
353
+ DogLog .log ("Swerve/ModuleTargets" , drivetrainState .ModuleTargets );
354
+ DogLog .log ("Swerve/RobotRelativeSpeeds" , drivetrainState .speeds );
367
355
}
368
356
369
357
private void startSimThread () {
0 commit comments