forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoFeetAutoBasketProgramV2.java
More file actions
385 lines (332 loc) · 15.7 KB
/
twoFeetAutoBasketProgramV2.java
File metadata and controls
385 lines (332 loc) · 15.7 KB
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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.DistanceSensor;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.TouchSensor;
import com.qualcomm.robotcore.util.ElapsedTime;
@Autonomous(name = "twoFeetAutoBasketProgramV2")
public class twoFeetAutoBasketProgramV2 extends LinearOpMode {
private DcMotorEx frontLeft, frontRight, backLeft, backRight;
private DcMotorEx leftLiftMotor, rightLiftMotor;
private DcMotorEx intakeSlidesMotor;
private Servo basketServo, intakePivotServo;
private CRServo intakeServoLeft, intakeServoRight;
private TouchSensor intakeTouchSensor;
boolean liftHoldingTrigger = false; // flag to determine if lift trigger is being held
boolean posUpdated = true; // flag to determine if lift position has been updated
int liftPosTier = 0; // 0 = bottom, 1 = middle, 2 = top
boolean intakeFull = false; // flag to determine if intake is full
boolean isRetracting = false; // flag to determine if horizontal slides are retracting
private static ElapsedTime forwardStopwatch = new ElapsedTime(); // timer for beginning forward setPower movement
private static ElapsedTime liftBreakTimer = new ElapsedTime(); // lets lift go down before intake movements
private static ElapsedTime intakeProcessTimer = new ElapsedTime();
private boolean noHighBasket = true;
private boolean noPickUp1 = true; // will use later
private boolean isliftUp = false; // will use later
private int i = 0;
private boolean basket1stPos = false;
private boolean basket2ndPos = false;
private boolean reachedForwardIntake = false;
private BNO055IMU imu;
private DistanceSensor distanceSensor;
private static final int targetRotationRight = 1680;
private static final int targetRotationLeft = 600;
private static final int forwardMovement = 325;
private static final int forwardIntakeMovement = 200; // helps align robot to middle block
private static final int secondForwardIntakeMovement = 75; // moves robot to middle block
private static final int backwardMovement = 1600;
private static final int leftwardMovement = 3100;
public void basket() {
if (basket1stPos == false) {
basketServo.setPosition(1.0);
}
else if ((basketServo.getPosition() > 0.45) && (basket1stPos == false)) {
basket1stPos = true;
}
sleep(3000);
basketServo.setPosition(0.0);
if (basketServo.getPosition() < 0.075) {
basket2ndPos = true;
basket1stPos = true;
}
sleep(3000);
}
public void rotateRight() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() - targetRotationRight);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + targetRotationRight);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + targetRotationRight);
backRight.setTargetPosition(backRight.getCurrentPosition() - targetRotationRight);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
}
public void rotateLeft() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + targetRotationLeft);
frontRight.setTargetPosition(frontRight.getCurrentPosition() - targetRotationLeft);
backLeft.setTargetPosition(backLeft.getCurrentPosition() - targetRotationLeft);
backRight.setTargetPosition(backRight.getCurrentPosition() + targetRotationLeft);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
}
public void strafeLeft() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + leftwardMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() - leftwardMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() - leftwardMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() + leftwardMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
}
public void forward() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + forwardMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + forwardMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + forwardMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() + forwardMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
if (frontLeft.getCurrentPosition() > 350) {
reachedForwardIntake = true;
resetMotors();
}
}
public void intakeForward() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + forwardIntakeMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + forwardIntakeMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + forwardIntakeMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() + forwardIntakeMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
// not using this right now
// if (frontLeft.getCurrentPosition() > 5) {
// telemetry.addData("Encoder pos for intake distance:", frontLeft.getCurrentPosition());
// telemetry.update();
// reachedForwardIntake = true;
// resetMotors();
// }
}
public void secondIntakeForward() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + secondForwardIntakeMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + secondForwardIntakeMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + secondForwardIntakeMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() + secondForwardIntakeMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
// not using this right now
// if (frontLeft.getCurrentPosition() > 5) {
// telemetry.addData("Encoder pos for intake distance:", frontLeft.getCurrentPosition());
// telemetry.update();
// reachedForwardIntake = true;
// resetMotors();
// }
}
public void backward() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() - backwardMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() - backwardMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() - backwardMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() - backwardMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
}
public void setLiftPosition(int targetPosition) {
leftLiftMotor.setTargetPosition(-targetPosition);
rightLiftMotor.setTargetPosition(targetPosition);
leftLiftMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
rightLiftMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
leftLiftMotor.setVelocity(1000);
rightLiftMotor.setVelocity(1000);
if (rightLiftMotor.getCurrentPosition() > 2890) {
isliftUp = true;
}
}
private void highBasketProcess() {
// add back when basket is fixed
// if (isliftUp == false) {
// setLiftPosition(3266); // low basket encoder positions
// }
// else if ((isliftUp == true) && (basket2ndPos == false)) {
// basket();
// }
basket2ndPos = true;
basket1stPos = true;
/* else */ if (basket2ndPos == true) { // add else back when basket is fixed
i += 1;
if (i == 1) {
setLiftPosition(0);
liftBreakTimer.reset();
while (liftBreakTimer.seconds() < 4.0995) {
}
intakeProcess();
stopMotors();
}
}
else {
noHighBasket = false;
}
}
public enum IntakeMode {
NORMAL,
REVERSE,
STOP
}
public void runIntakeMotors(twoFeetAutoBasketProgramV2.IntakeMode mode) {
switch (mode) {
case NORMAL:
intakeServoLeft.setPower(-1.0);
intakeServoRight.setPower(1.0);
break;
case REVERSE:
intakeServoLeft.setPower(1.0);
intakeServoRight.setPower(-1.0);
break;
case STOP:
intakeServoLeft.setPower(0.0);
intakeServoRight.setPower(0.0);
break;
}
}
public void intakeProcess() {
// will be used once we can pick up blocks
// if (intakeFull == false) {
// while (reachedForwardIntake == false) {
intakeProcessTimer.reset();
while (intakeProcessTimer.seconds() < .42) {
intakePivotServo.setPosition(1.0);
rotateLeft();
intakeForward(); // helps align robot to middle block
}
while (intakeProcessTimer.seconds() < .84) {
runIntakeMotors(twoFeetAutoBasketProgramV2.IntakeMode.NORMAL);
secondIntakeForward(); // moves robot to middle block
}
// } // will be used once we can pick up blocks
while (reachedForwardIntake == true) {
intakePivotServo.setPosition(0.0);
intakeFull = true;
runIntakeMotors(twoFeetAutoBasketProgramV2.IntakeMode.STOP);
}
// will be used once we can pick up blocks
// else if ((reachedForwardIntake == true) && (intakeFull == true) {
// runIntakeMotors(twoFeetAutoBasketProgramV2.IntakeMode.REVERSE);
//
// }
// else if (reachedForwardIntake == true) {
// intakePivotServo.setPosition(0.0);
// intakeFull = true;
// }
// }
}
@Override
public void runOpMode() throws InterruptedException {
// Initialize the hardware
frontLeft = hardwareMap.get(DcMotorEx.class, "frontLeft");
frontRight = hardwareMap.get(DcMotorEx.class, "frontRight");
backLeft = hardwareMap.get(DcMotorEx.class, "backLeft");
backRight = hardwareMap.get(DcMotorEx.class, "backRight");
// Servo for basket
basketServo = hardwareMap.get(Servo.class, "basketServo");
// Servo for intake
intakePivotServo = hardwareMap.get(Servo.class, "intakePivotServo");
// Initialize lift motors
leftLiftMotor = hardwareMap.get(DcMotorEx.class, "leftLiftMotor");
rightLiftMotor = hardwareMap.get(DcMotorEx.class, "rightLiftMotor");
// intake servos
intakePivotServo = hardwareMap.get(Servo.class, "intakePivotServo");
intakeServoLeft = hardwareMap.get(CRServo.class, "intakeServoLeft");
intakeServoRight = hardwareMap.get(CRServo.class, "intakeServoRight");
intakeTouchSensor = hardwareMap.get(TouchSensor.class, "intakeTouchSensor");
// Wait for the start signal
waitForStart();
frontLeft.setDirection(DcMotorEx.Direction.FORWARD);
frontRight.setDirection(DcMotorEx.Direction.REVERSE);
backLeft.setDirection(DcMotorEx.Direction.FORWARD);
backRight.setDirection(DcMotorEx.Direction.REVERSE);
frontLeft.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
frontRight.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
leftLiftMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
rightLiftMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
frontLeft.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
frontRight.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
backLeft.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
backRight.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
leftLiftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
rightLiftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
forwardStopwatch.reset();
// robot travels 20 inches in 1 sec
// goes 47.5 inches in 2 sec
// goes 66 inches in 3 sec
while (forwardStopwatch.seconds() < 0.35) {
frontLeft.setPower(0.9);
frontRight.setPower(0.9);
backLeft.setPower(0.9);
backRight.setPower(0.9);
telemetry.addData("Status", "Strafing Right");
telemetry.update();
}
strafeLeft();
rotateRight();
intakePivotServo.setPosition(-.2);
while (noHighBasket) {
highBasketProcess();
}
telemetry.addData("out of loop", rightLiftMotor.getCurrentPosition());
telemetry.update();
}
private void stopMotors() {
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
}
private void resetMotors() {
frontLeft.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
frontRight.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
}
}