Skip to content

Commit 3f9593e

Browse files
committed
Refactor Drive periodic logic and improve formatting
Refactored Drive.java to split periodic logic into smaller functions for sensor updates, controller updates, and desired speed computation. Improved code formatting and consistency across vision and drive subsystems. Minor cleanups and formatting changes in GoalPoseChooser, Vision, VisionConstants, and CameraIOPV for readability.
1 parent eabee08 commit 3f9593e

7 files changed

Lines changed: 94 additions & 110 deletions

File tree

src/main/deploy/pathplanner/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
"bumperOffsetX": 0.0,
3030
"bumperOffsetY": 0.0,
3131
"robotFeatures": []
32-
}
32+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import frc.robot.systems.vision.Vision;
1818
import frc.robot.systems.vision.VisionConstants;
1919
import frc.robot.systems.vision.VisionConstants.Orientation;
20-
2120
import org.littletonrobotics.junction.networktables.LoggedDashboardChooser;
2221

2322
public class RobotContainer {
@@ -39,8 +38,12 @@ public RobotContainer() {
3938
},
4039
new GyroIOPigeon2(),
4140
new Vision(new CameraIO[] {
42-
new CameraIOPV(VisionConstants.kRightCamName, VisionConstants.kRightCamTransform, Orientation.BACK),
43-
new CameraIOPV(VisionConstants.kLeftCamName, VisionConstants.kLeftCamTransform, Orientation.BACK)
41+
new CameraIOPV(
42+
VisionConstants.kRightCamName,
43+
VisionConstants.kRightCamTransform,
44+
Orientation.BACK),
45+
new CameraIOPV(
46+
VisionConstants.kLeftCamName, VisionConstants.kLeftCamTransform, Orientation.BACK)
4447
}));
4548
break;
4649

@@ -54,8 +57,12 @@ public RobotContainer() {
5457
},
5558
new GyroIO() {},
5659
new Vision(new CameraIO[] {
57-
new CameraIOPV(VisionConstants.kRightCamName, VisionConstants.kRightCamTransform, Orientation.BACK),
58-
new CameraIOPV(VisionConstants.kLeftCamName, VisionConstants.kLeftCamTransform, Orientation.BACK)
60+
new CameraIOPV(
61+
VisionConstants.kRightCamName,
62+
VisionConstants.kRightCamTransform,
63+
Orientation.BACK),
64+
new CameraIOPV(
65+
VisionConstants.kLeftCamName, VisionConstants.kLeftCamTransform, Orientation.BACK)
5966
}));
6067
break;
6168

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

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -230,22 +230,7 @@ public Command customFollowPathComamnd(PathPlannerPath path, PPHolonomicDriveCon
230230
this);
231231
}
232232

