-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRBotAutonomous.java
160 lines (127 loc) · 5.85 KB
/
RRBotAutonomous.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.util.ElapsedTime;
import java.util.List;
import org.firstinspires.ftc.robotcore.external.ClassFactory;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection;
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
@Autonomous(name = "RRBotAutonomous")
public class RRBotAutonomous extends LinearOpMode {
private static final String TFOD_MODEL_ASSET = "FreightFrenzy_BCDM.tflite";
private static final String[] LABELS = {
"Ball",
"Cube",
"Duck",
"Marker"
};
private static final String VUFORIA_KEY = "INSERT KEY HERE";
private VuforiaLocalizer vuforia; // Local instance of Vuforia
private TFObjectDetector tfod; // Local instance of TensorFlow Object
private ElapsedTime runtime = new ElapsedTime();
@Override
public void runOpMode() {
initVuforia();
initTfod();
// Activate TensorFlow Object Detection
if (tfod != null) {
tfod.activate();
tfod.setZoom(2, 16.0/9.0);
}
/** Wait for the game to begin */
telemetry.addData("Status", "Robot initialized, locating object");
telemetry.update();
int objectPos = getObjectPos();
telemetry.addData("Status", "Robot initialized, object located");
telemetry.update();
waitForStart();
if (opModeIsActive()) {
while (opModeIsActive()){
objectPos = getObjectPos();
telemetry.addData("Status", "Robot running, object located");
telemetry.update();
}
}
}
public int getObjectPos() {
runtime.reset();
int objectY = 0;
int objectPos = 0;
while (runtime.seconds() < 5) {
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());
// Get y position of recognized object with highest confidence
boolean objectDetected = false;
if (updatedRecognitions.size() == 1) {
objectY = (int) updatedRecognitions.get(0).getLeft();
objectDetected = true;
telemetry.addData("Object Detection", objectY);
} else if (updatedRecognitions.size() == 0) {
telemetry.addData("Object Detection", "No object detected");
} else {
telemetry.addData("Object Detection", "More than one object detected");
objectDetected = true;
int object = 0;
float highestConfidence = 0;
int i = 0;
for (Recognition recognition : updatedRecognitions) {
if (recognition.getConfidence() > highestConfidence) {
object = i;
highestConfidence = recognition.getConfidence();
}
i++;
}
objectY = (int) updatedRecognitions.get(object).getLeft();
telemetry.addData("Object Detection", objectY);
}
if (!objectDetected) {
objectPos = 0;
telemetry.addData("Target Location", "[\uD83D\uDC24][ ][ ]");
} else if (objectDetected && objectY > 600) {
objectPos = 1;
telemetry.addData("Target Location", "[ ][\uD83D\uDC24][ ]");
} else if (objectDetected) {
objectPos = 2;
telemetry.addData("Target Location", "[ ][ ][\uD83D\uDC24]");
} else {
telemetry.addData("Target Location", "[ ][ ][ ]");
}
telemetry.update();
}
}
}
return objectPos;
}
/**
* Initialize the Vuforia localization engine.
*/
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.cameraDirection = CameraDirection.BACK;
// Instantiate the Vuforia engine
vuforia = ClassFactory.getInstance().createVuforia(parameters);
}
/**
* 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.8f;
tfodParameters.isModelTensorFlow2 = true;
tfodParameters.inputSize = 320;
tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABELS);
}
}