forked from acmerobotics/road-runner-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrivingFieldCentric.java
396 lines (321 loc) · 13.7 KB
/
DrivingFieldCentric.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.TouchSensor;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.hardware.IMU;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
/**
* This file contains an example of an iterative (Non-Linear) "OpMode".
* An OpMode is a 'program' that runs in either the autonomous or the teleop period of an FTC match.
* The names of OpModes appear on the menu of the FTC Driver Station.
* When an selection is made from the menu, the corresponding OpMode
* class is instantiated on the Robot Controller and executed.
* <p>
* This particular OpMode just executes a basic Tank Drive Teleop for a two wheeled robot
* It includes all the skeletal structure that all iterative OpModes contain.
* <p>
* Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
* Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
*/
@TeleOp(name = "DrivingFC", group = "Iterative Opmode")
//@Disabled
public class DrivingFieldCentric extends OpMode {
// Declare OpMode members.
private ElapsedTime runtime = new ElapsedTime();
// Wheels
private DcMotor rightRear = null;
private DcMotor leftRear = null;
private DcMotor rightFront = null;
private DcMotor leftFront = null;
// Slider, Intake
private DcMotor sliderLeft = null;
private DcMotor sliderRight = null;
private TouchSensor sliderLimitSwitch = null;
private Servo intakeArmServoLeft = null;
private Servo intakeArmServoRight = null;
private Servo intakeHand = null;
private IMU imu;
// Variables
private double power = 1;
private double driveSpeed = .7;
private double drivePower = .7;
private double turboPower = 1;
private boolean turboStop = true;
private boolean turbo = false;
private boolean handClosed = true;
private boolean xPressed;
private boolean yPressed;
// Slider, Intake variables
private int target = 0;
private int sliderSpeed = 300;
private int current;
private int incr;
private int maxHeight = 6000;
private int minHeight = -10;
private boolean sliderLimits = true;
private boolean limitsPressed = false;
private ElapsedTime intakeTimer = new ElapsedTime();
private enum LiftState{
LIFT_START,
LIFT_LOW,
LIFT_MID,
LIFT_HIGH
};
LiftState liftState = LiftState.LIFT_START;
private int lowPole = 1400;
private int midPole = 2700;
private int highPole = 3950;
private double intakeArmPickupPosition = 0;
private double intakeArmMidPosition = 0.3;
private double intakeArmDropPosition = 0.85;
private double handOpenPos = 0.01;
private double handClosedPos = 0.13;
private enum ArmState{
ARM_INTAKE,
ARM_MID_TO_DROP,
ARM_MID_TO_INTAKE,
ARM_DROP
}
ArmState armState = ArmState.ARM_MID_TO_DROP;
private boolean aPressed = false;
public static double mapRange(double a1, double a2, double b1, double b2, double s){
return b1 + ((s - a1)*(b2 - b1))/(a2 - a1);
}
/*
* Code to run ONCE when the driver hits INIT
*/
@Override
public void init() {
telemetry.addData("Status: ", "Busy");
telemetry.update();
// Initialize hardware variables
rightRear = hardwareMap.get(DcMotor.class, "rightRear");
leftRear = hardwareMap.get(DcMotor.class, "leftRear");
rightFront = hardwareMap.get(DcMotor.class, "rightFront");
leftFront = hardwareMap.get(DcMotor.class, "leftFront");
sliderLeft = hardwareMap.get(DcMotor.class, "sliderLeft");
sliderRight = hardwareMap.get(DcMotor.class, "sliderRight");
sliderLimitSwitch = hardwareMap.get(TouchSensor.class, "sliderLimitSwitch");
intakeArmServoLeft = hardwareMap.get(Servo.class, "intakeArmServoL");
intakeArmServoRight = hardwareMap.get(Servo.class, "intakeArmServoR");
intakeHand = hardwareMap.get(Servo.class, "intakeHand");
// Motor Direction
rightRear.setDirection(DcMotor.Direction.FORWARD);
leftRear.setDirection(DcMotor.Direction.REVERSE);
rightFront.setDirection(DcMotor.Direction.FORWARD);
leftFront.setDirection(DcMotor.Direction.REVERSE);
sliderLeft.setDirection(DcMotor.Direction.REVERSE);
sliderRight.setDirection(DcMotor.Direction.FORWARD);
intakeArmServoRight.setDirection(Servo.Direction.REVERSE);
// Set encoder mode
sliderLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
sliderRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
sliderLeft.setTargetPosition(0);
sliderRight.setTargetPosition(0);
sliderLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
sliderRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
intakeTimer.startTime();
intakeTimer.reset();
// Retrieve the IMU from the hardware map
imu = hardwareMap.get(IMU.class, "imu");
IMU.Parameters parameters = new IMU.Parameters(new RevHubOrientationOnRobot(
RevHubOrientationOnRobot.LogoFacingDirection.RIGHT,
RevHubOrientationOnRobot.UsbFacingDirection.BACKWARD));
imu.initialize(parameters);
telemetry.addData("Status: ", "Done");
telemetry.update();
}
// Code to run REPEATEDLY after driver hits INIT but before they hit PLAY
@Override
public void init_loop() {}
// Code to be run ONCE after driver hits PLAY
@Override
public void start() {
intakeTimer.reset();
intakeTimer.startTime();
}
/*
* Code to run REPEATEDLY after the driver hits PLAY but before they hit STOP
*/
@Override
public void loop() {
// Gamepad Inputs
double G1leftStickY = gamepad1.left_stick_y * 1.1;
double G1leftStickX = -gamepad1.left_stick_x;
double G1rightStickX = gamepad1.right_stick_x;
double G2rightStickY = -gamepad2.right_stick_y;
double G2rightStickX = -gamepad2.right_stick_x;
double G2leftStickY = -gamepad2.left_stick_y;
// Holonomic mecanum wheel movement - Straight - Strafe - Turn
// rightRear.setPower(driveSpeed * (-G1leftStickY + 1.2 * G1leftStickX + 1.2 * -G1rightStickX));
// leftRear.setPower(driveSpeed * (-G1leftStickY + 1.2 * -G1leftStickX + 1.2 * G1rightStickX));
// rightFront.setPower(driveSpeed * (-G1leftStickY + 1.2 * -G1leftStickX + 1.2 * -G1rightStickX));
// leftFront.setPower(driveSpeed * (-G1leftStickY + 1.2 * G1leftStickX + 1.2 * G1rightStickX));
if (gamepad1.options) {
imu.resetYaw();
}
double botHeading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS);
double rotX = G1leftStickX * Math.cos(-botHeading) - G1leftStickY * Math.sin(-botHeading);
double rotY = G1leftStickX * Math.sin(-botHeading) + G1leftStickY * Math.cos(-botHeading);
double denominator = Math.max(Math.abs(rotY) + Math.abs(rotX) + Math.abs(G1rightStickX), 1.5);
double frontLeftPower = (rotY + rotX + G1rightStickX) / denominator;
double backLeftPower = (rotY - rotX + G1rightStickX) / denominator;
double frontRightPower = (rotY - rotX - G1rightStickX) / denominator;
double backRightPower = (rotY + rotX - G1rightStickX) / denominator;
rightRear.setPower(backRightPower * driveSpeed);
leftRear.setPower(backLeftPower * driveSpeed);
rightFront.setPower(frontRightPower * driveSpeed);
leftFront.setPower(frontLeftPower * driveSpeed);
// Reset slider encoders
if(gamepad2.back) {
sliderLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
sliderRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
sliderLeft.setTargetPosition(0);
sliderRight.setTargetPosition(0);
sliderRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
sliderLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
// Update lift target using joystick
current = sliderLeft.getCurrentPosition();
incr = (int)(G2leftStickY * sliderSpeed);
target += incr;
// Or set lift target using dpad
if(gamepad2.dpad_up){
target = highPole;
}
if(gamepad2.dpad_left || gamepad2.dpad_right){
target = midPole;
}
if(gamepad2.dpad_down){
target = lowPole;
}
if(gamepad2.left_bumper && !limitsPressed) {
limitsPressed = true;
sliderLimits = !sliderLimits;
} else if (!gamepad2.left_bumper) {
limitsPressed = false;
}
// Limit lift extension & retraction
if(target > maxHeight && sliderLimits)
{
target = maxHeight;
}
if(target < minHeight && sliderLimits)
{
target = minHeight;
}
// Prevent target from going farther than slider speed
if(Math.abs(target-current) > sliderSpeed)
{
target = current + (int)Math.signum(target-current) * sliderSpeed;
}
sliderLeft.setTargetPosition(target);
sliderRight.setTargetPosition(target);
// Power motors when more than 40 encoder ticks away from target
if(Math.abs(target - sliderLeft.getCurrentPosition()) >= 60 || Math.abs(target - sliderRight.getCurrentPosition()) >= 60){
sliderLeft.setPower(1);
sliderRight.setPower(1);
}
// Disable motors when target is reached
else {
sliderLeft.setPower(0);
sliderRight.setPower(0);
}
// Reset intake position
if (gamepad1.y && !yPressed && armState != armState.ARM_DROP) {
armState = ArmState.ARM_DROP;
yPressed = true;
}
else if(!gamepad2.y) {
yPressed = false;
}
// Set intake position using finite state machine
if(gamepad2.a && !aPressed || gamepad2.y && !yPressed) {
aPressed = true;
intakeTimer.reset();
switch (armState) {
case ARM_DROP:
armState = ArmState.ARM_MID_TO_INTAKE; // Next state
intakeArmServoLeft.setPosition(intakeArmDropPosition);
intakeArmServoRight.setPosition(intakeArmDropPosition);
break;
case ARM_MID_TO_INTAKE:
armState = ArmState.ARM_INTAKE; // Next state
intakeArmServoLeft.setPosition(intakeArmMidPosition);
intakeArmServoRight.setPosition(intakeArmMidPosition);
// Close hand when moving over middle bridge
intakeHand.setPosition(handClosedPos);
handClosed = true;
break;
case ARM_INTAKE:
armState = ArmState.ARM_MID_TO_DROP; // Next state
intakeArmServoLeft.setPosition(intakeArmPickupPosition);
intakeArmServoRight.setPosition(intakeArmPickupPosition);
break;
case ARM_MID_TO_DROP:
armState = ArmState.ARM_DROP; // Next state
intakeArmServoLeft.setPosition(intakeArmMidPosition);
intakeArmServoRight.setPosition(intakeArmMidPosition);
// Close hand when moving over middle bridge
intakeHand.setPosition(handClosedPos);
handClosed = true;
break;
default:
armState = ArmState.ARM_DROP;
}
}
else if (!gamepad2.a) {
aPressed = false;
}
// Set hand position
if(gamepad2.x && !xPressed) {
xPressed = true;
handClosed = !handClosed;
intakeHand.setPosition(handClosed ? handClosedPos : handOpenPos);
}
else if(!gamepad2.x) {
xPressed = false;
}
// Driving turbo mode
if (gamepad1.left_bumper && turboStop) {
turboStop = false;
turbo = !turbo;
driveSpeed = turbo ? turboPower : drivePower;
}
else if (!gamepad1.left_bumper) {
turboStop = true;
}
// Driver Hub Telemetry
telemetry.addData("Power mode: ", turbo ? "Turbo" : "No turbo");
telemetry.addData("Runtime", runtime);
telemetry.addData("Slider limits: ", sliderLimits ? "On" : "Off");
telemetry.addData("Limit Switch", !sliderLimitSwitch.isPressed());
telemetry.addData("Slider Left: ", sliderLeft.getCurrentPosition());
telemetry.addData("Slider Right: ", sliderRight.getCurrentPosition());
telemetry.addData("Slider Target ", target);
telemetry.addData("Arm State: ", armState);
telemetry.addData("Arm Hand: ", intakeHand.getPosition());
telemetry.addData("Arm pos: ", intakeArmServoLeft.getPosition());
telemetry.addData("botHeading",botHeading);
telemetry.update();
}
/*
* Code to run ONCE after the driver hits STOP
*/
@Override
public void stop() {
// Turn off all motor power
rightRear.setPower(0);
leftRear.setPower(0);
rightFront.setPower(0);
leftFront.setPower(0);
sliderLeft.setPower(0);
sliderRight.setPower(0);
}
}