-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
491 lines (394 loc) · 18.2 KB
/
code
File metadata and controls
491 lines (394 loc) · 18.2 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import org.firstinspires.ftc.robotcore.external.navigation.Position;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import java.util.List;
import org.firstinspires.ftc.robotcore.external.ClassFactory;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
/**
* This 2020-2021 OpMode illustrates the basics of using the TensorFlow Object Detection API to
* determine the position of the Ultimate Goal game elements.
*
* Use Android Studio 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.
*
* IMPORTANT: In order to use this OpMode, you need to obtain your own Vuforia license key as
* is explained below.
*/
@Autonomous(name = "Ff", group = "Concept")
public class Ff extends LinearOpMode {
private static final String TFOD_MODEL_ASSET = "UltimateGoal.tflite";
private static final String LABEL_FIRST_ELEMENT = "Quad";
private static final String LABEL_SECOND_ELEMENT = "Single";
private DcMotor leftDrive2 = null;
private DcMotor rightDrive = null;
private DcMotor rightDrive2 = null;
private DcMotor leftDrive = null;
private DcMotor Intake = null;
// private DcMotor test = null;
private Servo servo0 = null;
private DcMotor Conveyor = null;
//private Servo Stopper = null;
private DcMotor Launch1 = null;
private Servo servo1 = null;
private Servo servo2 = null;
private Servo servo3 = null;
private Servo right = null;
private Servo left = null;
private Servo armvert = null;
private Servo armclose = null;
private Servo ringclose = null;
private Servo trigger = null;
private DcMotor WobbleArm = null;
/*
* IMPORTANT: You need to obtain your own license key to use Vuforia. The string below with which
* 'parameters.vuforiaLicenseKey' is initialized is for illustration only, and will not function.
* A Vuforia 'Development' license key, can be obtained free of charge from the Vuforia developer
* web site at https://developer.vuforia.com/license-manager.
*
* Vuforia license keys are always 380 characters long, and look as if they contain mostly
* random data. As an example, here is a example of a fragment of a valid key:
* ... yIgIzTqZ4mWjk9wd3cZO9T1axEqzuhxoGlfOOI2dRzKS4T0hQ8kT ...
* Once you've obtained a license key, copy the string from the Vuforia web site
* and paste it in to your code on the next line, between the double quotes.
*/
private static final String VUFORIA_KEY =
"";
/**
* {@link #vuforia} is the variable we will use to store our instance of the Vuforia
* localization engine.
*/
private VuforiaLocalizer vuforia;
/**
* {@link #tfod} is the variable we will use to store our instance of the TensorFlow Object
* Detection engine.
*/
private TFObjectDetector tfod;
@Override
public void runOpMode() {
// The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
// first.
initVuforia();
initTfod();
leftDrive2 = hardwareMap.get(DcMotor.class, "left_drive2");
rightDrive = hardwareMap.get(DcMotor.class, "right_drive");
rightDrive2 = hardwareMap.get(DcMotor.class, "right_drive2");
leftDrive = hardwareMap.get(DcMotor.class, "left_drive");
Intake = hardwareMap.get(DcMotor.class, "Intake");
//test = hardwareMap.get(DcMotor.class, "test");
servo0 = hardwareMap.get(Servo.class, "servo0");
Conveyor = hardwareMap.get(DcMotor.class, "Conveyor");
Launch1 = hardwareMap.get(DcMotor.class, "Launch1");
// Stopper = hardwareMap.get(Servo.class, "Stopper");
servo1 = hardwareMap.get(Servo.class, "servo1");
servo2 = hardwareMap.get(Servo.class, "servo2");
servo3 = hardwareMap.get(Servo.class, "servo3");
right = hardwareMap.get(Servo.class, "right");
left = hardwareMap.get(Servo.class, "left");
armvert = hardwareMap.get(Servo.class, "armvert");
ringclose = hardwareMap.get(Servo.class, "ringclose");
armclose = hardwareMap.get(Servo.class, "armclose");
trigger = hardwareMap.get(Servo.class, "trigger");
WobbleArm = hardwareMap.get(DcMotor.class, "WobbleArm");
/**
* Activate TensorFlow Object Detection before we wait for the start command.
* Do it here so that the Camera Stream window will have the TensorFlow annotations visible.
**/
if (tfod != null) {
tfod.activate();
// The TensorFlow software will scale the input images from the camera to a lower resolution.
// This can result in lower detection accuracy at longer distances (> 55cm or 22").
// If your target is at distance greater than 50 cm (20") you can adjust the magnification value
// to artificially zoom in to the center of image. For best results, the "aspectRatio" argument
// should be set to the value of the images used to create the TensorFlow Object Detection model
// (typically 1.78 or 16/9).
// Uncomment the following line if you want to adjust the magnification and/or the aspect ratio of the input images.
//tfod.setZoom(2.5, 1.78);
}
/** Wait for the game to begin */
telemetry.addData(">", "PLEASE WAIT 5 SECONDS BEFORE RUNNING");
telemetry.update();
waitForStart();
if (opModeIsActive()) {
while (opModeIsActive()) {
if (tfod != null) {
// getUpdatedRecognitions() will return null if no new information is available since
// the last time that call was made.
List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
if (updatedRecognitions != null) {
telemetry.addData("# Object Detected", updatedRecognitions.size());
{
if (updatedRecognitions.size() == 0){
//no rings
telemetry.addData("TFOD", "Not Items Detected" );
telemetry.addData("Target Zone", "A");
telemetry.update();
telemetry.update();
// WobbleArm.setPower(-1);
//ringclose.setPosition(1);
sleep(1000);
//armvert.setPosition(-1);
ringclose.setPosition(1);
leftDrive2.setPower(-0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(3300);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(500);
WobbleArm.setPower(0.7);
sleep(700);
ringclose.setPosition(-1);
sleep(1000);
WobbleArm.setPower(-0.8);
sleep(200);
ringclose.setPosition(-1);
sleep(1000);
ringclose.setPosition(-1);
leftDrive2.setPower(0.5) ;
rightDrive.setPower(-0.5) ;
leftDrive.setPower(-0.5) ;
rightDrive2.setPower(0.5) ;
sleep(3650);
leftDrive2.setPower(0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(-0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(1750);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(100);
/* WobbleArm.setPower(0.4);
sleep(900);
ringclose.setPosition(1);
sleep(700);
WobbleArm.setPower(-0.8);
*///sleep(100);
leftDrive2.setPower(-0.8) ;
rightDrive.setPower(0.8) ;
leftDrive.setPower(0.8) ;
rightDrive2.setPower(-0.8) ;
sleep(1900);
/* leftDrive2.setPower(1) ;
rightDrive.setPower(1) ;
leftDrive.setPower(-1) ;
rightDrive2.setPower(-1) ;
*/
sleep(700);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(100);
/* leftDrive2.setPower(1) ;
rightDrive.setPower(-1) ;
leftDrive.setPower(-1) ;
rightDrive2.setPower(1) ;*/
sleep(30000);
leftDrive2.setPower(-0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(400);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(1000);
sleep(30000);
}
int i = 0;
for (Recognition recognition : updatedRecognitions) {
telemetry.addData(String.format("label (%d)", i), recognition.getLabel());
telemetry.addData(String.format(" left,top (%d)", i), "%.03f , %.03f",
recognition.getLeft(), recognition.getTop());
telemetry.addData(String.format(" right,bottom (%d)", i), "%.03f , %.03f",
recognition.getRight(), recognition.getBottom());
if (recognition.getLabel().equals("Single")) {
telemetry.addData("TargetZone", "B");
telemetry.update();
// WobbleArm.setPower(-1);
//ringclose.setPosition(1);
sleep(1000);
//armvert.setPosition(-1);
ringclose.setPosition(1);
leftDrive2.setPower(-0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(3300);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(500);
WobbleArm.setPower(0.7);
sleep(700);
ringclose.setPosition(-1);
sleep(1000);
WobbleArm.setPower(-0.8);
sleep(200);
ringclose.setPosition(-1);
sleep(1000);
ringclose.setPosition(-1);
leftDrive2.setPower(0.5) ;
rightDrive.setPower(-0.5) ;
leftDrive.setPower(-0.5) ;
rightDrive2.setPower(0.5) ;
sleep(3650);
leftDrive2.setPower(0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(-0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(1750);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(100);
/* WobbleArm.setPower(0.4);
sleep(900);
ringclose.setPosition(1);
sleep(700);
WobbleArm.setPower(-0.8);
*///sleep(100);
leftDrive2.setPower(-0.8) ;
rightDrive.setPower(0.8) ;
leftDrive.setPower(0.8) ;
rightDrive2.setPower(-0.8) ;
sleep(1900);
/* leftDrive2.setPower(1) ;
rightDrive.setPower(1) ;
leftDrive.setPower(-1) ;
rightDrive2.setPower(-1) ;
*/
sleep(700);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(100);
/* leftDrive2.setPower(1) ;
rightDrive.setPower(-1) ;
leftDrive.setPower(-1) ;
rightDrive2.setPower(1) ;*/
sleep(30000);
} else if (recognition.getLabel().equals("Quad")) {
telemetry.addData("TargetZone", "C");
telemetry.update();
// WobbleArm.setPower(-1);
//ringclose.setPosition(1);
sleep(1000);
//armvert.setPosition(-1);
ringclose.setPosition(1);
leftDrive2.setPower(-0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(5400);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(500);
WobbleArm.setPower(0.7);
sleep(700);
ringclose.setPosition(-1);
sleep(1000);
WobbleArm.setPower(-0.8);
sleep(200);
ringclose.setPosition(-1);
sleep(1000);
ringclose.setPosition(-1);
leftDrive2.setPower(0.5) ;
rightDrive.setPower(-0.5) ;
leftDrive.setPower(-0.5) ;
rightDrive2.setPower(0.5) ;
sleep(6400);
leftDrive2.setPower(0.5) ;
rightDrive.setPower(0.5) ;
leftDrive.setPower(-0.5) ;
rightDrive2.setPower(-0.5) ;
sleep(1690);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(100);
/* WobbleArm.setPower(0.4);
sleep(900);
ringclose.setPosition(1);
sleep(700);
WobbleArm.setPower(-0.8);
*///sleep(100);
leftDrive2.setPower(-0.8) ;
rightDrive.setPower(0.8) ;
leftDrive.setPower(0.8) ;
rightDrive2.setPower(-0.8) ;
sleep(3300);
/* leftDrive2.setPower(1) ;
rightDrive.setPower(1) ;
leftDrive.setPower(-1) ;
rightDrive2.setPower(-1) ;
*/
sleep(700);
leftDrive2.setPower(0.8) ;
rightDrive.setPower(-0.8) ;
leftDrive.setPower(-0.8) ;
rightDrive2.setPower(0.8);
sleep(600);
leftDrive2.setPower(0) ;
rightDrive.setPower(0) ;
leftDrive.setPower(0) ;
rightDrive2.setPower(0) ;
sleep(100);
/* leftDrive2.setPower(1) ;
rightDrive.setPower(-1) ;
leftDrive.setPower(-1) ;
rightDrive2.setPower(1) ;*/
sleep(30000);
}
}
telemetry.update();
}
}
}
}
}
if (tfod != null) {
tfod.shutdown();
}
}
private void initVuforia() {
/*
* Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
*/
VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
parameters.vuforiaLicenseKey = VUFORIA_KEY;
parameters.cameraName = hardwareMap.get(WebcamName.class, "Webcam 1");
// Instantiate the Vuforia engine
vuforia = ClassFactory.getInstance().createVuforia(parameters);
// Loading trackables is not necessary for the TensorFlow Object Detection engine.
}
// Initialize the TensorFlow Object Detection engine.
private void initTfod() {
int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
"tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
tfodParameters.minResultConfidence = 0.5f;
tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_FIRST_ELEMENT, LABEL_SECOND_ELEMENT);
}
}