-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncoderAutonomousBlue.java
446 lines (338 loc) · 17 KB
/
EncoderAutonomousBlue.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
package org.firstinspires.ftc.teamcode;
/**
* Created by Alex Z on 2/14/2017.
*/
import android.graphics.Color;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.ColorSensor;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorController;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.ServoController;
import com.qualcomm.robotcore.util.Range;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
/** NOTE:
*
* This autonomous program is not intended to be used as an actual mode, but as a
* base for potential encoder autonomous programs.
*/
@Autonomous(name="Blue Autonomous", group="Encoder Auto")
public class EncoderAutonomousBlue extends LinearOpMode {
/** Declaring the motor variables **/
private DcMotorController motorControllerL; // Left Motor Controllers
private DcMotorController motorControllerR; // Right Motor Controllers
private DcMotorController motorControllerA1; // Auxiliary Motor Controller 1
private DcMotorController motorControllerA2; // Auxiliary Motor Controller 2
private ServoController servoController; // Servo Controller
private DcMotor motorFrontL; // Front Left Motor
private DcMotor motorFrontR; // Front Right Motor
private DcMotor motorBackL; // Back Left Motor
private DcMotor motorBackR; // Back Right Motor
private DcMotor sweeperMotor; // Sweeper Motor
private DcMotor motorLauncher; // Continuous Catapult Launcher Motor
private DcMotor motorStrafe; // Sideways Strafe Motor
private Servo servo; // Ball Queue Servo
/** For Encoders and specific turn values **/
double ticksPerRev = 1120; // This is the specific value for AndyMark motors
double ticksPer360Turn = 4500; // The amount of ticks for a 360 degree turn
double tickTurnRatio = ticksPer360Turn / 360;
double inchToMm = 25.4; // For conversion between the vectors
double wheelDiameter = 4.0; // Diameter of the current omniwheels in inches
double ticksPerInch = (ticksPerRev / (wheelDiameter * 3.14159265));
/** Color Sensor Stuffs **/
ColorSensor colorBeacon;
boolean LEDState = false; // Tracks the mode of the color sensor; Active = true, Passive = false
public void runOpMode() throws InterruptedException{
/** This is the method that executes the code and what the robot should do **/
float hsvValues[] = {0F, 0F, 0F};
// Initializes the electronics
initElectronics(0);
telemetry.addData("Phase 1", "Init");
telemetry.update();
waitForStart();
telemetry.addData("Started Robot", "Now");
telemetry.update();
runToPositionEncoders();
/* Your code beneath this */
colorBeacon.enableLed(LEDState);
Color.RGBToHSV(colorBeacon.red() * 8, colorBeacon.green() * 8, colorBeacon.blue() * 8, hsvValues);
telemetry.addData("Clear", colorBeacon.alpha());
telemetry.addData("Red", colorBeacon.red());
telemetry.addData("Green", colorBeacon.green());
telemetry.addData("Blue", colorBeacon.blue());
telemetry.addData("Hue", hsvValues[0]);
telemetry.update();
// Robot is on blue team
if(colorBeacon.red() > colorBeacon.blue() && colorBeacon.red() > colorBeacon.green()) {
rotateDegreesLeft(0.2, 20);
}
else if(colorBeacon.blue() > colorBeacon.red() && colorBeacon.blue() > colorBeacon.green()) {
rotateDegreesRight(0.2, 20);
}
else {
encoderMove(0.3, -2, -2);
}
waitOneFullHardwareCycle();
}
/** These methods control the robot's movement **/
public void encoderMove(double power,
double leftInches, double rightInches) {
/** This method makes the motors move a certain distance **/
// Creating variables
int leftTarget;
int rightTarget;
// This allows for the use of negative power
if (power > 0 && power <= 1) {
// Assigning variables
leftTarget = (int)(leftInches * -ticksPerInch); // Value must be negative to go forward
rightTarget = (int)(rightInches * -ticksPerInch);
} else if (power < 0 && power >= -1) {
// Assigning variables
leftTarget = -1 * (int)(leftInches * -ticksPerInch); // Value must be negative to go forward
rightTarget = -1 * (int)(rightInches * -ticksPerInch);
} else if (power == 0) {
leftTarget = 0;
rightTarget = 0;
} else {
// Sets the power range
power = Range.clip(power, -1, 1);
power = Math.abs(power);
// Assigning variables
leftTarget = (int)(leftInches * -ticksPerInch); // Value must be negative to go forward
rightTarget = (int)(rightInches * -ticksPerInch);
}
// Setting the target positions
motorFrontL.setTargetPosition(motorFrontL.getCurrentPosition() + leftTarget);
motorFrontR.setTargetPosition(motorFrontR.getCurrentPosition() + rightTarget);
motorBackL.setTargetPosition(motorBackL.getCurrentPosition() + leftTarget);
motorBackR.setTargetPosition(motorBackR.getCurrentPosition() + rightTarget);
runToPositionEncoders();
// Sets the motors' position
motorFrontL.setPower(power);
motorFrontR.setPower(power);
motorBackL.setPower(power);
motorBackR.setPower(power);
// While loop for updating telemetry
while(motorFrontL.isBusy() && motorFrontR.isBusy() &&
motorBackL.isBusy() && motorBackR.isBusy() && opModeIsActive()){
// Updates the position of the motors
double frontLPos = motorFrontL.getCurrentPosition();
double frontRPos = motorFrontR.getCurrentPosition();
double backLPos = motorBackL.getCurrentPosition();
double backRPos = motorBackR.getCurrentPosition();
// Adds telemetry of the drive motors
telemetry.addData("MotorFrontL Pos", frontLPos);
telemetry.addData("MotorFrontR Pos", frontRPos);
telemetry.addData("MotorBackL Pos", backLPos);
telemetry.addData("MotorBackR Pos", backRPos);
// Updates the telemetry
telemetry.update();
}
// Stops the motors
stopMotion();
// Resets to run using encoders mode
runUsingEncoders();
}
public void rotateDegreesLeft(double power, int robotDegrees) {
/** Robot requires values of...
* 360 degrees =~ 4600 ticks
* 180 degrees =~ 2300 ticks **/
/** This method, given an input amount of degrees, makes the robot turn
* the amount of degrees specified around ITS center of rotation **/
// Sets the power range
power = Range.clip(power, -1, 1);
power = Math.abs(power);
// Setting variables
double robotTurn = robotDegrees * tickTurnRatio;
// Setting the target positions
motorFrontL.setTargetPosition((int)(motorFrontL.getCurrentPosition() + robotTurn));
motorFrontR.setTargetPosition((int)(motorFrontR.getCurrentPosition() + -robotTurn));
motorBackL.setTargetPosition((int)(motorBackL.getCurrentPosition() + robotTurn));
motorBackR.setTargetPosition((int)(motorBackR.getCurrentPosition() + -robotTurn));
runToPositionEncoders();
// Sets the motors' positions
motorFrontL.setPower(power);
motorFrontR.setPower(power);
motorBackL.setPower(power);
motorBackR.setPower(power);
// While loop for updating telemetry
while(motorFrontL.isBusy() && motorFrontR.isBusy() &&
motorBackL.isBusy() && motorBackR.isBusy() && opModeIsActive()){
// Updates the position of the motors
double frontLPos = motorFrontL.getCurrentPosition();
double frontRPos = motorFrontR.getCurrentPosition();
double backLPos = motorBackL.getCurrentPosition();
double backRPos = motorBackR.getCurrentPosition();
// Adds telemetry of the drive motors
telemetry.addData("MotorFrontL Pos", frontLPos);
telemetry.addData("MotorFrontR Pos", frontRPos);
telemetry.addData("MotorBackL Pos", backLPos);
telemetry.addData("MotorBackR Pos", backRPos);
// Updates the telemetry
telemetry.update();
}
// Stops the motors
stopMotion();
// Resets to run using encoders mode
runUsingEncoders();
}
public void rotateDegreesRight(double power, int robotDegrees) {
/** Robot requires values of...
* 360 degrees =~ 4600 ticks
* 180 degrees =~ 2300 ticks **/
/** This method, given an input amount of degrees, makes the robot turn
* the amount of degrees specified around ITS center of rotation **/
// Sets the power range
power = Range.clip(power, -1, 1);
power = Math.abs(power);
// Setting variables
double robotTurn = robotDegrees * tickTurnRatio;
// Setting the target positions
motorFrontL.setTargetPosition((int)(motorFrontL.getCurrentPosition() + -robotTurn));
motorFrontR.setTargetPosition((int)(motorFrontR.getCurrentPosition() + robotTurn));
motorBackL.setTargetPosition((int)(motorBackL.getCurrentPosition() + -robotTurn));
motorBackR.setTargetPosition((int)(motorBackR.getCurrentPosition() + robotTurn));
runToPositionEncoders();
// Sets the motors' positions
motorFrontL.setPower(power);
motorFrontR.setPower(power);
motorBackL.setPower(power);
motorBackR.setPower(power);
// While loop for updating telemetry
while(motorFrontL.isBusy() && motorFrontR.isBusy() &&
motorBackL.isBusy() && motorBackR.isBusy() && opModeIsActive()){
// Updates the position of the motors
double frontLPos = motorFrontL.getCurrentPosition();
double frontRPos = motorFrontR.getCurrentPosition();
double backLPos = motorBackL.getCurrentPosition();
double backRPos = motorBackR.getCurrentPosition();
// Adds telemetry of the drive motors
telemetry.addData("MotorFrontL Pos", frontLPos);
telemetry.addData("MotorFrontR Pos", frontRPos);
telemetry.addData("MotorBackL Pos", backLPos);
telemetry.addData("MotorBackR Pos", backRPos);
// Updates the telemetry
telemetry.update();
}
// Stops the motors
stopMotion();
// Resets to run using encoders mode
runUsingEncoders();
}
public void stopMotion() {
/** Stops all drive motor motion **/
motorFrontL.setPower(0);
motorFrontR.setPower(0);
motorBackL.setPower(0);
motorBackR.setPower(0);
}
/** ----------------------------------------- **/
/** These methods control the encoder modes of the motor **/
public void encoderMode(int mode) {
/**NOTE:
* This was made just for the sake of making the code look a bit neater
*
* Mode Numbers:
* 0 = RUN_TO_POSITION
* 1 = RUN_USING_ENCODER
* 2 = RUN_WITHOUT_ENCODER
* 3 = STOP_AND_RESET_ENCODERS
* 4 = RESET_ENCODERS
* **/
if (mode == 0) {
/** Sets the encoded motors to RUN_TO_POSITION **/
motorFrontL.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motorFrontR.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motorBackL.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motorBackR.setMode(DcMotor.RunMode.RUN_TO_POSITION);
} else if (mode == 1) {
/** Sets the encoders to RUN_USING_ENCODERS **/
motorFrontL.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
motorFrontR.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
motorBackL.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
motorBackR.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
} else if (mode == 2) {
/** Sets the encoders to RUN_WITHOUT_ENCODERS **/
motorFrontL.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
motorFrontR.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
motorBackL.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
motorBackR.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
} else if (mode == 3) {
/** Stops and resets the encoder values on each of the drive motors **/
motorFrontL.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motorFrontR.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motorBackL.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motorBackR.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
} else if (mode == 4) {
/** Resets the encoder values on each of the drive motors **/
motorFrontL.setMode(DcMotor.RunMode.RESET_ENCODERS);
motorFrontR.setMode(DcMotor.RunMode.RESET_ENCODERS);
motorBackL.setMode(DcMotor.RunMode.RESET_ENCODERS);
motorBackR.setMode(DcMotor.RunMode.RESET_ENCODERS);
}
}
public void resetEncoders() {
/** Resets the encoder values on each of the drive motors **/
motorFrontL.setMode(DcMotor.RunMode.RESET_ENCODERS);
motorFrontR.setMode(DcMotor.RunMode.RESET_ENCODERS);
motorBackL.setMode(DcMotor.RunMode.RESET_ENCODERS);
motorBackR.setMode(DcMotor.RunMode.RESET_ENCODERS);
}
public void runToPositionEncoders() {
/** Sets the encoded motors to RUN_TO_POSITION **/
motorFrontL.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motorFrontR.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motorBackL.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motorBackR.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
public void runUsingEncoders() {
/** Sets the encoders to RUN_USING_ENCODERS **/
motorFrontL.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
motorFrontR.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
motorBackL.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
motorBackR.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
public void addTelemetryData(String string1, String string2) {
telemetry.addData(string1, string2);
telemetry.update();
}
/** ----------------------------------------- **/
/** These methods are used to set up the robot **/
public void initElectronics(int mode) throws InterruptedException {
// To make the initialization of electronics much easier and nicer to read
/** Initializing and mapping electronics **/
if (mode == 0) {
/* Motors and servos (w/ controllers) */
motorControllerL = hardwareMap.dcMotorController.get("MC_L");
motorControllerR = hardwareMap.dcMotorController.get("MC_R");
motorControllerA1 = hardwareMap.dcMotorController.get("MC_A1");
motorControllerA2 = hardwareMap.dcMotorController.get("MC_A2");
servoController = hardwareMap.servoController.get("SC");
motorFrontL = hardwareMap.dcMotor.get("motorFrontL"); //P0 is actually the right
motorFrontR = hardwareMap.dcMotor.get("motorFrontR"); //P1 is actually the left
motorBackL = hardwareMap.dcMotor.get("motorBackL"); //P0
motorBackR = hardwareMap.dcMotor.get("motorBackR"); //P1
servo = hardwareMap.servo.get("servo");
motorLauncher = hardwareMap.dcMotor.get("motorLauncher"); //P0
sweeperMotor = hardwareMap.dcMotor.get("motorSweeper"); //P1
motorStrafe = hardwareMap.dcMotor.get("motorStrafe"); //P0 A2
/* Sensors */
colorBeacon = hardwareMap.colorSensor.get("colorBeacon");
/*Setting channel modes*/
runUsingEncoders();
motorLauncher.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sweeperMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
motorStrafe.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
motorFrontL.setDirection(DcMotorSimple.Direction.REVERSE);
motorBackL.setDirection(DcMotorSimple.Direction.REVERSE);
motorLauncher.setDirection(DcMotorSimple.Direction.REVERSE);
}
else if (mode == 1) {
}
}
/** ----------------------------------------- **/
}