Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce calculation in strategy functions #753

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ private Optional<EstimatedRobotPose> closestToCameraHeightStrategy(PhotonPipelin
.transformBy(target.getBestCameraToTarget().inverse())
.getZ());

if (alternateTransformDelta < smallestHeightDifference) {
if (alternateTransformDelta < smallestHeightDifference
&& alternateTransformDelta < bestTransformDelta) {
Comment on lines -488 to +489
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the point of this function is to find a transform such that the Z difference is minimized right? I think we can better represent this intent with something similar to (but not necessarily a) list comprehension? Probably out of scope, and I don't think anyone uses this function anyways.

smallestHeightDifference = alternateTransformDelta;
closestHeightTarget =
new EstimatedRobotPose(
Expand All @@ -495,9 +496,7 @@ private Optional<EstimatedRobotPose> closestToCameraHeightStrategy(PhotonPipelin
.transformBy(robotToCamera.inverse()),
result.getTimestampSeconds(),
result.getTargets());
}

if (bestTransformDelta < smallestHeightDifference) {
} else if (bestTransformDelta < smallestHeightDifference) {
smallestHeightDifference = bestTransformDelta;
closestHeightTarget =
new EstimatedRobotPose(
Expand Down Expand Up @@ -564,13 +563,12 @@ private Optional<EstimatedRobotPose> 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(
Expand Down
51 changes: 26 additions & 25 deletions photon-lib/src/main/native/cpp/photonlib/RobotPoseEstimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down
Loading