-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlueSight
439 lines (367 loc) · 19.8 KB
/
BlueSight
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
package org.firstinspires.ftc.teamcode.vision;
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.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
@Autonomous (name = "BlueRightScan", group = "Barcode")
public class BlueSight extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime();
// Calculate the COUNTS_PER_INCH for your specific drive train.
// Go to your motor vendor website to determine your motor's COUNTS_PER_MOTOR_REV
// For external drive gearing, set DRIVE_GEAR_REDUCTION as needed.
// For example, use a value of 2.0 for a 12-tooth spur gear driving a 24-tooth spur gear.
// This is gearing DOWN for less speed and more torque.
// For gearing UP, use a gear ratio less than 1.0. Note this will affect the direction of wheel rotation.
static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: TETRIX Motor Encoder
static final double DRIVE_GEAR_REDUCTION = 1.0 ; // No External Gearing.
static final double WHEEL_DIAMETER_INCHES = 4.0 ; // For figuring circumference
static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
(WHEEL_DIAMETER_INCHES * 3.1415);
static final double DRIVE_SPEED = 0.6;
static final double TURN_SPEED = 0.5;
// Define motors and servos
DcMotor fl;
DcMotor fr;
DcMotor bl;
DcMotor br;
Servo leftClaw;
Servo rightClaw;
DcMotor Lift;
DcMotor arm;
OpenCvCamera webcam;
BlueSightPipeline pipeline = new BlueSightPipeline(telemetry);
@Override
public void runOpMode() {
//initialize robot hardware
fl = hardwareMap.dcMotor.get("fL");
fr = hardwareMap.dcMotor.get("fR");
bl = hardwareMap.dcMotor.get("bL");
br = hardwareMap.dcMotor.get("bR");
Lift = hardwareMap.dcMotor.get("Lift");
leftClaw = hardwareMap.servo.get("LS");
rightClaw = hardwareMap.servo.get("RS");
arm = hardwareMap.dcMotor.get("Arm");
fr.setDirection(DcMotor.Direction.FORWARD);
fl.setDirection(DcMotor.Direction.REVERSE);
br.setDirection(DcMotor.Direction.FORWARD);
bl.setDirection(DcMotor.Direction.REVERSE);
arm.setDirection(DcMotor.Direction.FORWARD);
fr.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
fl.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
br.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
bl.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
arm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
arm.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
//FOR THE WEBCAM
/*
* Instantiate an OpenCvCamera object for the camera we'll be using.
* Webcam stream goes to RC phone
*/
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
webcam = OpenCvCameraFactory.getInstance().createWebcam(hardwareMap.get(WebcamName.class, "Webcam 1"), cameraMonitorViewId);
//webcam.startStreaming(320, 240, OpenCvCameraRotation.UPRIGHT);
//webcam.setPipeline(pipeline);
//waitForStart();
/*
* Open the connection to the camera device. New in v1.4.0 is the ability
* to open the camera asynchronously which allows faster init time, and
* better behavior when pressing stop during init (i.e. less of a chance
* of tripping the stuck watchdog)
*/
webcam.openCameraDeviceAsync(new OpenCvCamera.AsyncCameraOpenListener() {
@Override
public void onOpened() {
/*
* Tell the webcam to start streaming images to us! Note that you must make sure
* the resolution you specify is supported by the camera. If it is not, an exception
* will be thrown.
*
* Keep in mind that the SDK's UVC driver (what OpenCvWebcam uses under the hood) only
* supports streaming from the webcam in the uncompressed YUV image format. This means
* that the maximum resolution you can stream at and still get up to 30FPS is 480p (640x480).
* Streaming at e.g. 720p will limit you to up to 10FPS and so on and so forth.
*
* Also, we specify the rotation that the webcam is used in. This is so that the image
* from the camera sensor can be rotated such that it is always displayed with the image upright.
* For a front facing camera, rotation is defined assuming the user is looking at the screen.
* For a rear facing camera or a webcam, rotation is defined assuming the camera is facing
* away from the user.
*/
//320px x 340px
webcam.startStreaming(320, 240, OpenCvCameraRotation.UPRIGHT);
/*
* Specify the image processing pipeline we wish to invoke upon receipt
* of a frame from the camera. Note that switching pipelines on-the-fly
* (while a streaming session is in flight) *IS* supported.
*/
webcam.setPipeline(pipeline);
}
@Override
public void onError(int errorCode) {
telemetry.addData("errorCode", errorCode);
}
});
// Tell telemetry to update faster than the default 250ms period :)
telemetry.setMsTransmissionInterval(20);
telemetry.addLine("Waiting for start");
telemetry.update();
//Wait for the user to press start on the Driver Station
while (!isStarted() && !isStopRequested()){
leftClaw.setPosition(0.1);
rightClaw.setPosition(0.92);
}
//Manages Telemetry and stopping the stream
while (opModeIsActive()) {
sleep(2000);
telemetry.addData("Analysis", pipeline.getAnalysis());
telemetry.update();
//put on spike mark then go park
switch (pipeline.getAnalysis()) {
case LEFT:
encoderDrive(0.5,-11,-11,5.0); //Move 11 inches backwards
encoderDrive(0.5,8,-8,5.0); //Turn clockwise 90
encoderDrive(0.3,1.5,1.5,5.0); //Move 1 inch Forward
leftClaw.setPosition(0.2); //Open Left claw
sleep(1000);
encoderDrive(0.5,-2,-2,5.0); //Move 2 inches Backward
sleep(1000);
break;
case CENTER:
encoderDrive(0.5,-16,-16,5.0); //Move 16 inches backwards
leftClaw.setPosition(0.2); //Open Left Claw
sleep(1000);
encoderDrive(0.5,-2,-2,5.0); //Move 2 inches Backward
sleep(1000);
break;
case RIGHT:
encoderDrive(0.5,-11,-11,5.0); //Move 11 inches backwards
encoderDrive(0.5,-8,8,5.0); //Turn counterclockwise 90
encoderDrive(0.3,1.5,1.5,5.0); //Move 1 inch Forward
leftClaw.setPosition(0.2); //Open Left claw
sleep(1000);
encoderDrive(0.5,-2,-2,5.0); //Move 2 inches Backward
/* encoderDrive(0.5,-1,-1,5.0); //Move 1 inch Backward
encoderLeftStrafe(0.5,6,6,5.0); //Strafe 6 inches Left
encoderDrive(0.5,-28,-28,5.0); //Move 25 inches Backward*/
sleep(1000);
break;
}
//reminder to use the KNO3 auto transitioner once this code is working
webcam.stopStreaming();
webcam.closeCameraDevice();
break;
}
}
public void encoderDrive(double speed,
double leftInches, double rightInches,
double timeoutS) {
int newflTarget;
int newfrTarget;
int newblTarget;
int newbrTarget;
// Ensure that the opmode is still active
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
newflTarget = fl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newfrTarget = fr.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
newblTarget = bl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newbrTarget = br.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
fl.setTargetPosition(newflTarget);
fr.setTargetPosition(newfrTarget);
bl.setTargetPosition(newblTarget);
br.setTargetPosition(newbrTarget);
// Turn On RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_TO_POSITION);
fl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
br.setMode(DcMotor.RunMode.RUN_TO_POSITION);
bl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
fr.setPower(Math.abs(speed));
fl.setPower(Math.abs(speed));
br.setPower(Math.abs(speed));
bl.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
// However, if you require that BOTH motors have finished their moves before the robot continues
// onto the next step, use (isBusy() || isBusy()) in the loop test.
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(fr.isBusy() && br.isBusy() && bl.isBusy() && fl.isBusy())) {
// Display it for the driver.
telemetry.addData("Running to", " %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget);
telemetry.addData("Currently at", " at %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget,
fr.getCurrentPosition(), fl.getCurrentPosition(), br.getCurrentPosition(), bl.getCurrentPosition());
telemetry.update();
}
// Stop all motion;
fr.setPower(0);
fl.setPower(0);
br.setPower(0);
bl.setPower(0);
// Turn off RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250); // optional pause after each move.
}
}
public void encoderLeftStrafe(double speed, double leftInches, double rightInches, double timeoutS){
int newflTarget;
int newfrTarget;
int newblTarget;
int newbrTarget;
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
// For strafing one side's wheels 'attract' each other while the other side 'repels' each other
//The positive value will make the robot go to the left with current arrangement
newflTarget = fl.getCurrentPosition() - (int)(leftInches * COUNTS_PER_INCH);
newfrTarget = fr.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
newblTarget = bl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newbrTarget = br.getCurrentPosition() - (int)(rightInches * COUNTS_PER_INCH);
fl.setTargetPosition(newflTarget);
fr.setTargetPosition(newfrTarget);
bl.setTargetPosition(newblTarget);
br.setTargetPosition(newbrTarget);
// Turn On RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_TO_POSITION);
fl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
br.setMode(DcMotor.RunMode.RUN_TO_POSITION);
bl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
fr.setPower(Math.abs(speed));
fl.setPower(Math.abs(speed));
br.setPower(Math.abs(speed));
bl.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
// However, if you require that BOTH motors have finished their moves before the robot continues
// onto the next step, use (isBusy() || isBusy()) in the loop test.
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(fr.isBusy() && br.isBusy() && bl.isBusy() && fl.isBusy())) {
// Display it for the driver.
telemetry.addData("Running to", " %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget);
telemetry.addData("Currently at", " at %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget,
fr.getCurrentPosition(), fl.getCurrentPosition(), br.getCurrentPosition(), bl.getCurrentPosition());
telemetry.update();
}
// Stop all motion;
fr.setPower(0);
fl.setPower(0);
br.setPower(0);
bl.setPower(0);
// Turn off RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250); // optional pause after each move.
}
}
public void encoderRightStrafe(double speed, double leftInches, double rightInches, double timeoutS){
int newflTarget;
int newfrTarget;
int newblTarget;
int newbrTarget;
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
// For strafing one side's wheels 'attract' each other while the other side 'repels' each other
//The positive value will make the robot go to the left with current arrangement
newflTarget = fl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newfrTarget = fr.getCurrentPosition() - (int)(rightInches * COUNTS_PER_INCH);
newblTarget = bl.getCurrentPosition() - (int)(leftInches * COUNTS_PER_INCH);
newbrTarget = br.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
fl.setTargetPosition(newflTarget);
fr.setTargetPosition(newfrTarget);
bl.setTargetPosition(newblTarget);
br.setTargetPosition(newbrTarget);
// Turn On RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_TO_POSITION);
fl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
br.setMode(DcMotor.RunMode.RUN_TO_POSITION);
bl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
fr.setPower(Math.abs(speed));
fl.setPower(Math.abs(speed));
br.setPower(Math.abs(speed));
bl.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
// However, if you require that BOTH motors have finished their moves before the robot continues
// onto the next step, use (isBusy() || isBusy()) in the loop test.
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(fr.isBusy() && br.isBusy() && bl.isBusy() && fl.isBusy())) {
// Display it for the driver.
telemetry.addData("Running to", " %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget);
telemetry.addData("Currently at", " at %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget,
fr.getCurrentPosition(), fl.getCurrentPosition(), br.getCurrentPosition(), bl.getCurrentPosition());
telemetry.update();
}
// Stop all motion;
fr.setPower(0);
fl.setPower(0);
br.setPower(0);
bl.setPower(0);
// Turn off RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250); // optional pause after each move.
}
}
public void encoderArm(double speed, double ArmInches, double timeoutS) {
int newArmTarget;
// Ensure that the opmode is still active
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
newArmTarget = arm.getCurrentPosition() - (int)(ArmInches * COUNTS_PER_INCH);
arm.setTargetPosition(newArmTarget);
// Turn On RUN_TO_POSITION
arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
arm.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
// However, if you require that BOTH motors have finished their moves before the robot continues
// onto the next step, use (isBusy() || isBusy()) in the loop test.
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(arm.isBusy())) {
// Display it for the driver.
telemetry.addData("Running to", " %7d ", newArmTarget);
telemetry.addData("Currently at", " at %7d", newArmTarget,
arm.getCurrentPosition());
telemetry.update();
}
// Stop all motion;
arm.setPower(0);
// Turn off RUN_TO_POSITION
arm.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250); // optional pause after each move.
}
}
}