Skip to content

Commit 895111e

Browse files
ShmayaRStrflightmight09Nummun14
authored
Improved Swerve (#26)
* set swerve to use velocity * it workssssssssss * removed feedforward gains * Update MotorSubsystem.java Co-Authored-By: Nummun14 <[email protected]> --------- Co-authored-by: Strflightmight09 <[email protected]> Co-authored-by: Nummun14 <[email protected]>
1 parent fd4379e commit 895111e

File tree

5 files changed

+25
-51
lines changed

5 files changed

+25
-51
lines changed

src/main/java/frc/trigon/robot/commands/CommandConstants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public class CommandConstants {
4848
AutonomousConstants.ROBOT_CONFIG.moduleLocations,
4949
RobotContainer.SWERVE::getDriveWheelPositionsRadians,
5050
() -> RobotContainer.SWERVE.getHeading().getRadians(),
51-
(omegaRadiansPerSecond) -> RobotContainer.SWERVE.selfRelativeDriveWithoutSetpointGeneration(new ChassisSpeeds(0, 0, omegaRadiansPerSecond), null),
51+
(omegaRadiansPerSecond) -> RobotContainer.SWERVE.selfRelativeDriveWithoutSetpointGeneration(new ChassisSpeeds(0, 0, omegaRadiansPerSecond)),
5252
RobotContainer.SWERVE
5353
),
5454
CALCULATE_CAMERA_POSITION_COMMAND = new CameraPositionCalculationCommand(
5555
RobotContainer.ROBOT_POSE_ESTIMATOR::getEstimatedRobotPose,
5656
Rotation2d.fromDegrees(0),
57-
(omegaRadiansPerSecond) -> RobotContainer.SWERVE.selfRelativeDriveWithoutSetpointGeneration(new ChassisSpeeds(0, 0, omegaRadiansPerSecond), null),
57+
(omegaRadiansPerSecond) -> RobotContainer.SWERVE.selfRelativeDriveWithoutSetpointGeneration(new ChassisSpeeds(0, 0, omegaRadiansPerSecond)),
5858
RobotContainer.SWERVE
5959
);
6060

src/main/java/frc/trigon/robot/subsystems/MotorSubsystem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public abstract class MotorSubsystem extends edu.wpi.first.wpilibj2.command.Subs
3434
DISABLED_TRIGGER.onFalse(new InstantCommand(() -> {
3535
setAllSubsystemsBrakeAsync(true);
3636
IS_BRAKING = true;
37-
RobotContainer.SWERVE.resetSetpoint();
3837
}).ignoringDisable(true));
3938
}
4039

