Make Vision pose estimation examples use all vision measurements (#1706)

Resolves https://github.com/PhotonVision/photonvision/issues/1634

---------

Signed-off-by: Jade Turner <spacey-sooty@proton.me>
Co-authored-by: Sam Freund <techguy763@gmail.com>
This commit is contained in:
Jade
2025-04-15 11:33:42 +08:00
committed by GitHub
parent 63b1ff242c
commit 1c42755451
5 changed files with 46 additions and 40 deletions

View File

@@ -34,15 +34,7 @@ void Robot::RobotInit() {}
void Robot::RobotPeriodic() {
launcher.periodic();
drivetrain.Periodic();
auto visionEst = vision.GetEstimatedGlobalPose();
if (visionEst.has_value()) {
auto est = visionEst.value();
auto estPose = est.estimatedPose.ToPose2d();
auto estStdDevs = vision.GetEstimationStdDevs(estPose);
drivetrain.AddVisionMeasurement(est.estimatedPose.ToPose2d(), est.timestamp,
estStdDevs);
}
vision.Periodic();
drivetrain.Log();
}

View File

@@ -51,7 +51,10 @@ class Robot : public frc::TimedRobot {
private:
SwerveDrive drivetrain{};
Vision vision{};
Vision vision{[=, this](frc::Pose2d pose, units::second_t timestamp,
Eigen::Matrix<double, 3, 1> stddevs) {
drivetrain.AddVisionMeasurement(pose, timestamp, stddevs);
}};
GamepieceLauncher launcher{};
frc::XboxController controller{0};
};

View File

@@ -31,6 +31,7 @@
#include <photon/simulation/VisionTargetSim.h>
#include <photon/targeting/PhotonPipelineResult.h>
#include <functional>
#include <limits>
#include <memory>
@@ -41,7 +42,14 @@
class Vision {
public:
Vision() {
/**
* @param estConsumer Lamba that will accept a pose estimate and pass it to
* your desired SwerveDrivePoseEstimator.
*/
Vision(std::function<void(frc::Pose2d, units::second_t,
Eigen::Matrix<double, 3, 1>)>
estConsumer)
: estConsumer{estConsumer} {
photonEstimator.SetMultiTagFallbackStrategy(
photon::PoseStrategy::LOWEST_AMBIGUITY);
@@ -68,9 +76,7 @@ class Vision {
photon::PhotonPipelineResult GetLatestResult() { return m_latestResult; }
std::optional<photon::EstimatedRobotPose> GetEstimatedGlobalPose() {
std::optional<photon::EstimatedRobotPose> visionEst;
void Periodic() {
// Run each new pipeline result through our pose estimator
for (const auto& result : camera.GetAllUnreadResults()) {
// cache result and update pose estimator
@@ -87,9 +93,12 @@ class Vision {
GetSimDebugField().GetObject("VisionEstimation")->SetPoses({});
}
}
}
return visionEst;
if (visionEst) {
estConsumer(visionEst->estimatedPose.ToPose2d(), visionEst->timestamp,
GetEstimationStdDevs(visionEst->estimatedPose.ToPose2d()));
}
}
}
Eigen::Matrix<double, 3, 1> GetEstimationStdDevs(frc::Pose2d estimatedPose) {
@@ -149,4 +158,6 @@ class Vision {
// The most recent result, cached for calculating std devs
photon::PhotonPipelineResult m_latestResult;
std::function<void(frc::Pose2d, units::second_t, Eigen::Matrix<double, 3, 1>)>
estConsumer;
};