This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompetitionAutonomous.java
253 lines (208 loc) · 11 KB
/
CompetitionAutonomous.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
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
package org.firstinspires.ftc.teamcode;
import android.util.Log;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import com.qualcomm.hardware.rev.RevBlinkinLedDriver;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.util.ElapsedTime;
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.Recognition;
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
import org.firstinspires.ftc.teamcode.Shared.Drive;
//import org.firstinspires.ftc.teamcode.Drive;
import java.util.List;
@Autonomous(name = "Competition", group = "Autonomous")
//@Disabled
public class CompetitionAutonomous extends LinearOpMode {
private final String TAG = getClass().getName();
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 ElapsedTime runtime = new ElapsedTime();
private Servo Load;
private DcMotorEx Ring;
private RevBlinkinLedDriver blinkinLedDriver;
private static final String VUFORIA_KEY =
"AUmiib//////AAABmbrFAeBqbkd/hmTwBoU6jXFUjeYK8xAgeYu6r9ZuLmpgb4tqNl4oIhXkpbBXmCusnhPlxJ3DHEkExTnQKhvCU49Yu2jslI6vaQ+V5F21ZAbbBod6lm9zyBEpkujo7IOq2TdOaJSIdN5wW3zxrHTksfrzBuKZKRsArompruh7jrm/B4W3F/EunA8ymkVoi29W84q81XMwJyonWlS2sd3pebXvLW0YOKmA63QgdmtSpp9XVAccwiH8ND8rk7FXlIIucim1Ig5FmVPLIx88t7doptXh8uiXfHHMqXc1T1MrRvfemYaUqyg7I5lYLNjLhuRmBZO3BM/qoyjPhpMVtGNh6+z3VgaKhP7O6zI07W0mmMfO";
private VuforiaLocalizer vuforia;
private TFObjectDetector tfod;
@Override
public void runOpMode() {
Load = hardwareMap.get(Servo.class, "Load");
Ring = hardwareMap.get(DcMotorEx.class, "Ring");
blinkinLedDriver = hardwareMap.get(RevBlinkinLedDriver.class, "LEDs");
Ring.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.COLOR_WAVES_RAINBOW_PALETTE);
initVuforia();
initTfod();
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(1.75, 1.78);
}
Log.i(TAG, "runOpmode: Waiting for IMU...");
Drive drive = new Drive(this);
drive.init();
telemetry.addLine("IMU ready");
telemetry.update();
Log.i(TAG, "runOpmode: IMU ready");
waitForStart();
drive.turnSweeper(.3, 1);
runtime.reset();
int singleVotes = 0;
int quadVotes = 0;
int votes = 0;
long startMillis = System.currentTimeMillis();
while (System.currentTimeMillis() - startMillis < 2000) {
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.getRecognitions();
if (updatedRecognitions != null) {
votes++;
// step through the list of recognitions and display boundary info.
int i = 0;
for (Recognition recognition : updatedRecognitions) {
if (recognition.getConfidence() < 0.3) {
continue;
}
if (recognition.getLabel() == LABEL_FIRST_ELEMENT) {
quadVotes++;
} else {
singleVotes++;
}
}
}
}
sleep(100);
}
telemetry.addData("votes (single, quad)", "%d, %d", singleVotes, quadVotes);
telemetry.addData("total votes:", votes);
telemetry.addData("vote ratios (single, quad)", "%.3f, %.3f", singleVotes/(double)votes, quadVotes/(double)votes);
telemetry.addLine("Waiting for IMU...");
telemetry.update();
Log.i(TAG, String.format("runOpMode: votes (single, quad) %d, %d", singleVotes, quadVotes));
Log.i(TAG, String.format("total votes", votes));
Log.i(TAG, String.format("vote ratios (single, quad)", "%.3f, %.3f", singleVotes/(double)votes, quadVotes/(double)votes));
telemetry.addData("votes (single, quad)", "%d, %d", singleVotes, quadVotes);
telemetry.addData("total votes:", votes);
telemetry.addData("vote ratios (single, quad)", "%.3f, %.3f", singleVotes/(double)votes, quadVotes/(double)votes);
drive.runUsingEncoder();
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
double SPEED = 1;
if(singleVotes/(double)votes > 0.3){
//drive to square b
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.BLUE);
drive.vroomVroomMonitorTicks(SPEED, 16, 18, 10);
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, 0, 20, 10);
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, -34, 34, 10);
drive.ceaseMotion();
sleep(150);
drive.turnSweeper(.7, 1.0);
sleep(1000);
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, 10, -25, 10);
//sleep(1000); //Need to keep sweeper still until wobble goal comes to rest. Remove this when there is more code below
}else if(quadVotes/(double)votes > 0.3){
//drive to square c
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.RED);
drive.vroomVroomMonitorTicks(SPEED, 16, 12, 10);
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, 0, 83, 10);
drive.ceaseMotion();
sleep(150);
drive.turnSweeper(.7, 1.0);
sleep(1000); //Need to keep sweeper still until wobble goal comes to rest. Remove this when there is more code below
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, -27, -46, 4);
}else{
//drive to square a
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.GREEN);
drive.vroomVroomMonitorTicks(SPEED, 16, 16, 10);
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, 0, 34, 10);
drive.ceaseMotion();
sleep(150);
drive.turnSweeper(.7, 1.0);
Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
drive.vroomVroomMonitorTicks(SPEED/2, -20, -10, 4);
// Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
// drive.vroomVroomMonitorTicks(SPEED/2, 0, -12, 4);
// Log.i("DriveByEncoderOpMode", "************************ xyzabc *****************************");
// drive.vroomVroomMonitorTicks(SPEED/2, 0, 12, 4);
}
drive.ceaseMotion();
Ring.setVelocity(270, AngleUnit.DEGREES);
//Ring.setPower(.95);
sleep(1000);
Load.setPosition(0.7);
for(int i = 0; i < 5; i++) {
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.CP1_2_SPARKLE_2_ON_1);
sleep(100);
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.BLACK);
sleep(100);
}
Load.setPosition(.05);
sleep(1000);
Load.setPosition(0.7);
for(int i = 0; i < 5; i++) {
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.CP1_2_SPARKLE_2_ON_1);
sleep(100);
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.BLACK);
sleep(100);
}
Load.setPosition(.05);
sleep(1000);
Load.setPosition(0.7);
for(int i = 0; i < 5; i++) {
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.CP1_2_SPARKLE_2_ON_1);
sleep(100);
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.BLACK);
sleep(100);
}
Load.setPosition(.05);
sleep(1000);
blinkinLedDriver.setPattern(RevBlinkinLedDriver.BlinkinPattern.COLOR_WAVES_RAINBOW_PALETTE);
drive.vroomVroomMonitorTicks(SPEED/2, 0, 16, 10);
drive.ceaseMotion();
sleep(500);
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.cameraDirection = CameraDirection.BACK;
// 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.8f;
tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_FIRST_ELEMENT, LABEL_SECOND_ELEMENT);
}
}