Skip to content

Commit 51a4019

Browse files
authored
Merge pull request #1 from SpiceGears/advantagekit-swerve-adjustments-#1
[FIXED, NEEDS TEST] Swerve drive final config- merge into advantagekit-swerve branch
2 parents d206967 + 0101c22 commit 51a4019

File tree

3 files changed

+52
-68
lines changed

3 files changed

+52
-68
lines changed

src/main/java/frc/robot/Robot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Robot() {
4747
switch (Constants.currentMode) {
4848
case REAL:
4949
// Running on a real robot, log to a USB stick ("/U/logs")
50-
Logger.addDataReceiver(new WPILOGWriter());
50+
// Logger.addDataReceiver(new WPILOGWriter());
5151
Logger.addDataReceiver(new NT4Publisher());
5252
break;
5353

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public class DriveConstants {
3131
};
3232

3333
// Zeroed rotation values for each module, see setup instructions
34-
public static final Rotation2d frontLeftZeroRotation = new Rotation2d(0.0);
35-
public static final Rotation2d frontRightZeroRotation = new Rotation2d(0.0);
36-
public static final Rotation2d backLeftZeroRotation = new Rotation2d(0.0);
37-
public static final Rotation2d backRightZeroRotation = new Rotation2d(0.0);
34+
public static final Rotation2d frontLeftZeroRotation = new Rotation2d(-0.532);
35+
public static final Rotation2d frontRightZeroRotation = new Rotation2d(1.275);
36+
public static final Rotation2d backLeftZeroRotation = new Rotation2d(-0.451);
37+
public static final Rotation2d backRightZeroRotation = new Rotation2d(1.968);
3838

3939
// Device CAN IDs
4040
public static final int pigeonCanId = 11;
@@ -49,6 +49,11 @@ public class DriveConstants {
4949
public static final int frontRightTurnCanId = 5;
5050
public static final int backRightTurnCanId = 7;
5151

52+
public static final int frontLeftAbsoluteEncoderChannel = 0;
53+
public static final int frontRightAbsoluteEncoderChannel = 1;
54+
public static final int backLeftAbsoluteEncoderChannel = 3;
55+
public static final int backRightAbsoluteEncoderChannel = 2;
56+
5257
// Drive motor configuration
5358
public static final int driveMotorCurrentLimit = 40;
5459
public static final double wheelRadiusMeters = Meter.convertFrom(5, Centimeter);
@@ -77,20 +82,22 @@ public class DriveConstants {
7782
public static final double driveSimKv = 0.0789;
7883

7984
// Turn motor configuration
80-
public static final boolean turnInverted = false;
85+
public static final boolean turnInverted = true;
8186
public static final int turnMotorCurrentLimit = 20;
8287
public static final double turnMotorReduction = /*9424.0 / 203.0*/ 21.42;
8388
public static final DCMotor turnGearbox = DCMotor.getNEO(1);
8489

8590
// Turn encoder configuration
86-
public static final boolean turnEncoderInverted = true;
87-
public static final double turnEncoderPositionFactor = 1;
88-
// 2 * Math.PI; // Rotations -> Radians
91+
public static final boolean turnEncoderInverted = false;
92+
public static final double turnEncoderPositionFactor =
93+
2
94+
* Math.PI
95+
/ turnMotorReduction; // Rotations (0-1) * 360deg to convert 0-1 range to 0-360 -> Radians
8996
public static final double turnEncoderVelocityFactor =
90-
1; // (2 * Math.PI) / 60.0; // RPM -> Rad/Sec
97+
(2 * Math.PI) / 60.0 / turnMotorReduction; // RPM -> Rad/Sec
9198

9299
// Turn PID configuration
93-
public static final double turnKp = 0.1;
100+
public static final double turnKp = 0.2;
94101
public static final double turnKd = 0.0;
95102
public static final double turnSimP = 8.0;
96103
public static final double turnSimD = 0.0;

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

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package frc.robot.subsystems.drive;
99

10+
import static edu.wpi.first.units.Units.Degree;
11+
import static edu.wpi.first.units.Units.Radians;
1012
import static frc.robot.subsystems.drive.DriveConstants.*;
1113
import static frc.robot.util.SparkUtil.*;
1214

@@ -43,7 +45,9 @@ public class ModuleIOSpark implements ModuleIO {
4345
private final SparkBase driveSpark;
4446
private final SparkBase turnSpark;
4547
private final RelativeEncoder driveEncoder;
46-
private final AbsoluteEncoder turnEncoder;
48+
private final RelativeEncoder turnEncoder;
49+
50+
private final AnalogEncoder absoluteEncoder; // MODIFIED
4751

4852
// Closed loop controllers
4953
private final SparkClosedLoopController driveController;
@@ -92,27 +96,17 @@ public ModuleIOSpark(int module) {
9296
},
9397
MotorType.kBrushless);
9498
driveEncoder = driveSpark.getEncoder();
95-
// turnEncoder = turnSpark.getAbsoluteEncoder();
96-
97-
// # BEGIN CUSTOMIZED CODE:
98-
switch (module) {
99-
case 0:
100-
turnEncoder = new AbsoluteAnalogEncoder(0);
101-
break;
102-
case 1:
103-
turnEncoder = new AbsoluteAnalogEncoder(1);
104-
break;
105-
case 2:
106-
turnEncoder = new AbsoluteAnalogEncoder(3);
107-
break;
108-
case 3:
109-
turnEncoder = new AbsoluteAnalogEncoder(2);
110-
break;
111-
default:
112-
turnEncoder = new AbsoluteAnalogEncoder(5);
113-
}
99+
turnEncoder = turnSpark.getEncoder();
114100

115-
// # END CUSTOMIZED CODE.
101+
absoluteEncoder =
102+
new AnalogEncoder(
103+
switch (module) {
104+
case 0 -> frontLeftAbsoluteEncoderChannel;
105+
case 1 -> frontRightAbsoluteEncoderChannel;
106+
case 2 -> backLeftAbsoluteEncoderChannel;
107+
case 3 -> backRightAbsoluteEncoderChannel;
108+
default -> 0;
109+
});
116110

117111
driveController = driveSpark.getClosedLoopController();
118112
turnController = turnSpark.getClosedLoopController();
@@ -164,25 +158,22 @@ public ModuleIOSpark(int module) {
164158
.smartCurrentLimit(turnMotorCurrentLimit)
165159
.voltageCompensation(12.0);
166160
turnConfig
167-
.absoluteEncoder
168-
.inverted(turnEncoderInverted)
161+
.encoder
162+
// .inverted(turnEncoderInverted)
169163
.positionConversionFactor(turnEncoderPositionFactor)
170164
.velocityConversionFactor(turnEncoderVelocityFactor)
171-
.averageDepth(2);
172-
// We do NOT attach the RoboRIO analog absolute encoder to the SPARK as a
173-
// closed-loop sensor (the analog encoder is wired to the RoboRIO). Instead
174-
// we run the turn PID on the RoboRIO using WPILib's PIDController and
175-
// command voltages to the SPARK. Configure only the SPARK parameters here.
165+
.uvwAverageDepth(2);
176166
turnConfig
177167
.closedLoop
168+
.feedbackSensor(FeedbackSensor.kPrimaryEncoder)
178169
.positionWrappingEnabled(true)
179170
.positionWrappingInputRange(turnPIDMinInput, turnPIDMaxInput);
180171
turnConfig
181172
.signals
182-
.absoluteEncoderPositionAlwaysOn(true)
183-
.absoluteEncoderPositionPeriodMs((int) (1000.0 / odometryFrequency))
184-
.absoluteEncoderVelocityAlwaysOn(true)
185-
.absoluteEncoderVelocityPeriodMs(20)
173+
.primaryEncoderPositionAlwaysOn(true)
174+
.primaryEncoderPositionPeriodMs((int) (1000.0 / odometryFrequency))
175+
.primaryEncoderVelocityAlwaysOn(true)
176+
.primaryEncoderVelocityPeriodMs(20)
186177
.appliedOutputPeriodMs(20)
187178
.busVoltagePeriodMs(20)
188179
.outputCurrentPeriodMs(20);
@@ -193,6 +184,10 @@ public ModuleIOSpark(int module) {
193184
turnSpark.configure(
194185
turnConfig, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters));
195186

187+
tryUntilOk(
188+
turnSpark,
189+
5,
190+
() -> turnEncoder.setPosition(Radians.convertFrom(absoluteEncoder.get() * 360, Degree)));
196191
// Create odometry queues
197192
timestampQueue = SparkOdometryThread.getInstance().makeTimestampQueue();
198193
drivePositionQueue =
@@ -202,7 +197,7 @@ public ModuleIOSpark(int module) {
202197
// instead of going through the Spark-specific sampling/error path which can drop
203198
// or mis-time samples for a non-Spark sensor and produce jitter in AdvantageScope.
204199
turnPositionQueue =
205-
SparkOdometryThread.getInstance().registerSignal(() -> turnEncoder.getPosition());
200+
SparkOdometryThread.getInstance().registerSignal(turnSpark, turnEncoder::getPosition);
206201
//// MODIFIED
207202
}
208203

@@ -220,30 +215,12 @@ public void updateInputs(ModuleIOInputs inputs) {
220215
inputs.driveConnected = driveConnectedDebounce.calculate(!sparkStickyFault);
221216

222217
// Update turn inputs
223-
// Read the external RoboRIO-mounted analog absolute encoder directly. Avoid using the
224-
// Spark-registered sampling path for this signal because the analog encoder is not
225-
// attached to the Spark and mixing the two sampling paths can produce missing/erratic
226-
// samples in AdvantageScope.
227-
boolean turnReadOk = true;
228-
double turnPos = 0.0;
229-
double turnVel = 0.0;
230-
try {
231-
turnPos = turnEncoder.getPosition();
232-
turnVel = turnEncoder.getVelocity();
233-
} catch (Exception e) {
234-
// If reading fails, mark read as not-ok and leave defaults.
235-
turnReadOk = false;
236-
}
237-
if (turnReadOk) {
238-
inputs.turnPosition = new Rotation2d(turnPos).minus(zeroRotation);
239-
inputs.turnVelocityRadPerSec = turnVel;
240-
} else {
241-
// fallback: keep velocity zero if we couldn't read the analog encoder.
242-
inputs.turnVelocityRadPerSec = 0.0;
243-
}
244-
245-
// Still read Spark-side telemetry (applied output, bus voltage, current) via the
246-
// Spark-safe helpers.
218+
sparkStickyFault = false;
219+
ifOk(
220+
turnSpark,
221+
turnEncoder::getPosition,
222+
(value) -> inputs.turnPosition = new Rotation2d(value).minus(zeroRotation));
223+
ifOk(turnSpark, turnEncoder::getVelocity, (value) -> inputs.turnVelocityRadPerSec = value);
247224
ifOk(
248225
turnSpark,
249226
new DoubleSupplier[] {turnSpark::getAppliedOutput, turnSpark::getBusVoltage},

0 commit comments

Comments
 (0)