|
| 1 | +package frc.robot.vision; |
| 2 | + |
| 3 | +import static frc.robot.vision.VisionConfig.multiTagStdDevs; |
| 4 | +import static frc.robot.vision.VisionConfig.singleTagStdDevs; |
| 5 | +import static frc.robot.vision.VisionConfig.tagLayout; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import org.photonvision.EstimatedRobotPose; |
| 11 | +import org.photonvision.PhotonCamera; |
| 12 | +import org.photonvision.PhotonPoseEstimator; |
| 13 | +import org.photonvision.PhotonPoseEstimator.PoseStrategy; |
| 14 | +import org.photonvision.targeting.PhotonTrackedTarget; |
| 15 | + |
| 16 | +import edu.wpi.first.math.Matrix; |
| 17 | +import edu.wpi.first.math.VecBuilder; |
| 18 | +import edu.wpi.first.math.geometry.Transform3d; |
| 19 | +import edu.wpi.first.math.numbers.N1; |
| 20 | +import edu.wpi.first.math.numbers.N3; |
| 21 | + |
| 22 | +/* |
| 23 | + * This code is a modified version of Photonvision's own example and 7028's vision code: |
| 24 | + * https://github.com/PhotonVision/photonvision/blob/main/photonlib-java-examples/poseest |
| 25 | + * https://github.com/STMARobotics/frc-7028-2025 |
| 26 | + */ |
| 27 | + |
| 28 | +public class Vision { |
| 29 | + |
| 30 | + private final PhotonCamera cam; |
| 31 | + private final PhotonPoseEstimator photonEstimator; |
| 32 | + private Matrix<N3, N1> curStdDevs; |
| 33 | + |
| 34 | + //Construct Vision Instance |
| 35 | + public Vision(String cameraName, Transform3d camTransform){ |
| 36 | + cam = new PhotonCamera(cameraName); |
| 37 | + |
| 38 | + photonEstimator = new PhotonPoseEstimator( |
| 39 | + tagLayout, |
| 40 | + PoseStrategy.MULTI_TAG_PNP_ON_COPROCESSOR, |
| 41 | + camTransform); |
| 42 | + photonEstimator.setMultiTagFallbackStrategy(PoseStrategy.LOWEST_AMBIGUITY); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * The latest estimated robot pose on the field from vision data. This may be empty. This should |
| 47 | + * only be called once per loop. |
| 48 | + * |
| 49 | + * <p> |
| 50 | + * Also includes updates for the standard deviations, which can (optionally) be retrieved with |
| 51 | + * {@link getEstimationStdDevs} |
| 52 | + * |
| 53 | + * @return An {@link EstimatedRobotPose} with an estimated pose, estimate timestamp, and targets |
| 54 | + * used for estimation. |
| 55 | + */ |
| 56 | + public Optional<EstimatedRobotPose> getEstimatedGlobalPose() { |
| 57 | + Optional<EstimatedRobotPose> visionEst = Optional.empty(); |
| 58 | + for (var change : cam.getAllUnreadResults()) { |
| 59 | + visionEst = photonEstimator.update(change); |
| 60 | + updateEstimationStdDevs(visionEst, change.getTargets()); |
| 61 | + } |
| 62 | + return visionEst; |
| 63 | + } |
| 64 | + /** |
| 65 | + * Calculates new standard deviations This algorithm is a heuristic that creates dynamic standard |
| 66 | + * deviations based on number of tags, estimation strategy, and distance from the tags. |
| 67 | + * |
| 68 | + * @param estimatedPose The estimated pose to guess standard deviations for. |
| 69 | + * @param targets All targets in this camera frame |
| 70 | + */ |
| 71 | + private void updateEstimationStdDevs(Optional<EstimatedRobotPose> estimatedPose, List<PhotonTrackedTarget> targets){ |
| 72 | + if (estimatedPose.isEmpty()){ |
| 73 | + //No pose input. Default to single-tag std devs |
| 74 | + curStdDevs = singleTagStdDevs; |
| 75 | + |
| 76 | + } else { |
| 77 | + //Pose present, start Heuristic |
| 78 | + var estStdDevs = singleTagStdDevs; |
| 79 | + int numTags = 0; |
| 80 | + double avgDist = 0; |
| 81 | + |
| 82 | + //Precalculation - see how many tags we found, and calculate an average-distance metric |
| 83 | + for (var tgt : targets) { |
| 84 | + var tagPose = photonEstimator.getFieldTags().getTagPose(tgt.getFiducialId()); |
| 85 | + if (tagPose.isEmpty()){ |
| 86 | + continue; |
| 87 | + } |
| 88 | + numTags++; |
| 89 | + avgDist += tagPose.get() |
| 90 | + .toPose2d() |
| 91 | + .getTranslation() |
| 92 | + .getDistance(estimatedPose.get().estimatedPose.toPose2d().getTranslation()); |
| 93 | + } |
| 94 | + |
| 95 | + if (numTags == 0){ |
| 96 | + //No tags visible. Default to single-tag std devs |
| 97 | + curStdDevs = singleTagStdDevs; |
| 98 | + } else { |
| 99 | + // One or more tags visible, run the full heuristic. |
| 100 | + avgDist /= numTags; |
| 101 | + // Decrease std devs if multiple targets are visible |
| 102 | + if (numTags > 1) |
| 103 | + estStdDevs = multiTagStdDevs; |
| 104 | + // Increase std devs based on (average) distance |
| 105 | + if (numTags == 1 && avgDist > 4) { |
| 106 | + estStdDevs = VecBuilder.fill(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE); |
| 107 | + } else { |
| 108 | + estStdDevs = estStdDevs.times(1 + (avgDist * avgDist / 30)); |
| 109 | + } |
| 110 | + curStdDevs = estStdDevs; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns the latest standard deviations of the estimated pose from {@link |
| 117 | + * #getEstimatedGlobalPose()}, for use with {@link |
| 118 | + * edu.wpi.first.math.estimator.SwerveDrivePoseEstimator SwerveDrivePoseEstimator}. This should |
| 119 | + * only be used when there are targets visible. |
| 120 | + */ |
| 121 | + public Matrix<N3, N1> getEstimationStdDevs() { |
| 122 | + return curStdDevs; |
| 123 | + } |
| 124 | +} |
0 commit comments