Skip to content

Commit c93e011

Browse files
committed
Apply TalonFX settings correctly
Correctly apply the TalonFX settings to the motor controllers after all settings been, well, set. modified: src/main/java/frc/robot/Constants.java modified: src/main/java/frc/robot/RobotContainer.java modified: src/main/java/frc/robot/commands/DriveCommands.java modified: src/main/java/frc/robot/generated/TunerConstants.java modified: src/main/java/frc/robot/subsystems/drive/Drive.java modified: src/main/java/frc/robot/subsystems/drive/ModuleIOBlended.java modified: src/main/java/frc/robot/subsystems/drive/ModuleIOTalonFX.java modified: src/main/java/frc/robot/subsystems/drive/SwerveConstants.java
1 parent c637e49 commit c93e011

8 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/main/java/frc/robot/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public final class Constants {
6969
// under strict caveat emptor -- and submit any error and bugfixes
7070
// via GitHub issues.
7171
private static SwerveType swerveType = SwerveType.PHOENIX6; // PHOENIX6, YAGSL
72-
private static CTREPro phoenixPro = CTREPro.UNLICENSED; // LICENSED, UNLICENSED
72+
private static CTREPro phoenixPro = CTREPro.LICENSED; // LICENSED, UNLICENSED
7373
private static AutoType autoType = AutoType.PATHPLANNER; // PATHPLANNER, CHOREO
7474
private static VisionType visionType = VisionType.NONE; // PHOTON, LIMELIGHT, NONE
7575

@@ -137,7 +137,7 @@ public static final class DrivebaseConstants {
137137

138138
// Drive and Turn PID constants
139139
public static final PIDConstants drivePID = new PIDConstants(0.05, 0.0, 0.0);
140-
public static final PIDConstants steerPID = new PIDConstants(5.0, 0.0, 0.4);
140+
public static final PIDConstants steerPID = new PIDConstants(2.0, 0.0, 0.4);
141141

142142
// Hold time on motor brakes when disabled
143143
public static final double kWheelLockTime = 10; // seconds

src/main/java/frc/robot/RobotContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private void configureBindings() {
228228
m_drivebase,
229229
() -> -driveStickY.value(),
230230
() -> -driveStickX.value(),
231-
() -> turnStickX.value()));
231+
() -> -turnStickX.value()));
232232

233233
// ** Example Commands -- Remap, remove, or change as desired **
234234
// Press B button while driving --> ROBOT-CENTRIC

