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
|
|
|
|
2023-12-16 15:26:00 -05:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
|
2023-02-13 18:22:22 -08:00
|
|
|
#include <cmath>
|
2023-01-14 09:06:15 -06:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <map>
|
2024-08-02 11:57:34 -04:00
|
|
|
#include <memory>
|
2023-01-14 09:06:15 -06:00
|
|
|
#include <span>
|
|
|
|
|
#include <string>
|
|
|
|
|
#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>
|
2023-02-13 17:57:01 -05:00
|
|
|
#include <opencv2/calib3d.hpp>
|
|
|
|
|
#include <opencv2/core/mat.hpp>
|
|
|
|
|
#include <opencv2/core/types.hpp>
|
2023-02-13 18:22:22 -08:00
|
|
|
#include <units/math.h>
|
2023-01-14 09:06:15 -06:00
|
|
|
#include <units/time.h>
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
#include "photon/PhotonCamera.h"
|
|
|
|
|
#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>
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-04-18 12:50:23 -05:00
|
|
|
PhotonPoseEstimator::PhotonPoseEstimator(frc::AprilTagFieldLayout tags,
|
|
|
|
|
PoseStrategy strat,
|
|
|
|
|
frc::Transform3d robotToCamera)
|
|
|
|
|
: aprilTags(tags),
|
|
|
|
|
strategy(strat),
|
|
|
|
|
camera(nullptr),
|
|
|
|
|
m_robotToCamera(robotToCamera),
|
|
|
|
|
lastPose(frc::Pose3d()),
|
|
|
|
|
referencePose(frc::Pose3d()),
|
2023-12-16 15:26:00 -05:00
|
|
|
poseCacheTimestamp(-1_s) {
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PhotonPoseEstimator,
|
|
|
|
|
InstanceCount);
|
|
|
|
|
InstanceCount++;
|
|
|
|
|
}
|
2023-04-18 12:50:23 -05:00
|
|
|
|
2023-01-14 09:06:15 -06:00
|
|
|
PhotonPoseEstimator::PhotonPoseEstimator(frc::AprilTagFieldLayout tags,
|
|
|
|
|
PoseStrategy strat, PhotonCamera&& cam,
|
|
|
|
|
frc::Transform3d robotToCamera)
|
|
|
|
|
: aprilTags(tags),
|
|
|
|
|
strategy(strat),
|
2023-04-18 12:50:23 -05:00
|
|
|
camera(std::make_shared<PhotonCamera>(std::move(cam))),
|
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()),
|
2023-12-16 15:26:00 -05:00
|
|
|
poseCacheTimestamp(-1_s) {
|
|
|
|
|
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-01-14 09:06:15 -06:00
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::Update() {
|
2023-04-18 12:50:23 -05:00
|
|
|
if (!camera) {
|
|
|
|
|
FRC_ReportError(frc::warn::Warning, "[PhotonPoseEstimator] Missing camera!",
|
|
|
|
|
"");
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
auto result = camera->GetLatestResult();
|
|
|
|
|
return Update(result, camera->GetCameraMatrix(), camera->GetDistCoeffs());
|
2023-02-09 14:43:52 -05:00
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
|
2023-02-09 14:43:52 -05:00
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::Update(
|
|
|
|
|
const PhotonPipelineResult& result) {
|
2023-04-18 12:50:23 -05:00
|
|
|
// If camera is null, best we can do is pass null calibration data
|
|
|
|
|
if (!camera) {
|
|
|
|
|
return Update(result, std::nullopt, std::nullopt, this->strategy);
|
|
|
|
|
}
|
|
|
|
|
return Update(result, camera->GetCameraMatrix(), camera->GetDistCoeffs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
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) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 06:56:38 -04:00
|
|
|
return Update(result, cameraMatrixData, cameraDistCoeffs, 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,
|
|
|
|
|
PoseStrategy strategy) {
|
2023-01-14 09:06:15 -06:00
|
|
|
std::optional<EstimatedRobotPose> ret = std::nullopt;
|
|
|
|
|
|
|
|
|
|
switch (strategy) {
|
|
|
|
|
case LOWEST_AMBIGUITY:
|
|
|
|
|
ret = LowestAmbiguityStrategy(result);
|
|
|
|
|
break;
|
|
|
|
|
case CLOSEST_TO_CAMERA_HEIGHT:
|
|
|
|
|
ret = ClosestToCameraHeightStrategy(result);
|
|
|
|
|
break;
|
|
|
|
|
case CLOSEST_TO_REFERENCE_POSE:
|
|
|
|
|
ret = ClosestToReferencePoseStrategy(result);
|
|
|
|
|
break;
|
|
|
|
|
case CLOSEST_TO_LAST_POSE:
|
|
|
|
|
SetReferencePose(lastPose);
|
|
|
|
|
ret = ClosestToReferencePoseStrategy(result);
|
|
|
|
|
break;
|
|
|
|
|
case AVERAGE_BEST_TARGETS:
|
|
|
|
|
ret = AverageBestTargetsStrategy(result);
|
|
|
|
|
break;
|
2023-10-17 10:20:00 -04:00
|
|
|
case MULTI_TAG_PNP_ON_COPROCESSOR:
|
|
|
|
|
ret =
|
|
|
|
|
MultiTagOnCoprocStrategy(result, cameraMatrixData, cameraDistCoeffs);
|
|
|
|
|
break;
|
|
|
|
|
case MULTI_TAG_PNP_ON_RIO:
|
|
|
|
|
ret = MultiTagOnRioStrategy(result, cameraMatrixData, cameraDistCoeffs);
|
2023-02-13 17:57:01 -05: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) {
|
|
|
|
|
lastPose = ret.value().estimatedPose;
|
|
|
|
|
}
|
2023-01-14 09:06:15 -06:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::LowestAmbiguityStrategy(
|
|
|
|
|
PhotonPipelineResult result) {
|
|
|
|
|
double lowestAmbiguityScore = std::numeric_limits<double>::infinity();
|
|
|
|
|
auto targets = result.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{
|
|
|
|
|
fiducialPose.value()
|
|
|
|
|
.TransformBy(bestTarget.GetBestCameraToTarget().Inverse())
|
|
|
|
|
.TransformBy(m_robotToCamera.Inverse()),
|
2023-10-17 10:20:00 -04:00
|
|
|
result.GetTimestamp(), result.GetTargets(), LOWEST_AMBIGUITY};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose>
|
|
|
|
|
PhotonPoseEstimator::ClosestToCameraHeightStrategy(
|
|
|
|
|
PhotonPipelineResult result) {
|
|
|
|
|
units::meter_t smallestHeightDifference =
|
|
|
|
|
units::meter_t(std::numeric_limits<double>::infinity());
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose> pose = std::nullopt;
|
|
|
|
|
|
|
|
|
|
for (auto& target : result.GetTargets()) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-02-13 17:57:01 -05:00
|
|
|
frc::Pose3d const targetPose = fiducialPose.value();
|
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()),
|
2023-10-17 10:20:00 -04:00
|
|
|
result.GetTimestamp(), result.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()),
|
2023-10-17 10:20:00 -04:00
|
|
|
result.GetTimestamp(), result.GetTargets(), CLOSEST_TO_CAMERA_HEIGHT};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pose;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose>
|
|
|
|
|
PhotonPoseEstimator::ClosestToReferencePoseStrategy(
|
|
|
|
|
PhotonPipelineResult result) {
|
|
|
|
|
units::meter_t smallestDifference =
|
|
|
|
|
units::meter_t(std::numeric_limits<double>::infinity());
|
|
|
|
|
units::second_t stateTimestamp = units::second_t(0);
|
|
|
|
|
frc::Pose3d pose = lastPose;
|
|
|
|
|
|
|
|
|
|
auto targets = result.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;
|
|
|
|
|
stateTimestamp = result.GetTimestamp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bestDifference < smallestDifference) {
|
|
|
|
|
smallestDifference = bestDifference;
|
|
|
|
|
pose = bestPose;
|
|
|
|
|
stateTimestamp = result.GetTimestamp();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 10:20:00 -04:00
|
|
|
return EstimatedRobotPose{pose, stateTimestamp, result.GetTargets(),
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-10-17 10:20:00 -04:00
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::MultiTagOnCoprocStrategy(
|
2024-05-29 17:28:35 -04:00
|
|
|
PhotonPipelineResult result,
|
|
|
|
|
std::optional<PhotonCamera::CameraMatrix> camMat,
|
|
|
|
|
std::optional<PhotonCamera::DistortionMatrix> distCoeffs) {
|
2023-11-19 15:16:22 -05:00
|
|
|
if (result.MultiTagResult().result.isPresent) {
|
2023-10-17 10:20:00 -04:00
|
|
|
const auto field2camera = result.MultiTagResult().result.best;
|
|
|
|
|
|
|
|
|
|
const auto fieldToRobot =
|
|
|
|
|
frc::Pose3d() + field2camera + m_robotToCamera.Inverse();
|
2023-11-19 15:16:22 -05:00
|
|
|
return photon::EstimatedRobotPose(fieldToRobot, result.GetTimestamp(),
|
|
|
|
|
result.GetTargets(),
|
|
|
|
|
MULTI_TAG_PNP_ON_COPROCESSOR);
|
2023-10-17 10:20:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Update(result, std::nullopt, std::nullopt,
|
|
|
|
|
this->multiTagFallbackStrategy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose> PhotonPoseEstimator::MultiTagOnRioStrategy(
|
2024-05-29 17:28:35 -04:00
|
|
|
PhotonPipelineResult result,
|
|
|
|
|
std::optional<PhotonCamera::CameraMatrix> camMat,
|
|
|
|
|
std::optional<PhotonCamera::DistortionMatrix> distCoeffs) {
|
2023-02-13 17:57:01 -05:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2023-06-18 00:00:30 -04:00
|
|
|
// Need at least 2 targets
|
2023-02-13 17:57:01 -05:00
|
|
|
if (!result.HasTargets() || result.GetTargets().size() < 2) {
|
2023-04-18 12:50:23 -05:00
|
|
|
return Update(result, std::nullopt, std::nullopt,
|
|
|
|
|
this->multiTagFallbackStrategy);
|
2023-02-13 17:57:01 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-18 00:00:30 -04:00
|
|
|
if (!camMat || !distCoeffs) {
|
|
|
|
|
return Update(result, std::nullopt, std::nullopt,
|
|
|
|
|
this->multiTagFallbackStrategy);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
auto const targets = result.GetTargets();
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
imagePoints.emplace_back(targetCorners[cornerIdx].first,
|
|
|
|
|
targetCorners[cornerIdx].second);
|
|
|
|
|
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) {
|
|
|
|
|
return Update(result, camMat, distCoeffs, this->multiTagFallbackStrategy);
|
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
|
|
|
{
|
|
|
|
|
cv::Mat cameraMatCV(camMat->rows(), camMat->cols(), CV_64F);
|
|
|
|
|
cv::eigen2cv(*camMat, cameraMatCV);
|
|
|
|
|
cv::Mat distCoeffsMatCV(distCoeffs->rows(), distCoeffs->cols(), CV_64F);
|
|
|
|
|
cv::eigen2cv(*distCoeffs, distCoeffsMatCV);
|
|
|
|
|
|
|
|
|
|
cv::solvePnP(objectPoints, imagePoints, cameraMatCV, distCoeffsMatCV, rvec,
|
|
|
|
|
tvec, false, cv::SOLVEPNP_SQPNP);
|
|
|
|
|
}
|
2023-02-13 17:57:01 -05:00
|
|
|
|
2023-09-12 06:56:38 -04:00
|
|
|
const Pose3d pose = detail::ToPose3d(tvec, rvec);
|
2023-02-13 17:57:01 -05:00
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
return photon::EstimatedRobotPose(pose.TransformBy(m_robotToCamera.Inverse()),
|
|
|
|
|
result.GetTimestamp(), result.GetTargets(),
|
|
|
|
|
MULTI_TAG_PNP_ON_RIO);
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<EstimatedRobotPose>
|
|
|
|
|
PhotonPoseEstimator::AverageBestTargetsStrategy(PhotonPipelineResult result) {
|
|
|
|
|
std::vector<std::pair<frc::Pose3d, std::pair<double, units::second_t>>>
|
|
|
|
|
tempPoses;
|
|
|
|
|
double totalAmbiguity = 0;
|
|
|
|
|
|
|
|
|
|
auto targets = result.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()),
|
2023-10-17 10:20:00 -04:00
|
|
|
result.GetTimestamp(), result.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()),
|
|
|
|
|
std::make_pair(target.GetPoseAmbiguity(), result.GetTimestamp())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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),
|
2023-10-17 10:20:00 -04:00
|
|
|
result.GetTimestamp(), result.GetTargets(),
|
|
|
|
|
AVERAGE_BEST_TARGETS};
|
2023-01-14 09:06:15 -06:00
|
|
|
}
|
2023-11-19 15:16:22 -05:00
|
|
|
} // namespace photon
|