1616
1717import edu .wpi .first .hal .SimDouble ;
1818import edu .wpi .first .math .MathUtil ;
19+ import edu .wpi .first .math .controller .PIDController ;
1920import edu .wpi .first .math .geometry .Pose2d ;
2021import edu .wpi .first .math .geometry .Rotation2d ;
22+ import edu .wpi .first .math .geometry .Translation2d ;
2123import edu .wpi .first .math .kinematics .ChassisSpeeds ;
2224import edu .wpi .first .math .kinematics .SwerveDriveKinematics ;
2325import edu .wpi .first .math .kinematics .SwerveDriveOdometry ;
@@ -55,9 +57,12 @@ public class DriveSubsystem extends SubsystemBase {
5557 private final StructPublisher <ChassisSpeeds > m_currentChassisSpeedsPublisher ;
5658 private final StructArrayPublisher <SwerveModuleState > m_targetModuleStatePublisher ;
5759 private final StructArrayPublisher <SwerveModuleState > m_currentModuleStatePublisher ;
60+ private final PIDController m_orientationController = new PIDController (kTurnP , kTurnI ,
61+ kTurnD );
5862
5963 /** Creates a new DriveSubsystem. */
6064 public DriveSubsystem () {
65+ m_orientationController .enableContinuousInput (0 , 2 * Math .PI );
6166 m_posePublisher = NetworkTableInstance .getDefault ()
6267 .getStructTopic ("/SmartDashboard/Pose@DriveSubsystem" , Pose2d .struct )
6368 .publish ();
@@ -272,23 +277,62 @@ public void periodic() {
272277 m_posePublisher .set (m_odometry .update (getHeading (), getModulePositions ()));
273278 }
274279
280+ /**
281+ * Creates a {@code ChassisSpeeds} instance to drive the robot with joystick
282+ * input.
283+ *
284+ * @param forwardSpeed Forward speed supplier. Positive values make the robot
285+ * go forward (+X direction).
286+ * @param strafeSpeed Strafe speed supplier. Positive values make the robot
287+ * go to the left (+Y direction).
288+ * @param forwardOrientation Forward orientation supplier. Positive values make
289+ * the robot face forward (+X direction).
290+ * @param strafeOrientation Strafe orientation supplier. Positive values make
291+ * the robot face left (+Y direction).
292+ * @param isRobotRelative Supplier for determining if driving should be robot
293+ * relative.
294+ * @return a {@code ChassisSpeeds} instance to drive the robot with joystick
295+ * input
296+ */
297+ public ChassisSpeeds chassisSpeeds (DoubleSupplier forwardSpeed , DoubleSupplier strafeSpeed ,
298+ DoubleSupplier forwardOrientation , DoubleSupplier strafeOrientation ) {
299+ var orientation = new Translation2d (forwardOrientation .getAsDouble (), strafeOrientation .getAsDouble ());
300+ double rotSpeed = 0 ;
301+ if (orientation .getNorm () > 0.05 )
302+ rotSpeed = m_orientationController
303+ .calculate (getHeading ().getRadians (), orientation .getAngle ().getRadians ());
304+
305+ double fwdSpeed = MathUtil .applyDeadband (forwardSpeed .getAsDouble (), ControllerConstants .kDeadzone );
306+ fwdSpeed = Math .signum (fwdSpeed ) * Math .pow (fwdSpeed , 2 ) * kTeleopDriveMaxSpeed ;
307+
308+ double strSpeed = MathUtil .applyDeadband (strafeSpeed .getAsDouble (), ControllerConstants .kDeadzone );
309+ strSpeed = Math .signum (strSpeed ) * Math .pow (strSpeed , 2 ) * kTeleopDriveMaxSpeed ;
310+
311+ return chassisSpeeds (fwdSpeed , strSpeed , rotSpeed );
312+ }
313+
275314 /**
276315 * Creates a command to drive the robot with joystick input.
277316 *
278317 * @param forwardSpeed Forward speed supplier. Positive values make the robot
279318 * go forward (+X direction).
280319 * @param strafeSpeed Strafe speed supplier. Positive values make the robot
281320 * go to the left (+Y direction).
282- * @param rotation Rotation speed supplier. Positive values make the
283- * robot rotate CCW.
284- * @param isFieldRelative Supplier for determining if driving should be field
321+ * @param forwardOrientation Forward orientation supplier. Positive values make
322+ * the robot face forward (+X direction).
323+ * @param strafeOrientation Strafe orientation supplier. Positive values make
324+ * the
325+ * robot face left (+Y direction).
326+ * @param isRobotRelative Supplier for determining if driving should be robot
285327 * relative.
286328 * @return A command to drive the robot.
287329 */
288330 public Command driveCommand (DoubleSupplier forwardSpeed , DoubleSupplier strafeSpeed ,
289- DoubleSupplier rotation , BooleanSupplier isFieldRelative ) {
331+ DoubleSupplier forwardOrientation , DoubleSupplier strafeOrientation , BooleanSupplier isRobotRelative ) {
290332 return run (() -> {
291- drive (chassisSpeeds (forwardSpeed , strafeSpeed , rotation ), isFieldRelative .getAsBoolean ());
333+ drive (
334+ chassisSpeeds (forwardSpeed , strafeSpeed , forwardOrientation , strafeOrientation ),
335+ !isRobotRelative .getAsBoolean ());
292336 }).withName ("DefaultDriveCommand" );
293337 }
294338
@@ -297,11 +341,11 @@ public Command driveCommand(DoubleSupplier forwardSpeed, DoubleSupplier strafeSp
297341 *
298342 * @return A command to reset the gyro heading.
299343 */
300- public Command resetHeadingCommand () {
344+ public Command resetHeading () {
301345 return runOnce (m_gyro ::zeroYaw ).withName ("ResetHeadingCommand" );
302346 }
303347
304- public Command resetOdometryCommand (Pose2d pose ) {
348+ public Command resetOdometry (Pose2d pose ) {
305349 return runOnce (() -> m_odometry .resetPosition (getHeading (), getModulePositions (), pose ))
306350 .withName ("ResetOdometryCommand" );
307351 }
0 commit comments