33
44import wpilib
55
6- from commands2 import SubsystemBase
6+ from commands2 import Subsystem
77from wpimath .filter import SlewRateLimiter
88from wpimath .geometry import Pose2d , Rotation2d
99from wpimath .kinematics import (
1818from .maxswervemodule import MAXSwerveModule
1919
2020
21- class DriveSubsystem (SubsystemBase ):
21+ class DriveSubsystem (Subsystem ):
2222 def __init__ (self ) -> None :
2323 super ().__init__ ()
2424
@@ -48,7 +48,7 @@ def __init__(self) -> None:
4848 )
4949
5050 # The gyro sensor
51- self .gyro = wpilib .ADIS16470_IMU ()
51+ self .gyro = wpilib .ADIS16448_IMU ()
5252
5353 # Slew rate filter variables for controlling lateral acceleration
5454 self .currentRotation = 0.0
@@ -63,22 +63,24 @@ def __init__(self) -> None:
6363 self .odometry = SwerveDrive4Odometry (
6464 DriveConstants .kDriveKinematics ,
6565 Rotation2d .fromDegrees (self .gyro .getAngle ()),
66- [
66+ (
6767 self .frontLeft .getPosition (),
6868 self .frontRight .getPosition (),
6969 self .rearLeft .getPosition (),
7070 self .rearRight .getPosition (),
71- ] ,
71+ ) ,
7272 )
7373
7474 def periodic (self ) -> None :
7575 # Update the odometry in the periodic block
7676 self .odometry .update (
7777 Rotation2d .fromDegrees (self .gyro .getAngle ()),
78- self .frontLeft .getPosition (),
79- self .frontRight .getPosition (),
80- self .rearLeft .getPosition (),
81- self .rearRight .getPosition (),
78+ (
79+ self .frontLeft .getPosition (),
80+ self .frontRight .getPosition (),
81+ self .rearLeft .getPosition (),
82+ self .rearRight .getPosition (),
83+ ),
8284 )
8385
8486 def getPose (self ) -> Pose2d :
@@ -96,11 +98,13 @@ def resetOdometry(self, pose: Pose2d) -> None:
9698 """
9799 self .odometry .resetPosition (
98100 Rotation2d .fromDegrees (self .gyro .getAngle ()),
101+ (
102+ self .frontLeft .getPosition (),
103+ self .frontRight .getPosition (),
104+ self .rearLeft .getPosition (),
105+ self .rearRight .getPosition (),
106+ ),
99107 pose ,
100- self .frontLeft .getPosition (),
101- self .frontRight .getPosition (),
102- self .rearLeft .getPosition (),
103- self .rearRight .getPosition (),
104108 )
105109
106110 def drive (
@@ -202,13 +206,13 @@ def drive(
202206 if fieldRelative
203207 else ChassisSpeeds (xSpeedDelivered , ySpeedDelivered , rotDelivered )
204208 )
205- swerveModuleStates = SwerveDrive4Kinematics .desaturateWheelSpeeds (
209+ fl , fr , rl , rr = SwerveDrive4Kinematics .desaturateWheelSpeeds (
206210 swerveModuleStates , DriveConstants .kMaxSpeedMetersPerSecond
207211 )
208- self .frontLeft .setDesiredState (swerveModuleStates [ 0 ] )
209- self .frontRight .setDesiredState (swerveModuleStates [ 1 ] )
210- self .rearLeft .setDesiredState (swerveModuleStates [ 2 ] )
211- self .rearRight .setDesiredState (swerveModuleStates [ 3 ] )
212+ self .frontLeft .setDesiredState (fl )
213+ self .frontRight .setDesiredState (fr )
214+ self .rearLeft .setDesiredState (rl )
215+ self .rearRight .setDesiredState (rr )
212216
213217 def setX (self ) -> None :
214218 """Sets the wheels into an X formation to prevent movement."""
@@ -219,18 +223,23 @@ def setX(self) -> None:
219223 self .rearLeft .setDesiredState (SwerveModuleState (0 , Rotation2d .fromDegrees (- 45 )))
220224 self .rearRight .setDesiredState (SwerveModuleState (0 , Rotation2d .fromDegrees (45 )))
221225
222- def setModuleStates (self , desiredStates : typing .Tuple [SwerveModuleState ]) -> None :
226+ def setModuleStates (
227+ self ,
228+ desiredStates : typing .Tuple [
229+ SwerveModuleState , SwerveModuleState , SwerveModuleState , SwerveModuleState
230+ ],
231+ ) -> None :
223232 """Sets the swerve ModuleStates.
224233
225234 :param desiredStates: The desired SwerveModule states.
226235 """
227- desiredStates = SwerveDrive4Kinematics .desaturateWheelSpeeds (
236+ fl , fr , rl , rr = SwerveDrive4Kinematics .desaturateWheelSpeeds (
228237 desiredStates , DriveConstants .kMaxSpeedMetersPerSecond
229238 )
230- self .frontLeft .setDesiredState (desiredStates [ 0 ] )
231- self .frontRight .setDesiredState (desiredStates [ 1 ] )
232- self .rearLeft .setDesiredState (desiredStates [ 2 ] )
233- self .rearRight .setDesiredState (desiredStates [ 3 ] )
239+ self .frontLeft .setDesiredState (fl )
240+ self .frontRight .setDesiredState (fr )
241+ self .rearLeft .setDesiredState (rl )
242+ self .rearRight .setDesiredState (rr )
234243
235244 def resetEncoders (self ) -> None :
236245 """Resets the drive encoders to currently read a position of 0."""
@@ -248,7 +257,7 @@ def getHeading(self) -> float:
248257
249258 :returns: the robot's heading in degrees, from -180 to 180
250259 """
251- return Rotation2d .fromDegrees (self .gyro .getAngle ()).getDegrees ()
260+ return Rotation2d .fromDegrees (self .gyro .getAngle ()).degrees ()
252261
253262 def getTurnRate (self ) -> float :
254263 """Returns the turn rate of the robot.
0 commit comments