|
2 | 2 |
|
3 | 3 | import dev.doglog.DogLog;
|
4 | 4 | import edu.wpi.first.math.controller.PIDController;
|
| 5 | +import edu.wpi.first.math.geometry.Pose2d; |
5 | 6 | import edu.wpi.first.math.kinematics.ChassisSpeeds;
|
6 | 7 | import edu.wpi.first.wpilibj2.command.Command;
|
7 | 8 | import edu.wpi.first.wpilibj2.command.Commands;
|
@@ -38,36 +39,59 @@ public Trailblazer(SwerveSubsystem swerve, LocalizationSubsystem localization) {
|
38 | 39 | }
|
39 | 40 |
|
40 | 41 | 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( |
44 | 48 | () -> {
|
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; |
71 | 95 | }
|
72 | 96 |
|
73 | 97 | private ChassisSpeeds getSwerveSetpoint(
|
|
0 commit comments