66
77package wmironpatriots .subsystems .Swerve ;
88
9+ import static edu .wpi .first .units .Units .MetersPerSecond ;
10+ import static edu .wpi .first .units .Units .RadiansPerSecond ;
911import static edu .wpi .first .units .Units .Seconds ;
1012
1113import edu .wpi .first .math .estimator .SwerveDrivePoseEstimator ;
2224import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
2325import edu .wpi .first .wpilibj2 .command .Command ;
2426import edu .wpi .first .wpilibj2 .command .Subsystem ;
27+ import java .util .function .DoubleSupplier ;
2528import wmironpatriots .Constants ;
2629import wmironpatriots .Robot ;
2730import wmironpatriots .subsystems .Swerve .gyro .GyroHardware ;
@@ -43,7 +46,6 @@ public static Swerve create() {
4346
4447 return new Swerve (new GyroHardwareComp (), modules );
4548 } else {
46- // ! PLACEHOLDER FOR SIM HARDWARE INIT
4749 for (int i = 0 ; i < modules .length ; i ++) {
4850 modules [i ] = new Module (new ModuleHardwareSim (moduleConfigs [i ]));
4951 }
@@ -92,7 +94,7 @@ public void periodic() {
9294 }
9395
9496 if (DriverStation .isDisabled ()) {
95- stop ();
97+ stopAndLock ();
9698 }
9799 }
98100
@@ -105,6 +107,46 @@ public void simulationPeriodic() {
105107 !Double .isNaN (angularRate ) ? angularRate * Constants .LOOPTIME .in (Seconds ) : 0 ));
106108 }
107109
110+ /**
111+ * Drives robot based on magnitudes
112+ *
113+ * @param xSpeedMagnitude Input stream representing desired x speed magnitude
114+ * @param ySpeedMagnitude Input stream representing desired y speed magnitude
115+ * @param angularRateMagnitude Input stream representing desiredd angular rate magnitude
116+ * @return {@link Command}
117+ */
118+ public Command driveFromMagnitudes (
119+ DoubleSupplier xSpeedMagnitude ,
120+ DoubleSupplier ySpeedMagnitude ,
121+ DoubleSupplier angularRateMagnitude ) {
122+ return this .run (
123+ () ->
124+ setChassisSpeeds (
125+ ChassisSpeeds .fromFieldRelativeSpeeds (
126+ xSpeedMagnitude .getAsDouble ()
127+ * SwerveConstants .MAX_LINEAR_SPEED .in (MetersPerSecond ),
128+ ySpeedMagnitude .getAsDouble ()
129+ * SwerveConstants .MAX_LINEAR_SPEED .in (MetersPerSecond ),
130+ angularRateMagnitude .getAsDouble ()
131+ * SwerveConstants .MAX_ANGULAR_RATE .in (RadiansPerSecond ),
132+ getHeadingRotation2d ())));
133+ }
134+
135+ public Command stopAndLock () {
136+ return this .runOnce (
137+ () -> {
138+ var states =
139+ new SwerveModuleState [] {
140+ new SwerveModuleState (0.0 , Rotation2d .fromDegrees (45 )),
141+ new SwerveModuleState (0.0 , Rotation2d .fromDegrees (-45 )),
142+ new SwerveModuleState (0.0 , Rotation2d .fromDegrees (45 )),
143+ new SwerveModuleState (0.0 , Rotation2d .fromDegrees (-45 )),
144+ };
145+
146+ setSwerveModuleStates (states );
147+ });
148+ }
149+
108150 /**
109151 * Converts a {@link ChassisSpeeds} object representing robot relative speeds into {@link
110152 * SwerveModuleState} setpoint for each module
@@ -122,9 +164,19 @@ public void setChassisSpeeds(ChassisSpeeds speeds) {
122164 speeds = ChassisSpeeds .discretize (speeds , Constants .LOOPTIME .in (Seconds ));
123165
124166 var states = kinematics .toSwerveModuleStates (speeds );
125- SwerveDriveKinematics .desaturateWheelSpeeds (states , SwerveConstants .MAX_LINEAR_SPEED );
126- setpoint .set (states );
167+ SwerveDriveKinematics .desaturateWheelSpeeds (
168+ states , SwerveConstants .MAX_LINEAR_SPEED .in (MetersPerSecond ));
169+
170+ setSwerveModuleStates (states );
171+ }
127172
173+ /**
174+ * Sets the setpoints of each swerve module
175+ *
176+ * @param states {@link SwerveModuleState} array of desired states
177+ */
178+ public void setSwerveModuleStates (SwerveModuleState [] states ) {
179+ setpoint .set (states );
128180 for (int i = 0 ; i < modules .length ; i ++) {
129181 modules [i ].setSetpoints (states [i ]);
130182 }
0 commit comments