forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntoTheDeepTeleop.java
More file actions
303 lines (249 loc) · 10.8 KB
/
IntoTheDeepTeleop.java
File metadata and controls
303 lines (249 loc) · 10.8 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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.TouchSensor;
@TeleOp(name = "IntoTheDeepTeleop", group = "Drive")
public class IntoTheDeepTeleop extends OpMode {
// Declare thingies
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
@Override
public void init() {
// Initialize motors
frontLeft = hardwareMap.get(DcMotorEx.class, "frontLeft");
frontRight = hardwareMap.get(DcMotorEx.class, "frontRight");
backLeft = hardwareMap.get(DcMotorEx.class, "backLeft");
backRight = hardwareMap.get(DcMotorEx.class, "backRight");
intakeSlidesMotor = hardwareMap.get(DcMotorEx.class, "intakeSlidesMotor");
// Set motors' directions if needed
frontLeft.setDirection(DcMotorEx.Direction.FORWARD);
frontRight.setDirection(DcMotorEx.Direction.REVERSE);
backLeft.setDirection(DcMotorEx.Direction.FORWARD);
backRight.setDirection(DcMotorEx.Direction.REVERSE);
// Set motors to use encoders
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);
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);
// Servo for basket
basketServo = hardwareMap.get(Servo.class, "basketServo");
// 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");
// Set lift motors to use encoders
leftLiftMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
rightLiftMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
leftLiftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
rightLiftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
intakeSlidesMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
intakeSlidesMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
// Set all motors to zero power
setMotorPower(0, 0, 0, 0);
// initialize basket servo to 0
basketServo.setPosition(0.0);
// initialize intake pivot servo to 0
intakePivotServo.setPosition(0.0);
}
@Override
public void loop() {
// Get joystick values
double boost = gamepad1.right_trigger / 2; // Boost
double y = 0.0;
double x = 0.0;
if (gamepad1.dpad_up){
y = 0.5;
}else if (gamepad1.dpad_down){
y = -0.5;
}else if (gamepad1.dpad_left){
x = -0.5;
}else if (gamepad1.dpad_right){
x = 0.5;
}else {
y = -gamepad1.left_stick_y / 2; // Forward/backward
x = gamepad1.left_stick_x / 2; // Strafe
}
double rotation = gamepad1.right_stick_x; // Rotate
y += y * boost;
x += x * boost;
// Mecanum drive calculations
double frontLeftPower = (y + x + rotation);
double frontRightPower = (y - x - rotation);
double backLeftPower = (y - x + rotation);
double backRightPower = (y + x - rotation);
// Normalize the power values to be within -1 and 1
frontLeftPower /= Math.max(1.0, Math.abs(frontLeftPower));
frontRightPower /= Math.max(1.0, Math.abs(frontRightPower));
backLeftPower /= Math.max(1.0, Math.abs(backLeftPower));
backRightPower /= Math.max(1.0, Math.abs(backRightPower));
// Set motor power
setMotorPower(frontLeftPower, frontRightPower, backLeftPower, backRightPower);
// Check for lift and basket inputs
lift();
basket();
intake();
horizontalSlides();
// Display encoder tick values for each lift motor
telemetry.addData("Left Lift Motor Position:", leftLiftMotor.getCurrentPosition());
telemetry.addData("Right Lift Motor Position:", rightLiftMotor.getCurrentPosition());
telemetry.addData("Horizontal Slides Position:", intakeSlidesMotor.getCurrentPosition());
telemetry.update();
}
public void basket() {
if (gamepad2.b && (liftPosTier != 0)) {
basketServo.setPosition(1.0);
} else {
basketServo.setPosition(0.0);
}
}
public void setLiftPosition(int targetPosition) {
// Strafing configuration for mecanum wheels
leftLiftMotor.setTargetPosition(-targetPosition);
rightLiftMotor.setTargetPosition(targetPosition);
leftLiftMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
rightLiftMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
leftLiftMotor.setVelocity(2000);
rightLiftMotor.setVelocity(2000);
}
private void lift() {
if (gamepad2.right_bumper && !liftHoldingTrigger && !(liftPosTier >= 2)) {
liftHoldingTrigger = true;
posUpdated = false;
liftPosTier += 1;
} else if (gamepad2.left_bumper && !liftHoldingTrigger && !(liftPosTier <= 0)) {
liftHoldingTrigger = true;
posUpdated = false;
liftPosTier -= 1;
} else if (!gamepad2.right_bumper && !gamepad2.left_bumper) {
liftHoldingTrigger = false;
}
// handle setting lift position based on requested tier
if (!posUpdated) {
if (liftPosTier == 0) {
setLiftPosition(0);
} else if (liftPosTier == 1) {
setLiftPosition(1659); // high basket encoder positions
} else if (liftPosTier == 2) {
setLiftPosition(3266); // low basket encoder positions
}
posUpdated = true;
}
}
private void setMotorPower(double fl, double fr, double bl, double br) {
frontLeft.setPower(fl);
frontRight.setPower(fr);
backLeft.setPower(bl);
backRight.setPower(br);
}
// sets horizontal slides target position to 0
public void retractHorizontalSlides() {
intakeSlidesMotor.setTargetPosition(0);
intakeSlidesMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
intakeSlidesMotor.setVelocity(1000);
isRetracting = true;
}
public enum IntakeMode {
NORMAL,
REVERSE,
STOP
}
public void runIntakeMotors(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 intake() {
// intake is full or override button (a) has been pressed, stop running servos and retract to deposit in basket
if ((intakeTouchSensor.isPressed() || gamepad2.a) && !intakeFull) {
intakeFull = true;
runIntakeMotors(IntakeMode.STOP);
intakePivotServo.setPosition(0.0);
retractHorizontalSlides();
}
// button is being held pivot intake and run intake motors
if (gamepad2.x) {
intakePivotServo.setPosition(1.0);
runIntakeMotors(IntakeMode.NORMAL);
}
if (gamepad2.y) {
intakePivotServo.setPosition(0.0);
runIntakeMotors(IntakeMode.STOP);
retractHorizontalSlides();
}
// intake slides have fully retracted, with threshold of 20 encoder ticks
if (intakeSlidesMotor.getCurrentPosition() > -20) {
// set isRetracting flag back to false
isRetracting = false;
// if intake is full, run intake motors to deposit block in basket
if (intakeFull) {
intakeFull = false;
// start intaking block after 0.3 seconds
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
runIntakeMotors(IntakeMode.REVERSE);
}
},
300);
// stop unloading block after 2 seconds
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
runIntakeMotors(IntakeMode.STOP);
}
},
2000);
}
}
}
public void horizontalSlides() {
if (!isRetracting) {
if (gamepad2.dpad_up && (intakeSlidesMotor.getCurrentPosition() >= -930)) { // max extension -930 ticks
intakeSlidesMotor.setMode(DcMotorEx.RunMode.RUN_WITHOUT_ENCODER);
intakeSlidesMotor.setPower(-0.8);
} else if (gamepad2.dpad_down && (intakeSlidesMotor.getCurrentPosition() <= 0)) {
intakeSlidesMotor.setMode(DcMotorEx.RunMode.RUN_WITHOUT_ENCODER);
intakeSlidesMotor.setPower(0.8);
} else {
intakeSlidesMotor.setPower(0.0);
}
}
}
@Override
public void stop() {
setMotorPower(0, 0, 0, 0);
}
}