-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVision.java
More file actions
435 lines (387 loc) · 16.3 KB
/
Copy pathVision.java
File metadata and controls
435 lines (387 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) 2021-2025 Littleton Robotics
// http://github.com/Mechanical-Advantage
//
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file
// at the root directory of this project.
package frc.robot.subsystems.vision;
import static frc.robot.subsystems.vision.VisionConstants.*;
import edu.wpi.first.apriltag.AprilTagFieldLayout;
import edu.wpi.first.math.Matrix;
import edu.wpi.first.math.VecBuilder;
import edu.wpi.first.math.filter.LinearFilter;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rectangle2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;
import edu.wpi.first.wpilibj.Alert;
import edu.wpi.first.wpilibj.Alert.AlertType;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
import frc.robot.subsystems.vision.VisionIO.PoseObservation;
import frc.robot.util.VisionThread;
import frc.robot.util.VisionThread.VisionInputs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.function.Supplier;
import org.littletonrobotics.junction.Logger;
public class Vision extends SubsystemBase {
// Cached arena boundary for withinBoundaries test (avoids allocations per observation)
private static final Rectangle2d arenaRectangle;
static {
double halfWidth = minRobotWidthHalfMeters;
Translation2d cornerA = new Translation2d(halfWidth, halfWidth);
Translation2d cornerB =
new Translation2d(fieldXLenMeters - halfWidth, fieldYLenMeters - halfWidth);
arenaRectangle = new Rectangle2d(cornerA, cornerB);
}
private final VisionConsumer consumer;
private final Supplier<Pose2d> chassisPoseSupplier;
private final VisionIO[] io;
private final VisionInputs[] visionInputs;
private final VisionIOInputsAutoLogged[] inputs;
private final Alert[] disconnectedAlerts;
// Initialize logging values
private ArrayList<Pose3d> allTagPoses = new ArrayList<Pose3d>();
private ArrayList<Pose3d> allRobotPoses = new ArrayList<Pose3d>();
private ArrayList<Pose3d> allRobotPosesAccepted = new ArrayList<Pose3d>();
private ArrayList<Pose3d> allRobotPosesRejected = new ArrayList<Pose3d>();
// List to store acceptable observations
private ArrayList<TestedObservation> observations = new ArrayList<TestedObservation>();
// Initialize logging values
private ArrayList<Pose3d> tagPoses = new ArrayList<Pose3d>();
private ArrayList<Pose3d> robotPoses = new ArrayList<Pose3d>();
private ArrayList<Pose3d> robotPosesAccepted = new ArrayList<Pose3d>();
private ArrayList<Pose3d> robotPosesRejected = new ArrayList<Pose3d>();
// Initialize scoring results
private EnumMap<VisionTest, Double> testResults = new EnumMap<>(VisionTest.class);
LinearFilter[] cameraPassRate = {
LinearFilter.movingAverage(20),
LinearFilter.movingAverage(20),
LinearFilter.movingAverage(20),
LinearFilter.movingAverage(20)
};
// Cycle counter for throttled logging
private int loopCounter = 0;
public Vision(VisionConsumer consumer, Supplier<Pose2d> chassisPoseSupplier, VisionIO... io) {
this.consumer = consumer;
this.chassisPoseSupplier = chassisPoseSupplier;
this.io = io;
// Register each VisionIO with VisionThread for background polling
this.visionInputs = new VisionInputs[io.length];
for (int i = 0; i < io.length; i++) {
visionInputs[i] = VisionThread.getInstance().registerVisionIO(io[i]);
}
// Initialize inputs for AdvantageKit logging
this.inputs = new VisionIOInputsAutoLogged[io.length];
for (int i = 0; i < inputs.length; i++) {
inputs[i] = new VisionIOInputsAutoLogged();
}
// Initialize disconnected alerts
this.disconnectedAlerts = new Alert[io.length];
for (int i = 0; i < inputs.length; i++) {
disconnectedAlerts[i] =
new Alert(
"Vision camera " + Integer.toString(i) + " is disconnected.", AlertType.kWarning);
}
}
/**
* Returns the yaw angle to the best target, which can be used for simple servoing with vision.
*
* @param cameraIndex The index of the camera to use.
*/
public Rotation2d getTargetX(int cameraIndex) {
return inputs[cameraIndex].latestTargetObservation.yaw();
}
@Override
public void periodic() {
long visionStart = Constants.PROFILING_ENABLED ? System.nanoTime() : 0;
loopCounter++;
// Copy cached inputs from background thread (should be fast - volatile reads)
for (int i = 0; i < io.length; i++) {
visionInputs[i].getSnapshot().copyTo(inputs[i]);
}
long t1 = Constants.PROFILING_ENABLED ? System.nanoTime() : 0;
// Log inputs via AdvantageKit (throttled - serialization is expensive)
// Note: Throttling reduces CPU load but loses data granularity for replay
if (loopCounter % kLoggingDivisor == 0) {
for (int i = 0; i < io.length; i++) {
Logger.processInputs("Vision/Camera" + Integer.toString(i), inputs[i]);
}
}
long t2 = Constants.PROFILING_ENABLED ? System.nanoTime() : 0;
// Initialize logging values
allTagPoses.clear();
allRobotPoses.clear();
allRobotPosesAccepted.clear();
allRobotPosesRejected.clear();
// List to store acceptable observations
observations.clear();
// Loop over cameras
for (int cameraIndex = 0; cameraIndex < io.length; cameraIndex++) {
// Update disconnected alert
disconnectedAlerts[cameraIndex].set(!inputs[cameraIndex].connected);
// Initialize logging values
tagPoses.clear();
robotPoses.clear();
robotPosesAccepted.clear();
robotPosesRejected.clear();
// Add tag poses
for (int tagId : inputs[cameraIndex].tagIds) {
var tagPose = getAprilTagLayout().getTagPose(tagId);
if (tagPose.isPresent()) {
tagPoses.add(tagPose.get());
}
}
// Loop over pose observations
for (var observation : inputs[cameraIndex].poseObservations) {
testResults.clear();
testResults.put(VisionTest.moreThanZeroTags, VisionTest.moreThanZeroTags.test(observation));
testResults.put(VisionTest.unambiguous, VisionTest.unambiguous.test(observation));
testResults.put(VisionTest.pitchError, VisionTest.pitchError.test(observation));
testResults.put(VisionTest.rollError, VisionTest.rollError.test(observation));
testResults.put(VisionTest.heightError, VisionTest.heightError.test(observation));
testResults.put(VisionTest.withinBoundaries, VisionTest.withinBoundaries.test(observation));
testResults.put(VisionTest.distanceToTags, VisionTest.distanceToTags.test(observation));
// Multiply all test scores - loop avoids stream overhead and boxing
double totalScore = 1.0;
for (Double score : testResults.values()) {
totalScore *= score;
}
observations.add(new TestedObservation(observation, cameraIndex, testResults, totalScore));
// Add pose to log
robotPoses.add(observation.pose());
if (totalScore > minScore) {
robotPosesAccepted.add(observation.pose());
} else {
robotPosesRejected.add(observation.pose());
}
cameraPassRate[cameraIndex].calculate(totalScore);
}
// Log camera datadata
if (kLogIndividualCameraPoses) {
Logger.recordOutput(
"Vision/Camera" + Integer.toString(cameraIndex) + "/TagPoses",
tagPoses.toArray(new Pose3d[tagPoses.size()]));
Logger.recordOutput(
"Vision/Camera" + Integer.toString(cameraIndex) + "/RobotPoses",
robotPoses.toArray(new Pose3d[robotPoses.size()]));
Logger.recordOutput(
"Vision/Camera" + Integer.toString(cameraIndex) + "/RobotPosesAccepted",
robotPosesAccepted.toArray(new Pose3d[robotPosesAccepted.size()]));
Logger.recordOutput(
"Vision/Camera" + Integer.toString(cameraIndex) + "/RobotPosesRejected",
robotPosesRejected.toArray(new Pose3d[robotPosesRejected.size()]));
Logger.recordOutput(
"Vision/Camera" + Integer.toString(cameraIndex) + "/PassRate",
cameraPassRate[cameraIndex].lastValue());
}
allTagPoses.addAll(tagPoses);
allRobotPoses.addAll(robotPoses);
allRobotPosesAccepted.addAll(robotPosesAccepted);
allRobotPosesRejected.addAll(robotPosesRejected);
}
long t3 = Constants.PROFILING_ENABLED ? System.nanoTime() : 0;
// Remove unacceptable observations
observations.removeIf(o -> o.score < minScore);
// Sort the list of acceptable observations by timestamp
observations.sort(
(lhs, rhs) -> (int) Math.signum(lhs.observation.timestamp() - rhs.observation.timestamp()));
for (var o : observations) {
// Calculate standard deviations
double linearStdDev = linearStdDevBaseline / o.score;
double angularStdDev = angularStdDevBaseline / o.score;
// Send acceptable vision observations to the pose estimator with their stddevs
consumer.accept(
o.observation.pose().toPose2d(),
o.observation.timestamp(),
VecBuilder.fill(linearStdDev, linearStdDev, angularStdDev));
Logger.recordOutput("Vision/Summary/ObservationScore", o.score);
}
long t4 = Constants.PROFILING_ENABLED ? System.nanoTime() : 0;
// Log summary data (throttled along with processInputs)
if (loopCounter % kLoggingDivisor == 0) {
if (kLogSummaryPoses) {
Logger.recordOutput("Vision/Summary/TagPoses", allTagPoses.toArray(Pose3d[]::new));
Logger.recordOutput("Vision/Summary/RobotPoses", allRobotPoses.toArray(Pose3d[]::new));
}
if (kLogAcceptedPoses) {
Logger.recordOutput(
"Vision/Summary/RobotPosesAccepted", allRobotPosesAccepted.toArray(Pose3d[]::new));
}
if (kLogRejectedPoses) {
Logger.recordOutput(
"Vision/Summary/RobotPosesRejected", allRobotPosesRejected.toArray(Pose3d[]::new));
}
}
long t5 = Constants.PROFILING_ENABLED ? System.nanoTime() : 0;
// Profiling output
if (Constants.PROFILING_ENABLED) {
Logger.recordOutput("Profiling/Vision/SnapshotMs", (t1 - visionStart) / 1_000_000);
Logger.recordOutput("Profiling/Vision/ProcessInputsMs", (t2 - t1) / 1_000_000);
Logger.recordOutput("Profiling/Vision/CameraLoopMs", (t3 - t2) / 1_000_000);
Logger.recordOutput("Profiling/Vision/ConsumerMs", (t4 - t3) / 1_000_000);
Logger.recordOutput("Profiling/Vision/SummaryLogMs", (t5 - t4) / 1_000_000);
Logger.recordOutput("Profiling/Vision/TotalMs", (t5 - visionStart) / 1_000_000);
}
}
@FunctionalInterface
public static interface VisionConsumer {
public void accept(
Pose2d visionRobotPoseMeters,
double timestampSeconds,
Matrix<N3, N1> visionMeasurementStdDevs);
}
// Associate observations with their camera
public static record TestedObservation(
PoseObservation observation,
int cameraIndex,
EnumMap<VisionTest, Double> testResults,
double score) {}
// Caching for AprilTag layout (volatile for thread-safe lazy initialization)
private static volatile AprilTagFieldLayout cachedLayout = null;
/** Returns the AprilTag layout to use, loading it if necessary. Thread-safe. */
public static synchronized AprilTagFieldLayout getAprilTagLayout() {
if (cachedLayout == null) {
// Try to load custom layout only if requested and not connected to FMS
if (useCustomAprilTagLayout && !DriverStation.isFMSAttached()) {
try {
cachedLayout = new AprilTagFieldLayout(customAprilTagLayoutPath);
} catch (IOException e) {
System.err.println("Error loading custom AprilTag layout: " + e.getMessage());
}
}
// Otherwise load default layout
if (cachedLayout == null) {
cachedLayout = AprilTagFieldLayout.loadField(defaultAprilTagFieldLayout);
}
}
return cachedLayout;
}
public enum VisionTest {
unambiguous {
/**
* Penalizes ambiguous observations of a single tag, where ambiguity is defined as the ratio
* of best:alternate pose reprojection errors. This is between 0 and 1 (0 being no ambiguity,
* and 1 meaning both have the same reprojection error). Numbers above 0.2 are likely to be
* ambiguous.
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
if (observation.tagCount() == 1) {
return 1.0 - normalizedSigmoid(observation.ambiguity(), ambiguityTolerance, 4.0);
} else {
return 1.0;
}
}
},
pitchError {
/**
* We assume that the robot is constrained to an orientation that is flat on the field.
* Penalizes poses with significantly nonzero pitch.
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
return 1.0
- normalizedSigmoid(
Math.abs(observation.pose().getRotation().getY()), pitchToleranceRadians, 1.0);
}
},
rollError {
/**
* We assume that the robot is constrained to an orientation that is flat on the field.
* Penalizes poses with significantly nonzero roll.
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
return 1.0
- normalizedSigmoid(
Math.abs(observation.pose().getRotation().getX()), rollToleranceRadians, 1.0);
}
},
heightError {
/**
* We assume that the robot is constrained to an orientation that is flat on the field.
* Penalizes poses with significantly nonzero elevation.
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
return 1.0
- normalizedSigmoid(Math.abs(observation.pose().getZ()), elevationToleranceMeters, 1.0);
}
},
withinBoundaries {
/**
* Penalizes poses that, when projected to the floor, lie outside of the field boundaries
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
boolean pass = arenaRectangle.contains(observation.pose().toPose2d().getTranslation());
return (pass ? 1.0 : 0.0);
}
},
moreThanZeroTags {
/**
* Penalizes observations that see zero tags
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
return Math.min(observation.tagCount(), 1.0);
}
},
distanceToTags {
/**
* Rewards observations that see tags closer to the robot
*
* @param observation The pose observation to check
* @return Score between 0 and 1
*/
@Override
public double test(PoseObservation observation) {
return 1.0
- normalizedSigmoid(observation.averageTagDistance(), tagDistanceToleranceMeters, 1.0);
}
};
public abstract double test(PoseObservation observation);
}
/**
* Calculates a normalized sigmoid function with a tunable midpoint and steepness. The output is
* always between 0 and 1.
*
* @param x The input value.
* @param midpoint The x-value where the output should be 0.5.
* @param steepness The factor controlling the curve's steepness. Higher values result in a
* steeper curve, lower values result in a more gradual curve. Must be greater than 0.
* @return The sigmoid output for the given input, between 0 and 1.
*/
public static double normalizedSigmoid(double x, double midpoint, double steepness) {
if (steepness <= 0) {
throw new IllegalArgumentException("Steepness must be a positive value.");
}
double exponent = -steepness * (x - midpoint);
return 1.0 / (1.0 + Math.exp(exponent));
}
}