Skip to content

Commit cc0abdf

Browse files
committed
feat: AprilTag vision IO and implementation for Limelight
1 parent 2c65005 commit cc0abdf

File tree

3 files changed

+841
-0
lines changed

3 files changed

+841
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.team1540.robot2024.subsystems.vision;
2+
3+
import edu.wpi.first.math.geometry.Pose2d;
4+
import org.littletonrobotics.junction.AutoLog;
5+
6+
public interface AprilTagVisionIO {
7+
@AutoLog
8+
class AprilTagVisionIOInputs {
9+
public Pose2d estimatedPoseMeters = new Pose2d();
10+
public boolean hasTarget = false;
11+
public int[] seenTargetIDs = {};
12+
public Pose2d[] targetPosesMeters = {};
13+
14+
public double pipelineLatency = 0.0;
15+
public double captureLatency = 0.0;
16+
}
17+
18+
default void updateInputs(AprilTagVisionIOInputs inputs) {}
19+
20+
default String getName() {
21+
return "";
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.team1540.robot2024.subsystems.vision;
2+
3+
import edu.wpi.first.math.geometry.Pose2d;
4+
import edu.wpi.first.math.geometry.Pose3d;
5+
import edu.wpi.first.math.geometry.Rotation3d;
6+
import org.team1540.robot2024.util.vision.LimelightHelpers;
7+
8+
public class AprilTagVisionIOLimelight implements AprilTagVisionIO {
9+
private final String name;
10+
11+
public AprilTagVisionIOLimelight(String name, Pose3d cameraOffsetMeters) {
12+
this.name = name;
13+
LimelightHelpers.setCameraPose_RobotSpace(
14+
name,
15+
cameraOffsetMeters.getX(),
16+
cameraOffsetMeters.getY(),
17+
cameraOffsetMeters.getZ(),
18+
Math.toDegrees(cameraOffsetMeters.getRotation().getX()),
19+
Math.toDegrees(cameraOffsetMeters.getRotation().getY()),
20+
Math.toDegrees(cameraOffsetMeters.getRotation().getZ()));
21+
}
22+
23+
@Override
24+
public void updateInputs(AprilTagVisionIOInputs inputs) {
25+
LimelightHelpers.Results results = LimelightHelpers.getLatestResults(name).targetingResults;
26+
LimelightHelpers.LimelightTarget_Fiducial[] fiducialTargets = results.targets_Fiducials;
27+
28+
inputs.estimatedPoseMeters = new Pose3d(
29+
results.botpose_wpiblue[0],
30+
results.botpose_wpiblue[1],
31+
results.botpose_wpiblue[2],
32+
new Rotation3d(
33+
Math.toRadians(results.botpose_wpiblue[3]),
34+
Math.toRadians(results.botpose_wpiblue[4]),
35+
Math.toRadians(results.botpose_wpiblue[5])
36+
)
37+
).toPose2d();
38+
inputs.hasTarget = results.valid;
39+
inputs.seenTargetIDs = new int[fiducialTargets.length];
40+
for (int i = 0; i < fiducialTargets.length; i++) inputs.seenTargetIDs[i] = (int) fiducialTargets[i].fiducialID; // fiducial tag ids had better be ints
41+
inputs.targetPosesMeters = new Pose2d[fiducialTargets.length];
42+
for (int i = 0; i < fiducialTargets.length; i++) inputs.targetPosesMeters[i] = fiducialTargets[i].getTargetPose_RobotSpace2D();
43+
44+
inputs.captureLatency = results.latency_capture;
45+
inputs.pipelineLatency = results.latency_pipeline;
46+
}
47+
48+
@Override
49+
public String getName() {
50+
return name;
51+
}
52+
}

0 commit comments

Comments
 (0)