-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfessionalAuto.java
More file actions
413 lines (326 loc) · 14.3 KB
/
Copy pathProfessionalAuto.java
File metadata and controls
413 lines (326 loc) · 14.3 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
/*
* 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;
import com.acmerobotics.roadrunner.trajectory.Trajectory;
import com.acmerobotics.roadrunner.trajectory.TrajectoryBuilder;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.ServoImplEx;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.teamcode.trajectorysequence.TrajectorySequence;
import org.openftc.apriltag.AprilTagDetection;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
import com.acmerobotics.roadrunner.geometry.Pose2d;
import org.firstinspires.ftc.teamcode.drive.SampleMecanumDrive;
import java.util.ArrayList;
@Autonomous(name="Professional Auto", group="Competition")
public class ProfessionalAuto extends LinearOpMode
{
/*
private ElapsedTime runtime = new ElapsedTime();
DcMotor motorFrontLeft = hardwareMap.dcMotor.get("motorFrontLeft");
DcMotor motorBackLeft = hardwareMap.dcMotor.get("motorBackLeft");
DcMotor motorFrontRight = hardwareMap.dcMotor.get("motorFrontRight");
DcMotor motorBackRight = hardwareMap.dcMotor.get("motorBackRight");
*/
public DcMotorEx lift;
public ServoImplEx servoLeft;
public ServoImplEx servoRight;
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;
int ID_TAG_OF_INTEREST1 = 17;
int ID_TAG_OF_INTEREST2 = 18;
int ID_TAG_OF_INTEREST3 = 19; // Tag ID 18 from the 36h11 family
AprilTagDetection tagOfInterest = null;
@Override
public void runOpMode()
{
lift = hardwareMap.get(DcMotorEx.class, "lift");
// Needed for internal run to position PIDs
lift.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Lift will suspend in midair rather than coasting when at 0 power
lift.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
servoLeft = hardwareMap.get(ServoImplEx.class, "servoLeft");
servoRight = hardwareMap.get(ServoImplEx.class, "servoRight");
servoLeft.setPwmEnable();
servoRight.setPwmEnable();
float closeDistance = 1.0f;
float openDistance = 0.6f;
boolean closeClaw = true;
// put in run opmode
SampleMecanumDrive drive = new SampleMecanumDrive(hardwareMap);
//
// Trajectory location1Part1 = drive.trajectoryBuilder(new Pose2d())
// .forward(63d)
// .build();
double forwardDistance = 50;
double strafeDistane = 28 ;
// Use to go to tile 2
forwardDistance = forwardDistance;
TrajectorySequence location1 = drive.trajectorySequenceBuilder(new Pose2d())
.strafeLeft(strafeDistane)
// .turn(10)
.forward(forwardDistance)
.turn(Math.toRadians(142.5)) // extra 7.5 for lee-way
// .waitSeconds(2)
// .waitSeconds(1)
.build();
// Trajectory location1Part2 = drive.trajectoryBuilder(new Pose2d()).strafeLeft(40d).build();
TrajectorySequence location2 = drive.trajectorySequenceBuilder(new Pose2d())
.forward(forwardDistance)
// .waitSeconds(1)
.build();
TrajectorySequence location3 = drive.trajectorySequenceBuilder(new Pose2d())
.strafeRight(strafeDistane)
.forward(forwardDistance)
// .waitSeconds(2)
// .waitSeconds(1)
.build();
// Trajectory location3Part1 = drive.trajectoryBuilder(new Pose2d())
// .forward(63d)
// .build();
// Trajectory location3Part2 = drive.trajectoryBuilder(new Pose2d()).strafeRight(40d).build();
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
camera = OpenCvCameraFactory.getInstance().createWebcam(hardwareMap.get(WebcamName.class, "Webcam"), 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())
{
// Starts closed with cone and closeClaw = true
if (closeClaw) {
// Servo only go 0 to 1
servoRight.setPosition(closeDistance); // Close
servoLeft.setPosition(1.0f - closeDistance); // Close
}
if (!closeClaw) {
servoRight.setPosition(openDistance); // Open
servoLeft.setPosition(1.0f - openDistance); // Open
}
// Starts closed with cone and closeClaw = true
if (closeClaw) {
// Servo only go 0 to 1
servoRight.setPosition(closeDistance); // Close
servoLeft.setPosition(1.0f - closeDistance); // Close
}
if (!closeClaw) {
servoRight.setPosition(openDistance); // Open
servoLeft.setPosition(1.0f - openDistance); // Open
}
ArrayList<AprilTagDetection> currentDetections = aprilTagDetectionPipeline.getLatestDetections();
if(currentDetections.size() != 0)
{
boolean tag1Found = false;
boolean tag2Found = false;
boolean tag3Found = false;
for(AprilTagDetection tag : currentDetections)
{
if(tag.id == ID_TAG_OF_INTEREST1)
{
tagOfInterest = tag;
tag1Found = true;
break;
}
else if(tag.id == ID_TAG_OF_INTEREST2)
{
tagOfInterest = tag;
tag2Found = true;
break;
}
else if(tag.id == ID_TAG_OF_INTEREST3)
{
tagOfInterest = tag;
tag3Found = true;
break;
}
}
if(tag1Found)
{
telemetry.addLine("Tag of interest is in sight!\n\nLocation data:");
tagToTelemetry(tagOfInterest);
}
else if(tag2Found)
{
telemetry.addLine("Tag of interest is in sight!\n\nLocation data:");
tagToTelemetry(tagOfInterest);
}
else if(tag3Found)
{
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();
}
boolean hasRun = false;
while (opModeIsActive()) {
/* Actually do something useful */
// lift up to stop dragging cone
if (!hasRun) {
lift.setPower(1);
sleep(1000);
lift.setPower(0);
sleep(200);
}
if (tagOfInterest == null) {
/*
* Insert your autonomous code here, presumably running some default configuration
* since the tag was never sighted during INIT
*/
// Just in case go to location 1 if it fails
// drive.followTrajectory(location2);
// telemetry.addData("It failed, going to location 2 (Straight)", "");
// telemetry.update();
if(hasRun == true) {
break;
}
hasRun = true;
} else {
/*
* Insert your autonomous code here, probably using the tag pose to decide your configuration.
*/
// e.g.
if (tagOfInterest.pose.x <= 20) {
if (tagOfInterest.id == 17 && hasRun == false) {
//Run auto for Image 1
telemetry.addData(">", "Running Auto for Image 1");
telemetry.update();
//Do auto Code ~ 20 seconds
//Move to Area 1 ~ 10 seconds
drive.followTrajectorySequence(location1);
lift.setPower(1);
sleep(1750);
lift.setPower(0);
sleep(200);
// Go forward slightly, it's at 45 degrees to the middle junction in the third square
// new TrajectoryBuilder(new Pose2d())
// .forward(6)
// .build();
// This line above is untested
hasRun = true;
} else if (tagOfInterest.id == 18 && hasRun == false) {
//Run auto for Image 2
telemetry.addData(">", "Running Auto for Image 2");
telemetry.update();
//Do auto Code ~ 20 seconds
//Move to Area 2 ~ 10 seconds
drive.followTrajectorySequence(location2);
hasRun = true;
} else if (tagOfInterest.id == 19 && hasRun == false) {
//Run auto for Image 3
telemetry.addData(">", "Running Auto for Image 3");
telemetry.update();
//Do auto Code ~ 20 seconds
//Move to Area 3 ~ 10 seconds
drive.followTrajectorySequence(location3);
hasRun = true;
}
} else {
telemetry.addData(">", "Detected Other Teams April Tag");
}
}
}
}
void tagToTelemetry(AprilTagDetection detection)
{
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", Math.toDegrees(detection.pose.yaw)));
telemetry.addLine(String.format("Rotation Pitch: %.2f degrees", Math.toDegrees(detection.pose.pitch)));
telemetry.addLine(String.format("Rotation Roll: %.2f degrees", Math.toDegrees(detection.pose.roll)));
}
}