Skip to content

Commit

Permalink
Add support for single tag PhotonVision results (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Dec 27, 2024
1 parent 71ca304 commit 90be723
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package frc.robot.subsystems.vision;

import static frc.robot.subsystems.vision.VisionConstants.*;

import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Transform3d;
Expand Down Expand Up @@ -57,7 +59,7 @@ public void updateInputs(VisionIOInputs inputs) {
}

// Add pose observation
if (result.multitagResult.isPresent()) {
if (result.multitagResult.isPresent()) { // Multitag result
var multitagResult = result.multitagResult.get();

// Calculate robot pose
Expand All @@ -83,6 +85,33 @@ public void updateInputs(VisionIOInputs inputs) {
multitagResult.fiducialIDsUsed.size(), // Tag count
totalTagDistance / result.targets.size(), // Average tag distance
PoseObservationType.PHOTONVISION)); // Observation type

} else if (!result.targets.isEmpty()) { // Single tag result
var target = result.targets.get(0);

// Calculate robot pose
var tagPose = aprilTagLayout.getTagPose(target.fiducialId);
if (tagPose.isPresent()) {
Transform3d fieldToTarget =
new Transform3d(tagPose.get().getTranslation(), tagPose.get().getRotation());
Transform3d cameraToTarget = target.bestCameraToTarget;
Transform3d fieldToCamera = fieldToTarget.plus(cameraToTarget.inverse());
Transform3d fieldToRobot = fieldToCamera.plus(robotToCamera.inverse());
Pose3d robotPose = new Pose3d(fieldToRobot.getTranslation(), fieldToRobot.getRotation());

// Add tag ID
tagIds.add((short) target.fiducialId);

// Add observation
poseObservations.add(
new PoseObservation(
result.getTimestampSeconds(), // Timestamp
robotPose, // 3D pose estimate
target.poseAmbiguity, // Ambiguity
1, // Tag count
cameraToTarget.getTranslation().getNorm(), // Average tag distance
PoseObservationType.PHOTONVISION)); // Observation type
}
}
}

Expand Down

0 comments on commit 90be723

Please sign in to comment.