-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2_person_controls_draft.java
More file actions
422 lines (361 loc) · 15.5 KB
/
2_person_controls_draft.java
File metadata and controls
422 lines (361 loc) · 15.5 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
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
package org.firstinspires.ftc.teamcode;
//imports
//eventloop components
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
//hardware components
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.DcMotor;
//utilities
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
import java.util.*;
import java.lang.Math;
@TeleOp(name = "Zeyad & T Waterloo");
public class ZT_Mecanum extends LinearOpMode {
//control variables
//misc.
private ElapsedTime runtime = new ElapsedTime();
boolean initial = true;
final int TPR_R = 560; //TPR = ticks per revolution, R = REV
final int TPR_H = 288; //TPR = ticks per revolution, H = Hex motor
final double Arm_rot_deg = 145;
//drive
double x = 0; //left stick x value
double y = 0; //left stick y value
double r = 0; //right stick x value
double denom = 0; //denominator for scaling power
double FLP = 0; //front left power
double FRP = 0; //front right power
double BLP = 0; //back left power
double BRP = 0; //back right power
boolean forward_orientation = true;
//claws
boolean specimen_claw_open_status = false;
boolean sample_claw_open_status = false;
double claw_closed_position = 0.0;
double claw_open_position = 0.5;
double rotation_position = 0.5; //current submersible claw rotation
double pecking_position = 0.0; //current pecking arm position
double rotation_initial = 0.5; //rotation neutral position
double peck_initial = 0; //peck initial position
double peck_2 = 0.7; //peck standby position
//extensions
final int rack_teeth = 115;
final int pinion_teeth = 32;
final int extension_max = (int)(Math.floor(TPR_S*(rack_teeth/pinion_teeth))); //maximum extension in ticks -> int(floor([ticks/rev]*rack teeth / pinion teeth))
final int extension_min = 0; // extension minomum in ticks
final int arm_max = (int)(Math.floor(TPR_H*(Arm_rot_deg/360)*125/60));
final int arm_min = 0;
final List<Double> peck_positions = Arrays.asList(0,0.7,0.8);
//hardware variables
//Dc Motors
//drivetrain
private DcMotor frontLeft = null;
private DcMotor frontRight = null;
private DcMotor backLeft = null;
private DcMotor backRight = null;
//extensions
private DcMotor vertical_1 = null;
private DcMotor vertical_2 = null;
private DcMotor horizontal = null;
//arm
private DcMotor armMotor = null;
//Servos
//Continuous
//private CRServo sub_peck = null;
//Angular
private Servo specimen_claw = null;
private Servo sample_claw = null;
private Servo sub_rotation = null;
private Servo sub_peck = null;
@Override
public void runOpMode(){
telemetry.addData("Status:","Ready");
telemetry.update();
//initialize hardware variables
//Dc Motors
//drivetrain
frontLeft = hardwareMap.get(DcMotor.class,"frontLeft");
frontRight = hardwareMap.get(DcMotor.class,"frontRight");
backLeft = hardwareMap.get(DcMotor.class,"backLeft");
backRight = hardwareMap.get(DcMotor.class,"backRight");
//extensions
vertical_1 = hardwareMap.get(DcMotor.class,"Vertical 1");
vertical_2 = hardwareMap.get(DcMotor.class,"Vertical 2");
horizontal = hardwareMap.get(DcMotor.class,"Horizontal");
//arm
armMotor = hardwareMap.get(DcMotor.class,"armMotor");
//Servos
//continuous
// sub_peck = hardwareMap.get(CRServo.class,"peck");
//angular
specimen_claw = hardwareMap.get(Servo.class,"specimen claw");
sample_claw = hardwareMap.get(Servo.class,"sample claw");
sub_rotation = hardwareMap.get(Servo.class,"rotation");
sub_peck = hardwareMap.get(Servo.class,"peck");
//set motor directions
frontRight.setDirection(DcMotor.Direction.REVERSE);
backLeft.setDirection(DcMotor.Direction.REVERSE);
specimen_claw.setDirection(Servo.Direction.REVERSE);
sample_claw.setDirection(Servo.Direction.REVERSE);
sub_rotation.setDirection(Servo.Direction.REVERSE);
//reset encoders on all DcMotors
frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
vertical_1.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
vertical_2.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
horizontal.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
armMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
//set zero power behaviour to brake
frontLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
frontRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
backLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
backRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
vertical_1.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
vertical_2.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
horizontal.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
armMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
waitForStart();
runtime.reset();
while (opModeIsActive()){
//ensure specimen claw is closed following the end of autonomous
if(initial){
specimen_claw.setPosition(claw_closed_position);
initial = false;
}
//--------------------------------------------- DRIVER CONTROLS ---------------------------------------------
//mecanum drivetrain
//joystick inputs
x = gamepad1.left_stick_x;
y = gamepad1.left_stick_y;
r = gamepad1.right_stick_x;
denom = Math.max((Math.abs(y)+Math.abs(x)+Math.abs(r)),1); //power values passed are clipped to 1; this denominator helps adjust for this issue.
//power variables for each motor
FLP = (y+x+r)/denom;
FRP = (y-x-r)/denom;
BLP = (y-x+r)/denom;
BRP = (y+x-r)/denom;
//setting power to the motors
frontLeft.setPower(FLP);
frontRight.setPower(FRP);
backLeft.setPower(BLP);
backRight.setPower(BRP);
//vertical extension; see V_ext method (ln 312)
if (gamepad1.dpad_up && !gamepad1.dpad_down){
V_ext(1);
}
if (gamepad1.dpad_down && !gamepad1.dpad_up){
V_ext(-1);
}
//horizontal extension; see H_ext method (ln 323)
if (gamepad1.dpad_right && !gamepad1.dpad_left){
H_ext(1);
}
if (gamepad1.dpad_left && !gamepad1.dpad_right){
H_ext(-1);
}
//arm movement; see move_arm method (ln 331)
if (gamepad1.right_bumper){
move_arm(-1);
}
if (gamepad1.left_bumper){
move_arm(1);
}
//sub arm (peck); adjusting the targeted positional value for the peck, actual movement called at end of opMode (ln 305)
if (gamepad1.right_trigger>0.3){
//pecking(1);
pecking_position+=0.05;
} else if (gamepad1.left_trigger>0.3){
//pecking(-1);
pecking_position-=0.05;
}
//claw controls
if (gamepad1.a){
if (!sample_claw_open_status){
sample_claw.setPosition(claw_open_position);
sample_claw_open_status = true;
} else if (sample_claw_open_status){
sample_claw.setPosition(claw_closed_position);
sample_claw_open_status = false;
}
}
if (gamepad1.b){
if(!specimen_claw_open_status){
specimen_claw.setPosition(claw_open_position);
specimen_claw_open_status = true;
} else if (specimen_claw_open_status){
specimen_claw.setPosition(claw_closed_position);
specimen_claw_open_status = false;
}
}
//submersible claw rotation; adjusting the targeted positional value for the claw rotation, actual movement called at end of opMode (ln 304)
if (gamepad1.y && !gamepad1.x){
rotation_position += 0.05;
} else if (gamepad1.x && !gamepad1.y){
rotation_position -= 0.05;
}
// robot 90 rotation; values are placeholders, testing required for actual values
if (gamepad1.RightStickButton){
if(forward_orientation){
//if facing forward (away from driver), rotate 90 deg counter clockwise to face submersible
//left side motors rotate backward, right side motors rotate forward
frontLeft.setPower(-1);
frontRight.setPower(1);
backLeft.setPower(-1);
backRight.setPower(1);
sleep(50); //adjust value until rotation is 90 deg
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
} else if (!forward_orientation){
//if facing sideways (towards the submersible), rotate 90 deg clockwise to face forward
//left side motors rotate forward, right side motors rotate backward
frontLeft.setPower(1);
frontRight.setPower(-1);
backLeft.setPower(1);
backRight.setPower(-1);
sleep(50); //adjust value until rotation is 90 deg
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
}
}
//--------------------------------------------- OPPERATOR CONTROLS ---------------------------------------------
//opperator manual inputs
if (gamepad2.a){
if (!sample_claw_open_status){
sample_claw.setPosition(claw_open_position);
sample_claw_open_status = true;
} else if (sample_claw_open_status){
sample_claw.setPosition(claw_closed_position);
sample_claw_open_status = false;
}
}
if (gamepad2.b){
if(!specimen_claw_open_status){
specimen_claw.setPosition(claw_open_position);
specimen_claw_open_status = true;
} else if (specimen_claw_open_status){
specimen_claw.setPosition(claw_closed_position);
specimen_claw_open_status = false;
}
}
if (gamepad2.y && !gamepad1.y && gamepad1.x){
rotation_position += 0.05;
} else if (gamepad2.x && !gamepad1.x && gamepad1.y){
rotation_position -= 0.05;
}
//preset controls
//vertical extension
if (gamepad2.dpad_up && !gamepad2.dpad_down){
//preset_V_ext(extension_max);
specimen_hang();
}
if (gamepad2.dpad_down && !gamepad2.dpad_up){
//preset_V_ext(extension_min);
wall_grab();
}
//horizontal extension
if (gamepad2.dpad_right && !gamepad2.dpad_left){
//preset_H_ext(extension_max);
sub_grab();
}
if (gamepad2.dpad_left && !gamepad2.dpad_right){
//preset_H_ext(extension_min);
sub_retraction();
}
sub_rotation.setPosition(rotation_position);
sub_peck.setPosition(pecking_position);
}
}
public void V_ext(int m){
vertical_1.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
vertical_2.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
vertical_1.setPower(m);
vertical_2.setPower(m);
sleep(25);
vertical_1.setPower(0);
vertical_2.setPower(0);
}
public void H_ext(int v){
horizontal.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
horizontal.setPower(v);
sleep(25);
horizontal.setPower(0);
}
public void move_arm(int n){
armMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
armMotor.setPower(n);
sleep(25);
armMotor.setPower(0);
}
public void pecking(int p){
sub_peck.setPower(p);
sleep(25);
sub_peck.setPower(0);
}
public void preset_V_ext(int tV){
vertical_1.setTargetPosition(tV);
vertical_2.setTargetPosition(tV);
vertical_1.setMode(DcMotor.RunMode.RUN_TO_POSITION);
vertical_2.setMode(DcMotor.RunMode.RUN_TO_POSITION);
vertical_1.setPower(1);
vertical_2.setPower(1);
}
public void preset_H_ext(int tH){
horizontal.setTargetPosition(tH);
horizontal.setMode(DcMotor.RunMode.RUN_TO_POSITION);
horizontal.setPower(1);
}
public void preset_arm_ext(int tA){
armMotor.setTargetPosition(tA);
armMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
armMotor.setPower(1);
}
public void preset_peck_ext(double tP){
pecking_position = tP;
}
public void wall_grab(){
preset_V_ext(extension_min);
preset_arm_ext(arm_min);
specimen_claw.setPosition(claw_open_position);
specimen_claw_open_status = true;
rotation_position = rotation_initial;
preset_peck_ext(peck_initial);
sleep(50);
preset_H_ext(extension_min);
}
public void specimen_hang(){
preset_V_ext(extension_max);
preset_arm_ext(arm_max);
rotation_position = rotation_initial;
preset_peck_ext(peck_initial);
sleep(50);
preset_H_ext(extension_min);
}
public void sub_grab(){
preset_V_ext(extension_min);
preset_arm_ext(arm_min);
sample_claw.setPosition(claw_open_position);
sample_claw = true;
rotation_position = rotation_initial;
preset_peck_ext(peck_2);
sleep(50);
preset_H_ext(extension_max);
}
public void sub_retraction(){
preset_V_ext(extension_min);
preset_arm_ext(arm_min);
rotation_position = rotation_initial;
preset_peck_ext(peck_initial);
sleep(50);
preset_H_ext(extension_min);
}
}