-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
135 lines (111 loc) · 4.53 KB
/
Camera.java
File metadata and controls
135 lines (111 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package frc.robot.subsystems;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.apriltag.AprilTagFieldLayout;
import edu.wpi.first.apriltag.AprilTagFields;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Transform3d;
import java.io.IOException;
import java.util.Optional;
public class Camera extends SubsystemBase {
private NetworkTable table;
// TODO: these dont really need to be long, value will not exceed 320
private long x;
private long y;
private long width;
private long height;
private boolean lock;
private long tagId;
private double distanceCm;
private double angleOffset;
private AprilTagFieldLayout fieldLayout;
private Pose2d estimatedPose;
public Camera() {
table = NetworkTableInstance.getDefault().getTable("datatable");
try {
fieldLayout = AprilTagFieldLayout.loadFromResource(AprilTagFields.k2025ReefscapeAndyMark.m_resourceFile);
} catch (IOException e) {
// Handle exception
System.err.println("Failed to load AprilTag field layout: " + e.getMessage());
}
}
@Override
public void periodic() {
x = table.getEntry("x").getInteger(0);
SmartDashboard.putNumber("camera x", x);
y = table.getEntry("y").getInteger(0);
SmartDashboard.putNumber("camera y", y);
width = table.getEntry("width").getInteger(0);
SmartDashboard.putNumber("camera width", width);
height = table.getEntry("height").getInteger(0);
SmartDashboard.putNumber("camera height", height);
lock = table.getEntry("lock").getBoolean(true);
SmartDashboard.putBoolean("has lock", lock);
tagId = table.getEntry("tag_id").getInteger(0);
distanceCm = table.getEntry("distance_cm").getDouble(0);
angleOffset = table.getEntry("angle_offset").getDouble(0);
SmartDashboard.putNumber("AprilTag ID", tagId);
SmartDashboard.putNumber("Distance (cm)", distanceCm);
SmartDashboard.putNumber("Angle Offset", angleOffset);
if (lock && tagId > 0) {
Optional<Pose3d> tagPose = fieldLayout.getTagPose((int)tagId);
if (tagPose.isPresent()) {
// Calculate robot position using AprilTag data
double focalLength = 320.0; // Example value, needs calibration
double tagSize = 0.166; // Tag size in meters
// Calculate distance using the data from the Raspberry Pi
double distance = distanceCm / 100.0; // Convert cm to meters
// Calculate angle based on the angleOffset from the Pi
double angleRad = Math.toRadians(angleOffset);
// Create the transform
Transform3d cameraToTag = new Transform3d(
distance, 0, 0,
new Rotation3d(0, 0, angleRad)
);
// Camera position on robot - adjust for your robot
Transform3d robotToCamera = new Transform3d(
0.25, 0, 0.5,
new Rotation3d()
);
// Calculate robot pose
Pose3d cameraPose = tagPose.get().transformBy(cameraToTag.inverse());
Pose3d robotPose = cameraPose.transformBy(robotToCamera.inverse());
estimatedPose = robotPose.toPose2d();
SmartDashboard.putNumber("Pose X", estimatedPose.getX());
SmartDashboard.putNumber("Pose Y", estimatedPose.getY());
SmartDashboard.putNumber("Pose Angle", estimatedPose.getRotation().getDegrees());
}
}
}
public long getX() {
return x;
}
public long getY() {
return y;
}
public long getWidth() {
return width;
}
public long getHeight() {
return height;
}
public boolean isLocked() {
return lock;
}
public long getTagId() {
return tagId;
}
public double getDistanceCm() {
return distanceCm;
}
public double getAngleOffset() {
return angleOffset;
}
public Pose2d getEstimatedPose() {
return estimatedPose;
}
}