diff --git a/photon-lib/src/main/java/org/photonvision/PhotonPoseEstimator.java b/photon-lib/src/main/java/org/photonvision/PhotonPoseEstimator.java index 0eecc02880..b1e70d468f 100644 --- a/photon-lib/src/main/java/org/photonvision/PhotonPoseEstimator.java +++ b/photon-lib/src/main/java/org/photonvision/PhotonPoseEstimator.java @@ -485,7 +485,8 @@ private Optional closestToCameraHeightStrategy(PhotonPipelin .transformBy(target.getBestCameraToTarget().inverse()) .getZ()); - if (alternateTransformDelta < smallestHeightDifference) { + if (alternateTransformDelta < smallestHeightDifference + && alternateTransformDelta < bestTransformDelta) { smallestHeightDifference = alternateTransformDelta; closestHeightTarget = new EstimatedRobotPose( @@ -495,9 +496,7 @@ private Optional closestToCameraHeightStrategy(PhotonPipelin .transformBy(robotToCamera.inverse()), result.getTimestampSeconds(), result.getTargets()); - } - - if (bestTransformDelta < smallestHeightDifference) { + } else if (bestTransformDelta < smallestHeightDifference) { smallestHeightDifference = bestTransformDelta; closestHeightTarget = new EstimatedRobotPose( @@ -564,13 +563,12 @@ private Optional closestToReferencePoseStrategy( double altDifference = Math.abs(calculateDifference(referencePose, altTransformPosition)); double bestDifference = Math.abs(calculateDifference(referencePose, bestTransformPosition)); - if (altDifference < smallestPoseDelta) { + if (altDifference < smallestPoseDelta && altDifference < bestDifference) { smallestPoseDelta = altDifference; lowestDeltaPose = new EstimatedRobotPose( altTransformPosition, result.getTimestampSeconds(), result.getTargets()); - } - if (bestDifference < smallestPoseDelta) { + } else if (bestDifference < smallestPoseDelta) { smallestPoseDelta = bestDifference; lowestDeltaPose = new EstimatedRobotPose( diff --git a/photon-lib/src/main/native/cpp/photonlib/RobotPoseEstimator.cpp b/photon-lib/src/main/native/cpp/photonlib/RobotPoseEstimator.cpp index 4d25a0a482..55db187516 100644 --- a/photon-lib/src/main/native/cpp/photonlib/RobotPoseEstimator.cpp +++ b/photon-lib/src/main/native/cpp/photonlib/RobotPoseEstimator.cpp @@ -153,22 +153,23 @@ RobotPoseEstimator::ClosestToCameraHeightStrategy() { continue; } frc::Pose3d targetPose = fiducialPose.value(); - units::meter_t alternativeDifference = units::math::abs( - p.second.Z() - - targetPose.TransformBy(target.GetAlternateCameraToTarget().Inverse()) - .Z()); - units::meter_t bestDifference = units::math::abs( - p.second.Z() - - targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()).Z()); - if (alternativeDifference < smallestHeightDifference) { + auto altCameraTransform = + targetPose.TransformBy(target.GetAlternateCameraToTarget().Inverse()); + units::meter_t alternativeDifference = + units::math::abs(p.second.Z() - altCameraTransform.Z()); + auto bestCameraTransform = + targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()); + units::meter_t bestDifference = + units::math::abs(p.second.Z() - bestCameraTransform.Z()); + + if (alternativeDifference < smallestHeightDifference && + alternativeDifference < bestDifference) { smallestHeightDifference = alternativeDifference; - pose = targetPose.TransformBy( - target.GetAlternateCameraToTarget().Inverse()); + pose = altCameraTransform; stateTimestamp = p.first->GetLatestResult().GetTimestamp(); - } - if (bestDifference < smallestHeightDifference) { + } else if (bestDifference < smallestHeightDifference) { smallestHeightDifference = bestDifference; - pose = targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()); + pose = bestCameraTransform; stateTimestamp = p.first->GetLatestResult().GetTimestamp(); } } @@ -198,25 +199,25 @@ RobotPoseEstimator::ClosestToReferencePoseStrategy() { continue; } frc::Pose3d targetPose = fiducialPose.value(); + auto altCameraTransform = + targetPose.TransformBy(target.GetAlternateCameraToTarget().Inverse()); units::meter_t alternativeDifference = units::math::abs(referencePose.Translation().Distance( - targetPose - .TransformBy(target.GetAlternateCameraToTarget().Inverse()) - .Translation())); + altCameraTransform.Translation())); + auto bestCameraTansform = + targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()); units::meter_t bestDifference = units::math::abs(referencePose.Translation().Distance( - targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()) - .Translation())); - if (alternativeDifference < smallestDifference) { + bestCameraTansform.Translation())); + + if (alternativeDifference < smallestDifference && + alternativeDifference < bestDifference) { smallestDifference = alternativeDifference; - pose = targetPose.TransformBy( - target.GetAlternateCameraToTarget().Inverse()); + pose = altCameraTransform; stateTimestamp = p.first->GetLatestResult().GetTimestamp(); - } - - if (bestDifference < smallestDifference) { + } else if (bestDifference < smallestDifference) { smallestDifference = bestDifference; - pose = targetPose.TransformBy(target.GetBestCameraToTarget().Inverse()); + pose = bestCameraTransform; stateTimestamp = p.first->GetLatestResult().GetTimestamp(); } }