2023-01-14 09:06:15 -06:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2023-04-18 18:49:40 -04:00
|
|
|
* Copyright (c) PhotonVision
|
2023-01-14 09:06:15 -06:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/PhotonPoseEstimator.h"
|
2023-01-14 09:06:15 -06:00
|
|
|
|
|
|
|
|
#include <limits>
|
2026-01-11 13:58:53 -05:00
|
|
|
#include <optional>
|
2023-01-14 09:06:15 -06:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2023-10-15 12:17:40 -04:00
|
|
|
#include <Eigen/Core>
|
2023-01-14 09:06:15 -06:00
|
|
|
#include <frc/Errors.h>
|
|
|
|
|
#include <frc/geometry/Pose3d.h>
|
|
|
|
|
#include <frc/geometry/Rotation3d.h>
|
|
|
|
|
#include <frc/geometry/Transform3d.h>
|
2025-11-10 21:28:28 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2023-02-13 17:57:01 -05:00
|
|
|
#include <opencv2/calib3d.hpp>
|
|
|
|
|
#include <opencv2/core/mat.hpp>
|
|
|
|
|
#include <opencv2/core/types.hpp>
|
2025-08-10 06:55:04 -07:00
|
|
|
#include <units/angle.h>
|
2023-02-13 18:22:22 -08:00
|
|
|
#include <units/math.h>
|
2023-01-14 09:06:15 -06:00
|
|
|
#include <units/time.h>
|
2026-01-11 13:58:53 -05:00
|
|
|
#include <wpi/deprecated.h>
|
2023-01-14 09:06:15 -06:00
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/PhotonCamera.h"
|
2025-10-12 12:26:12 -07:00
|
|
|
#include "photon/estimation/TargetModel.h"
|
|
|
|
|
#include "photon/estimation/VisionEstimation.h"
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/targeting/PhotonPipelineResult.h"
|
|
|
|
|
#include "photon/targeting/PhotonTrackedTarget.h"
|
2023-01-14 09:06:15 -06:00
|
|
|
|
2024-05-29 17:28:35 -04:00
|
|
|
#define OPENCV_DISABLE_EIGEN_TENSOR_SUPPORT
|
|
|
|
|
#include <opencv2/core/eigen.hpp>
|
2026-01-11 13:58:53 -05:00
|
|
|
WPI_IGNORE_DEPRECATED
|
2023-11-19 15:16:22 -05:00
|
|
|
namespace photon {
|
2023-02-13 17:57:01 -05:00
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
cv::Point3d ToPoint3d(const frc::Translation3d& translation);
|
|
|
|
|
std::optional<std::array<cv::Point3d, 4>> CalcTagCorners(
|
|
|
|
|
int tagID, const frc::AprilTagFieldLayout& aprilTags);
|
|
|
|
|
frc::Pose3d ToPose3d(const cv::Mat& tvec, const cv::Mat& rvec);
|
|
|
|
|
cv::Point3d TagCornerToObjectPoint(units::meter_t cornerX,
|
|
|
|
|
units::meter_t cornerY, frc::Pose3d tagPose);
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
PhotonPoseEstimator::PhotonPoseEstimator(frc::AprilTagFieldLayout tags,
|
|
|
|
|
frc::Transform3d robotToCamera)
|
|
|
|
|
: aprilTags(tags),
|
|
|
|
|
m_robotToCamera(robotToCamera),
|
|
|
|
|
lastPose(frc::Pose3d()),
|
|
|
|
|
referencePose(frc::Pose3d()),
|
|
|
|
|
poseCacheTimestamp(-1_s),
|
|
|
|
|
headingBuffer(frc::TimeInterpolatableBuffer<frc::Rotation2d>(1_s)) {
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PhotonPoseEstimator,
|
|
|
|
|
InstanceCount);
|
|
|
|
|
InstanceCount++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 12:50:23 -05:00
|
|
|
PhotonPoseEstimator::PhotonPoseEstimator(frc::AprilTagFieldLayout tags,
|
|
|
|
|
PoseStrategy strat,
|
|
|
|
|
frc::Transform3d robotToCamera)
|
|
|
|
|
: aprilTags(tags),
|
|
|
|
|
strategy(strat),
|
2023-01-14 09:06:15 -06:00
|
|
|
m_robotToCamera(robotToCamera),
|
|
|
|
|
lastPose(frc::Pose3d()),
|
2023-02-13 18:22:22 -08:00
|
|
|
referencePose(frc::Pose3d()),
|
2025-08-10 06:55:04 -07:00
|
|
|
poseCacheTimestamp(-1_s),
|
|
|
|
|
headingBuffer(frc::TimeInterpolatableBuffer<frc::Rotation2d>(1_s)) {
|
2023-12-16 15:26:00 -05:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PhotonPoseEstimator,
|
|
|
|
|
InstanceCount);
|
|
|
|
|
InstanceCount++;
|
|
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
void PhotonPoseEstimator::SetMultiTagFallbackStrategy(PoseStrategy strategy) {
|
2023-10-17 10:20:00 -04:00
|
|
|
if (strategy == MULTI_TAG_PNP_ON_COPROCESSOR ||
|
|
|
|
|
strategy == MULTI_TAG_PNP_ON_RIO) {
|
2023-02-13 17:57:01 -05:00
|
|
|
FRC_ReportError(
|
|
|
|
|
frc::warn::Warning,
|
|
|
|
|
"Fallback cannot be set to MULTI_TAG_PNP! Setting to lowest ambiguity",
|
|
|
|
|
"");
|
|
|
|
|
strategy = LOWEST_AMBIGUITY;
|
|
|
|
|
}
|
2023-02-13 18:22:22 -08:00
|
|
|
if (this->multiTagFallbackStrategy != strategy) {
|
|
|
|
|
InvalidatePoseCache();
|
|
|
|
|
}
|
2023-02-13 17:57:01 -05:00
|
|
|
multiTagFallbackStrategy = strategy;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 12:50:23 -05:00
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::Update(
|
2024-05-29 17:28:35 -04:00
|
|
|
const PhotonPipelineResult& result,
|
|
|
|
|
std::optional<PhotonCamera::CameraMatrix> cameraMatrixData,
|
2025-10-12 12:26:12 -07:00
|
|
|
std::optional<PhotonCamera::DistortionMatrix> cameraDistCoeffs,
|
|
|
|
|
std::optional<ConstrainedSolvepnpParams> constrainedPnpParams) {
|
2023-02-13 18:22:22 -08:00
|
|
|
// Time in the past -- give up, since the following if expects times > 0
|
|
|
|
|
if (result.GetTimestamp() < 0_s) {
|
2024-08-31 13:44:19 -04:00
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Result timestamp was reported in the past!");
|
2023-02-13 18:22:22 -08:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the pose cache timestamp was set, and the result is from the same
|
|
|
|
|
// timestamp, return an empty result
|
|
|
|
|
if (poseCacheTimestamp > 0_s &&
|
|
|
|
|
units::math::abs(poseCacheTimestamp - result.GetTimestamp()) < 0.001_ms) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remember the timestamp of the current result used
|
|
|
|
|
poseCacheTimestamp = result.GetTimestamp();
|
|
|
|
|
|
|
|
|
|
// If no targets seen, trivial case -- return empty result
|
2023-01-14 09:06:15 -06:00
|
|
|
if (!result.HasTargets()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 12:26:12 -07:00
|
|
|
return Update(result, cameraMatrixData, cameraDistCoeffs,
|
|
|
|
|
constrainedPnpParams, this->strategy);
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::Update(
|
2024-05-29 17:28:35 -04:00
|
|
|
const PhotonPipelineResult& result,
|
|
|
|
|
std::optional<PhotonCamera::CameraMatrix> cameraMatrixData,
|
|
|
|
|
std::optional<PhotonCamera::DistortionMatrix> cameraDistCoeffs,
|
2025-10-12 12:26:12 -07:00
|
|
|
std::optional<ConstrainedSolvepnpParams> constrainedPnpParams,
|
2024-05-29 17:28:35 -04:00
|
|
|
PoseStrategy strategy) {
|
2023-01-14 09:06:15 -06:00
|
|
|
std::optional<EstimatedRobotPose> ret = std::nullopt;
|
|
|
|
|
|
|
|
|
|
switch (strategy) {
|
|
|
|
|
case LOWEST_AMBIGUITY:
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = EstimateLowestAmbiguityPose(result);
|
2023-01-14 09:06:15 -06:00
|
|
|
break;
|
|
|
|
|
case CLOSEST_TO_CAMERA_HEIGHT:
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = EstimateClosestToCameraHeightPose(result);
|
2023-01-14 09:06:15 -06:00
|
|
|
break;
|
|
|
|
|
case CLOSEST_TO_REFERENCE_POSE:
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = EstimateClosestToReferencePose(result, this->referencePose);
|
2023-01-14 09:06:15 -06:00
|
|
|
break;
|
|
|
|
|
case CLOSEST_TO_LAST_POSE:
|
|
|
|
|
SetReferencePose(lastPose);
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = EstimateClosestToReferencePose(result, this->referencePose);
|
2023-01-14 09:06:15 -06:00
|
|
|
break;
|
|
|
|
|
case AVERAGE_BEST_TARGETS:
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = EstimateAverageBestTargetsPose(result);
|
2023-01-14 09:06:15 -06:00
|
|
|
break;
|
2023-10-17 10:20:00 -04:00
|
|
|
case MULTI_TAG_PNP_ON_COPROCESSOR:
|
2026-01-11 13:58:53 -05:00
|
|
|
if (!result.MultiTagResult()) {
|
|
|
|
|
ret = Update(result, this->multiTagFallbackStrategy);
|
|
|
|
|
} else {
|
|
|
|
|
ret = EstimateCoprocMultiTagPose(result);
|
|
|
|
|
}
|
2023-10-17 10:20:00 -04:00
|
|
|
break;
|
|
|
|
|
case MULTI_TAG_PNP_ON_RIO:
|
2026-01-11 13:58:53 -05:00
|
|
|
if (!cameraMatrixData && !cameraDistCoeffs) {
|
2024-08-04 14:23:46 -04:00
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"No camera calibration provided to multi-tag-on-rio!",
|
|
|
|
|
"");
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = Update(result, this->multiTagFallbackStrategy);
|
|
|
|
|
}
|
|
|
|
|
ret =
|
|
|
|
|
EstimateRioMultiTagPose(result, *cameraMatrixData, *cameraDistCoeffs);
|
|
|
|
|
if (!ret) {
|
|
|
|
|
ret = Update(result, this->multiTagFallbackStrategy);
|
2024-08-04 14:23:46 -04:00
|
|
|
}
|
2023-02-13 17:57:01 -05:00
|
|
|
break;
|
2026-01-11 13:58:53 -05:00
|
|
|
case CONSTRAINED_SOLVEPNP: {
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
if (!cameraMatrixData || !cameraDistCoeffs) {
|
|
|
|
|
FRC_ReportError(
|
|
|
|
|
frc::warn::Warning,
|
|
|
|
|
"No camera calibration data provided for Constrained SolvePnP!");
|
|
|
|
|
ret = Update(result, this->multiTagFallbackStrategy);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!constrainedPnpParams) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!constrainedPnpParams->headingFree &&
|
|
|
|
|
!headingBuffer.Sample(result.GetTimestamp()).has_value()) {
|
|
|
|
|
ret = Update(result, cameraMatrixData, cameraDistCoeffs, {},
|
|
|
|
|
this->multiTagFallbackStrategy);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Pose3d fieldToRobotSeed;
|
|
|
|
|
|
|
|
|
|
if (result.MultiTagResult().has_value()) {
|
|
|
|
|
fieldToRobotSeed =
|
|
|
|
|
frc::Pose3d{} + (result.MultiTagResult()->estimatedPose.best +
|
|
|
|
|
m_robotToCamera.Inverse());
|
|
|
|
|
} else {
|
|
|
|
|
std::optional<EstimatedRobotPose> nestedUpdate =
|
|
|
|
|
Update(result, cameraMatrixData, cameraDistCoeffs, {},
|
|
|
|
|
this->multiTagFallbackStrategy);
|
|
|
|
|
|
|
|
|
|
if (!nestedUpdate.has_value()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldToRobotSeed = nestedUpdate->estimatedPose;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = EstimateConstrainedSolvepnpPose(
|
|
|
|
|
result, *cameraMatrixData, *cameraDistCoeffs, fieldToRobotSeed,
|
|
|
|
|
constrainedPnpParams->headingFree,
|
|
|
|
|
constrainedPnpParams->headingScalingFactor);
|
|
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
|
ret = Update(result, cameraMatrixData, cameraDistCoeffs, {},
|
|
|
|
|
this->multiTagFallbackStrategy);
|
|
|
|
|
}
|
2025-10-12 12:26:12 -07:00
|
|
|
break;
|
2026-01-11 13:58:53 -05:00
|
|
|
}
|
2025-08-10 06:55:04 -07:00
|
|
|
case PNP_DISTANCE_TRIG_SOLVE:
|
2026-01-11 13:58:53 -05:00
|
|
|
ret = EstimatePnpDistanceTrigSolvePose(result);
|
2025-08-10 06:55:04 -07:00
|
|
|
break;
|
2023-01-14 09:06:15 -06:00
|
|
|
default:
|
|
|
|
|
FRC_ReportError(frc::warn::Warning, "Invalid Pose Strategy selected!",
|
|
|
|
|
"");
|
2023-02-13 18:22:22 -08:00
|
|
|
ret = std::nullopt;
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-05 09:50:36 -05:00
|
|
|
if (ret) {
|
2024-08-31 13:44:19 -04:00
|
|
|
lastPose = ret->estimatedPose;
|
2024-02-05 09:50:36 -05:00
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
bool ShouldEstimate(const PhotonPipelineResult& result) {
|
|
|
|
|
// Time in the past -- give up, since the following if expects times > 0
|
|
|
|
|
if (result.GetTimestamp() < 0_s) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Result timestamp was reported in the past!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no targets seen, trivial case -- can't do estimation
|
|
|
|
|
return result.HasTargets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose>
|
|
|
|
|
PhotonPoseEstimator::EstimateLowestAmbiguityPose(
|
|
|
|
|
PhotonPipelineResult cameraResult) {
|
|
|
|
|
if (!ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
double lowestAmbiguityScore = std::numeric_limits<double>::infinity();
|
2026-01-11 13:58:53 -05:00
|
|
|
auto targets = cameraResult.GetTargets();
|
2023-02-11 23:44:22 +11:00
|
|
|
auto foundIt = targets.end();
|
|
|
|
|
for (auto it = targets.begin(); it != targets.end(); ++it) {
|
|
|
|
|
if (it->GetPoseAmbiguity() < lowestAmbiguityScore) {
|
|
|
|
|
foundIt = it;
|
|
|
|
|
lowestAmbiguityScore = it->GetPoseAmbiguity();
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 23:44:22 +11:00
|
|
|
if (foundIt == targets.end()) {
|
2023-01-14 09:06:15 -06:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 23:44:22 +11:00
|
|
|
auto& bestTarget = *foundIt;
|
2023-01-14 09:06:15 -06:00
|
|
|
|
|
|
|
|
std::optional<frc::Pose3d> fiducialPose =
|
|
|
|
|
aprilTags.GetTagPose(bestTarget.GetFiducialId());
|
|
|
|
|
if (!fiducialPose) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Tried to get pose of unknown April Tag: {}",
|
|
|
|
|
bestTarget.GetFiducialId());
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EstimatedRobotPose{
|
2024-08-31 13:44:19 -04:00
|
|
|
fiducialPose->TransformBy(bestTarget.GetBestCameraToTarget().Inverse())
|
2023-01-14 09:06:15 -06:00
|
|
|
.TransformBy(m_robotToCamera.Inverse()),
|
2026-01-11 13:58:53 -05:00
|
|
|
cameraResult.GetTimestamp(), cameraResult.GetTargets(), LOWEST_AMBIGUITY};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose>
|
2026-01-11 13:58:53 -05:00
|
|
|
PhotonPoseEstimator::EstimateClosestToCameraHeightPose(
|
|
|
|
|
PhotonPipelineResult cameraResult) {
|
|
|
|
|
if (!ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
units::meter_t smallestHeightDifference =
|
|
|
|
|
units::meter_t(std::numeric_limits<double>::infinity());
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose> pose = std::nullopt;
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
for (auto& target : cameraResult.GetTargets()) {
|
2023-01-14 09:06:15 -06:00
|
|
|
std::optional<frc::Pose3d> fiducialPose =
|
|
|
|
|
aprilTags.GetTagPose(target.GetFiducialId());
|
|
|
|
|
if (!fiducialPose) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Tried to get pose of unknown April Tag: {}",
|
|
|
|
|
target.GetFiducialId());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-08-31 13:44:19 -04:00
|
|
|
frc::Pose3d const targetPose = *fiducialPose;
|
2023-01-14 09:06:15 -06:00
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
units::meter_t const alternativeDifference = units::math::abs(
|
2023-01-14 09:06:15 -06:00
|
|
|
m_robotToCamera.Z() -
|
|
|
|
|
targetPose.TransformBy(target.GetAlternateCameraToTarget().Inverse())
|
|
|
|
|
.Z());
|
|
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
units::meter_t const bestDifference = units::math::abs(
|
2023-01-14 09:06:15 -06:00
|
|
|
m_robotToCamera.Z() -
|
|
|
|
|
targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()).Z());
|
|
|
|
|
|
|
|
|
|
if (alternativeDifference < smallestHeightDifference) {
|
|
|
|
|
smallestHeightDifference = alternativeDifference;
|
|
|
|
|
pose = EstimatedRobotPose{
|
|
|
|
|
targetPose.TransformBy(target.GetAlternateCameraToTarget().Inverse())
|
|
|
|
|
.TransformBy(m_robotToCamera.Inverse()),
|
2026-01-11 13:58:53 -05:00
|
|
|
cameraResult.GetTimestamp(), cameraResult.GetTargets(),
|
|
|
|
|
CLOSEST_TO_CAMERA_HEIGHT};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
if (bestDifference < smallestHeightDifference) {
|
|
|
|
|
smallestHeightDifference = bestDifference;
|
|
|
|
|
pose = EstimatedRobotPose{
|
|
|
|
|
targetPose.TransformBy(target.GetBestCameraToTarget().Inverse())
|
|
|
|
|
.TransformBy(m_robotToCamera.Inverse()),
|
2026-01-11 13:58:53 -05:00
|
|
|
cameraResult.GetTimestamp(), cameraResult.GetTargets(),
|
|
|
|
|
CLOSEST_TO_CAMERA_HEIGHT};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pose;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose>
|
2026-01-11 13:58:53 -05:00
|
|
|
PhotonPoseEstimator::EstimateClosestToReferencePose(
|
|
|
|
|
PhotonPipelineResult cameraResult, frc::Pose3d referencePose) {
|
|
|
|
|
if (!ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
units::meter_t smallestDifference =
|
|
|
|
|
units::meter_t(std::numeric_limits<double>::infinity());
|
|
|
|
|
units::second_t stateTimestamp = units::second_t(0);
|
|
|
|
|
frc::Pose3d pose = lastPose;
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
auto targets = cameraResult.GetTargets();
|
2023-02-11 23:44:22 +11:00
|
|
|
for (auto& target : targets) {
|
2023-01-14 09:06:15 -06:00
|
|
|
std::optional<frc::Pose3d> fiducialPose =
|
|
|
|
|
aprilTags.GetTagPose(target.GetFiducialId());
|
|
|
|
|
if (!fiducialPose) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Tried to get pose of unknown April Tag: {}",
|
|
|
|
|
target.GetFiducialId());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
frc::Pose3d targetPose = fiducialPose.value();
|
|
|
|
|
|
|
|
|
|
const auto altPose =
|
|
|
|
|
targetPose.TransformBy(target.GetAlternateCameraToTarget().Inverse())
|
|
|
|
|
.TransformBy(m_robotToCamera.Inverse());
|
|
|
|
|
const auto bestPose =
|
|
|
|
|
targetPose.TransformBy(target.GetBestCameraToTarget().Inverse())
|
|
|
|
|
.TransformBy(m_robotToCamera.Inverse());
|
|
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
units::meter_t const alternativeDifference = units::math::abs(
|
2023-01-14 09:06:15 -06:00
|
|
|
referencePose.Translation().Distance(altPose.Translation()));
|
2023-02-13 17:57:01 -05:00
|
|
|
units::meter_t const bestDifference = units::math::abs(
|
2023-01-14 09:06:15 -06:00
|
|
|
referencePose.Translation().Distance(bestPose.Translation()));
|
|
|
|
|
if (alternativeDifference < smallestDifference) {
|
|
|
|
|
smallestDifference = alternativeDifference;
|
|
|
|
|
pose = altPose;
|
2026-01-11 13:58:53 -05:00
|
|
|
stateTimestamp = cameraResult.GetTimestamp();
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bestDifference < smallestDifference) {
|
|
|
|
|
smallestDifference = bestDifference;
|
|
|
|
|
pose = bestPose;
|
2026-01-11 13:58:53 -05:00
|
|
|
stateTimestamp = cameraResult.GetTimestamp();
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
return EstimatedRobotPose{pose, stateTimestamp, cameraResult.GetTargets(),
|
2023-10-17 10:20:00 -04:00
|
|
|
CLOSEST_TO_REFERENCE_POSE};
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<std::array<cv::Point3d, 4>> detail::CalcTagCorners(
|
|
|
|
|
int tagID, const frc::AprilTagFieldLayout& aprilTags) {
|
|
|
|
|
if (auto tagPose = aprilTags.GetTagPose(tagID); tagPose.has_value()) {
|
|
|
|
|
return std::array{TagCornerToObjectPoint(-3_in, -3_in, *tagPose),
|
|
|
|
|
TagCornerToObjectPoint(+3_in, -3_in, *tagPose),
|
|
|
|
|
TagCornerToObjectPoint(+3_in, +3_in, *tagPose),
|
|
|
|
|
TagCornerToObjectPoint(-3_in, +3_in, *tagPose)};
|
|
|
|
|
} else {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::Point3d detail::ToPoint3d(const frc::Translation3d& translation) {
|
|
|
|
|
return cv::Point3d(-translation.Y().value(), -translation.Z().value(),
|
|
|
|
|
+translation.X().value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::Point3d detail::TagCornerToObjectPoint(units::meter_t cornerX,
|
|
|
|
|
units::meter_t cornerY,
|
|
|
|
|
frc::Pose3d tagPose) {
|
|
|
|
|
frc::Translation3d cornerTrans =
|
|
|
|
|
tagPose.Translation() +
|
|
|
|
|
frc::Translation3d(0.0_m, cornerX, cornerY).RotateBy(tagPose.Rotation());
|
|
|
|
|
return ToPoint3d(cornerTrans);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Pose3d detail::ToPose3d(const cv::Mat& tvec, const cv::Mat& rvec) {
|
|
|
|
|
using namespace frc;
|
|
|
|
|
using namespace units;
|
|
|
|
|
|
|
|
|
|
cv::Mat R;
|
|
|
|
|
cv::Rodrigues(rvec, R); // R is 3x3
|
|
|
|
|
|
|
|
|
|
R = R.t(); // rotation of inverse
|
|
|
|
|
cv::Mat tvecI = -R * tvec; // translation of inverse
|
|
|
|
|
|
2023-10-15 12:17:40 -04:00
|
|
|
Eigen::Matrix<double, 3, 1> tv;
|
2023-02-13 17:57:01 -05:00
|
|
|
tv[0] = +tvecI.at<double>(2, 0);
|
|
|
|
|
tv[1] = -tvecI.at<double>(0, 0);
|
|
|
|
|
tv[2] = -tvecI.at<double>(1, 0);
|
2023-10-15 12:17:40 -04:00
|
|
|
Eigen::Matrix<double, 3, 1> rv;
|
2023-02-13 17:57:01 -05:00
|
|
|
rv[0] = +rvec.at<double>(2, 0);
|
|
|
|
|
rv[1] = -rvec.at<double>(0, 0);
|
|
|
|
|
rv[2] = +rvec.at<double>(1, 0);
|
|
|
|
|
|
|
|
|
|
return Pose3d(Translation3d(meter_t{tv[0]}, meter_t{tv[1]}, meter_t{tv[2]}),
|
2023-10-15 12:17:40 -04:00
|
|
|
Rotation3d(rv));
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
std::optional<EstimatedRobotPose>
|
|
|
|
|
PhotonPoseEstimator::EstimateCoprocMultiTagPose(
|
|
|
|
|
PhotonPipelineResult cameraResult) {
|
|
|
|
|
if (!cameraResult.MultiTagResult() || !ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
2023-10-17 10:20:00 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
const auto field2camera = cameraResult.MultiTagResult()->estimatedPose.best;
|
2025-02-01 14:09:43 -08:00
|
|
|
|
|
|
|
|
const auto fieldToRobot =
|
|
|
|
|
frc::Pose3d() + field2camera + m_robotToCamera.Inverse();
|
2026-01-11 13:58:53 -05:00
|
|
|
return photon::EstimatedRobotPose(fieldToRobot, cameraResult.GetTimestamp(),
|
|
|
|
|
cameraResult.GetTargets(),
|
2025-02-01 14:09:43 -08:00
|
|
|
MULTI_TAG_PNP_ON_COPROCESSOR);
|
2023-10-17 10:20:00 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::EstimateRioMultiTagPose(
|
|
|
|
|
PhotonPipelineResult cameraResult, PhotonCamera::CameraMatrix cameraMatrix,
|
|
|
|
|
PhotonCamera::DistortionMatrix distCoeffs) {
|
2025-02-01 14:09:43 -08:00
|
|
|
// Need at least 2 targets
|
2026-01-11 13:58:53 -05:00
|
|
|
if (cameraResult.GetTargets().size() < 2 || !ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
2023-06-18 00:00:30 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
const auto targets = cameraResult.GetTargets();
|
2023-02-13 17:57:01 -05:00
|
|
|
|
|
|
|
|
// List of corners mapped from 3d space (meters) to the 2d camera screen
|
|
|
|
|
// (pixels).
|
|
|
|
|
std::vector<cv::Point3f> objectPoints;
|
|
|
|
|
std::vector<cv::Point2f> imagePoints;
|
|
|
|
|
|
|
|
|
|
// Add all target corners to main list of corners
|
|
|
|
|
for (auto target : targets) {
|
|
|
|
|
int id = target.GetFiducialId();
|
|
|
|
|
if (auto const tagCorners = detail::CalcTagCorners(id, aprilTags);
|
|
|
|
|
tagCorners.has_value()) {
|
|
|
|
|
auto const targetCorners = target.GetDetectedCorners();
|
|
|
|
|
for (size_t cornerIdx = 0; cornerIdx < 4; ++cornerIdx) {
|
2024-08-31 13:44:19 -04:00
|
|
|
imagePoints.emplace_back(targetCorners[cornerIdx].x,
|
|
|
|
|
targetCorners[cornerIdx].y);
|
2023-02-13 17:57:01 -05:00
|
|
|
objectPoints.emplace_back((*tagCorners)[cornerIdx]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-17 22:16:10 -07:00
|
|
|
// We should only do multi-tag if at least 2 tags (* 4 corners/tag)
|
|
|
|
|
if (imagePoints.size() < 8) {
|
2026-01-11 13:58:53 -05:00
|
|
|
return std::nullopt;
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
|
2023-09-12 06:56:38 -04:00
|
|
|
// Output mats for results
|
2023-02-13 17:57:01 -05:00
|
|
|
cv::Mat const rvec(3, 1, cv::DataType<double>::type);
|
|
|
|
|
cv::Mat const tvec(3, 1, cv::DataType<double>::type);
|
|
|
|
|
|
2024-05-29 17:28:35 -04:00
|
|
|
{
|
2026-01-11 13:58:53 -05:00
|
|
|
cv::Mat cameraMatCV(cameraMatrix.rows(), cameraMatrix.cols(), CV_64F);
|
|
|
|
|
cv::eigen2cv(cameraMatrix, cameraMatCV);
|
|
|
|
|
cv::Mat distCoeffsMatCV(distCoeffs.rows(), distCoeffs.cols(), CV_64F);
|
|
|
|
|
cv::eigen2cv(distCoeffs, distCoeffsMatCV);
|
2024-05-29 17:28:35 -04:00
|
|
|
|
|
|
|
|
cv::solvePnP(objectPoints, imagePoints, cameraMatCV, distCoeffsMatCV, rvec,
|
|
|
|
|
tvec, false, cv::SOLVEPNP_SQPNP);
|
|
|
|
|
}
|
2023-02-13 17:57:01 -05:00
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
const frc::Pose3d pose = detail::ToPose3d(tvec, rvec);
|
2023-02-13 17:57:01 -05:00
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
return photon::EstimatedRobotPose(
|
|
|
|
|
pose.TransformBy(m_robotToCamera.Inverse()), cameraResult.GetTimestamp(),
|
|
|
|
|
cameraResult.GetTargets(), MULTI_TAG_PNP_ON_RIO);
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
2025-08-10 06:55:04 -07:00
|
|
|
std::optional<EstimatedRobotPose>
|
2026-01-11 13:58:53 -05:00
|
|
|
PhotonPoseEstimator::EstimatePnpDistanceTrigSolvePose(
|
|
|
|
|
PhotonPipelineResult cameraResult) {
|
|
|
|
|
if (!ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
PhotonTrackedTarget bestTarget = cameraResult.GetBestTarget();
|
2025-08-10 06:55:04 -07:00
|
|
|
std::optional<frc::Rotation2d> headingSampleOpt =
|
2026-01-11 13:58:53 -05:00
|
|
|
headingBuffer.Sample(cameraResult.GetTimestamp());
|
2025-08-10 06:55:04 -07:00
|
|
|
if (!headingSampleOpt) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"There was no heading data! Use AddHeadingData to add it!");
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Rotation2d headingSample = headingSampleOpt.value();
|
|
|
|
|
|
|
|
|
|
frc::Translation2d camToTagTranslation =
|
|
|
|
|
frc::Translation3d(
|
|
|
|
|
bestTarget.GetBestCameraToTarget().Translation().Norm(),
|
|
|
|
|
frc::Rotation3d(0_rad, -units::degree_t(bestTarget.GetPitch()),
|
|
|
|
|
-units::degree_t(bestTarget.GetYaw())))
|
|
|
|
|
.RotateBy(m_robotToCamera.Rotation())
|
|
|
|
|
.ToTranslation2d()
|
|
|
|
|
.RotateBy(headingSample);
|
|
|
|
|
|
|
|
|
|
std::optional<frc::Pose3d> fiducialPose =
|
|
|
|
|
aprilTags.GetTagPose(bestTarget.GetFiducialId());
|
|
|
|
|
if (!fiducialPose) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Tried to get pose of unknown April Tag: {}",
|
|
|
|
|
bestTarget.GetFiducialId());
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Pose2d tagPose = fiducialPose.value().ToPose2d();
|
|
|
|
|
|
|
|
|
|
frc::Translation2d fieldToCameraTranslation =
|
|
|
|
|
tagPose.Translation() - camToTagTranslation;
|
|
|
|
|
|
|
|
|
|
frc::Translation2d camToRobotTranslation =
|
|
|
|
|
(-m_robotToCamera.Translation().ToTranslation2d())
|
|
|
|
|
.RotateBy(headingSample);
|
|
|
|
|
|
|
|
|
|
frc::Pose2d robotPose = frc::Pose2d(
|
|
|
|
|
fieldToCameraTranslation + camToRobotTranslation, headingSample);
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
return EstimatedRobotPose{frc::Pose3d(robotPose), cameraResult.GetTimestamp(),
|
|
|
|
|
cameraResult.GetTargets(), PNP_DISTANCE_TRIG_SOLVE};
|
2025-08-10 06:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-14 09:06:15 -06:00
|
|
|
std::optional<EstimatedRobotPose>
|
2026-01-11 13:58:53 -05:00
|
|
|
PhotonPoseEstimator::EstimateAverageBestTargetsPose(
|
|
|
|
|
PhotonPipelineResult cameraResult) {
|
|
|
|
|
if (!ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
std::vector<std::pair<frc::Pose3d, std::pair<double, units::second_t>>>
|
|
|
|
|
tempPoses;
|
|
|
|
|
double totalAmbiguity = 0;
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
auto targets = cameraResult.GetTargets();
|
2023-02-11 23:44:22 +11:00
|
|
|
for (auto& target : targets) {
|
2023-01-14 09:06:15 -06:00
|
|
|
std::optional<frc::Pose3d> fiducialPose =
|
|
|
|
|
aprilTags.GetTagPose(target.GetFiducialId());
|
|
|
|
|
if (!fiducialPose) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning,
|
|
|
|
|
"Tried to get pose of unknown April Tag: {}",
|
|
|
|
|
target.GetFiducialId());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Pose3d targetPose = fiducialPose.value();
|
|
|
|
|
// Ambiguity = 0, use that pose
|
|
|
|
|
if (target.GetPoseAmbiguity() == 0) {
|
|
|
|
|
return EstimatedRobotPose{
|
|
|
|
|
targetPose.TransformBy(target.GetBestCameraToTarget().Inverse())
|
|
|
|
|
.TransformBy(m_robotToCamera.Inverse()),
|
2026-01-11 13:58:53 -05:00
|
|
|
cameraResult.GetTimestamp(), cameraResult.GetTargets(),
|
|
|
|
|
AVERAGE_BEST_TARGETS};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
totalAmbiguity += 1. / target.GetPoseAmbiguity();
|
|
|
|
|
|
|
|
|
|
tempPoses.push_back(std::make_pair(
|
|
|
|
|
targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()),
|
2026-01-11 13:58:53 -05:00
|
|
|
std::make_pair(target.GetPoseAmbiguity(),
|
|
|
|
|
cameraResult.GetTimestamp())));
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Translation3d transform = frc::Translation3d();
|
|
|
|
|
frc::Rotation3d rotation = frc::Rotation3d();
|
|
|
|
|
|
|
|
|
|
for (std::pair<frc::Pose3d, std::pair<double, units::second_t>>& pair :
|
|
|
|
|
tempPoses) {
|
2023-02-13 17:57:01 -05:00
|
|
|
double const weight = (1. / pair.second.first) / totalAmbiguity;
|
2023-01-14 09:06:15 -06:00
|
|
|
transform = transform + pair.first.Translation() * weight;
|
|
|
|
|
rotation = rotation + pair.first.Rotation() * weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EstimatedRobotPose{frc::Pose3d(transform, rotation),
|
2026-01-11 13:58:53 -05:00
|
|
|
cameraResult.GetTimestamp(),
|
|
|
|
|
cameraResult.GetTargets(), AVERAGE_BEST_TARGETS};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
2025-10-12 12:26:12 -07:00
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
std::optional<EstimatedRobotPose>
|
|
|
|
|
PhotonPoseEstimator::EstimateConstrainedSolvepnpPose(
|
|
|
|
|
photon::PhotonPipelineResult cameraResult,
|
|
|
|
|
photon::PhotonCamera::CameraMatrix cameraMatrix,
|
|
|
|
|
photon::PhotonCamera::DistortionMatrix distCoeffs, frc::Pose3d seedPose,
|
|
|
|
|
bool headingFree, double headingScaleFactor) {
|
|
|
|
|
if (!ShouldEstimate(cameraResult)) {
|
|
|
|
|
return std::nullopt;
|
2025-10-12 12:26:12 -07:00
|
|
|
}
|
2026-01-21 18:13:47 -05:00
|
|
|
// Need heading if heading fixed
|
2026-01-11 13:58:53 -05:00
|
|
|
if (!headingFree) {
|
2026-01-21 18:13:47 -05:00
|
|
|
if (!headingBuffer.Sample(cameraResult.GetTimestamp())) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
} else {
|
|
|
|
|
// If heading fixed, force rotation component
|
|
|
|
|
seedPose = frc::Pose3d{
|
|
|
|
|
seedPose.Translation(),
|
|
|
|
|
frc::Rotation3d{
|
|
|
|
|
headingBuffer.Sample(cameraResult.GetTimestamp()).value()}};
|
|
|
|
|
}
|
2025-10-12 12:26:12 -07:00
|
|
|
}
|
2026-01-11 13:58:53 -05:00
|
|
|
std::vector<photon::PhotonTrackedTarget> targets{
|
|
|
|
|
cameraResult.GetTargets().begin(), cameraResult.GetTargets().end()};
|
2025-10-12 12:26:12 -07:00
|
|
|
|
|
|
|
|
std::optional<photon::PnpResult> pnpResult =
|
|
|
|
|
VisionEstimation::EstimateRobotPoseConstrainedSolvePNP(
|
2026-01-11 13:58:53 -05:00
|
|
|
cameraMatrix, distCoeffs, targets, m_robotToCamera, seedPose,
|
|
|
|
|
aprilTags, photon::kAprilTag36h11, headingFree,
|
|
|
|
|
frc::Rotation2d{
|
|
|
|
|
headingBuffer.Sample(cameraResult.GetTimestamp()).value()},
|
|
|
|
|
headingScaleFactor);
|
2025-10-12 12:26:12 -07:00
|
|
|
|
|
|
|
|
if (!pnpResult) {
|
2026-01-11 13:58:53 -05:00
|
|
|
return std::nullopt;
|
2025-10-12 12:26:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frc::Pose3d best = frc::Pose3d{} + pnpResult->best;
|
|
|
|
|
|
2026-01-11 13:58:53 -05:00
|
|
|
return EstimatedRobotPose{best, cameraResult.GetTimestamp(),
|
|
|
|
|
cameraResult.GetTargets(),
|
2025-10-12 12:26:12 -07:00
|
|
|
PoseStrategy::CONSTRAINED_SOLVEPNP};
|
|
|
|
|
}
|
2023-11-19 15:16:22 -05:00
|
|
|
} // namespace photon
|