This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignalSleeveDemo.java
132 lines (112 loc) · 6.15 KB
/
SignalSleeveDemo.java
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
package org.firstinspires.ftc.teamcode;
import com.acmerobotics.dashboard.FtcDashboard;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.openftc.apriltag.AprilTagDetection;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
import org.openftc.easyopencv.OpenCvWebcam;
import java.util.ArrayList;
@TeleOp(name="Signal Sleeve Demo", group="Concept")
public class SignalSleeveDemo extends LinearOpMode {
AprilTagDetectionPipeline aprilTagDetectionPipeline;
double tagsize = 0.02;
double fx = 822.317;
double fy = 822.317;
double cx = 319.495;
double cy = 242.502;
int numFramesWithoutDetection = 0;
final float DECIMATION_HIGH = 2;
final float DECIMATION_LOW = 1;
final float THRESHOLD_HIGH_DECIMATION_RANGE_METERS = 1.0f;
final int THRESHOLD_NUM_FRAMES_NO_DETECTION_BEFORE_LOW_DECIMATION = 4;
@Override
public void runOpMode() {
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
OpenCvWebcam webcam = OpenCvCameraFactory.getInstance().createWebcam(hardwareMap.get(WebcamName.class, "Webcam 1"), cameraMonitorViewId);
FtcDashboard dashboard = FtcDashboard.getInstance();
dashboard.startCameraStream(webcam, 0);
aprilTagDetectionPipeline = new AprilTagDetectionPipeline(tagsize, fx, fy, cx, cy);
webcam.setPipeline(aprilTagDetectionPipeline);
telemetry.addLine("Waiting for start");
telemetry.update();
waitForStart();
webcam.setMillisecondsPermissionTimeout(2500); // Timeout for obtaining permission is configurable. Set before opening.
webcam.openCameraDeviceAsync(new OpenCvCamera.AsyncCameraOpenListener()
{
@Override
public void onOpened()
{
/*
* Tell the webcam to start streaming images to us! Note that you must make sure
* the resolution you specify is supported by the camera. If it is not, an exception
* will be thrown.
*
* Keep in mind that the SDK's UVC driver (what OpenCvWebcam uses under the hood) only
* supports streaming from the webcam in the uncompressed YUV image format. This means
* that the maximum resolution you can stream at and still get up to 30FPS is 480p (640x480).
* Streaming at e.g. 720p will limit you to up to 10FPS and so on and so forth.
*
* Also, we specify the rotation that the webcam is used in. This is so that the image
* from the camera sensor can be rotated such that it is always displayed with the image upright.
* For a front facing camera, rotation is defined assuming the user is looking at the screen.
* For a rear facing camera or a webcam, rotation is defined assuming the camera is facing
* away from the user.
*/
webcam.startStreaming(640, 480, OpenCvCameraRotation.UPRIGHT);
}
@Override
public void onError(int errorCode)
{
/*
* This will be called if the camera could not be opened
*/
}
});
while(opModeIsActive()) {
ArrayList<AprilTagDetection> detections = aprilTagDetectionPipeline.getDetectionsUpdate();
/*
* Send some stats to the telemetry
*/
telemetry.addData("Frame Count", webcam.getFrameCount());
telemetry.addData("FPS", String.format("%.2f", webcam.getFps()));
telemetry.addData("Total frame time ms", webcam.getTotalFrameTimeMs());
telemetry.addData("Pipeline time ms", webcam.getPipelineTimeMs());
telemetry.addData("Overhead time ms", webcam.getOverheadTimeMs());
telemetry.addData("Theoretical max FPS", webcam.getCurrentPipelineMaxFps());
if(detections != null) {
// If we don't see any tags
if (detections.size() == 0) {
numFramesWithoutDetection++;
// If we haven't seen a tag for a few frames, lower the decimation
// so we can hopefully pick one up if we're e.g. far back
if (numFramesWithoutDetection >= THRESHOLD_NUM_FRAMES_NO_DETECTION_BEFORE_LOW_DECIMATION) {
aprilTagDetectionPipeline.setDecimation(DECIMATION_LOW);
}
}
// We do see tags!
else {
numFramesWithoutDetection = 0;
// If the target is within 1 meter, turn on high decimation to
// increase the frame rate
if (detections.get(0).pose.z < THRESHOLD_HIGH_DECIMATION_RANGE_METERS) {
aprilTagDetectionPipeline.setDecimation(DECIMATION_HIGH);
}
for (AprilTagDetection detection : detections) {
telemetry.addLine(String.format("\nDetected tag ID=%d", detection.id));
telemetry.addLine(String.format("Translation X: %.2f metres", detection.pose.x));
telemetry.addLine(String.format("Translation Y: %.2f metres", detection.pose.y));
telemetry.addLine(String.format("Translation Z: %.2f metres", detection.pose.z));
telemetry.addLine(String.format("Rotation Yaw: %.2f degrees", Math.toDegrees(detection.pose.yaw)));
telemetry.addLine(String.format("Rotation Pitch: %.2f degrees", Math.toDegrees(detection.pose.pitch)));
telemetry.addLine(String.format("Rotation Roll: %.2f degrees", Math.toDegrees(detection.pose.roll)));
}
}
}
telemetry.update();
sleep(250);
}
}
}