|
34 | 34 | import java.util.Map; |
35 | 35 | import java.util.Objects; |
36 | 36 | import java.util.Optional; |
| 37 | +import java.util.function.Consumer; |
37 | 38 | import java.util.function.Predicate; |
| 39 | +import java.util.stream.Collectors; |
38 | 40 | import org.photonvision.EstimatedRobotPose; |
39 | 41 | import org.photonvision.PhotonCamera; |
40 | 42 | import org.photonvision.PhotonPoseEstimator; |
@@ -379,24 +381,44 @@ public void resetHeadingData(double timestampSeconds, Rotation3d heading) { |
379 | 381 | } |
380 | 382 |
|
381 | 383 | /** |
382 | | - * Sends all unread robot-pose estimations from all cameras to the provided consumer. |
| 384 | + * Sends all validated unread robot-pose estimations from all cameras to the provided consumer. |
383 | 385 | * |
384 | | - * <p>This method is supposed to be called from a routine updating drive-train pose with pose |
385 | | - * estimates from the photon vision cameras. |
| 386 | + * <p>This method should be called from a routine updating drive-train pose with pose estimates |
| 387 | + * from the photon vision cameras. |
386 | 388 | * |
387 | | - * @param poseEstimateConsumer Functional interface for consuming computed pose estimates. |
| 389 | + * @param poseEstimateConsumer Consumer for validated pose estimates. |
388 | 390 | */ |
389 | 391 | public void processAllUnreadResults(PoseEstimateConsumer<C> poseEstimateConsumer) { |
| 392 | + processAllUnreadResults(poseEstimateConsumer, pose -> {}); |
| 393 | + } |
| 394 | + |
| 395 | + /** |
| 396 | + * Sends all unread robot-pose estimations from all cameras to the provided consumers. |
| 397 | + * |
| 398 | + * <p>This method should be called from a routine updating drive-train pose with pose estimates |
| 399 | + * from the photon vision cameras. |
| 400 | + * |
| 401 | + * @param poseEstimateConsumer Consumer for validated pose estimates. |
| 402 | + * @param rejectedPoseConsumer Consumer for rejected pose estimates. |
| 403 | + */ |
| 404 | + public void processAllUnreadResults( |
| 405 | + PoseEstimateConsumer<C> poseEstimateConsumer, |
| 406 | + Consumer<EstimatedRobotPose> rejectedPoseConsumer) { |
390 | 407 | for (PhotonCameraWrapper<C> cameraWrapper : cameraWrappers) { |
391 | | - List<EstimatedRobotPose> poses = |
| 408 | + Map<Boolean, List<EstimatedRobotPose>> poses = |
392 | 409 | cameraWrapper.photonCamera.getAllUnreadResults().stream() |
393 | 410 | .map(cameraWrapper.estimator::update) // PhotonPipelineResult -> EstimatedRobotPose |
394 | 411 | .flatMap(Optional::stream) // Convert Stream<Optional<P>> -> Stream<P> |
395 | | - .filter(isPoseValid) |
396 | | - .toList(); |
| 412 | + .collect(Collectors.partitioningBy(isPoseValid)); |
| 413 | + |
| 414 | + List<EstimatedRobotPose> validatedPoses = poses.get(Boolean.TRUE); |
| 415 | + for (EstimatedRobotPose pose : validatedPoses) { |
| 416 | + poseEstimateConsumer.addEstimatedRobotPose(pose, cameraWrapper.camera); |
| 417 | + } |
| 418 | + cameraWrapper.robotPosePublisher.publish(validatedPoses); |
397 | 419 |
|
398 | | - poses.forEach(pose -> poseEstimateConsumer.addEstimatedRobotPose(pose, cameraWrapper.camera)); |
399 | | - cameraWrapper.robotPosePublisher.publish(poses); |
| 420 | + List<EstimatedRobotPose> rejectedPoses = poses.get(Boolean.FALSE); |
| 421 | + rejectedPoses.forEach(rejectedPoseConsumer); |
400 | 422 | } |
401 | 423 | } |
402 | 424 |
|
|
0 commit comments