-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoBase.java
506 lines (448 loc) · 17 KB
/
AutoBase.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.hardware.bosch.JustLoggingAccelerationIntegrator;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
import org.firstinspires.ftc.robotcore.external.Telemetry;
import org.firstinspires.ftc.robotcore.external.navigation.*;
import java.util.Locale;
/**
* AutoPathController is an abstraction of the drivetrain. Use this for Autos
* where a predefined path needs to be executed.
*/
public class AutoBase {
static DcMotor left_front, right_front, left_back, right_back;
Telemetry telemetry;
// https://first-tech-challenge.github.io/SkyStone/com/qualcomm/hardware/bosch/BNO055IMUImpl.html
static BNO055IMU imu;
Orientation angles;
Acceleration gravity;
// Orientation relative to signing the top of the field.
// https://github.com/acmerobotics/road-runner/blob/master/gui/src/main/resources/field.png
double currentAngle;
static final ElapsedTime runtime = new ElapsedTime();
// Drive speed constants
static final double DRIVE_SPEED = 1.0;
static final double TURN_SPEED = 1.0;
static final double HEADING_THRESHOLD = 1 ; // As tight as we can make it with an integer gyro
static final double P_TURN_COEFF = 0.1; // Larger is more responsive, but also less stable
static final double P_DRIVE_COEFF = 0.15; // Larger is more responsive, but also less stable
static final double LEFT_DAMPING_CONSTANT = 0.885;
static final double RIGHT_DAMPING_CONSTANT = 0.885;
static final double DISTANCE_TO_ANGLE_CONSTANT = 1.2;
// Motor encoder configuration constant
static final double TETRIX_TICKS_PER_MOTOR_REV = 1440;
static final double ANDYMARK_TICKS_PER_MOTOR_REV = 1120;
static final double PULSES_PER_REVOLUTION = ANDYMARK_TICKS_PER_MOTOR_REV;
static final double WHEEL_DIAMETER_IN = 4;
static final double PULSES_PER_IN = PULSES_PER_REVOLUTION / (WHEEL_DIAMETER_IN * 3.1415);
static double FR_TO_BL_DIMENSION_IN;
static double FL_TO_BR_DIMENSION_IN;
public void InitAuto(
HardwareMap hardwareMap,
String left_front_name,
String right_front_name,
String left_back_name,
String right_back_name,
Telemetry telemetry,
double fr_to_bl_dimension_in,
double fl_to_br_dimension_in
) {
FR_TO_BL_DIMENSION_IN = fr_to_bl_dimension_in;
FL_TO_BR_DIMENSION_IN = fr_to_bl_dimension_in;
initIMU(hardwareMap);
left_front = hardwareMap.get(DcMotor.class, left_front_name);
right_front = hardwareMap.get(DcMotor.class, right_front_name);
left_back = hardwareMap.get(DcMotor.class, left_back_name);
right_back = hardwareMap.get(DcMotor.class, right_back_name);
right_front.setDirection(DcMotor.Direction.REVERSE);
right_back.setDirection(DcMotor.Direction.REVERSE);
this.telemetry = telemetry;
ZeroPowerBehaviorBRAKE();
}
// public void update() {
// angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
// gravity = imu.getGravity();
// telemetry.addData("Angles: ", angles.toString());
// telemetry.addData("Gravity: ", gravity.toString());
// }
private static void initIMU(HardwareMap hardwareMap) {
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
parameters.calibrationDataFile = "BNO055IMUCalibration.json"; // see the calibration sample opmode
parameters.loggingEnabled = true;
parameters.loggingTag = "IMU";
parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
imu = hardwareMap.get(BNO055IMU.class, "imu");
imu.initialize(parameters);
imu.startAccelerationIntegration(new Position(), new Velocity(), 1000);
}
private static void ZeroPowerBehaviorBRAKE() {
left_front.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
right_front.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
left_back.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
right_back.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
}
private static void setRunUsingEncoders() {
left_front.setMode(DcMotor.RunMode.RUN_USING_ENCODERS);
right_front.setMode(DcMotor.RunMode.RUN_USING_ENCODERS);
left_back.setMode(DcMotor.RunMode.RUN_USING_ENCODERS);
right_back.setMode(DcMotor.RunMode.RUN_USING_ENCODERS);
}
private static void setRunWithoutEncoders() {
left_front.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODERS);
right_front.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODERS);
left_back.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODERS);
right_back.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODERS);
}
private static void setRunToPosition() {
left_front.setMode(DcMotor.RunMode.RUN_TO_POSITION);
right_front.setMode(DcMotor.RunMode.RUN_TO_POSITION);
left_back.setMode(DcMotor.RunMode.RUN_TO_POSITION);
right_back.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
private static void resetEncoders() {
// method may be stopandreset, but I'm not sure
left_front.setMode(DcMotor.RunMode.RESET_ENCODERS);
right_front.setMode(DcMotor.RunMode.RESET_ENCODERS);
left_back.setMode(DcMotor.RunMode.RESET_ENCODERS);
right_back.setMode(DcMotor.RunMode.RESET_ENCODERS);
}
private static void setTargetPositionsForward(int distance) {
left_front.setTargetPosition(distance);
right_front.setTargetPosition(distance);
left_back.setTargetPosition(distance);
right_back.setTargetPosition(distance);
}
private static void setTargetPositionsBackward(int distance) {
left_front.setTargetPosition(-distance);
right_front.setTargetPosition(-distance);
left_back.setTargetPosition(-distance);
right_back.setTargetPosition(-distance);
}
private static void setTargetPositionsLeft(int distance) {
left_front.setTargetPosition(-distance);
right_front.setTargetPosition(distance);
left_back.setTargetPosition(distance);
right_back.setTargetPosition(-distance);
}
private static void setTargetPositionsRight(int distance) {
left_front.setTargetPosition(distance);
right_front.setTargetPosition(-distance);
left_back.setTargetPosition(-distance);
right_back.setTargetPosition(distance);
}
private static void setTargetPositionsNW(int distance) {
left_front.setTargetPosition(0);
right_front.setTargetPosition(distance);
left_back.setTargetPosition(distance);
right_back.setTargetPosition(0);
}
private static void setTargetPositionsNE(int distance) {
left_front.setTargetPosition(distance);
right_front.setTargetPosition(0);
left_back.setTargetPosition(0);
right_back.setTargetPosition(distance);
}
private static void setTargetPositionsSW(int distance) {
left_front.setTargetPosition(-distance);
right_front.setTargetPosition(0);
left_back.setTargetPosition(0);
right_back.setTargetPosition(-distance);
}
private static void setTargetPositionsSE(int distance) {
left_front.setTargetPosition(0);
right_front.setTargetPosition(-distance);
left_back.setTargetPosition(-distance);
right_back.setTargetPosition(0);
}
private static void setTargetPositionsTurnLeft(int distance) {
left_front.setTargetPosition(-distance);
right_front.setTargetPosition(distance);
left_back.setTargetPosition(-distance);
right_back.setTargetPosition(distance);
}
private static void setTargetPositionsTurnRight(int distance) {
left_front.setTargetPosition(distance);
right_front.setTargetPosition(-distance);
left_back.setTargetPosition(distance);
right_back.setTargetPosition(-distance);
}
private static void stopDriving() {
left_front.setPower(0);
right_front.setPower(0);
left_back.setPower(0);
right_back.setPower(0);
}
private static void goForward(double MotorPower) {
left_front.setPower(MotorPower);
right_front.setPower(MotorPower);
left_back.setPower(MotorPower);
right_back.setPower(MotorPower);
}
private static void goBackward(double MotorPower) {
left_front.setPower(-MotorPower);
right_front.setPower(-MotorPower);
left_back.setPower(-MotorPower);
right_back.setPower(-MotorPower);
}
private static void goLeft(double MotorPower) {
left_front.setPower(-MotorPower);
right_front.setPower(MotorPower);
left_back.setPower(MotorPower);
right_back.setPower(-MotorPower);
}
private static void goRight(double MotorPower) {
left_front.setPower(MotorPower);
right_front.setPower(-MotorPower);
left_back.setPower(-MotorPower);
right_back.setPower(MotorPower);
}
private static void goNW(double MotorPower) {
left_front.setPower(0);
right_front.setPower(MotorPower);
left_back.setPower(MotorPower);
right_back.setPower(0);
}
private static void goNE(double MotorPower) {
left_front.setPower(MotorPower);
right_front.setPower(0);
left_back.setPower(0);
right_back.setPower(MotorPower);
}
private static void goSW(double MotorPower) {
left_front.setPower(-MotorPower);
right_front.setPower(0);
left_back.setPower(0);
right_back.setPower(-MotorPower);
}
private static void goSE(double MotorPower) {
left_front.setPower(0);
right_front.setPower(-MotorPower);
left_back.setPower(-MotorPower);
right_back.setPower(0);
}
private static void goTurnLeft(double MotorPower) {
left_front.setPower(-MotorPower);
right_front.setPower(MotorPower);
left_back.setPower(-MotorPower);
right_back.setPower(MotorPower);
}
private static void goTurnRight(double MotorPower) {
left_front.setPower(MotorPower);
right_front.setPower(-MotorPower);
left_back.setPower(MotorPower);
right_back.setPower(-MotorPower);
}
public void driveForward(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsForward(distanceIN);
setRunToPosition();
goForward(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void driveBackward(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsBackward(distanceIN);
setRunToPosition();
goBackward(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void strafeLeft(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsLeft(distanceIN);
setRunToPosition();
goLeft(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void strafeRight(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsRight(distanceIN);
setRunToPosition();
goRight(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void strafeNW(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsNW(distanceIN);
setRunToPosition();
goNW(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void strafeNE(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsNE(distanceIN);
setRunToPosition();
goNE(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void strafeSW(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsSW(distanceIN);
setRunToPosition();
goSW(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void strafeSE(int distanceIN, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
resetEncoders();
setTargetPositionsSE(distanceIN);
setRunToPosition();
goSE(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void turnLeft(int degrees, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
int hypotenuse = (int) Math.sqrt(Math.pow(FL_TO_BR_DIMENSION_IN/2, 2) + Math.pow(FR_TO_BL_DIMENSION_IN/2, 2));
resetEncoders();
setTargetPositionsTurnLeft((hypotenuse/90)*degrees);
setRunToPosition();
goTurnLeft(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
public void turnRight(int degrees, double MotorPower) {
if (MotorPower == (double) 2) {
MotorPower = DRIVE_SPEED;
}
int hypotenuse = (int) Math.sqrt(Math.pow(FL_TO_BR_DIMENSION_IN/2, 2) + Math.pow(FR_TO_BL_DIMENSION_IN/2, 2));
resetEncoders();
setTargetPositionsTurnRight((hypotenuse/90)*degrees);
setRunToPosition();
goTurnRight(MotorPower);
while (
left_front.isBusy() &&
right_front.isBusy() &&
left_back.isBusy() &&
right_back.isBusy()
) {
// waiting for target position to be reached
}
stopDriving();
setRunUsingEncoders();
}
}
/*
// During init:
AutoBase auto = new AutoBase();
auto.InitAuto()
// During run loop:
auto.driveForward(distanceIN, (double) 2);
auto.driveBackward(distanceIN, (double) 2);
auto.strafeLeft(distanceIN, (double) 2);
auto.strafeRight(distanceIN, (double) 2);
auto.strafeNW(distanceIN, (double) 2);
auto.strafeNE(distanceIN, (double) 2);
auto.strafeSW(distanceIN, (double) 2);
auto.strafeSE(distanceIN, (double) 2);
auto.turnLeft(degrees, (double) 2);
auto.turnRight(degrees, (double) 2);
*/