diff --git a/photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java b/photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java index 1951ec78af..4452368603 100644 --- a/photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java +++ b/photon-lib/src/main/java/org/photonvision/simulation/PhotonCameraSim.java @@ -423,7 +423,7 @@ public PhotonPipelineResult process( var pnpSim = new PnpResult(); if (tgt.fiducialID >= 0 && tgt.getFieldVertices().size() == 4) { // single AprilTag solvePNP pnpSim = - OpenCVHelp.solvePNP_SQUARE( + OpenCVHelp.solvePNP_SQPNP( prop.getIntrinsics(), prop.getDistCoeffs(), tgt.getModel().vertices, diff --git a/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp b/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp index 281df4b743..0b0f469555 100644 --- a/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp +++ b/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp @@ -210,9 +210,11 @@ std::optional PhotonCamera::GetCameraMatrix() { auto camCoeffs = cameraIntrinsicsSubscriber.Get(); if (camCoeffs.size() == 9) { PhotonCamera::CameraMatrix retVal = - Eigen::Map(camCoeffs.data()); + Eigen::Map>( + camCoeffs.data()); return retVal; } + return std::nullopt; } diff --git a/photon-lib/src/main/native/cpp/photon/simulation/PhotonCameraSim.cpp b/photon-lib/src/main/native/cpp/photon/simulation/PhotonCameraSim.cpp index bba7325782..567e6fed99 100644 --- a/photon-lib/src/main/native/cpp/photon/simulation/PhotonCameraSim.cpp +++ b/photon-lib/src/main/native/cpp/photon/simulation/PhotonCameraSim.cpp @@ -203,7 +203,7 @@ PhotonPipelineResult PhotonCameraSim::Process( std::optional pnpSim = std::nullopt; if (tgt.fiducialId >= 0 && tgt.GetFieldVertices().size() == 4) { - pnpSim = OpenCVHelp::SolvePNP_Square( + pnpSim = OpenCVHelp::SolvePNP_SQPNP( prop.GetIntrinsics(), prop.GetDistCoeffs(), tgt.GetModel().GetVertices(), noisyTargetCorners); } @@ -371,9 +371,10 @@ void PhotonCameraSim::SubmitProcessedFrame(const PhotonPipelineResult& result, ts.targetPoseEntry.Set(poseData, recieveTimestamp); } - auto intrinsics = prop.GetIntrinsics(); - std::vector intrinsicsView{intrinsics.data(), - intrinsics.data() + intrinsics.size()}; + Eigen::Matrix intrinsics = + prop.GetIntrinsics(); + std::span intrinsicsView{intrinsics.data(), + intrinsics.data() + intrinsics.size()}; ts.cameraIntrinsicsPublisher.Set(intrinsicsView, recieveTimestamp); auto distortion = prop.GetDistCoeffs(); diff --git a/photon-lib/src/test/native/cpp/VisionSystemSimTest.cpp b/photon-lib/src/test/native/cpp/VisionSystemSimTest.cpp index e57c91c4f0..07fa3b89a1 100644 --- a/photon-lib/src/test/native/cpp/VisionSystemSimTest.cpp +++ b/photon-lib/src/test/native/cpp/VisionSystemSimTest.cpp @@ -465,8 +465,8 @@ TEST_F(VisionSystemSimTest, TestPoseEstimation) { camEigen, distEigen, targets2, layout, photon::kAprilTag16h5); ASSERT_TRUE(results2); frc::Pose3d pose2 = frc::Pose3d{} + results2->best; - ASSERT_NEAR(5, pose2.X().to(), 0.01); - ASSERT_NEAR(1, pose2.Y().to(), 0.01); + ASSERT_NEAR(robotPose.X().to(), pose2.X().to(), 0.01); + ASSERT_NEAR(robotPose.Y().to(), pose2.Y().to(), 0.01); ASSERT_NEAR(0, pose2.Z().to(), 0.01); ASSERT_NEAR(units::degree_t{5}.convert().to(), pose2.Rotation().Z().to(), 0.01); diff --git a/photon-targeting/src/main/native/include/photon/estimation/VisionEstimation.h b/photon-targeting/src/main/native/include/photon/estimation/VisionEstimation.h index bb621f4f83..8924e5d0c6 100644 --- a/photon-targeting/src/main/native/include/photon/estimation/VisionEstimation.h +++ b/photon-targeting/src/main/native/include/photon/estimation/VisionEstimation.h @@ -47,6 +47,8 @@ static std::vector GetVisibleLayoutTags( return retVal; } +#include + static std::optional EstimateCamPosePNP( const Eigen::Matrix& cameraMatrix, const Eigen::Matrix& distCoeffs, @@ -101,8 +103,15 @@ static std::optional EstimateCamPosePNP( auto verts = tagModel.GetFieldVertices(tag.pose); objectTrls.insert(objectTrls.end(), verts.begin(), verts.end()); } - return OpenCVHelp::SolvePNP_SQPNP(cameraMatrix, distCoeffs, objectTrls, - points); + auto ret = OpenCVHelp::SolvePNP_SQPNP(cameraMatrix, distCoeffs, objectTrls, + points); + if (ret) { + // Invert best/alt transforms + ret->best = ret->best.Inverse(); + ret->alt = ret->alt.Inverse(); + } + + return ret; } }