Skip to content

Commit f140495

Browse files
fix and apply drive pos/vel constants
also turn on smartdashboard for debugging
1 parent 90fc642 commit f140495

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/main/java/org/carlmontrobotics/lib199/swerve/SwerveModule.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ public SwerveModule(SwerveConfig config, ModuleType type, SparkMax drive, SparkM
7777
this.type = type;
7878
this.drive = drive;
7979

80-
double positionConstant = config.wheelDiameterMeters * Math.PI / config.driveGearing;
8180
driveConfig.inverted(config.driveInversion[arrIndex]);
8281
turnConfig.inverted(config.turnInversion[arrIndex]);
8382

84-
// drive.getEncoder().setPositionConversionFactor(positionConstant); //no such thing
85-
// drive.getEncoder().setVelocityConversionFactor(positionConstant / 60); //no such thing
86-
final double drivePositionFactor = positionConstant;
87-
final double turnPositionFactor = positionConstant / 60;
83+
double drivePositionFactor = config.wheelDiameterMeters * Math.PI / config.driveGearing;
84+
final double driveVelocityFactor = drivePositionFactor / 60;//why by 60?
85+
driveConfig.encoder
86+
.positionConversionFactor(drivePositionFactor)
87+
.velocityConversionFactor(driveVelocityFactor);
8888

8989
maxControllableAccerlationRps2 = 0;
9090
final double normalForceNewtons = 83.2 /* lbf */ * 4.4482 /* N/lbf */ / 4 /* numModules */;
@@ -108,10 +108,10 @@ public SwerveModule(SwerveConfig config, ModuleType type, SparkMax drive, SparkM
108108
config.drivekI[arrIndex],
109109
config.drivekD[arrIndex]);
110110

111-
/* offset for 1 CANcoder count */
111+
/* offset for 1 relative encoder count */
112112
drivetoleranceMPerS = (1.0
113-
/ (double)(drive.configAccessor.encoder.getCountsPerRevolution()) * positionConstant)
114-
/ Units.millisecondsToSeconds(drive.configAccessor.signals.getPrimaryEncoderPositionPeriodMs() * drive.configAccessor.absoluteEncoder.getAverageDepth());
113+
/ (double)(drive.configAccessor.encoder.getCountsPerRevolution()) * drivePositionFactor)
114+
/ Units.millisecondsToSeconds(drive.configAccessor.signals.getPrimaryEncoderPositionPeriodMs() * drive.configAccessor.encoder.getAverageDepth());
115115
drivePIDController.setTolerance(drivetoleranceMPerS);
116116

117117
//System.out.println("Velocity Constant: " + (positionConstant / 60));
@@ -134,21 +134,22 @@ public SwerveModule(SwerveConfig config, ModuleType type, SparkMax drive, SparkM
134134

135135
turnConstraints = new TrapezoidProfile.Constraints(maxAchievableTurnVelocityRps, maxAchievableTurnAccelerationRps2);
136136
lastAngle = 0.0;
137-
turnPIDController = new ProfiledPIDController(config.turnkP[arrIndex],
138-
config.turnkI[arrIndex],
139-
config.turnkD[arrIndex],
140-
turnConstraints);
137+
turnPIDController = new ProfiledPIDController(
138+
config.turnkP[arrIndex],
139+
config.turnkI[arrIndex],
140+
config.turnkD[arrIndex],
141+
turnConstraints);
141142
turnPIDController.enableContinuousInput(-.5, .5);
142143
turnPIDController.setTolerance(turnToleranceRot);
143-
144-
CANcoderConfiguration configs = new CANcoderConfiguration();
145-
configs.MagnetSensor.AbsoluteSensorDiscontinuityPoint = .5;
146-
this.turnEncoder = turnEncoder;
147-
this.turnEncoder.getConfigurator().apply(configs);
148144

149145
this.driveModifier = config.driveModifier;
150146
this.reversed = config.reversed[arrIndex];
151147
this.turnZeroDeg = config.turnZeroDeg[arrIndex];
148+
149+
CANcoderConfiguration CANconfig = new CANcoderConfiguration();
150+
CANconfig.MagnetSensor.AbsoluteSensorDiscontinuityPoint = .5;
151+
// CANconfig.MagnetSensor.MagnetOffset=-turnZeroDeg; //done in getModuleAngle.
152+
this.turnEncoder.getConfigurator().apply(CANconfig);
152153

153154
turnPIDController.reset(getModuleAngle());
154155

@@ -186,7 +187,7 @@ public ModuleType getType() {
186187
private double prevTurnVelocity = 0;
187188
public void periodic() {
188189
drivePeriodic();
189-
//updateSmartDashboard();
190+
updateSmartDashboard();
190191
turnPeriodic();
191192
}
192193

0 commit comments

Comments
 (0)