forked from InspirationRobotics/FTC-2023-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenCV_AprilTagDetection.java
208 lines (171 loc) · 7.17 KB
/
OpenCV_AprilTagDetection.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2021 OpenFTC Team
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.firstinspires.ftc.teamcode.OpenCV;
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.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
import org.openftc.apriltag.AprilTagDetection;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
import java.util.ArrayList;
@TeleOp
public class OpenCV_AprilTagDetection extends LinearOpMode
{
OpenCvCamera camera;
AprilTagDetectionPipeline aprilTagDetectionPipeline;
static final double FEET_PER_METER = 3.28084;
// Lens intrinsics
// UNITS ARE PIXELS
// NOTE: this calibration is for the C920 webcam at 800x448.
// You will need to do your own calibration for other configurations!
double fx = 578.272;
double fy = 578.272;
double cx = 402.145;
double cy = 221.506;
// UNITS ARE METERS
double tagsize = 0.166;
// tag ID of april tag
//blue alliance
int left = 1;
int center = 2;
int right = 3;
//red alliance
int Left = 4;
int Center = 5;
int Right = 6;
AprilTagDetection tagOfInterest;
@Override
public void runOpMode()
{
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
camera = OpenCvCameraFactory.getInstance().createWebcam(hardwareMap.get(WebcamName.class, "Webcam 1"), cameraMonitorViewId);
aprilTagDetectionPipeline = new AprilTagDetectionPipeline(tagsize, fx, fy, cx, cy);
camera.setPipeline(aprilTagDetectionPipeline);
camera.openCameraDeviceAsync(new OpenCvCamera.AsyncCameraOpenListener()
{
@Override
public void onOpened()
{
camera.startStreaming(800,448, OpenCvCameraRotation.UPRIGHT);
}
@Override
public void onError(int errorCode)
{
}
});
telemetry.setMsTransmissionInterval(50);
/*
* The INIT-loop:
* This REPLACES waitForStart!
*/
while (!isStarted() && !isStopRequested())
{
ArrayList<AprilTagDetection> currentDetections = aprilTagDetectionPipeline.getLatestDetections();
if(currentDetections.size() != 0)
{
boolean tagFound = false;
for(AprilTagDetection tag : currentDetections)
{
if(tag.id == left || tag.id == center || tag.id == right || tag.id == Left || tag.id == Center || tag.id == Right)
{
tagOfInterest = tag;
tagFound = true;
break;
}
}
if(tagFound)
{
telemetry.addLine("Tag of interest is in sight!\n\nLocation data:");
tagToTelemetry(tagOfInterest);
}
else
{
telemetry.addLine("Don't see tag of interest :(");
if(tagOfInterest == null)
{
telemetry.addLine("(The tag has never been seen)");
}
else
{
telemetry.addLine("\nBut we HAVE seen the tag before; last seen at:");
tagToTelemetry(tagOfInterest);
}
}
}
else
{
telemetry.addLine("Don't see tag of interest :(");
if(tagOfInterest == null)
{
telemetry.addLine("(The tag has never been seen)");
}
else
{
telemetry.addLine("\nBut we HAVE seen the tag before; last seen at:");
tagToTelemetry(tagOfInterest);
}
}
telemetry.update();
sleep(20);
}
/*
* The START command just came in: now work off the latest snapshot acquired
* during the init loop.
*/
/* Update the telemetry */
if(tagOfInterest != null)
{
telemetry.addLine("Tag snapshot:\n");
tagToTelemetry(tagOfInterest);
telemetry.update();
}
else
{
telemetry.addLine("No tag snapshot available, it was never sighted during the init loop :(");
telemetry.update();
}
/* Actually do something useful */
if(tagOfInterest.id == left){
//left code
}else if (tagOfInterest.id == center){
//center code
}else if (tagOfInterest.id == right)
//right code
/* You wouldn't have this in your autonomous, this is just to prevent the sample from ending */
while (opModeIsActive()) {sleep(20);}
}
void tagToTelemetry(AprilTagDetection detection)
{
Orientation rot = Orientation.getOrientation(detection.pose.R, AxesReference.INTRINSIC, AxesOrder.YXZ, AngleUnit.DEGREES);
telemetry.addLine(String.format("\nDetected tag ID=%d", detection.id));
telemetry.addLine(String.format("Translation X: %.2f feet", detection.pose.x*FEET_PER_METER));
telemetry.addLine(String.format("Translation Y: %.2f feet", detection.pose.y*FEET_PER_METER));
telemetry.addLine(String.format("Translation Z: %.2f feet", detection.pose.z*FEET_PER_METER));
telemetry.addLine(String.format("Rotation Yaw: %.2f degrees", rot.firstAngle));
telemetry.addLine(String.format("Rotation Pitch: %.2f degrees", rot.secondAngle));
telemetry.addLine(String.format("Rotation Roll: %.2f degrees", rot.thirdAngle));
}
}