-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRBotDriveAuto.java
620 lines (514 loc) · 26.9 KB
/
RRBotDriveAuto.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
import org.firstinspires.ftc.robotcore.external.navigation.RelicRecoveryVuMark;
import java.util.Locale;
/**
* Functionality to move automatically move robot and use glyph arm during autonomous mode.
* @author Andrew Hollabaugh
* @since 2017-12-22
*/
public class RRBotDriveAuto
{
RRBotHardware robot;
RRBotGlyphArm glyphArm;
LinearOpMode opMode;
private ElapsedTime runtime = new ElapsedTime();
//gyro variables
private BNO055IMU imu;
private Orientation angles;
private final double COUNTS_PER_MOTOR_REV = 560; //andymark 20:1 gearbox
private final double DRIVE_GEAR_REDUCTION = 2.0; //drive train geared down 1:2
private final double WHEEL_DIAMETER_INCHES = 4.0;
private final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCHES * 3.1415);
private final double GLYPH_PLACE_DRIVE_SPEED_SLOW = 0.25;
private final double GLYPH_PLACE_DRIVE_SPEED = 0.7;
private final double GLYPH_PLACE_TURN_SPEED = 0.6;
private final int TURN_90 = 83;
private final int TURN_45_0 = 45;
private final int TURN_45_1 = TURN_90 / 2 - 15;
private final int TURN_135 = 135 - 15;
private final int TURN_135_B = 135;
private final double GLYPH_PLACE_DISTANCE_0_1_LEFT = 27.5;
private final double GLYPH_PLACE_DISTANCE_0_1_CENTER = 45;
private final double GLYPH_PLACE_DISTANCE_0_1_RIGHT = 39;
private final double GLYPH_PLACE_DISTANCE_0_2 = 9;
private final double GLYPH_PLACE_DISTANCE_0_2_alt = 9;
private final double GLYPH_PLACE_DISTANCE_0_3 = 5;
private final double GLYPH_PLACE_DISTANCE_1_1 = 24;
private final double GLYPH_PLACE_DISTANCE_1_2_LEFT = -10.75;
private final double GLYPH_PLACE_DISTANCE_1_2_CENTER = -3;
private final double GLYPH_PLACE_DISTANCE_1_2_RIGHT = 3.75;
private final double GLYPH_PLACE_DISTANCE_1_3 = 6;
private final double GLYPH_PLACE_DISTANCE_1_4 = 5;
private final double GLYPH_PLACE_DISTANCE_B_0_1_RIGHT = 30;
private final double GLYPH_PLACE_DISTANCE_B_0_1_CENTER = 46;
private final double GLYPH_PLACE_DISTANCE_B_0_1_LEFT = 40;
private final double GLYPH_PLACE_DISTANCE_B_0_2 = 9;
private final double GLYPH_PLACE_DISTANCE_B_0_3 = 5;
private final double GLYPH_PLACE_DISTANCE_B_1_1 = 27;
private final double GLYPH_PLACE_DISTANCE_B_1_2_RIGHT = -5;
private final double GLYPH_PLACE_DISTANCE_B_1_2_CENTER = 0;
private final double GLYPH_PLACE_DISTANCE_B_1_2_LEFT = 5.5;
private final double GLYPH_PLACE_DISTANCE_B_1_3 = 6;
private final double GLYPH_PLACE_DISTANCE_B_1_4 = 5;
/**
* Constructor gets hardware, glyph arm, and opmode objects from the opmode when it is constructed
* @param robot Hardware class for the robot
* @param glyphArm For controlling the glyph arm
* @param opMode Opmode object, used for things like sleep and whileOpModeIsActive
*/
public RRBotDriveAuto(RRBotHardware robot, RRBotGlyphArm glyphArm, LinearOpMode opMode)
{
this.robot = robot;
this.glyphArm = glyphArm;
this.opMode = opMode;
}
public void runOpMode() {}
/**
* Places the starting glyph in autonomous mode in the correct column
* @param allianceColor "red" or "blue"
* @param fieldPos the balancing stone the robot is on (0 for towards audience, 1 for away from audience)
* @param pictograph pictograph object scanned using vuforia
*/
public void AutoPlaceGlyph(String allianceColor, int fieldPos, RelicRecoveryVuMark pictograph)
{
initGyro();
//home the glyph arm if not already homed
while(!glyphArm.hasHomed())
{
glyphArm.HomeArm();
}
opMode.sleep(250);
if(allianceColor.equals("red"))
{
if(fieldPos == 0)
{
//red 0 start position
//drive forwards a differing amount depending on pictograph
if(pictograph == RelicRecoveryVuMark.CENTER)
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, GLYPH_PLACE_DISTANCE_0_1_CENTER, GLYPH_PLACE_DISTANCE_0_1_CENTER, 30);
}
else if(pictograph == RelicRecoveryVuMark.RIGHT)
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, GLYPH_PLACE_DISTANCE_0_1_RIGHT, GLYPH_PLACE_DISTANCE_0_1_RIGHT, 30);
}
else //left column is the fallback if vuforia fails
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, GLYPH_PLACE_DISTANCE_0_1_LEFT, GLYPH_PLACE_DISTANCE_0_1_LEFT, 30);
}
//turn 135 degrees or 45 degrees depending on pictograph
if(pictograph == RelicRecoveryVuMark.RIGHT || pictograph == RelicRecoveryVuMark.CENTER)
{
TurnByGyro("right", TURN_135, GLYPH_PLACE_TURN_SPEED);
}
else
{
TurnByGyro("right", TURN_45_0, GLYPH_PLACE_TURN_SPEED);
}
glyphArm.UpdateValues(); //must be run before every call from GlyphArm class to update arm/wrist positions and motor speed
glyphArm.MoveGlyphWristToState(GlyphWristState.BACK); //move the wrist to the BACK position
opMode.sleep(500);
//move forwards a differing amount depending on the pictograph
if(pictograph == RelicRecoveryVuMark.LEFT)
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_0_2, GLYPH_PLACE_DISTANCE_0_2, 5);
}
else
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_0_2_alt, GLYPH_PLACE_DISTANCE_0_2_alt, 5);
}
glyphArm.UpdateValues();
robot.grabber2Servo.setPosition(glyphArm.GRABBER_RELEASE_POS); //release the glyph
//back up so robot is not touching the glyph
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -GLYPH_PLACE_DISTANCE_0_3, -GLYPH_PLACE_DISTANCE_0_3, 2);
opMode.sleep(300);
glyphArm.UpdateValues();
glyphArm.MoveGlyphWristToState(GlyphWristState.START); //move wrist back to vertical START position so it is zeroed for teleop
}
else if(fieldPos == 1)
{
//red 1 start position
//drive forwards
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, GLYPH_PLACE_DISTANCE_1_1, GLYPH_PLACE_DISTANCE_1_1, 7);
//strafe left or right a differing amount depending on the pictograph
if(pictograph == RelicRecoveryVuMark.CENTER)
{
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_1_2_CENTER, 5);
}
else if(pictograph == RelicRecoveryVuMark.RIGHT)
{
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_1_2_RIGHT, 5);
}
else //left column is the fallback if vuforia fails
{
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_1_2_LEFT, 5);
}
//turn 45 degrees
TurnByGyro("left", TURN_45_1, GLYPH_PLACE_TURN_SPEED);
glyphArm.UpdateValues(); //must be run before every call from GlyphArm class to update arm/wrist positions and motor speed
glyphArm.MoveGlyphWristToState(GlyphWristState.BACK); //move wrist to BACK position
opMode.sleep(500);
//drive forwards
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_1_3, GLYPH_PLACE_DISTANCE_1_3, 5);
glyphArm.UpdateValues();
robot.grabber2Servo.setPosition(glyphArm.GRABBER_RELEASE_POS); //release the glyph
//move backwards so robot is not touching the glyph
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -GLYPH_PLACE_DISTANCE_1_4, -GLYPH_PLACE_DISTANCE_1_4, 2);
opMode.sleep(300);
glyphArm.UpdateValues();
glyphArm.MoveGlyphWristToState(GlyphWristState.START); //move wrist back to vertical START position so it is zeroed for teleop
}
}
else if(allianceColor.equals("blue"))
{
if(fieldPos == 0)
{
//blue 0 start position
//drive backwards a differing amount depending on the pictograph
if(pictograph == RelicRecoveryVuMark.CENTER)
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, -GLYPH_PLACE_DISTANCE_B_0_1_CENTER, -GLYPH_PLACE_DISTANCE_B_0_1_CENTER, 7);
}
else if(pictograph == RelicRecoveryVuMark.RIGHT)
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, -GLYPH_PLACE_DISTANCE_B_0_1_RIGHT, -GLYPH_PLACE_DISTANCE_B_0_1_RIGHT, 10);
}
else //left column is the fallback if vuforia fails
{
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, -GLYPH_PLACE_DISTANCE_B_0_1_LEFT, -GLYPH_PLACE_DISTANCE_B_0_1_LEFT, 7);
}
//turn a differing amount depending on the pictograph
if(pictograph == RelicRecoveryVuMark.RIGHT)
{
TurnByGyro("right", TURN_135, GLYPH_PLACE_TURN_SPEED);
}
else
{
TurnByGyro("right", TURN_45_0, GLYPH_PLACE_TURN_SPEED);
}
glyphArm.UpdateValues(); //must be run before every call from GlyphArm class to update arm/wrist positions and motor speed
glyphArm.MoveGlyphWristToState(GlyphWristState.BACK); //move wrist to BACK position
opMode.sleep(500);
//move forwards
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_B_0_2, GLYPH_PLACE_DISTANCE_B_0_2, 5);
glyphArm.UpdateValues();
robot.grabber2Servo.setPosition(glyphArm.GRABBER_RELEASE_POS); //release the glyph
//move backwards so robot is not touching the glyph
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -GLYPH_PLACE_DISTANCE_B_0_3, -GLYPH_PLACE_DISTANCE_B_0_3, 2);
opMode.sleep(300);
glyphArm.UpdateValues();
glyphArm.MoveGlyphWristToState(GlyphWristState.START); //move wrist back to vertical START position so it is zeroed for teleop
}
else if(fieldPos == 1)
{
//blue 1 start position
//drive backwards
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED_SLOW, -GLYPH_PLACE_DISTANCE_B_1_1, -GLYPH_PLACE_DISTANCE_B_1_1, 7);
//strafe left or right a differing amount depending on pictograph
if(pictograph == RelicRecoveryVuMark.CENTER)
{
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_B_1_2_CENTER, 5);
}
else if(pictograph == RelicRecoveryVuMark.RIGHT)
{
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_B_1_2_RIGHT, 5);
}
else //left column is the fallback if vuforia fails
{
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_B_1_2_LEFT, 5);
}
//turn 135 degrees
TurnByGyro("left", TURN_135_B, GLYPH_PLACE_TURN_SPEED);
glyphArm.UpdateValues(); //must be run before every call from GlyphArm class to update arm/wrist positions and motor speed
glyphArm.MoveGlyphWristToState(GlyphWristState.BACK); //move wrist to BACK position
opMode.sleep(500);
//drive forwards
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, GLYPH_PLACE_DISTANCE_B_1_3, GLYPH_PLACE_DISTANCE_B_1_3, 5);
glyphArm.UpdateValues();
robot.grabber2Servo.setPosition(glyphArm.GRABBER_RELEASE_POS); //release the glyph
//drive backwards so robot is not touching the glyph
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -GLYPH_PLACE_DISTANCE_B_1_4, -GLYPH_PLACE_DISTANCE_B_1_4, 2);
opMode.sleep(300);
glyphArm.UpdateValues();
glyphArm.MoveGlyphWristToState(GlyphWristState.START); //move wrist back to vertical START position so it is zeroed for teleop
}
}
}
/**
* UNFINISHED Gets a glyph from the glyph pit during autonomous using dead reckoning
* @param allianceColor "red" or "blue"
* @param fieldPos starting balancing stone
* @param pictograph pictograph object read using vuforia
*/
public void MultiGlyphGet(String allianceColor, int fieldPos, RelicRecoveryVuMark pictograph)
{
if(fieldPos == 0 && allianceColor.equals("red"))
{
//move backwards
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -2, -2, 5);
//turn and strafe depending on the column the first glyph was place so the robot is facing the glyph pile and in the center
if(pictograph == RelicRecoveryVuMark.CENTER)
{
TurnByGyro("right", TURN_135, GLYPH_PLACE_TURN_SPEED);
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, 3, 5);
}
else if(pictograph == RelicRecoveryVuMark.RIGHT)
{
TurnByGyro("right", TURN_135, GLYPH_PLACE_TURN_SPEED);
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, 6, 5);
}
else
{
TurnByGyro("left", TURN_135_B, GLYPH_PLACE_TURN_SPEED);
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, 4.25, 5);
}
glyphArm.UpdateValues();
glyphArm.MoveGlyphWristToState(GlyphWristState.BACK); //move wrist to BACK position
opMode.sleep(500);
glyphArm.UpdateValues();
glyphArm.RotateGrabber(); //move the second grabber up so two glyphs can be picked up at once
opMode.sleep(500);
glyphArm.UpdateValues();
//open grabbers
robot.grabber1Servo.setPosition(glyphArm.GRABBER_OPEN_POS);
robot.grabber2Servo.setPosition(glyphArm.GRABBER_OPEN_POS);
//move forwards into glyph pile
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, 26, 26, 10);
glyphArm.UpdateValues();
//close grabbers and turn on belts
robot.grabber1Servo.setPosition(glyphArm.GRABBER_CLOSE_POS);
robot.grabber1Belt.setPower(glyphArm.GRABBER_BELT_SPEED);
robot.grabber2Servo.setPosition(glyphArm.GRABBER_CLOSE_POS);
robot.grabber2Belt.setPower(glyphArm.GRABBER_BELT_SPEED);
//move backwards back to cryptobox
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -20, -20, 10);
//turn 135 degrees
TurnByGyro("left", TURN_135_B, GLYPH_PLACE_TURN_SPEED);
//turn off grabber belts
robot.grabber1Belt.setPower(0);
robot.grabber2Belt.setPower(0);
//strafe to align with column
EncoderDriveSideways(GLYPH_PLACE_DRIVE_SPEED, -2, 5);
//drive forwards into cryptobox
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, 5, 5, 10);
glyphArm.UpdateValues();
//release glyph(s)
robot.grabber1Servo.setPosition(glyphArm.GRABBER_RELEASE_POS);
robot.grabber2Servo.setPosition(glyphArm.GRABBER_RELEASE_POS);
//move backwards so robot is not touching glyph
EncoderDriveTank(GLYPH_PLACE_DRIVE_SPEED, -5, -5, 10);
glyphArm.UpdateValues();
glyphArm.RotateGrabber();
opMode.sleep(500);
glyphArm.UpdateValues();
glyphArm.MoveGlyphWristToState(GlyphWristState.START);
}
}
/**
* Initialize the BNO055IMU gyro
*/
public void initGyro()
{
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
parameters.calibrationDataFile = "BNO055IMUCalibration.json";
parameters.loggingEnabled = true;
parameters.loggingTag = "IMU";
imu = opMode.hardwareMap.get(BNO055IMU.class, "imu");
imu.initialize(parameters);
}
/**
* Turn the robot a specified angle using the gyro
* @param direction "left" or "right"
* @param angle turn angle in degrees
* @param speed turn speed
*/
public void TurnByGyro(String direction, int angle, double speed)
{
//get angle values from the gyro
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
float startHeading = angles.firstAngle; //define the starting angle
//turn on motors to turn the robot in the specified direction
if(direction.equals("left"))
{
robot.rearRightMotor.setPower(speed);
robot.rearLeftMotor.setPower(-speed);
robot.frontRightMotor.setPower(speed);
robot.frontLeftMotor.setPower(-speed);
}
else if(direction.equals("right"))
{
robot.rearRightMotor.setPower(-speed);
robot.rearLeftMotor.setPower(speed);
robot.frontRightMotor.setPower(-speed);
robot.frontLeftMotor.setPower(speed);
}
//keep looping until the difference between the current heading and the start heading equals the specified angle
while(opMode.opModeIsActive() && Math.abs(angles.firstAngle - startHeading) < angle)
{
//get values from the gyro
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
//report the current heading to the driver station
opMode.telemetry.addData("heading", formatAngle(AngleUnit.DEGREES, angles.firstAngle));
opMode.telemetry.update();
}
TurnOffMotors();
}
/**
* Moves the robot in the tank drive style
* @param speed motor speed
* @param leftInches distance for left side of robot in inches
* @param rightInches distance for right side of robot in inches
* @param timeoutS timeout in seconds
*/
public void EncoderDriveTank(double speed, double leftInches, double rightInches, double timeoutS)
{
//reset encoders
robot.rearLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
opMode.idle();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
int newRearLeftTarget;
int newRearRightTarget;
int newFrontLeftTarget;
int newFrontRightTarget;
//ensure that the opmode is still active
if(opMode.opModeIsActive())
{
//determine target positions
newRearLeftTarget = robot.rearLeftMotor.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
newRearRightTarget = robot.rearRightMotor.getCurrentPosition() + (int) (rightInches * COUNTS_PER_INCH);
newFrontLeftTarget = robot.frontLeftMotor.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
newFrontRightTarget = robot.frontRightMotor.getCurrentPosition() + (int) (rightInches * COUNTS_PER_INCH);
robot.rearLeftMotor.setTargetPosition(newRearLeftTarget);
robot.rearRightMotor.setTargetPosition(newRearRightTarget);
robot.frontLeftMotor.setTargetPosition(newFrontLeftTarget);
robot.frontRightMotor.setTargetPosition(newFrontRightTarget);
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
runtime.reset();
//turn on motors to specified speed
robot.rearLeftMotor.setPower(Math.abs(speed));
robot.rearRightMotor.setPower(Math.abs(speed));
robot.frontLeftMotor.setPower(Math.abs(speed));
robot.frontRightMotor.setPower(Math.abs(speed));
//keep looping until one of the motors finished its movement
while(opMode.opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(robot.rearLeftMotor.isBusy() && robot.rearRightMotor.isBusy() && robot.frontLeftMotor.isBusy() && robot.frontRightMotor.isBusy()))
{
//report target and current positions to driver station
opMode.telemetry.addData("Path1", "Running to %7d :%7d", newRearLeftTarget, newRearRightTarget, newFrontLeftTarget, newFrontRightTarget);
opMode.telemetry.addData("Path2", "Running at %7d :%7d",
robot.rearLeftMotor.getCurrentPosition(),
robot.rearRightMotor.getCurrentPosition(),
robot.frontLeftMotor.getCurrentPosition(),
robot.frontRightMotor.getCurrentPosition());
opMode.telemetry.update();
}
TurnOffMotors();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}
/**
* Moves the robot side to side (strafing) a specified distance
* @param speed drive motor speed
* @param distance strafe distance in inches
* @param timeoutS timeout in seconds
*/
public void EncoderDriveSideways(double speed, double distance, double timeoutS)
{
//reset encoders
robot.rearLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
opMode.idle();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
int newRearLeftTarget;
int newRearRightTarget;
int newFrontLeftTarget;
int newFrontRightTarget;
//ensure that the opmode is still active
if(opMode.opModeIsActive())
{
//calculate target positions, negative for two motors so the robot strafes
newRearLeftTarget = robot.rearLeftMotor.getCurrentPosition() + (int) (-distance * Math.sqrt(2) * COUNTS_PER_INCH);
newRearRightTarget = robot.rearRightMotor.getCurrentPosition() + (int) (distance * Math.sqrt(2) * COUNTS_PER_INCH);
newFrontLeftTarget = robot.frontLeftMotor.getCurrentPosition() + (int) (distance * Math.sqrt(2) * COUNTS_PER_INCH);
newFrontRightTarget = robot.frontRightMotor.getCurrentPosition() + (int) (-distance * Math.sqrt(2) * COUNTS_PER_INCH);
robot.rearLeftMotor.setTargetPosition(newRearLeftTarget);
robot.rearRightMotor.setTargetPosition(newRearRightTarget);
robot.frontLeftMotor.setTargetPosition(newFrontLeftTarget);
robot.frontRightMotor.setTargetPosition(newFrontRightTarget);
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
runtime.reset();
robot.rearLeftMotor.setPower(Math.abs(speed));
robot.rearRightMotor.setPower(Math.abs(speed));
robot.frontLeftMotor.setPower(Math.abs(speed));
robot.frontRightMotor.setPower(Math.abs(speed));
//keep looping until one of the motors finished its movement
while (opMode.opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(robot.rearLeftMotor.isBusy() && robot.rearRightMotor.isBusy() && robot.frontLeftMotor.isBusy() && robot.frontRightMotor.isBusy()))
{
//report current and target positions to driver station
opMode.telemetry.addData("Path1", "Running to %7d :%7d", newRearLeftTarget, newRearRightTarget, newFrontLeftTarget, newFrontRightTarget);
opMode.telemetry.addData("Path2", "Running at %7d :%7d",
robot.rearLeftMotor.getCurrentPosition(),
robot.rearRightMotor.getCurrentPosition(),
robot.frontLeftMotor.getCurrentPosition(),
robot.frontRightMotor.getCurrentPosition());
opMode.telemetry.update();
}
TurnOffMotors();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}
/**
* Turns off all drive motors
*/
public void TurnOffMotors()
{
robot.rearRightMotor.setPower(0);
robot.rearLeftMotor.setPower(0);
robot.frontRightMotor.setPower(0);
robot.frontLeftMotor.setPower(0);
}
//gyro angle formatting methods
String formatAngle(AngleUnit angleUnit, double angle)
{
return formatDegrees(AngleUnit.DEGREES.fromUnit(angleUnit, angle));
}
String formatDegrees(double degrees)
{
return String.format(Locale.getDefault(), "%.1f", AngleUnit.DEGREES.normalize(degrees));
}
}