src/main/java/frc/robot/commands/DriveCommands.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,15 @@ public static Command fieldRelativeDriveAtAngle(
179179
private static Translation2d getLinearVelocity(double x, double y) {
180180
// Apply deadband
181181
double linearMagnitude = MathUtil.applyDeadband(Math.hypot(x, y), OperatorConstants.kDeadband);
182-
Rotation2d linearDirection = new Rotation2d(x, y);
182+
Rotation2d linearDirection = new Rotation2d(Math.atan2(y, x));
183183

184184
// Square magnitude for more precise control
185185
// NOTE: The x & y values range from -1 to +1, so their squares are as well
186186
linearMagnitude = linearMagnitude * linearMagnitude;
187187

188188
// Return new linear velocity
189189
return new Pose2d(new Translation2d(), linearDirection)
190-
.transformBy(
191-
new Transform2d(linearVelocityFilter.calculate(linearMagnitude), 0.0, new Rotation2d()))
190+
.transformBy(new Transform2d(linearMagnitude, 0.0, new Rotation2d()))
192191
.getTranslation();
193192
}
194193

@@ -198,7 +197,7 @@ private static Translation2d getLinearVelocity(double x, double y) {
198197
*/
199198
private static double getOmega(double omega) {
200199
omega = MathUtil.applyDeadband(omega, OperatorConstants.kDeadband);
201-
return omegaFilter.calculate(Math.copySign(omega * omega, omega));
200+
return Math.copySign(omega * omega, omega);
202201
}
203202

204203
/***************************************************************************/

src/main/java/frc/robot/generated/TunerConstants.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class TunerConstants {
138138
private static final int kFrontLeftDriveMotorId = 1;
139139
private static final int kFrontLeftSteerMotorId = 2;
140140
private static final int kFrontLeftEncoderId = 3;
141-
private static final Angle kFrontLeftEncoderOffset = Rotations.of(-0.472412109375);
141+
private static final Angle kFrontLeftEncoderOffset = Rotations.of(0.087158203125);
142142
private static final boolean kFrontLeftSteerMotorInverted = true;
143143
private static final boolean kFrontLeftEncoderInverted = false;
144144

@@ -149,7 +149,7 @@ public class TunerConstants {
149149
private static final int kFrontRightDriveMotorId = 4;
150150
private static final int kFrontRightSteerMotorId = 5;
151151
private static final int kFrontRightEncoderId = 6;
152-
private static final Angle kFrontRightEncoderOffset = Rotations.of(-0.11376953125);
152+
private static final Angle kFrontRightEncoderOffset = Rotations.of(0.410888671875);
153153
private static final boolean kFrontRightSteerMotorInverted = true;
154154
private static final boolean kFrontRightEncoderInverted = false;
155155

@@ -160,7 +160,7 @@ public class TunerConstants {
160160
private static final int kBackLeftDriveMotorId = 7;
161161
private static final int kBackLeftSteerMotorId = 8;
162162
private static final int kBackLeftEncoderId = 9;
163-
private static final Angle kBackLeftEncoderOffset = Rotations.of(0.409423828125);
163+
private static final Angle kBackLeftEncoderOffset = Rotations.of(-0.1142578125);
164164
private static final boolean kBackLeftSteerMotorInverted = true;
165165
private static final boolean kBackLeftEncoderInverted = false;
166166

@@ -171,7 +171,7 @@ public class TunerConstants {
171171
private static final int kBackRightDriveMotorId = 10;
172172
private static final int kBackRightSteerMotorId = 11;
173173
private static final int kBackRightEncoderId = 12;
174-
private static final Angle kBackRightEncoderOffset = Rotations.of(0.086669921875);
174+
private static final Angle kBackRightEncoderOffset = Rotations.of(-0.471923828125);
175175
private static final boolean kBackRightSteerMotorInverted = true;
176176
private static final boolean kBackRightEncoderInverted = false;
177177

src/main/java/frc/robot/subsystems/drive/Drive.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ public Drive() {
129129
for (int i = 0; i < 4; i++) {
130130
switch (modType) {
131131
case 0b00000000: // ALL-CTRE
132-
// TODO: ADD CASE FOR USING NAVX!!!
133-
throw new RuntimeException(
134-
"For an all-CTRE drive base, use Phoenix Tuner X Swerve Generator instead of YAGSL!");
132+
if (kImuType == "navx" || kImuType == "navx_spi") {
133+
modules[i] = new Module(new ModuleIOTalonFX(i), i);
134+
} else {
135+
throw new RuntimeException(
136+
"For an all-CTRE drive base, use Phoenix Tuner X Swerve Generator instead of YAGSL!");
137+
}
135138
case 0b00010000: // Blended Talon Drive / NEO Steer
136139
modules[i] = new Module(new ModuleIOBlended(i), i);
137140
break;

src/main/java/frc/robot/subsystems/drive/ModuleIOBlended.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ public ModuleIOBlended(int module) {
185185
constants.DriveMotorInverted
186186
? InvertedValue.Clockwise_Positive
187187
: InvertedValue.CounterClockwise_Positive;
188-
PhoenixUtil.tryUntilOk(5, () -> driveTalon.getConfigurator().apply(driveConfig, 0.25));
189-
PhoenixUtil.tryUntilOk(5, () -> driveTalon.setPosition(0.0, 0.25));
190188

191189
// Configure turn motor
192190
var turnConfig = new SparkMaxConfig();
@@ -234,7 +232,6 @@ public ModuleIOBlended(int module) {
234232
constants.EncoderInverted
235233
? SensorDirectionValue.Clockwise_Positive
236234
: SensorDirectionValue.CounterClockwise_Positive;
237-
cancoder.getConfigurator().apply(cancoderConfig);
238235

239236
// Set motor Closed Loop Output type based on Phoenix Pro status
240237
constants.DriveMotorClosedLoopOutput =
@@ -243,6 +240,12 @@ public ModuleIOBlended(int module) {
243240
case UNLICENSED -> ClosedLoopOutputType.Voltage;
244241
};
245242

243+
// Finally, apply the configs to the motor controllers
244+
PhoenixUtil.tryUntilOk(5, () -> driveTalon.getConfigurator().apply(driveConfig, 0.25));
245+
PhoenixUtil.tryUntilOk(5, () -> driveTalon.setPosition(0.0, 0.25));
246+
cancoder.getConfigurator().apply(cancoderConfig);
247+
248+
246249
// Create timestamp queue
247250
timestampQueue = PhoenixOdometryThread.getInstance().makeTimestampQueue();
248251

src/main/java/frc/robot/subsystems/drive/ModuleIOTalonFX.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ public ModuleIOTalonFX(int module) {
128128
constants.DriveMotorInverted
129129
? InvertedValue.Clockwise_Positive
130130
: InvertedValue.CounterClockwise_Positive;
131-
tryUntilOk(5, () -> driveTalon.getConfigurator().apply(driveConfig, 0.25));
132-
tryUntilOk(5, () -> driveTalon.setPosition(0.0, 0.25));
133131

134132
// Configure turn motor
135133
var turnConfig = new TalonFXConfiguration();
@@ -147,7 +145,6 @@ public ModuleIOTalonFX(int module) {
147145
constants.SteerMotorInverted
148146
? InvertedValue.Clockwise_Positive
149147
: InvertedValue.CounterClockwise_Positive;
150-
tryUntilOk(5, () -> turnTalon.getConfigurator().apply(turnConfig, 0.25));
151148

152149
// Configure CANCoder
153150
CANcoderConfiguration cancoderConfig = constants.EncoderInitialConfigs;
@@ -156,7 +153,6 @@ public ModuleIOTalonFX(int module) {
156153
constants.EncoderInverted
157154
? SensorDirectionValue.Clockwise_Positive
158155
: SensorDirectionValue.CounterClockwise_Positive;
159-
cancoder.getConfigurator().apply(cancoderConfig);
160156

161157
// Set motor Closed Loop Output type and CANCoder feedback type based on Phoenix Pro status
162158
switch (Constants.getPhoenixPro()) {
@@ -170,6 +166,12 @@ public ModuleIOTalonFX(int module) {
170166
constants.SteerMotorClosedLoopOutput = ClosedLoopOutputType.Voltage;
171167
}
172168

169+
// Finally, apply the configs to the motor controllers
170+
tryUntilOk(5, () -> driveTalon.getConfigurator().apply(driveConfig, 0.25));
171+
tryUntilOk(5, () -> driveTalon.setPosition(0.0, 0.25));
172+
tryUntilOk(5, () -> turnTalon.getConfigurator().apply(turnConfig, 0.25));
173+
cancoder.getConfigurator().apply(cancoderConfig);
174+
173175
// Create timestamp queue
174176
timestampQueue = PhoenixOdometryThread.getInstance().makeTimestampQueue();
175177

src/main/java/frc/robot/subsystems/drive/SwerveConstants.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public class SwerveConstants {
137137
-Units.rotationsToRadians(TunerConstants.FrontLeft.EncoderOffset) + Math.PI;
138138
kFLDriveInvert = TunerConstants.FrontLeft.DriveMotorInverted;
139139
kFLSteerInvert = TunerConstants.FrontLeft.SteerMotorInverted;
140-
kFLEncoderInvert = false;
140+
kFLEncoderInvert = TunerConstants.FrontLeft.EncoderInverted;
141141
kFLXPosMeters = TunerConstants.FrontLeft.LocationX;
142142
kFLYPosMeters = TunerConstants.FrontLeft.LocationY;
143143
// Front Right
@@ -153,7 +153,7 @@ public class SwerveConstants {
153153
kFREncoderOffset = -Units.rotationsToRadians(TunerConstants.FrontRight.EncoderOffset);
154154
kFRDriveInvert = TunerConstants.FrontRight.DriveMotorInverted;
155155
kFRSteerInvert = TunerConstants.FrontRight.SteerMotorInverted;
156-
kFREncoderInvert = false;
156+
kFREncoderInvert = TunerConstants.FrontRight.EncoderInverted;
157157
kFRXPosMeters = TunerConstants.FrontRight.LocationX;
158158
kFRYPosMeters = TunerConstants.FrontRight.LocationY;
159159
// Back Left
@@ -170,7 +170,7 @@ public class SwerveConstants {
170170
-Units.rotationsToRadians(TunerConstants.BackLeft.EncoderOffset) + Math.PI;
171171
kBLDriveInvert = TunerConstants.BackLeft.DriveMotorInverted;
172172
kBLSteerInvert = TunerConstants.BackLeft.SteerMotorInverted;
173-
kBLEncoderInvert = false;
173+
kBLEncoderInvert = TunerConstants.BackLeft.EncoderInverted;
174174
kBLXPosMeters = TunerConstants.BackLeft.LocationX;
175175
kBLYPosMeters = TunerConstants.BackLeft.LocationY;
176176
// Back Right
@@ -186,7 +186,7 @@ public class SwerveConstants {
186186
kBREncoderOffset = -Units.rotationsToRadians(TunerConstants.BackRight.EncoderOffset);
187187
kBRDriveInvert = TunerConstants.BackRight.DriveMotorInverted;
188188
kBRSteerInvert = TunerConstants.BackRight.SteerMotorInverted;
189-
kBREncoderInvert = false;
189+
kBREncoderInvert = TunerConstants.BackRight.EncoderInverted;
190190
kBRXPosMeters = TunerConstants.BackRight.LocationX;
191191
kBRYPosMeters = TunerConstants.BackRight.LocationY;
192192
break;

0 commit comments

Comments
 (0)