Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 333aa0f

Browse files
committed
Improve logging in followSegment() and support indefinite following
1 parent c2e2d3d commit 333aa0f

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

src/main/java/frc/robot/autos/trailblazer/Trailblazer.java

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

33
import dev.doglog.DogLog;
44
import edu.wpi.first.math.controller.PIDController;
5+
import edu.wpi.first.math.geometry.Pose2d;
56
import edu.wpi.first.math.kinematics.ChassisSpeeds;
67
import edu.wpi.first.wpilibj2.command.Command;
78
import edu.wpi.first.wpilibj2.command.Commands;
@@ -38,36 +39,59 @@ public Trailblazer(SwerveSubsystem swerve, LocalizationSubsystem localization) {
3839
}
3940

4041
public Command followSegment(AutoSegment segment) {
41-
return Commands.runOnce(() -> pathTracker.resetAndSetPoints(segment.points))
42-
.alongWith(
43-
Commands.run(
42+
return followSegment(segment, true);
43+
}
44+
45+
public Command followSegment(AutoSegment segment, boolean shouldEnd) {
46+
var command =
47+
Commands.runOnce(
4448
() -> {
45-
pathTracker.updateRobotState(
46-
localization.getPose(), swerve.getFieldRelativeSpeeds());
47-
48-
var currentAutoPointIndex = pathTracker.getCurrentPointIndex();
49-
var currentAutoPoint = segment.points.get(currentAutoPointIndex);
50-
51-
var constrainedVelocityGoal =
52-
getSwerveSetpoint(currentAutoPoint, segment.defaultConstraints);
53-
swerve.setFieldRelativeAutoSpeeds(constrainedVelocityGoal);
54-
55-
DogLog.log("Trailblazer/Tracker/CurrentPointIndex", currentAutoPointIndex);
56-
if (previousAutoPointIndex != currentAutoPointIndex) {
57-
// Currently tracked point has changed, trigger side effects
58-
59-
// Each of the points in (previous, current]
60-
var pointsToRunSideEffectsFor =
61-
segment.points.subList(
62-
previousAutoPointIndex + 1, currentAutoPointIndex + 1);
63-
for (var passedPoint : pointsToRunSideEffectsFor) {
64-
passedPoint.command.schedule();
65-
}
66-
previousAutoPointIndex = currentAutoPointIndex;
67-
}
68-
},
69-
swerve))
70-
.until(pathTracker::isFinished);
49+
pathTracker.resetAndSetPoints(segment.points);
50+
51+
DogLog.log(
52+
"Trailblazer/CurrentSegment/InitialPoints",
53+
segment.points.stream()
54+
.map(point -> point.poseSupplier.get())
55+
.toArray(Pose2d[]::new));
56+
})
57+
.alongWith(
58+
Commands.run(
59+
() -> {
60+
pathTracker.updateRobotState(
61+
localization.getPose(), swerve.getFieldRelativeSpeeds());
62+
63+
var currentAutoPointIndex = pathTracker.getCurrentPointIndex();
64+
var currentAutoPoint = segment.points.get(currentAutoPointIndex);
65+
66+
var constrainedVelocityGoal =
67+
getSwerveSetpoint(currentAutoPoint, segment.defaultConstraints);
68+
swerve.setFieldRelativeAutoSpeeds(constrainedVelocityGoal);
69+
70+
DogLog.log("Trailblazer/Tracker/CurrentPointIndex", currentAutoPointIndex);
71+
if (previousAutoPointIndex != currentAutoPointIndex) {
72+
// Currently tracked point has changed, trigger side effects
73+
74+
// Each of the points in (previous, current]
75+
var pointsToRunSideEffectsFor =
76+
segment.points.subList(
77+
previousAutoPointIndex + 1, currentAutoPointIndex + 1);
78+
for (var passedPoint : pointsToRunSideEffectsFor) {
79+
DogLog.log(
80+
"Trailblazer/Tracker/CommandTriggered",
81+
passedPoint.command.getName());
82+
passedPoint.command.schedule();
83+
}
84+
previousAutoPointIndex = currentAutoPointIndex;
85+
}
86+
},
87+
swerve))
88+
.withName("FollowSegmentIndefinitely");
89+
90+
if (shouldEnd) {
91+
return command.until(pathTracker::isFinished).withName("FollowSegmentUntilFinished");
92+
}
93+
94+
return command;
7195
}
7296

7397
private ChassisSpeeds getSwerveSetpoint(

0 commit comments

Comments
 (0)