forked from ftctechnh/ftc_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVuforiaPhoneTesting.java
More file actions
249 lines (211 loc) · 11.6 KB
/
Copy pathVuforiaPhoneTesting.java
File metadata and controls
249 lines (211 loc) · 11.6 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (c) 2017 FIRST. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided that
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of FIRST nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.firstinspires.ftc.teamcode;
import com.disnodeteam.dogecv.CameraViewDisplay;
import com.disnodeteam.dogecv.DogeCV;
import com.disnodeteam.dogecv.Dogeforia;
import com.disnodeteam.dogecv.detectors.roverrukus.GoldAlignDetector;
import com.disnodeteam.dogecv.filters.LeviColorFilter;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix;
import org.firstinspires.ftc.robotcore.external.matrices.VectorF;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackable;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables;
import java.util.ArrayList;
import java.util.List;
import static org.firstinspires.ftc.robotcore.external.navigation.AngleUnit.DEGREES;
import static org.firstinspires.ftc.robotcore.external.navigation.AxesOrder.XYZ;
import static org.firstinspires.ftc.robotcore.external.navigation.AxesOrder.YZX;
import static org.firstinspires.ftc.robotcore.external.navigation.AxesReference.EXTRINSIC;
import static org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection.BACK;
import static org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection.FRONT;
/**
* This file contains an example of an iterative (Non-Linear) "OpMode".
* An OpMode is a 'program' that runs in either the autonomous or the teleop period of an FTC match.
* The names of OpModes appear on the menu of the FTC Driver Station.
* When an selection is made from the menu, the corresponding OpMode
* class is instantiated on the Robot Controller and executed.
*
* This particular OpMode just executes a basic Tank Drive Teleop for a two wheeled robot
* It includes all the skeletal structure that all iterative OpModes contain.
*
* Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
* Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
*/
@TeleOp(name="vuforia Phone Testing", group="DogeCV")
public class VuforiaPhoneTesting extends OpMode {
//Elapsed time and measurement constants
private ElapsedTime runtime = new ElapsedTime();
private static final float mmPerInch = 25.4f;
private static final float mmFTCFieldWidth = (12*6) * mmPerInch; // the width of the FTC field (from the center point to the outer panels)
private static final float mmTargetHeight = (6) * mmPerInch; // the height of the center of the target image above the floor
// Select which camera you want use. The FRONT camera is the one on the same side as the screen.
// Valid choices are: BACK or FRONT
private static final VuforiaLocalizer.CameraDirection CAMERA_CHOICE = BACK;
//Vuforia variables
private OpenGLMatrix lastLocation = null;
boolean targetVisible;
Dogeforia vuforia;
WebcamName webcamName;
List<VuforiaTrackable> allTrackables = new ArrayList<VuforiaTrackable>();
//Detector object
GoldAlignDetector detector;
@Override
public void init() {
// Setup camera and Vuforia parameters
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
// Set Vuforia parameters
parameters.vuforiaLicenseKey = " -- LICENSE KEY GOES HERE -- ";
parameters.fillCameraMonitorViewParent = true;
// Init Dogeforia
vuforia = new Dogeforia(parameters);
vuforia.enableConvertFrameToBitmap();
// Set target names
VuforiaTrackables targetsRoverRuckus = this.vuforia.loadTrackablesFromAsset("RoverRuckus");
VuforiaTrackable blueRover = targetsRoverRuckus.get(0);
blueRover.setName("Blue-Rover");
VuforiaTrackable redFootprint = targetsRoverRuckus.get(1);
redFootprint.setName("Red-Footprint");
VuforiaTrackable frontCraters = targetsRoverRuckus.get(2);
frontCraters.setName("Front-Craters");
VuforiaTrackable backSpace = targetsRoverRuckus.get(3);
backSpace.setName("Back-Space");
// For convenience, gather together all the trackable objects in one easily-iterable collection */
allTrackables.addAll(targetsRoverRuckus);
// Set trackables' location on field
OpenGLMatrix blueRoverLocationOnField = OpenGLMatrix
.translation(0, mmFTCFieldWidth, mmTargetHeight)
.multiplied(Orientation.getRotationMatrix(EXTRINSIC, XYZ, DEGREES, 90, 0, 0));
blueRover.setLocation(blueRoverLocationOnField);
OpenGLMatrix redFootprintLocationOnField = OpenGLMatrix
.translation(0, -mmFTCFieldWidth, mmTargetHeight)
.multiplied(Orientation.getRotationMatrix(EXTRINSIC, XYZ, DEGREES, 90, 0, 180));
redFootprint.setLocation(redFootprintLocationOnField);
OpenGLMatrix frontCratersLocationOnField = OpenGLMatrix
.translation(-mmFTCFieldWidth, 0, mmTargetHeight)
.multiplied(Orientation.getRotationMatrix(EXTRINSIC, XYZ, DEGREES, 90, 0 , 90));
frontCraters.setLocation(frontCratersLocationOnField);
OpenGLMatrix backSpaceLocationOnField = OpenGLMatrix
.translation(mmFTCFieldWidth, 0, mmTargetHeight)
.multiplied(Orientation.getRotationMatrix(EXTRINSIC, XYZ, DEGREES, 90, 0, -90));
backSpace.setLocation(backSpaceLocationOnField);
//Set camera displacement
final int CAMERA_FORWARD_DISPLACEMENT = 110; // eg: Camera is 110 mm in front of robot center
final int CAMERA_VERTICAL_DISPLACEMENT = 200; // eg: Camera is 200 mm above ground
final int CAMERA_LEFT_DISPLACEMENT = 0; // eg: Camera is ON the robot's center line
// Set phone location on robot
OpenGLMatrix phoneLocationOnRobot = OpenGLMatrix
.translation(CAMERA_FORWARD_DISPLACEMENT, CAMERA_LEFT_DISPLACEMENT, CAMERA_VERTICAL_DISPLACEMENT)
.multiplied(Orientation.getRotationMatrix(EXTRINSIC, YZX, DEGREES,
CAMERA_CHOICE == FRONT ? 90 : -90, 0, 0));
//Set info for the trackables
for (VuforiaTrackable trackable : allTrackables) {
((VuforiaTrackableDefaultListener)trackable.getListener()).setPhoneInformation(phoneLocationOnRobot, parameters.cameraDirection);
}
//Activate targets
targetsRoverRuckus.activate();
detector = new GoldAlignDetector(); // Create a gold aligndetector
detector.init(hardwareMap.appContext,CameraViewDisplay.getInstance(), 0, true);
detector.yellowFilter = new LeviColorFilter(LeviColorFilter.ColorPreset.YELLOW, 100); // Create new filter
detector.useDefaults(); // Use default settings
detector.areaScoringMethod = DogeCV.AreaScoringMethod.MAX_AREA; // Can also be PERFECT_AREA
//detector.perfectAreaScorer.perfectArea = 10000; // Uncomment if using PERFECT_AREA scoring
//Setup Vuforia
vuforia.setDogeCVDetector(detector); // Set the Vuforia detector
vuforia.enableDogeCV(); //Enable the DogeCV-Vuforia combo
vuforia.showDebug(); // Show debug info
vuforia.start(); // Start the detector
}
/*
* Code to run REPEATEDLY when the driver hits INIT
*/
@Override
public void init_loop() {
}
/*
* Code to run ONCE when the driver hits PLAY
*/
@Override
public void start() {
//Reset timer
runtime.reset();
}
/*
* Code to run REPEATEDLY when the driver hits PLAY
*/
@Override
public void loop() {
//Assume we can't find a target
targetVisible = false;
//Loop through trackables - if we find one, get the location
for (VuforiaTrackable trackable : allTrackables) {
if (((VuforiaTrackableDefaultListener)trackable.getListener()).isVisible()) {
//We found a target! Print data to telemetry
telemetry.addData("Visible Target", trackable.getName());
targetVisible = true;
// getUpdatedRobotLocation() will return null if no new information is available since the last time that call was made, or if the trackable is not currently visible.
OpenGLMatrix robotLocationTransform = ((VuforiaTrackableDefaultListener)trackable.getListener()).getUpdatedRobotLocation();
if (robotLocationTransform != null) {
lastLocation = robotLocationTransform;
}
break;
}
}
// Provide feedback as to where the robot is located (if we know).
if (targetVisible) {
// Express position (translation) of robot in inches.
VectorF translation = lastLocation.getTranslation();
telemetry.addData("Pos (in)", "{X, Y, Z} = %.1f, %.1f, %.1f",
translation.get(0) / mmPerInch, translation.get(1) / mmPerInch, translation.get(2) / mmPerInch);
// Express the rotation of the robot in degrees.
Orientation rotation = Orientation.getOrientation(lastLocation, EXTRINSIC, XYZ, DEGREES);
telemetry.addData("Rot (deg)", "{Roll, Pitch, Heading} = %.0f, %.0f, %.0f", rotation.firstAngle, rotation.secondAngle, rotation.thirdAngle);
}
else {
//No visible target
telemetry.addData("Visible Target", "none");
}
// Update telemetry
telemetry.update();
}
/*
* Code to run ONCE after the driver hits STOP
*/
@Override
public void stop() {
vuforia.stop();
}
}