src/main/java/frc/trigon/robot/subsystems/swerve/Swerve.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.pathplanner.lib.util.DriveFeedforwards;
44
import com.pathplanner.lib.util.swerve.SwerveSetpoint;
5-
import com.pathplanner.lib.util.swerve.SwerveSetpointGenerator;
65
import edu.wpi.first.math.geometry.Pose2d;
76
import edu.wpi.first.math.geometry.Rotation2d;
87
import edu.wpi.first.math.geometry.Translation3d;
@@ -19,7 +18,6 @@
1918
import frc.trigon.robot.subsystems.MotorSubsystem;
2019
import frc.trigon.robot.subsystems.swerve.swervemodule.SwerveModule;
2120
import frc.trigon.robot.subsystems.swerve.swervemodule.SwerveModuleConstants;
22-
import lib.hardware.RobotHardwareStats;
2321
import lib.hardware.phoenix6.Phoenix6SignalThread;
2422
import lib.hardware.phoenix6.pigeon2.Pigeon2Gyro;
2523
import lib.hardware.phoenix6.pigeon2.Pigeon2Signal;
@@ -33,10 +31,8 @@ public class Swerve extends MotorSubsystem {
3331
private final Pigeon2Gyro gyro = SwerveConstants.GYRO;
3432
private final SwerveModule[] swerveModules = SwerveConstants.SWERVE_MODULES;
3533
private final Phoenix6SignalThread phoenix6SignalThread = Phoenix6SignalThread.getInstance();
36-
private final SwerveSetpointGenerator setpointGenerator = new SwerveSetpointGenerator(AutonomousConstants.ROBOT_CONFIG, SwerveModuleConstants.MAXIMUM_MODULE_ROTATIONAL_SPEED_RADIANS_PER_SECOND);
3734
public Pose2d targetPathPlannerPose = new Pose2d();
3835
public boolean isPathPlannerDriving = false;
39-
private SwerveSetpoint previousSetpoint = new SwerveSetpoint(getSelfRelativeVelocity(), getModuleStates(), DriveFeedforwards.zeros(AutonomousConstants.ROBOT_CONFIG.numModules));
4036

4137
public Swerve() {
4238
setName("Swerve");
@@ -53,7 +49,7 @@ public void setBrake(boolean brake) {
5349
public void sysIDDrive(double targetCurrent) {
5450
SwerveModuleState[] a = SwerveConstants.KINEMATICS.toSwerveModuleStates(new ChassisSpeeds(0, 0, 2));
5551
for (int i = 0; i < 4; i++) {
56-
swerveModules[i].setDriveMotorTargetCurrent(targetCurrent);
52+
swerveModules[i].setDriveMotorTargetVoltage(targetCurrent);
5753
swerveModules[i].setTargetAngle(a[i].angle);
5854
}
5955
}
@@ -176,33 +172,22 @@ public void drivePathPlanner(ChassisSpeeds targetPathPlannerFeedforwardSpeeds, b
176172
* Used for PathPlanner because it generates setpoints automatically.
177173
*
178174
* @param targetSpeeds the pre generated robot relative target speeds
179-
* @param feedforwards the feedforwards to use
180175
*/
181-
public void selfRelativeDriveWithoutSetpointGeneration(ChassisSpeeds targetSpeeds, DriveFeedforwards feedforwards) {
176+
public void selfRelativeDriveWithoutSetpointGeneration(ChassisSpeeds targetSpeeds) {
182177
// if (isStill(targetSpeeds)) {
183178
// stop();
184179
// return;
185180
// }
186181

187182
final SwerveModuleState[] swerveModuleStates = SwerveConstants.KINEMATICS.toSwerveModuleStates(targetSpeeds);
188-
final double[] targetForcesNm;
189-
if (feedforwards == null)
190-
targetForcesNm = new double[]{0, 0, 0, 0};
191-
else
192-
targetForcesNm = feedforwards.linearForcesNewtons();
193-
194-
setTargetModuleStates(swerveModuleStates, targetForcesNm);
183+
setTargetModuleStates(swerveModuleStates);
195184
}
196185

197186
public Rotation2d getDriveRelativeAngle() {
198187
final Rotation2d currentAngle = RobotContainer.ROBOT_POSE_ESTIMATOR.getEstimatedRobotPose().getRotation();
199188
return Flippable.isRedAlliance() ? currentAngle.rotateBy(Rotation2d.fromDegrees(180)) : currentAngle;
200189
}
201190

202-
public void resetSetpoint() {
203-
previousSetpoint = new SwerveSetpoint(getSelfRelativeVelocity(), getModuleStates(), DriveFeedforwards.zeros(AutonomousConstants.ROBOT_CONFIG.numModules));
204-
}
205-
206191
public void initializeDrive(boolean shouldUseClosedLoop) {
207192
setClosedLoop(shouldUseClosedLoop);
208193
resetRotationController();
@@ -279,23 +264,18 @@ void selfRelativeDrive(double xPower, double yPower, FlippableRotation2d targetA
279264
* @param targetSpeeds the desired robot-relative targetSpeeds
280265
*/
281266
public void selfRelativeDrive(ChassisSpeeds targetSpeeds) {
282-
previousSetpoint = setpointGenerator.generateSetpoint(
283-
previousSetpoint,
284-
targetSpeeds,
285-
RobotHardwareStats.getPeriodicTimeSeconds()
286-
);
287-
288-
// if (isStill(previousSetpoint.robotRelativeSpeeds())) {
267+
// if (isStill(targetSpeeds) {
289268
// stop();
290269
// return;
291270
// }
292271

293-
setTargetModuleStates(previousSetpoint.moduleStates(), previousSetpoint.feedforwards().linearForcesNewtons());
272+
final SwerveModuleState[] swerveModuleStates = SwerveConstants.KINEMATICS.toSwerveModuleStates(targetSpeeds);
273+
setTargetModuleStates(swerveModuleStates);
294274
}
295275

296-
private void setTargetModuleStates(SwerveModuleState[] swerveModuleStates, double[] targetForcesNm) {
276+
private void setTargetModuleStates(SwerveModuleState[] swerveModuleStates) {
297277
for (int i = 0; i < swerveModules.length; i++)
298-
swerveModules[i].setTargetState(swerveModuleStates[i], targetForcesNm[i]);
278+
swerveModules[i].setTargetState(swerveModuleStates[i]);
299279
}
300280

301281
private void setClosedLoop(boolean shouldUseClosedLoop) {

src/main/java/frc/trigon/robot/subsystems/swerve/swervemodule/SwerveModule.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package frc.trigon.robot.subsystems.swerve.swervemodule;
22

33
import com.ctre.phoenix6.controls.PositionVoltage;
4-
import com.ctre.phoenix6.controls.TorqueCurrentFOC;
5-
import com.ctre.phoenix6.controls.VelocityTorqueCurrentFOC;
4+
import com.ctre.phoenix6.controls.VelocityVoltage;
65
import com.ctre.phoenix6.controls.VoltageOut;
76
import edu.wpi.first.math.geometry.Rotation2d;
87
import edu.wpi.first.math.kinematics.SwerveModulePosition;
@@ -25,9 +24,8 @@ public class SwerveModule {
2524
private final CANcoderEncoder steerEncoder;
2625
private final PositionVoltage steerPositionRequest = new PositionVoltage(0).withEnableFOC(SwerveModuleConstants.ENABLE_FOC);
2726
private final double wheelDiameter;
28-
private final VelocityTorqueCurrentFOC driveVelocityRequest = new VelocityTorqueCurrentFOC(0).withUpdateFreqHz(1000);
27+
private final VelocityVoltage driveVelocityRequest = new VelocityVoltage(0).withUpdateFreqHz(1000);
2928
private final VoltageOut driveVoltageRequest = new VoltageOut(0).withEnableFOC(SwerveModuleConstants.ENABLE_FOC);
30-
private final TorqueCurrentFOC driveTorqueCurrentFOCRequest = new TorqueCurrentFOC(0);
3129
private boolean shouldDriveMotorUseClosedLoop = true;
3230
private SwerveModuleState targetState = new SwerveModuleState();
3331
private double[]
@@ -50,15 +48,14 @@ public SwerveModule(int moduleID, double offsetRotations, double wheelDiameter)
5048
configureHardware(offsetRotations);
5149
}
5250

53-
public void setTargetState(SwerveModuleState targetState, double targetForceNm) {
51+
public void setTargetState(SwerveModuleState targetState) {
5452
if (willOptimize(targetState)) {
5553
targetState.optimize(getCurrentAngle());
56-
targetForceNm *= -1;
5754
}
5855

5956
this.targetState = targetState;
6057
setTargetAngle(targetState.angle);
61-
setTargetVelocity(targetState.speedMetersPerSecond, targetForceNm);
58+
setTargetVelocity(targetState.speedMetersPerSecond);
6259
}
6360

6461
public void setBrake(boolean brake) {
@@ -96,8 +93,8 @@ public void shouldDriveMotorUseClosedLoop(boolean shouldDriveMotorUseClosedLoop)
9693
this.shouldDriveMotorUseClosedLoop = shouldDriveMotorUseClosedLoop;
9794
}
9895

99-
public void setDriveMotorTargetCurrent(double targetCurrent) {
100-
driveMotor.setControl(driveTorqueCurrentFOCRequest.withOutput(targetCurrent));
96+
public void setDriveMotorTargetVoltage(double targetVoltage) {
97+
driveMotor.setControl(driveVoltageRequest.withOutput(targetVoltage));
10198
}
10299

103100
public void setTargetAngle(Rotation2d angle) {
@@ -145,21 +142,19 @@ private boolean willOptimize(SwerveModuleState state) {
145142
* The target velocity is set using either closed loop or open loop depending on {@link this#shouldDriveMotorUseClosedLoop}.
146143
*
147144
* @param targetVelocityMetersPerSecond the target velocity, in meters per second
148-
* @param targetForceNm the target force of the module in newton meters
149145
*/
150-
private void setTargetVelocity(double targetVelocityMetersPerSecond, double targetForceNm) {
146+
private void setTargetVelocity(double targetVelocityMetersPerSecond) {
151147
if (shouldDriveMotorUseClosedLoop) {
152-
setTargetClosedLoopVelocity(targetVelocityMetersPerSecond, targetForceNm);
148+
setTargetClosedLoopVelocity(targetVelocityMetersPerSecond);
153149
return;
154150
}
155151

156152
setTargetOpenLoopVelocity(targetVelocityMetersPerSecond);
157153
}
158154

159-
private void setTargetClosedLoopVelocity(double targetVelocityMetersPerSecond, double targetForceNm) {
155+
private void setTargetClosedLoopVelocity(double targetVelocityMetersPerSecond) {
160156
final double targetVelocityRotationsPerSecond = metersToDriveWheelRotations(targetVelocityMetersPerSecond);
161-
final double targetAccelerationRotationsPerSecondSquared = metersToDriveWheelRotations(targetForceNm);
162-
driveMotor.setControl(driveVelocityRequest.withVelocity(targetVelocityRotationsPerSecond).withAcceleration(targetAccelerationRotationsPerSecondSquared));
157+
driveMotor.setControl(driveVelocityRequest.withVelocity(targetVelocityRotationsPerSecond));
163158
}
164159

165160
private void setTargetOpenLoopVelocity(double targetVelocityMetersPerSecond) {

src/main/java/frc/trigon/robot/subsystems/swerve/swervemodule/SwerveModuleConstants.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static SimpleMotorSimulation createSteerMotorSimulation() {
5858
return new SimpleMotorSimulation(STEER_MOTOR_GEARBOX, REAR_STEER_MOTOR_GEAR_RATIO, STEER_MOMENT_OF_INERTIA);
5959
}
6060

61-
static TalonFXConfiguration generateDriveMotorConfiguration() {
61+
public static TalonFXConfiguration generateDriveMotorConfiguration() {
6262
final TalonFXConfiguration config = new TalonFXConfiguration();
6363

6464
config.Audio.BeepOnBoot = false;
@@ -77,12 +77,12 @@ static TalonFXConfiguration generateDriveMotorConfiguration() {
7777
config.ClosedLoopRamps.TorqueClosedLoopRampPeriod = 0.1;
7878
config.OpenLoopRamps.VoltageOpenLoopRampPeriod = 0.1;
7979

80-
config.Slot0.kP = RobotHardwareStats.isSimulation() ? 50 : 50;
80+
config.Slot0.kP = RobotHardwareStats.isSimulation() ? 0 : 50;
8181
config.Slot0.kI = RobotHardwareStats.isSimulation() ? 0 : 0;
8282
config.Slot0.kD = RobotHardwareStats.isSimulation() ? 0 : 0;
83-
config.Slot0.kS = RobotHardwareStats.isSimulation() ? 0.4708 : 5.25;
84-
config.Slot0.kV = RobotHardwareStats.isSimulation() ? 0 : 0;
85-
config.Slot0.kA = RobotHardwareStats.isSimulation() ? 0 : DRIVE_MOTOR_GEAR_RATIO / (1 / DCMotor.getKrakenX60Foc(1).KtNMPerAmp);
83+
config.Slot0.kS = RobotHardwareStats.isSimulation() ? 0.016046 : 5.25;
84+
config.Slot0.kV = RobotHardwareStats.isSimulation() ? 0.8774 : 0;
85+
config.Slot0.kA = RobotHardwareStats.isSimulation() ? 0.020691 : 0;
8686

8787
config.Feedback.VelocityFilterTimeConstant = 0;
8888

0 commit comments

Comments
 (0)