-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_new.java
451 lines (350 loc) · 17.3 KB
/
encoder_new.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
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
package org.firstinspires.ftc.teamcode;
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.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
import org.openftc.easyopencv.OpenCvInternalCamera;
import org.openftc.easyopencv.OpenCvPipeline;
import java.util.ArrayList;
import java.util.List;
@Autonomous(name = "encoder_new")
public class encoder_new extends LinearOpMode {
public OpenCvInternalCamera phoneCam;
public PowerPlayDeterminationPipeline pipeline;
private ElapsedTime runtime = new ElapsedTime();
public DcMotor frontLeft = null;
public DcMotor frontRight = null;
public DcMotor backRight = null;
public DcMotor backLeft = null;
//ColorSensor color_sensor;
public double pink_area;
public double green_area;
public double purple_area;
public static final double SERVO_POSITION = 0.7;
public static final double SERVO_RETRACTED_POSITION = 1.0;
/* Declare OpMode members. */
// private ElapsedTime run = new ElapsedTime();
Robot robot = new Robot(); // use the class created to define a Pushbot's hardware
double clawOffset = 0.0; // Servo mid position
final double CLAW_SPEED = 0.02; // sets rate to move servo
double TPR = 384.5;
double TP360 = TPR * 8;
public static final double STRAFE_SPEED = 0.5;
public static final String VUFORIA_KEY = "AQFvn1X/////AAABmfD5QbW72U/KuuZnETqfRvxtsghR7zWjrmjy6vu4H4g1VnCMWG5HPq3XhUD1kyMrbjP5lRruLzhYzgHtr9dK4TCjv0K0GTEK8Ww52Y++exIFmBRDTm84bjzBw0CPo0oeSx82fTU+c0S8B5Q4QFYWSCP5FD1pJESKDz3H5WnA0LoEFMkjpjwhMEbYqpVW6PdvO+QqSHBKBQjzKKstLDbEiZI+7A+A++dEaNehTtpy3cL3Jz0Jui6w6Fu3M/Dij9EwVHJTEEOJS4LEQdDV4U3lfkAuVxVxb0SGChr/1qcd+YzKQ/Cv3a5rKmMja54LkCfQ2dgRI3FapdPVZglZ2rr1nc0KTKI5T+Q3lzzhpc1brGZS";
VuforiaLocalizer vuforia = null;
static DcMotor lift;
public Servo flipper;
public Servo clawRight;
public Servo clawLeft;
public double COUNTS_PER_MOTOR_REV;
public double DRIVE_GEAR_REDUCTION;
public double WHEEL_DIAMETER_INCH;
public double COUNTS_PER_INCH;
public double ROBOT_DIAMETER;
public double ROBOT_CIRCUMFERENCE;
double power = 0.5;
public void cvinit() {
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
//color_sensor = hardwareMap.colorSensor.get("color");
telemetry.addData("Say", "Hello Driver");
// VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(cameraMonitorViewId);
// parameters.vuforiaLicenseKey = VUFORIA_KEY;
// parameters.useExtendedTracking = false;
// parameters.cameraName = hardwareMap.get(WebcamName.class, "Webcam 1");
// this.vuforia = ClassFactory.getInstance().createVuforia(parameters);
// // Load the trackable objects from the Assets file, and give them meaningful names
// VuforiaTrackables targetsPowerPlay = this.vuforia.loadTrackablesFromAsset("PowerPlay");
// targetsPowerPlay.get(0).setName("Red Audience Wall");
// targetsPowerPlay.get(1).setName("Red Rear Wall");
// targetsPowerPlay.get(2).setName("Blue Audience Wall");
// targetsPowerPlay.get(3).setName("Blue Rear Wall");
//
// // Start tracking targets in the background
// targetsPowerPlay.activate();
phoneCam = OpenCvCameraFactory.getInstance().createInternalCamera(OpenCvInternalCamera.CameraDirection.BACK, cameraMonitorViewId);
pipeline = new PowerPlayDeterminationPipeline();
phoneCam.setPipeline(pipeline);
phoneCam.setViewportRenderingPolicy(OpenCvCamera.ViewportRenderingPolicy.MAXIMIZE_EFFICIENCY);
phoneCam.pauseViewport();
phoneCam.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.
*/
//phoneCam.startStreaming(320, 240, OpenCvCameraRotation.UPRIGHT);
phoneCam.startStreaming(1280, 720, OpenCvCameraRotation.SIDEWAYS_LEFT);
}
@Override
public void onError(int errorCode) {
/*
* This will be called if the camera could not be opened
*/
telemetry.addData("Camera OnError", 0);
}
});
}
public void closeCamera() {
phoneCam.stopStreaming();
phoneCam.closeCameraDevice();
}
/*
* Code to run REPEATEDLY after the driver hits INIT, but before they hit PLAY
*/
/* computer vision start ------------------------------------------------ */
public class PowerPlayDeterminationPipeline extends OpenCvPipeline {
private boolean showContours = true;
int ringnum = 0;
int frameNumber = 0;
/* bounding rect and contours */
private List<MatOfPoint> contours = new ArrayList<>();
Rect bounding_rect_orange_global = new Rect();
private List<MatOfPoint> contours_orange = new ArrayList<>();
private List<MatOfPoint> contours_green = new ArrayList<>();
private List<MatOfPoint> contours_pink = new ArrayList<>();
private List<MatOfPoint> contours_purple = new ArrayList<>();
private Rect roi = new Rect(109, 0, 234, 198);
public synchronized void setShowCountours(boolean enabled) {
showContours = enabled;
}
public synchronized List<MatOfPoint> getContours() {
return contours;
}
double largest_area;
public Mat processFrame(Mat rgba) {
Size size = new Size(352, 198);
frameNumber++;
telemetry.addData("GotFrame ", frameNumber);
telemetry.addData("Say", "Got Frame");
Imgproc.resize(rgba, rgba, size);
rgba = new Mat(rgba.clone(), roi);
/* bounding boxes */
Rect bounding_rect_orange = new Rect();
/* matricies: hsv, thresholded, and rgba/thresholded cropped */
Mat hsv = new Mat();
Mat grey = new Mat();
Mat thresholded_green = new Mat();
Mat thresholded_purple = new Mat();
Mat thresholded_pink = new Mat();
/* change colorspace */
Imgproc.cvtColor(rgba, hsv, Imgproc.COLOR_RGB2HSV, 3);
/* threshold */
// Core.inRange(hsv, new Scalar(47, 150, 150), new Scalar(93, 255, 255), thresholded_green);
// Core.inRange(hsv, new Scalar(313, 150, 150), new Scalar(93, 255, 255), thresholded_purple);
// Core.inRange(hsv, new Scalar(148, 100, 100), new Scalar(179, 255, 255), thresholded_pink);
Core.inRange(hsv, new Scalar(47, 100, 100), new Scalar(93, 255, 255), thresholded_green);
Core.inRange(hsv, new Scalar(109, 100, 100), new Scalar(93, 255, 255), thresholded_purple);
Core.inRange(hsv, new Scalar(148, 100, 100), new Scalar(179, 255, 255), thresholded_pink);
/* find contours */
contours_orange = new ArrayList<>();
contours_green = new ArrayList<>();
contours_purple = new ArrayList<>();
contours_pink = new ArrayList<>();
Imgproc.findContours(thresholded_green, contours_green, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.findContours(thresholded_purple, contours_purple, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
//Imgproc.findContours(thresholded_pink, contours_pink, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
/* create a bounding rect based on the largest contour */
if (showContours && !contours_green.isEmpty()) {
largest_area = 0;
for (int i = 0; i < contours_green.size(); i++) /* iterate through the contours */ {
double area = Imgproc.contourArea(contours_green.get(i)); /* get contour area */
if (area > largest_area) {
largest_area = area; /* save the largest contour area */
/* get a bounding rectangle based on the largest contour */
bounding_rect_orange = Imgproc.boundingRect(contours_green.get(i));
}
green_area = largest_area;
}
/* draw the contours and the bounding rect */
Imgproc.drawContours(rgba, contours_green, -1, new Scalar(255, 255, 0), 1, 8);
}
if (showContours && !contours_purple.isEmpty()) {
largest_area = 0;
for (int i = 0; i < contours_purple.size(); i++) /* iterate through the contours */ {
double area = Imgproc.contourArea(contours_purple.get(i)); /* get contour area */
if (area > largest_area) {
largest_area = area; /* save the largest contour area */
/* get a bounding rectangle based on the largest contour */
bounding_rect_orange = Imgproc.boundingRect(contours_purple.get(i));
}
purple_area = largest_area;
}
/* draw the contours and the bounding rect */
Imgproc.drawContours(rgba, contours_purple, -1, new Scalar(255, 255, 0), 1, 8);
}
/*
/*if (showContours && !contours_pink.isEmpty()) {
largest_area = 0;
for (int i = 0; i < contours_pink.size(); i++) {
// double area = Imgproc.contourArea(contours_pink.get(i));
// if (area > largest_area) {
//largest_area = area;
//
bounding_rect_orange = Imgproc.boundingRect(contours_pink.get(i));
}
pink_area = largest_area;
}
Imgproc.drawContours(rgba, contours_pink, -1, new Scalar(255, 255, 0), 1, 8);
}*/
bounding_rect_orange_global = bounding_rect_orange;
telemetry.addData("Area ", largest_area);
hsv.release();
thresholded_green.release();
thresholded_pink.release();
thresholded_purple.release();
grey.release();
/* return the rgba matrix */
return rgba;
}
@Override
public void onViewportTapped() {
phoneCam.resumeViewport();
}
}
/* computer vision end ------------------------------------------------------------------------------------- */
@Override
public void runOpMode() {
telemetry.addData("Status", "Initialized");
telemetry.update();
frontLeft = hardwareMap.dcMotor.get("frontLeft");
frontRight = hardwareMap.dcMotor.get("frontRight");
backLeft = hardwareMap.dcMotor.get("backLeft");
backRight = hardwareMap.dcMotor.get("backRight");
lift = hardwareMap.dcMotor.get("lift");
flipper = hardwareMap.servo.get("flipper");
clawLeft = hardwareMap.servo.get("clawLeft");
clawRight = hardwareMap.servo.get("clawRight");
frontLeft.setDirection(DcMotor.Direction.REVERSE);
//frontRight.setDirection(DcMotor.Direction.REVERSE);
backLeft.setDirection(DcMotor.Direction.REVERSE);
//backRight.setDirection(DcMotor.Direction.REVERSE);
COUNTS_PER_MOTOR_REV = 537.7;
DRIVE_GEAR_REDUCTION = 1; // This is < 1.0 if geared UP (32 teeth to 16 teeth)
WHEEL_DIAMETER_INCH = 1.889765 * 2; // For figuring circumference
COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCH * 3.1415);
ROBOT_CIRCUMFERENCE = ROBOT_DIAMETER * 3.1415;
cvinit();
waitForStart();
runtime.reset();
// tile forward
// flipper.setPosition(1);
// sleep(4500);
encoderMoveINCH(49, 49, 0.3);
encoderStrafeINCH(24, 0.8);
// lift.setPower(1);
// sleep(4500);
// lift.setPower(0);
// telemetry.addData("status", "liftworking");
// flipper.setPosition(1);
// sleep(4500);
// flipper.setPosition(0);
// if(green_area > 100){
// //green
// encoderStrafeINCH(6, 0.8);
// encoderMoveINCH(12, 12, 0.3);
// }
// else if(purple_area < 100){
// //purple
// encoderStrafeINCH(-6, 0.8);
// encoderMoveINCH(12, 12, 0.3);
// }
// else{
// //pink
// z encoderMoveINCH(12, 12, 0.3);
}
//encoderStrafeINCH(-12, 0.8);
//encoderMoveINCH(20, -20, 0.3);
// encoderMoveINCH(10, -10, 0.5);
//deposit
//starfe 0.5 tile
//turn 90 right
//pick cone
int leftPos = 0;
int rightPos = 0;
public void encoderStrafeINCH(double inches, double power) {
int ticks = (int) (inches * COUNTS_PER_INCH);
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() - ticks);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + ticks);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + ticks);
backRight.setTargetPosition(backRight.getCurrentPosition() - ticks);
frontLeft.setPower(power);
frontRight.setPower(power);
backLeft.setPower(power);
backRight.setPower(power);
frontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//continue moving
while (opModeIsActive() && ((frontLeft.isBusy() && frontRight.isBusy() && backLeft.isBusy() && backRight.isBusy()))) {
}
// Stop all motion;
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
}
public void encoderMoveINCH(double leftINCH, double rightINCH, double power) {
int newLeftTarget = frontLeft.getCurrentPosition() + (int) (leftINCH * COUNTS_PER_INCH);
int newRightTarget = frontRight.getCurrentPosition() + (int) (rightINCH * COUNTS_PER_INCH);
double leftSpeed;
double rightSpeed;
frontLeft.setTargetPosition(newLeftTarget);
backLeft.setTargetPosition(newLeftTarget);
frontRight.setTargetPosition(newRightTarget);
backRight.setTargetPosition(newRightTarget);
if (Math.abs(leftINCH) > Math.abs(rightINCH)) {
leftSpeed = power;
rightSpeed = (power * rightINCH) / leftINCH;
} else {
rightSpeed = power;
leftSpeed = (power * leftINCH) / rightINCH;
}
frontLeft.setPower(leftSpeed);
frontRight.setPower(rightSpeed);
backLeft.setPower(leftSpeed);
backRight.setPower(rightSpeed);
frontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//continue movinggg
while (opModeIsActive() && ((frontLeft.isBusy() && frontRight.isBusy() && backLeft.isBusy() && backRight.isBusy()))) {
}
// Stop all motion;
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
}
}