-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFRobot.java
More file actions
393 lines (336 loc) · 13.2 KB
/
FFRobot.java
File metadata and controls
393 lines (336 loc) · 13.2 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
386
387
388
389
390
391
392
393
package org.firstinspires.ftc.teamcode;
import static java.lang.Math.abs;
import android.graphics.Path;
import com.qualcomm.hardware.motors.RevRoboticsCoreHexMotor;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Gamepad;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.Range;
import java.lang.reflect.Field;
//created by Sucheth Seethella
public class FFRobot {
//note that the motors are now all in a single row
private DcMotor blDrive = null;
private DcMotor brDrive = null;
private DcMotor flDrive = null;
private DcMotor frDrive = null;
private DcMotor intake = null;
public double leftPower;
public double rightPower;
private DcMotor linearSlide = null;//Tesrng btiyuc
private DcMotor carousel = null;
private Servo arm = null;
private int zero = 0; //linear slide encoders
private final int MAX = 4100;
private final int MIN = 0;
private int currentMax = MAX;
private int currentMin = MIN;
private int lastPos = 0;
private int stageTwo = FieldMeasurements.getStageHeight(1);
private int stageOne = FieldMeasurements.getStageHeight(2);
private int stageThree = FieldMeasurements.getStageHeight(3);
DcMotorSimple.Direction motF = DcMotorSimple.Direction.FORWARD;
DcMotorSimple.Direction motR = DcMotorSimple.Direction.REVERSE;
Servo.Direction serR = Servo.Direction.REVERSE;
Servo.Direction serF = Servo.Direction.FORWARD;
private double drive;
private double turn;
private double strafe;
private double frontLeftPower;
private double frontRightPower;
private double backLeftPower;
private double backRightPower;
private double lastDrivePos;
public void init(HardwareMap hwdMap){
this.blDrive = hwdMap.get(DcMotor.class, "blDrive");
this.brDrive = hwdMap.get(DcMotor.class, "brDrive");
this.flDrive = hwdMap.get(DcMotor.class, "flDrive");
this.frDrive = hwdMap.get(DcMotor.class, "frDrive");
this.intake = hwdMap.get(DcMotor.class, "intake");
this.linearSlide = hwdMap.get(DcMotor.class, "linearSlide");
this.carousel = hwdMap.get(DcMotor.class, "carousel");
this.arm = hwdMap.get(Servo.class, "arm");
//intake.resetDeviceConfigurationForOpMode();
this.blDrive.setDirection(motF);
this.brDrive.setDirection(motR);
this.flDrive.setDirection(motF);
this.frDrive.setDirection(motR);
this.linearSlide.setDirection(motR);
this.arm.setDirection(serR);
this.linearSlide.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
this.linearSlide.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
this.linearSlide.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.flDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.frDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.blDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.brDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
}
//auto drive methods
public void leftPow(double pow){
this.blDrive.setPower(-pow);
this.flDrive.setPower(-pow);
}
public void rightPow(double pow){
this.brDrive.setPower(-pow);
this.frDrive.setPower(-pow);
}
public void drive(double lPow, double rPow){
this.leftPow(lPow);
this.rightPow(rPow);
}
public void drive(double bothPow){ //override of drive(double, double)
drive(bothPow, bothPow);
}
public void strafe(double pow){ //Pos = right , Neg = left
blDrive.setPower(-pow);
flDrive.setPower(pow);
brDrive.setPower(pow);
frDrive.setPower(-pow);
}
public void strafe(String s, double pow) {
pow = Math.abs(pow);
if (s.equals("right"))
this.strafe(pow);
else if (s.equals("left"))
this.strafe(-pow);
}
public void driveTo(int pos) {
setDriveMode(DcMotor.RunMode.RUN_USING_ENCODER);
setDriveMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
setDriveMode(DcMotor.RunMode.RUN_TO_POSITION);
setTargetPos(pos);
if (pos > 0) {
drive(0.5);
while (flDrive.getCurrentPosition() < pos) {
double diff = ((flDrive.getCurrentPosition() - frDrive.getCurrentPosition()) * 0.0015);
frDrive.setPower(0.5 + diff);
diff = ((flDrive.getCurrentPosition() - blDrive.getCurrentPosition()) * 0.0015);
blDrive.setPower(0.5 + diff);
diff = ((flDrive.getCurrentPosition() - brDrive.getCurrentPosition()) * 0.0015);
brDrive.setPower(0.5 + diff);
}
} else {
drive(-0.5);
while (flDrive.getCurrentPosition() > pos) {
double diff = ((flDrive.getCurrentPosition() - frDrive.getCurrentPosition()) * 0.0015);
frDrive.setPower(-0.5 + diff);
diff = ((flDrive.getCurrentPosition() - blDrive.getCurrentPosition()) * 0.0015);
blDrive.setPower(-0.5 + diff);
diff = ((flDrive.getCurrentPosition() - brDrive.getCurrentPosition()) * 0.0015);
brDrive.setPower(-0.5 + diff);
}
}
brake();
setDriveMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
public void strafeTo(int pos) {
setDriveMode(DcMotor.RunMode.RUN_USING_ENCODER);
setDriveMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
setDriveMode(DcMotor.RunMode.RUN_TO_POSITION);
setTargetPos(pos);
if (pos > 0) {
strafe(0.5);
while(flDrive.getCurrentPosition() < pos) {
double diff = (Math.abs((flDrive.getCurrentPosition()) - Math.abs(frDrive.getCurrentPosition())) * 0.0015);
frDrive.setPower(-0.5 + diff);
diff = (Math.abs((flDrive.getCurrentPosition()) - Math.abs(brDrive.getCurrentPosition())) * 0.0015);
brDrive.setPower(0.5 + diff);
diff = (Math.abs((flDrive.getCurrentPosition()) - Math.abs(blDrive.getCurrentPosition())) * 0.0015);
blDrive.setPower(-0.5 + diff);
}
}
if (pos < 0) {
strafe(-0.5);
while(flDrive.getCurrentPosition() > pos) {
double diff = (Math.abs((flDrive.getCurrentPosition()) - Math.abs(frDrive.getCurrentPosition())) * 0.0015);
frDrive.setPower(0.5 + diff);
diff = (Math.abs((flDrive.getCurrentPosition()) - Math.abs(brDrive.getCurrentPosition())) * 0.0015);
brDrive.setPower(-0.5 + diff);
diff = (Math.abs((flDrive.getCurrentPosition()) - Math.abs(blDrive.getCurrentPosition())) * 0.0015);
blDrive.setPower(0.5 + diff);
}
}
brake();
setDriveMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
public int timeForTurn(int angle) {
return (angle*1700/180);
}
public void setTargetPos(int pos) {
flDrive.setTargetPosition(pos);
frDrive.setTargetPosition(pos);
blDrive.setTargetPosition(pos);
brDrive.setTargetPosition(pos);
}
public void setDriveMode(DcMotor.RunMode mode) {
this.blDrive.setMode(mode);
this.brDrive.setMode(mode);
this.flDrive.setMode(mode);
this.frDrive.setMode(mode);
}
public void brake() {
this.drive(0.0);
}
public void reverse() {
double flPow = flDrive.getPower();
double frPow = frDrive.getPower();
double blPow = blDrive.getPower();
double brPow = brDrive.getPower();
flDrive.setPower(-flPow);
frDrive.setPower(-frPow);
brDrive.setPower(-brPow);
blDrive.setPower(-blPow);
}
//teleop methods
public void mechanumPov(Gamepad gp, Gamepad gp2) {
drive = (-gp.left_stick_y);
strafe = (gp.left_stick_x);
turn = (gp.right_stick_x);
//drive measures forward and backwards - is unchanged for all motors
//strafe is subtracted from the front right and the back left
//turn needs to affect the right if the stick is moved right, left if moved left
//right stick right is positive
//so subtract turn from the right, and add to left
frontLeftPower = (drive + strafe + turn);
frontRightPower = (drive - strafe - turn);
backLeftPower = (drive - strafe + turn);
backRightPower = (drive + strafe - turn);
//to slow down the drive if needed
double slowDown = gp.left_bumper ? 8.0 : 1.0;
slowDown = gp.right_bumper ? 2.0 : slowDown;
this.flDrive.setPower((frontLeftPower)/(slowDown));
this.blDrive.setPower((backLeftPower)/(slowDown));
this.frDrive.setPower((frontRightPower)/(slowDown));
this.brDrive.setPower((backRightPower)/(slowDown));
}
public double getFrontLeftPower(){
return flDrive.getPower();
}
public double getBackLeftPower(){
return blDrive.getPower();
}
public double getBackRightPower(){
return brDrive.getPower();
}
public double getFrontRightPower(){
return frDrive.getPower();
}
public double getIntakePower(){return intake.getCurrentPosition();}
public double getLinearPower(){return linearSlide.getPower();}
public double getCarouselPower(){return carousel.getPower();}
//auto methods for control
public void setCarouselPower(double pow){
carousel.setPower(pow);
}
public void setLinearPower(double pow)
{
linearSlide.setPower(-pow); //the motor is orientated as up is negative - this makes it more intuitive with our min and max values
}
public void setLinearPower(String direction, double pow) {
pow = abs(pow);
if (direction.equals("up")) {
linearSlide.setPower(-pow);
}
if (direction.equals("down")) {
linearSlide.setPower(pow);
}
}
public void turnIntake(double pow){
intake.setPower(pow);
}
public void setArmPos(double pow) { arm.setPosition(pow); }
//Gamepad 1 Methods
public void carouselPower(Gamepad gp) { //Blue - Left Trigger || Red - Right Trigger
if(gp.right_trigger > 0)
carousel.setPower(-(gp.right_trigger)/2);
else if(gp.left_trigger > 0)
carousel.setPower((gp.left_trigger)/2);
else
carousel.setPower(zero);
}
public void dpadDrive(Gamepad gp) { //use if you simple want to move straight in cardinal directions.
if (gp.dpad_down) {
drive(-0.5);
} else if (gp.dpad_left) {
strafe("left", 0.5);
} else if (gp.dpad_right) {
strafe("right", 0.5);
} else if (gp.dpad_up) {
drive(0.5);
} else {
brake();
}
}
//GamePad 2 Methods
public void intake(Gamepad gp) { //negative is push out || positive is take in
if (gp.right_stick_y != 0)
intake.setPower(gp.right_stick_y);
else
intake.setPower(0);
}
public void armPower(Gamepad gp) { //right is raise || left is lower
if (gp.right_bumper)
arm.setPosition(0.45);
else if (gp.left_bumper)
arm.setPosition(0.55);
else
arm.setPosition(0.5);
}
public void linearPower(Gamepad gp){ //dynamically will set a "max" - stage one, two or three, or above all stages
if (gp.y)
currentMax = stageThree;
else if(gp.b)
currentMax = stageTwo;
else if (gp.a)
currentMax = stageOne;
else{
currentMax = MAX;
}
// if (getSlideEncoder() > currentMax || getSlideEncoder() < currentMin) {
//
// }
//else {
if (gp.left_stick_y == 0) {
// double offsetPow;
// if (getSlideEncoder() < lastPos) {
// offsetPow = lastPos - getSlideEncoder() * 0.0015;
// } else if (getSlideEncoder() > lastPos) {
// offsetPow = lastPos - getSlideEncoder() * 0.0005;
// } else {
// offsetPow = 0;
// }
linearSlide.setPower(0);
}
else {
if(getSlideEncoder() >= currentMax && gp.left_stick_y < 0)
linearSlide.setPower(0);
else if (isMin() && gp.left_stick_y > 0)
linearSlide.setPower(0);
else
linearSlide.setPower(gp.left_stick_y);
}
// }
lastPos = getSlideEncoder();
}
public boolean isMax(){
return getSlideEncoder() >= MAX;
}
public int getMax(){
return MAX;
}
public boolean isMin(){
return getSlideEncoder() <= MIN;
}
public int getMin(){
return MIN;
}
public int getSlideEncoder(){ //the "max" and "min" are too counterintuitive
return -1*linearSlide.getCurrentPosition();
}
public int getCurrentMax() {
return currentMax;
}
}