Skip to content

Commit b176a3b

Browse files
committed
Use all unread results from PhotonVision
1 parent 7b00d4c commit b176a3b

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

src/main/java/org/lasarobotics/vision/AprilTagCamera.java

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -170,61 +170,64 @@ private void run() {
170170
// Return if camera is not connected
171171
if (!m_camera.isConnected()) return;
172172

173-
// Update and log inputs
174-
var pipelineResult = m_camera.getLatestResult();
173+
// Get all unread results
174+
var pipelineResults = m_camera.getAllUnreadResults();
175175

176-
// Return if result is non-existent or invalid
177-
if (!pipelineResult.hasTargets()) {
178-
m_latestResult.set(null);
179-
return;
180-
}
181-
if (pipelineResult.targets.size() == 1
182-
&& pipelineResult.targets.get(0).getPoseAmbiguity() > APRILTAG_POSE_AMBIGUITY_THRESHOLD) {
183-
m_latestResult.set(null);
184-
return;
185-
}
186-
187-
// Update pose estimate
188-
m_poseEstimator.update(pipelineResult).ifPresent(estimatedRobotPose -> {
189-
// Make sure the measurement is valid
190-
if (!isPoseValid(estimatedRobotPose.estimatedPose)) {
176+
// Iterate through all results
177+
for (var pipelineResult : pipelineResults) {
178+
// Return if result is non-existent or invalid
179+
if (!pipelineResult.hasTargets()) {
191180
m_latestResult.set(null);
192-
return;
193-
}
194-
195-
// Get distance to closest tag
196-
var closestTagDistance = Units.Meters.of(100.0);
197-
// Number of tags in range
198-
int numOfTagsInRange = 0;
199-
// Loop through all targets used for this estimate
200-
for (var target : estimatedRobotPose.targetsUsed) {
201-
// Get distance to tag
202-
var tagDistance = Units.Meters.of(target.getBestCameraToTarget().getTranslation().getNorm());
203-
// Check if tag distance is closest yet
204-
if (tagDistance.lte(closestTagDistance)) closestTagDistance = tagDistance;
205-
// Increment number of tags in range if applicable
206-
if (tagDistance.lte(MAX_TAG_DISTANCE)) numOfTagsInRange++;
181+
continue;
207182
}
208-
209-
// Ignore if tags are too far
210-
if (numOfTagsInRange < 2 && estimatedRobotPose.targetsUsed.size() > 1) {
183+
if (pipelineResult.targets.size() == 1
184+
&& pipelineResult.targets.get(0).getPoseAmbiguity() > APRILTAG_POSE_AMBIGUITY_THRESHOLD) {
211185
m_latestResult.set(null);
212-
return;
186+
continue;
213187
}
214188

215-
// Calculate standard deviation
216-
double xyStdDev = getStandardDeviation(closestTagDistance, estimatedRobotPose.targetsUsed.size());
217-
double thetaStdDev = RobotState.isDisabled()
218-
? xyStdDev
219-
: Double.MAX_VALUE;
220-
221-
// Set result
222-
var result = new AprilTagCamera.Result(
223-
estimatedRobotPose,
224-
VecBuilder.fill(xyStdDev, xyStdDev, thetaStdDev)
225-
);
226-
m_latestResult.set(result);
227-
});
189+
// Update pose estimate
190+
m_poseEstimator.update(pipelineResult).ifPresent(estimatedRobotPose -> {
191+
// Make sure the measurement is valid
192+
if (!isPoseValid(estimatedRobotPose.estimatedPose)) {
193+
m_latestResult.set(null);
194+
return;
195+
}
196+
197+
// Get distance to closest tag
198+
var closestTagDistance = Units.Meters.of(100.0);
199+
// Number of tags in range
200+
int numOfTagsInRange = 0;
201+
// Loop through all targets used for this estimate
202+
for (var target : estimatedRobotPose.targetsUsed) {
203+
// Get distance to tag
204+
var tagDistance = Units.Meters.of(target.getBestCameraToTarget().getTranslation().getNorm());
205+
// Check if tag distance is closest yet
206+
if (tagDistance.lte(closestTagDistance)) closestTagDistance = tagDistance;
207+
// Increment number of tags in range if applicable
208+
if (tagDistance.lte(MAX_TAG_DISTANCE)) numOfTagsInRange++;
209+
}
210+
211+
// Ignore if tags are too far
212+
if (numOfTagsInRange < 2 && estimatedRobotPose.targetsUsed.size() > 1) {
213+
m_latestResult.set(null);
214+
return;
215+
}
216+
217+
// Calculate standard deviation
218+
double xyStdDev = getStandardDeviation(closestTagDistance, estimatedRobotPose.targetsUsed.size());
219+
double thetaStdDev = RobotState.isDisabled()
220+
? xyStdDev
221+
: Double.MAX_VALUE;
222+
223+
// Set result
224+
var result = new AprilTagCamera.Result(
225+
estimatedRobotPose,
226+
VecBuilder.fill(xyStdDev, xyStdDev, thetaStdDev)
227+
);
228+
m_latestResult.set(result);
229+
});
230+
}
228231
}
229232

230233
/**

0 commit comments

Comments
 (0)