Skip to content

Commit 936aaf3

Browse files
committed
AprilTag detection
1 parent 3eb119e commit 936aaf3

File tree

1 file changed

+100
-34
lines changed

1 file changed

+100
-34
lines changed

src/main/java/frc/robot/subsystems/vision/CamRIO.java

Lines changed: 100 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
package frc.robot.subsystems.vision;
66

77
import java.io.Console;
8+
import java.util.HashSet;
89

910
import org.opencv.core.Mat;
11+
import org.opencv.core.Point;
12+
import org.opencv.core.Scalar;
13+
import org.opencv.imgproc.Imgproc;
1014

15+
import edu.wpi.first.apriltag.AprilTagDetector;
16+
import edu.wpi.first.apriltag.AprilTagDetector.QuadThresholdParameters;
1117
import edu.wpi.first.cameraserver.CameraServer;
1218
import edu.wpi.first.cscore.CvSink;
1319
import edu.wpi.first.cscore.CvSource;
1420
import edu.wpi.first.cscore.UsbCamera;
21+
import edu.wpi.first.wpilibj.Timer;
1522
import edu.wpi.first.wpilibj2.command.SubsystemBase;
1623
import frc.robot.Constants;
1724

@@ -50,40 +57,99 @@ public void camInit() {
5057
//init visionThread
5158
visionThread = new Thread(
5259
() -> {
53-
// Get the USB camera from CameraServer
54-
UsbCamera camera = CameraServer.startAutomaticCapture();
55-
56-
//set resolution/viewport
57-
//lower resolutions == more fps, better RIO perfromance
58-
59-
camera.setResolution(640, 480);
60-
61-
//get the CvSink, this caputres/records video
62-
CvSink cvSink = CameraServer.getVideo();
63-
64-
CvSource videoStream = CameraServer.putVideo(cameraVision.cameraName, 640, 480);
65-
66-
// We should re-use one mat instead of 2, beacuse mats are memory heavy
67-
// 2 mats, one for regular images, one for grayscale (AprilTags)
68-
Mat mat = new Mat();
69-
Mat greymap = new Mat();
70-
71-
while (!Thread.interrupted()) {
72-
// tell sink to grab video frames from the camera and put it into the source mat
73-
if (cvSink.grabFrame(mat) == 0) {
74-
videoStream.notifyError("There was an error grabbing a frame from: ");
75-
videoStream.notifyError(cameraVision.cameraName);
76-
videoStream.notifyError(" sink err");
77-
videoStream.notifyError(cvSink.getError());
78-
continue;
79-
}
80-
videoStream.putFrame(mat);
81-
}
82-
83-
84-
});
85-
visionThread.setDaemon(true);
86-
visionThread.start();
60+
var camera = CameraServer.startAutomaticCapture();
61+
62+
var cameraWidth = 640;
63+
var cameraHeight = 480;
64+
65+
camera.setResolution(cameraWidth, cameraHeight);
66+
67+
var cvSink = CameraServer.getVideo();
68+
var outputStream = CameraServer.putVideo("RioApriltags", cameraWidth, cameraHeight);
69+
70+
var mat = new Mat();
71+
var grayMat = new Mat();
72+
73+
var pt0 = new Point();
74+
var pt1 = new Point();
75+
var pt2 = new Point();
76+
var pt3 = new Point();
77+
var center = new Point();
78+
var red = new Scalar(0, 0, 255);
79+
var green = new Scalar(0, 255, 0);
80+
81+
var aprilTagDetector = new AprilTagDetector();
82+
83+
var config = aprilTagDetector.getConfig();
84+
config.quadSigma = 0.8f;
85+
aprilTagDetector.setConfig(config);
86+
87+
var quadThreshParams = aprilTagDetector.getQuadThresholdParameters();
88+
quadThreshParams.minClusterPixels = 250;
89+
quadThreshParams.criticalAngle *= 5; // default is 10
90+
quadThreshParams.maxLineFitMSE *= 1.5;
91+
aprilTagDetector.setQuadThresholdParameters(quadThreshParams);
92+
93+
aprilTagDetector.addFamily("tag16h5");
94+
95+
var timer = new Timer();
96+
timer.start();
97+
var count = 0;
98+
99+
while (!Thread.interrupted()) {
100+
if (cvSink.grabFrame(mat) == 0) {
101+
outputStream.notifyError(cvSink.getError());
102+
continue;
103+
}
104+
105+
Imgproc.cvtColor(mat, grayMat, Imgproc.COLOR_RGB2GRAY);
106+
107+
var results = aprilTagDetector.detect(grayMat);
108+
109+
var set = new HashSet<>();
110+
111+
for (var result: results) {
112+
count += 1;
113+
pt0.x = result.getCornerX(0);
114+
pt1.x = result.getCornerX(1);
115+
pt2.x = result.getCornerX(2);
116+
pt3.x = result.getCornerX(3);
117+
118+
pt0.y = result.getCornerY(0);
119+
pt1.y = result.getCornerY(1);
120+
pt2.y = result.getCornerY(2);
121+
pt3.y = result.getCornerY(3);
122+
123+
center.x = result.getCenterX();
124+
center.y = result.getCenterY();
125+
126+
set.add(result.getId());
127+
128+
Imgproc.line(mat, pt0, pt1, red, 5);
129+
Imgproc.line(mat, pt1, pt2, red, 5);
130+
Imgproc.line(mat, pt2, pt3, red, 5);
131+
Imgproc.line(mat, pt3, pt0, red, 5);
132+
133+
Imgproc.circle(mat, center, 4, green);
134+
Imgproc.putText(mat, String.valueOf(result.getId()), pt2, Imgproc.FONT_HERSHEY_SIMPLEX, 2, green, 7);
135+
136+
};
137+
138+
for (var id : set){
139+
System.out.println("Tag: " + String.valueOf(id));
140+
}
141+
142+
if (timer.advanceIfElapsed(1.0)){
143+
System.out.println("detections per second: " + String.valueOf(count));
144+
count = 0;
145+
}
146+
147+
outputStream.putFrame(mat);
148+
}
149+
aprilTagDetector.close();
150+
});
151+
visionThread.setDaemon(true);
152+
visionThread.start();
87153
}
88154

89155
@Override

0 commit comments

Comments
 (0)