35 std::shared_ptr<photon::PhotonCamera>
camera;
39 Eigen::Matrix<double, 3, 1> singleTagStdDevs,
40 Eigen::Matrix<double, 3, 1> multiTagStdDevs)
41 : m_cameraEstimators{estms}, m_singleTagStdDevs{singleTagStdDevs},
42 m_multiTagStdDevs{multiTagStdDevs} {
43 for (
auto &est : m_cameraEstimators) {
44 est.estimator.SetMultiTagFallbackStrategy(
45 photon::PoseStrategy::LOWEST_AMBIGUITY);
49 std::optional<photon::EstimatedRobotPose>
51 photon::PhotonCamera &camera,
double maxAbmiguity = 0.2) {
52 est.SetReferencePose(prevPose);
53 auto camResult = camera.GetLatestResult();
55 std::optional<photon::EstimatedRobotPose> visionEst;
57 if (camResult.HasTargets() &&
58 (camResult.targets.size() > 1 ||
59 camResult.targets[0].GetPoseAmbiguity() <= maxAbmiguity)) {
60 visionEst = est.Update(camResult);
63 if (visionEst.has_value()) {
64 auto estimatedPose = visionEst.value().estimatedPose;
66 if (estimatedPose.X() > 0_m && estimatedPose.X() <= 100_m &&
67 estimatedPose.Y() > 0_m && estimatedPose.Y() <= 100_m) {
86 for (
auto &est : m_cameraEstimators) {
89 est.estimator, *est.camera);
90 if (camPose.has_value()) {
91 auto pose = camPose.value();
92 AddVisionMeasurement(pose, estimator, est);
97 Eigen::Matrix<double, 3, 1>
101 Eigen::Matrix<double, 3, 1> estStdDevs = m_singleTagStdDevs;
103 units::meter_t avgDist = 0_m;
106 for (
const auto &tgt : pose.targetsUsed) {
108 photonEst.
estimator.GetFieldLayout().GetTagPose(tgt.GetFiducialId());
109 if (tagPose.has_value()) {
111 avgDist += tagPose.value().ToPose2d().Translation().Distance(
112 pose.estimatedPose.ToPose2d().Translation());
123 estStdDevs = m_multiTagStdDevs;
126 if (numTags == 1 && avgDist > 4_m) {
127 estStdDevs = (Eigen::MatrixXd(3, 1) << std::numeric_limits<double>::max(),
128 std::numeric_limits<double>::max(),
129 std::numeric_limits<double>::max())
132 estStdDevs = estStdDevs * (1 + (avgDist.value() * avgDist.value() / 30));
139 void AddVisionMeasurement(photon::EstimatedRobotPose &estimate,
140 frc::SwerveDrivePoseEstimator<4U> &estimator,
141 PhotonCameraEstimator &photonEst) {
143 wpi::array<double, 3> newStdDevs{stdDevs(0), stdDevs(1), stdDevs(2)};
144 estimator.AddVisionMeasurement(estimate.estimatedPose.ToPose2d(),
145 estimate.timestamp, newStdDevs);
148 std::vector<PhotonCameraEstimator> &m_cameraEstimators;
149 Eigen::Matrix<double, 3, 1> m_singleTagStdDevs;
150 Eigen::Matrix<double, 3, 1> m_multiTagStdDevs;
152 units::second_t lastEstTimestamp{0_s};