44# the WPILib BSD license file in the root directory of this project.
55#
66
7- from rev import CANSparkMax , SparkMaxAbsoluteEncoder
7+ from rev import SparkMax , SparkMaxConfig , ClosedLoopConfig , SparkBase
88from wpimath .geometry import Rotation2d
99from wpimath .kinematics import SwerveModuleState , SwerveModulePosition
1010
@@ -24,97 +24,98 @@ def __init__(
2424 self .chassisAngularOffset = 0
2525 self .desiredState = SwerveModuleState (0.0 , Rotation2d ())
2626
27- self .drivingSparkMax = CANSparkMax (
28- drivingCANId , CANSparkMax .MotorType .kBrushless
29- )
30- self .turningSparkMax = CANSparkMax (
31- turningCANId , CANSparkMax .MotorType .kBrushless
32- )
27+ self .drivingSparkMax = SparkMax (drivingCANId , SparkMax .MotorType .kBrushless )
28+ self .turningSparkMax = SparkMax (turningCANId , SparkMax .MotorType .kBrushless )
3329
34- # Factory reset, so we get the SPARKS MAX to a known state before configuring
35- # them. This is useful in case a SPARK MAX is swapped out.
36- self .drivingSparkMax .restoreFactoryDefaults ()
37- self .turningSparkMax .restoreFactoryDefaults ()
30+ self .drivingConfig = SparkMaxConfig ()
31+ self .turningConfig = SparkMaxConfig ()
3832
3933 # Setup encoders and PID controllers for the driving and turning SPARKS MAX.
4034 self .drivingEncoder = self .drivingSparkMax .getEncoder ()
41- self .turningEncoder = self .turningSparkMax .getAbsoluteEncoder (
42- SparkMaxAbsoluteEncoder .Type .kDutyCycle
35+ self .turningEncoder = self .turningSparkMax .getAbsoluteEncoder ()
36+ self .drivingPidController = self .drivingSparkMax .getClosedLoopController ()
37+ self .turningPidController = self .turningSparkMax .getClosedLoopController ()
38+ self .drivingConfig .closedLoop .setFeedbackSensor (
39+ ClosedLoopConfig .FeedbackSensor .kPrimaryEncoder
40+ )
41+ self .turningConfig .closedLoop .setFeedbackSensor (
42+ ClosedLoopConfig .FeedbackSensor .kAbsoluteEncoder
4343 )
44- self .drivingPIDController = self .drivingSparkMax .getPIDController ()
45- self .turningPIDController = self .turningSparkMax .getPIDController ()
46- self .drivingPIDController .setFeedbackDevice (self .drivingEncoder )
47- self .turningPIDController .setFeedbackDevice (self .turningEncoder )
4844
4945 # Apply position and velocity conversion factors for the driving encoder. The
5046 # native units for position and velocity are rotations and RPM, respectively,
5147 # but we want meters and meters per second to use with WPILib's swerve APIs.
52- self .drivingEncoder . setPositionConversionFactor (
48+ self .drivingConfig . encoder . positionConversionFactor (
5349 ModuleConstants .kDrivingEncoderPositionFactor
5450 )
55- self .drivingEncoder . setVelocityConversionFactor (
51+ self .drivingConfig . encoder . velocityConversionFactor (
5652 ModuleConstants .kDrivingEncoderVelocityFactor
5753 )
5854
5955 # Apply position and velocity conversion factors for the turning encoder. We
6056 # want these in radians and radians per second to use with WPILib's swerve
6157 # APIs.
62- self .turningEncoder . setPositionConversionFactor (
58+ self .turningConfig . absoluteEncoder . positionConversionFactor (
6359 ModuleConstants .kTurningEncoderPositionFactor
6460 )
65- self .turningEncoder . setVelocityConversionFactor (
61+ self .turningConfig . absoluteEncoder . velocityConversionFactor (
6662 ModuleConstants .kTurningEncoderVelocityFactor
6763 )
6864
6965 # Invert the turning encoder, since the output shaft rotates in the opposite direction of
7066 # the steering motor in the MAXSwerve Module.
71- self .turningEncoder .setInverted (ModuleConstants .kTurningEncoderInverted )
67+ self .turningConfig .absoluteEncoder .inverted (
68+ ModuleConstants .kTurningEncoderInverted
69+ )
7270
7371 # Enable PID wrap around for the turning motor. This will allow the PID
7472 # controller to go through 0 to get to the setpoint i.e. going from 350 degrees
7573 # to 10 degrees will go through 0 rather than the other direction which is a
7674 # longer route.
77- self .turningPIDController . setPositionPIDWrappingEnabled (True )
78- self .turningPIDController . setPositionPIDWrappingMinInput (
75+ self .turningConfig . closedLoop . positionWrappingEnabled (True )
76+ self .turningConfig . closedLoop . positionWrappingMinInput (
7977 ModuleConstants .kTurningEncoderPositionPIDMinInput
8078 )
81- self .turningPIDController . setPositionPIDWrappingMaxInput (
79+ self .turningConfig . closedLoop . positionWrappingMaxInput (
8280 ModuleConstants .kTurningEncoderPositionPIDMaxInput
8381 )
8482
8583 # Set the PID gains for the driving motor. Note these are example gains, and you
8684 # may need to tune them for your own robot!
87- self .drivingPIDController . setP (ModuleConstants .kDrivingP )
88- self .drivingPIDController . setI (ModuleConstants .kDrivingI )
89- self .drivingPIDController . setD (ModuleConstants .kDrivingD )
90- self .drivingPIDController . setFF (ModuleConstants .kDrivingFF )
91- self .drivingPIDController . setOutputRange (
85+ self .drivingConfig . closedLoop . P (ModuleConstants .kDrivingP )
86+ self .drivingConfig . closedLoop . I (ModuleConstants .kDrivingI )
87+ self .drivingConfig . closedLoop . D (ModuleConstants .kDrivingD )
88+ self .drivingConfig . closedLoop . velocityFF (ModuleConstants .kDrivingFF )
89+ self .drivingConfig . closedLoop . outputRange (
9290 ModuleConstants .kDrivingMinOutput , ModuleConstants .kDrivingMaxOutput
9391 )
9492
9593 # Set the PID gains for the turning motor. Note these are example gains, and you
9694 # may need to tune them for your own robot!
97- self .turningPIDController . setP (ModuleConstants .kTurningP )
98- self .turningPIDController . setI (ModuleConstants .kTurningI )
99- self .turningPIDController . setD (ModuleConstants .kTurningD )
100- self .turningPIDController . setFF (ModuleConstants .kTurningFF )
101- self .turningPIDController . setOutputRange (
95+ self .turningConfig . closedLoop . P (ModuleConstants .kTurningP )
96+ self .turningConfig . closedLoop . I (ModuleConstants .kTurningI )
97+ self .turningConfig . closedLoop . D (ModuleConstants .kTurningD )
98+ self .turningConfig . closedLoop . velocityFF (ModuleConstants .kTurningFF )
99+ self .turningConfig . closedLoop . outputRange (
102100 ModuleConstants .kTurningMinOutput , ModuleConstants .kTurningMaxOutput
103101 )
104102
105- self .drivingSparkMax .setIdleMode (ModuleConstants .kDrivingMotorIdleMode )
106- self .turningSparkMax .setIdleMode (ModuleConstants .kTurningMotorIdleMode )
107- self .drivingSparkMax .setSmartCurrentLimit (
108- ModuleConstants .kDrivingMotorCurrentLimit
109- )
110- self .turningSparkMax .setSmartCurrentLimit (
111- ModuleConstants .kTurningMotorCurrentLimit
112- )
103+ self .drivingConfig .setIdleMode (ModuleConstants .kDrivingMotorIdleMode )
104+ self .turningConfig .setIdleMode (ModuleConstants .kTurningMotorIdleMode )
105+ # XXX -- can we set current limits?
113106
114107 # Save the SPARK MAX configurations. If a SPARK MAX browns out during
115108 # operation, it will maintain the above configurations.
116- self .drivingSparkMax .burnFlash ()
117- self .turningSparkMax .burnFlash ()
109+ self .drivingSparkMax .configure (
110+ self .drivingConfig ,
111+ SparkBase .ResetMode .kResetSafeParameters ,
112+ SparkBase .PersistMode .kPersistParameters ,
113+ )
114+ self .turningSparkMax .configure (
115+ self .turningConfig ,
116+ SparkBase .ResetMode .kResetSafeParameters ,
117+ SparkBase .PersistMode .kPersistParameters ,
118+ )
118119
119120 self .chassisAngularOffset = chassisAngularOffset
120121 self .desiredState .angle = Rotation2d (self .turningEncoder .getPosition ())
@@ -158,16 +159,16 @@ def setDesiredState(self, desiredState: SwerveModuleState) -> None:
158159 )
159160
160161 # Optimize the reference state to avoid spinning further than 90 degrees.
161- optimizedDesiredState = SwerveModuleState .optimize (
162+ SwerveModuleState .optimize (
162163 correctedDesiredState , Rotation2d (self .turningEncoder .getPosition ())
163164 )
164165
165166 # Command driving and turning SPARKS MAX towards their respective setpoints.
166- self .drivingPIDController .setReference (
167- optimizedDesiredState .speed , CANSparkMax .ControlType .kVelocity
167+ self .drivingPidController .setReference (
168+ correctedDesiredState .speed , SparkMax .ControlType .kVelocity
168169 )
169- self .turningPIDController .setReference (
170- optimizedDesiredState .angle .radians (), CANSparkMax .ControlType .kPosition
170+ self .turningPidController .setReference (
171+ correctedDesiredState .angle .radians (), SparkMax .ControlType .kPosition
171172 )
172173
173174 self .desiredState = desiredState
0 commit comments