Skip to content

Commit 432cd16

Browse files
authored
Merge pull request #40 from DeepBlueRobotics/fix-swerve-drive
Set Swerve Drive Motor Voltage in `periodic()`
2 parents 6806784 + 555b98c commit 432cd16

1 file changed

Lines changed: 36 additions & 28 deletions

File tree

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum ModuleType {FL, FR, BL, BR};
4040
private boolean reversed;
4141
private Timer timer;
4242
private SimpleMotorFeedforward forwardSimpleMotorFF, backwardSimpleMotorFF, turnSimpleMotorFeedforward;
43-
private double lastAngle, maxAchievableTurnVelocityDps, maxAchievableTurnAccelerationMps2, turnToleranceDeg, angleDiff;
43+
private double desiredSpeed, lastAngle, maxAchievableTurnVelocityDps, maxAchievableTurnAccelerationMps2, turnToleranceDeg, angleDiff;
4444

4545
public SwerveModule(SwerveConfig config, ModuleType type, CANSparkMax drive, CANSparkMax turn, CANCoder turnEncoder,
4646
int arrIndex, Supplier<Float> pitchDegSupplier, Supplier<Float> rollDegSupplier) {
@@ -126,23 +126,40 @@ public ModuleType getType() {
126126

127127
private double prevTurnVelocity = 0;
128128
public void periodic() {
129-
double measuredAngleDegs = getModuleAngle();
130-
TrapezoidProfile.State goal = turnPIDController.getGoal();
131-
goal = new TrapezoidProfile.State(goal.position, goal.velocity);
132-
133-
double period = turnPIDController.getPeriod();
134-
double optimalTurnVelocityDps = Math.abs(MathUtil.inputModulus(goal.position-measuredAngleDegs, -180, 180))/period;
135-
setMaxTurnVelocity(Math.min(maxAchievableTurnVelocityDps, optimalTurnVelocityDps));
136-
137-
double turnSpeedCorrectionDps = turnPIDController.calculate(measuredAngleDegs) * turnSimpleMotorFeedforward.maxAchievableVelocity(12,0);
138-
TrapezoidProfile.State state = turnPIDController.getSetpoint();
139-
double turnVolts = turnSimpleMotorFeedforward.calculate(prevTurnVelocity+turnSpeedCorrectionDps, (state.velocity-prevTurnVelocity) / period);
140-
if (!turnPIDController.atGoal()) {
141-
turn.setVoltage(MathUtil.clamp(turnVolts, -12.0, 12.0));
142-
} else {
143-
turn.setVoltage(turnSimpleMotorFeedforward.calculate(goal.velocity));
129+
130+
// Drive Control
131+
{
132+
double actualSpeed = getCurrentSpeed();
133+
double targetVoltage = (actualSpeed >= 0 ? forwardSimpleMotorFF :
134+
backwardSimpleMotorFF).calculate(desiredSpeed, calculateAntiGravitationalA(pitchDegSupplier.get(), rollDegSupplier.get()));//clippedAcceleration);
135+
136+
// Use robot characterization as a simple physical model to account for internal resistance, frcition, etc.
137+
// Add a PID adjustment for error correction (also "drives" the actual speed to the desired speed)
138+
targetVoltage += drivePIDController.calculate(actualSpeed, desiredSpeed);
139+
double appliedVoltage = MathUtil.clamp(targetVoltage, -12, 12);
140+
drive.setVoltage(appliedVoltage);
141+
}
142+
143+
// Turn Control
144+
{
145+
double measuredAngleDegs = getModuleAngle();
146+
TrapezoidProfile.State goal = turnPIDController.getGoal();
147+
goal = new TrapezoidProfile.State(goal.position, goal.velocity);
148+
149+
double period = turnPIDController.getPeriod();
150+
double optimalTurnVelocityDps = Math.abs(MathUtil.inputModulus(goal.position-measuredAngleDegs, -180, 180))/period;
151+
setMaxTurnVelocity(Math.min(maxAchievableTurnVelocityDps, optimalTurnVelocityDps));
152+
153+
double turnSpeedCorrectionDps = turnPIDController.calculate(measuredAngleDegs) * turnSimpleMotorFeedforward.maxAchievableVelocity(12,0);
154+
TrapezoidProfile.State state = turnPIDController.getSetpoint();
155+
double turnVolts = turnSimpleMotorFeedforward.calculate(prevTurnVelocity+turnSpeedCorrectionDps, (state.velocity-prevTurnVelocity) / period);
156+
if (!turnPIDController.atGoal()) {
157+
turn.setVoltage(MathUtil.clamp(turnVolts, -12.0, 12.0));
158+
} else {
159+
turn.setVoltage(turnSimpleMotorFeedforward.calculate(goal.velocity));
160+
}
161+
prevTurnVelocity = state.velocity;
144162
}
145-
prevTurnVelocity = state.velocity;
146163
}
147164

148165
/**
@@ -177,22 +194,13 @@ private double calculateAntiGravitationalA(Float gyroPitchDeg, Float gyroRollDeg
177194
SmartDashboard.putNumber("AntiGravitational accelration", antiGravitationalA);
178195
return antiGravitationalA;
179196
}
197+
180198
/**
181199
* Sets the speed for the drive motor controller.
182200
* @param speedMps The desired speed in meters per second.
183201
*/
184202
private void setSpeed(double speedMps) {
185-
// Compute desired and actual speeds in m/s
186-
double desiredSpeed = speedMps * driveModifier;
187-
double actualSpeed = getCurrentSpeed();
188-
double targetVoltage = (actualSpeed >= 0 ? forwardSimpleMotorFF :
189-
backwardSimpleMotorFF).calculate(desiredSpeed, calculateAntiGravitationalA(pitchDegSupplier.get(), rollDegSupplier.get()));//clippedAcceleration);
190-
191-
// Use robot characterization as a simple physical model to account for internal resistance, frcition, etc.
192-
// Add a PID adjustment for error correction (also "drives" the actual speed to the desired speed)
193-
targetVoltage += drivePIDController.calculate(actualSpeed, desiredSpeed);
194-
double appliedVoltage = MathUtil.clamp(targetVoltage, -12, 12);
195-
drive.setVoltage(appliedVoltage);
203+
desiredSpeed = speedMps * driveModifier;
196204
}
197205

198206
/**

0 commit comments

Comments
 (0)