233-
/*
234-
* If you broke it into functions: updateData -> setChassisSpeeds(data, state) -> runSwerve(speeds)
235-
* Periodic Logic:
236-
* Data Collection(Modules, gyro, Vision, odometry)
237-
* Update Controllers use
238-
* Setting Chassis Speeds based on driveState
239-
* Teleop sets based of joystick, Autons sets based on pathplanner(ppDesiredSpeeds), etc. etc.
240-
* runSwerve() function sets the desired state to modules
241-
* State is set by setDriveState() and related commands which also resets the controllers
242-
*/
243-
@Override
244-
public void periodic() {
245-
///////////////////// DATA COLLECTION AND UPDATE CONTROLLERS AND ODOMETRY \\\\\\\\\\\\\\\\\\
246-
/* Modules */
247-
for (Module module : modules) module.periodic();
248-
233+
private void updateSensorsAndOdometry() {
249234
/* GYRO */
250235
gyro.updateInputs(gyroInputs);
251236
Logger.processInputs("Drive/Gyro", gyroInputs);
@@ -265,31 +250,24 @@ public void periodic() {
265250
if (observation.hasObserved())
266251
poseEstimator.addVisionMeasurement(observation.pose(), observation.timeStamp(), observation.stdDevs());
267252

268-
Logger.recordOutput(
269-
observation.camName() + "/stdDevX", observation.stdDevs().get(0));
270-
Logger.recordOutput(
271-
observation.camName() + "/stdDevY", observation.stdDevs().get(1));
272-
Logger.recordOutput(
273-
observation.camName() + "/stdDevTheta",
274-
observation.stdDevs().get(2));
275-
// Logger.recordOutput(observation.camName()+"/TransformFromOdometry",
276-
// odometry.getPoseMeters().minus(observation.pose()));
253+
Logger.recordOutput(observation.camName() + "/stdDevX", observation.stdDevs().get(0));
254+
Logger.recordOutput(observation.camName() + "/stdDevY", observation.stdDevs().get(1));
255+
Logger.recordOutput(observation.camName() + "/stdDevTheta", observation.stdDevs().get(2));
277256
}
278257

279258
poseEstimator.update(robotRotation, getModulePositions());
280259
odometry.update(robotRotation, getModulePositions());
281260

282261
field.setRobotPose(getPoseEstimate());
262+
}
283263

284-
// Logger.recordOutput("Drive/Odometry/FieldCurrentChassisSpeeds",
285-
// ChassisSpeeds.fromRobotRelativeSpeeds(getRobotChassisSpeeds(), robotRotation));
286-
287-
/* Updating Controllers */
264+
private void updateDriveControllers() {
288265
headingController.updateHeadingController();
289266
autoAlignController.updateAlignmentControllers();
290267
GoalPoseChooser.updateSideStuff();
268+
}
291269

292-
///////////////////// SETTING DESIRED SPEEDS FROM DRIVE STATE \\\\\\\\\\\\\\\\\\
270+
private void computeDesiredSpeeds() {
293271
ChassisSpeeds teleopSpeeds =
294272
teleopController.computeChassiSpeeds(getPoseEstimate().getRotation(), getRobotChassisSpeeds(), false);
295273
switch (driveState) {
@@ -312,8 +290,7 @@ public void periodic() {
312290
headingController.getSnapOutput(getPoseEstimate().getRotation()));
313291
break;
314292
case INTAKE_HEADING_ALIGN:
315-
goalRotation = AllianceFlipUtil.apply(
316-
GoalPoseChooser.getIntakePose(getPoseEstimate()).getRotation());
293+
goalRotation = AllianceFlipUtil.apply(GoalPoseChooser.getIntakePose(getPoseEstimate()).getRotation());
317294
desiredSpeeds = new ChassisSpeeds(
318295
teleopSpeeds.vxMetersPerSecond,
319296
teleopSpeeds.vyMetersPerSecond,
@@ -327,8 +304,6 @@ public void periodic() {
327304
headingController.getSnapOutput(getPoseEstimate().getRotation()));
328305
break;
329306
case DRIVE_TO_CORAL:
330-
desiredSpeeds = autoAlignController.calculate(goalPose, getPoseEstimate());
331-
break;
332307
case DRIVE_TO_INTAKE:
333308
desiredSpeeds = autoAlignController.calculate(goalPose, getPoseEstimate());
334309
break;
@@ -343,9 +318,10 @@ public void periodic() {
343318
case DRIVE_TO_ALGAE:
344319
// desiredSpeeds = autoAlignController.calculate(goalPose, getPoseEstimate());
345320
ChassisSpeeds algaeAlignSpeeds = autoAlignController.calculate(goalPose, getPoseEstimate());
346-
double forwardJoy = (goalPose.getX() > AllianceFlipUtil.apply(frc.robot.game.FieldConstants.kReefCenter.getX()))
347-
? -teleopSpeeds.vxMetersPerSecond
348-
: teleopSpeeds.vxMetersPerSecond;
321+
double forwardJoy =
322+
(goalPose.getX() > AllianceFlipUtil.apply(frc.robot.game.FieldConstants.kReefCenter.getX()))
323+
? -teleopSpeeds.vxMetersPerSecond
324+
: teleopSpeeds.vxMetersPerSecond;
349325
if (AllianceFlipUtil.shouldFlip()) forwardJoy *= -1;
350326
desiredSpeeds = new ChassisSpeeds(
351327
/* Flips speed to preserve field relative. Not best solution, but probably good enough? */
@@ -389,8 +365,16 @@ public void periodic() {
389365
default:
390366
/* Defaults to Teleop control if no other cases are run*/
391367
}
368+
}
392369

393-
///////////////////////// SETS DESIRED CHASSIS SPEED TO MODULES \\\\\\\\\\\\\\\\\\\\\\\\
370+
@Override
371+
public void periodic() {
372+
for (Module module : modules) module.periodic();
373+
374+
updateSensorsAndOdometry();
375+
updateDriveControllers();
376+
computeDesiredSpeeds();
377+
394378
if (desiredSpeeds != null) runSwerve(desiredSpeeds);
395379
}
396380

@@ -409,8 +393,6 @@ public void setDriveState(DriveState state) {
409393
driveState = state;
410394
switch (driveState) {
411395
case PROCESSOR_HEADING_ALIGN:
412-
headingController.reset(getPoseEstimate().getRotation(), gyroInputs.yawVelocityPS);
413-
break;
414396
case REEF_HEADING_ALIGN:
415397
headingController.reset(getPoseEstimate().getRotation(), gyroInputs.yawVelocityPS);
416398
break;

src/main/java/frc/robot/systems/drive/controllers/GoalPoseChooser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ public static Pose2d getTargetPose(int pTagID) {
151151
}
152152

153153
public static Pose2d getTargetPose(int pTagID, double pXOffsetM, double pYOffsetM) {
154-
Pose2d tagPose =
155-
FieldConstants.kFieldLayout.getTagPose(pTagID).map(Pose3d::toPose2d).orElse(null);
154+
Pose2d tagPose = FieldConstants.kFieldLayout
155+
.getTagPose(pTagID)
156+
.map(Pose3d::toPose2d)
157+
.orElse(null);
156158

157159
Translation2d tagTranslation = tagPose.getTranslation()
158160
.plus(new Translation2d(

src/main/java/frc/robot/systems/vision/CameraIOPV.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REBELLION 10014
2+
13
package frc.robot.systems.vision;
24

35
import edu.wpi.first.math.geometry.Pose2d;
@@ -12,7 +14,6 @@
1214
import frc.robot.game.FieldConstants;
1315
import frc.robot.systems.vision.VisionConstants.CameraSimConfigs;
1416
import frc.robot.systems.vision.VisionConstants.Orientation;
15-
1617
import java.util.List;
1718
import java.util.Optional;
1819
import org.photonvision.EstimatedRobotPose;
@@ -40,32 +41,28 @@ public CameraIOPV(String pName, Transform3d pCameraTransform, Orientation pOrien
4041
this.mPhotonCam = new PhotonCamera(mCamName);
4142
this.mCameraTransform = pCameraTransform;
4243
this.mOrientation = pOrientation;
43-
44+
4445
PhotonCamera.setVersionCheckEnabled(false); // Avoid version spam
4546

4647
mPoseEstimator = new PhotonPoseEstimator(
47-
FieldConstants.kFieldLayout,
48-
PoseStrategy.MULTI_TAG_PNP_ON_COPROCESSOR,
49-
pCameraTransform
50-
);
48+
FieldConstants.kFieldLayout, PoseStrategy.MULTI_TAG_PNP_ON_COPROCESSOR, pCameraTransform);
5149

5250
mPoseEstimator.setMultiTagFallbackStrategy(PoseStrategy.CLOSEST_TO_LAST_POSE);
5351

5452
if (Constants.currentMode == Mode.SIM) {
5553
setupSimulation();
5654
}
5755
}
58-
56+
5957
private void setupSimulation() {
6058
mVisionSim = new VisionSystemSim("main");
6159
mVisionSim.addAprilTags(FieldConstants.kFieldLayout);
6260

6361
SimCameraProperties cameraProps = new SimCameraProperties();
6462
cameraProps.setCalibration(
65-
(int) CameraSimConfigs.resWidth.value,
66-
(int) CameraSimConfigs.resHeight.value,
67-
new Rotation2d(CameraSimConfigs.fovDeg.value)
68-
);
63+
(int) CameraSimConfigs.resWidth.value,
64+
(int) CameraSimConfigs.resHeight.value,
65+
new Rotation2d(CameraSimConfigs.fovDeg.value));
6966
cameraProps.setCalibError(CameraSimConfigs.avgErrorPx.value, CameraSimConfigs.errorStdDevPx.value);
7067
cameraProps.setFPS(CameraSimConfigs.fps.value);
7168
cameraProps.setAvgLatencyMs(CameraSimConfigs.avgLatencyMs.value);
@@ -75,7 +72,6 @@ private void setupSimulation() {
7572
mVisionSim.addCamera(mCameraSim, mCameraTransform);
7673
}
7774

78-
7975
@Override
8076
public void updateInputs(CameraIOInputs pInputs, Pose2d pLastRobotPose, Pose2d pSimOdomPose) {
8177
pInputs.iCamName = mCamName;
@@ -106,7 +102,8 @@ public void updateInputs(CameraIOInputs pInputs, Pose2d pLastRobotPose, Pose2d p
106102
pInputs.iIsConnected = mPhotonCam.isConnected();
107103

108104
if (latestValidResult == null || !latestValidResult.hasTargets()) {
109-
DriverStation.reportWarning("No valid pose found in unread PhotonVision results for " + mCamName, false);
105+
DriverStation.reportWarning(
106+
"No valid pose found in unread PhotonVision results for " + mCamName, false);
110107
pInputs.iHasTarget = false;
111108
return;
112109
}
@@ -125,10 +122,10 @@ public void updateInputs(CameraIOInputs pInputs, Pose2d pLastRobotPose, Pose2d p
125122
pInputs.iLatestTimestamp = latestValidResult.getTimestampSeconds();
126123

127124
latestEstimatedRobotPose.ifPresent(est -> {
128-
if (mOrientation == Orientation.BACK)
129-
pInputs.iLatestEstimatedRobotPose = est.estimatedPose.transformBy(new Transform3d(new Translation3d(), new Rotation3d(0.0, 0.0, Math.PI)));
130-
else
131-
pInputs.iLatestEstimatedRobotPose = est.estimatedPose;
125+
if (mOrientation == Orientation.BACK)
126+
pInputs.iLatestEstimatedRobotPose = est.estimatedPose.transformBy(
127+
new Transform3d(new Translation3d(), new Rotation3d(0.0, 0.0, Math.PI)));
128+
else pInputs.iLatestEstimatedRobotPose = est.estimatedPose;
132129

133130
int count = est.targetsUsed.size();
134131
Transform3d[] tagTransforms = new Transform3d[count];
@@ -137,13 +134,13 @@ public void updateInputs(CameraIOInputs pInputs, Pose2d pLastRobotPose, Pose2d p
137134
for (int i = 0; i < count; i++) {
138135
tagTransforms[i] = est.targetsUsed.get(i).getBestCameraToTarget();
139136
ambiguities[i] = est.targetsUsed.get(i).getPoseAmbiguity();
140-
}
137+
}
141138

142139
pInputs.iNumberOfTargets = count;
143140
pInputs.iLatestTagTransforms = tagTransforms;
144141
pInputs.iLatestTagAmbiguities = ambiguities;
145142
});
146-
143+
147144
} catch (Exception e) {
148145
e.printStackTrace();
149146
resetInputs(pInputs);

0 commit comments

Comments
 (0)