forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanualTeleop.java
More file actions
272 lines (211 loc) · 10.2 KB
/
manualTeleop.java
File metadata and controls
272 lines (211 loc) · 10.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
package org.firstinspires.ftc.teamcode;
import static java.lang.Math.abs;
import static java.lang.Math.atan2;
import static java.lang.Math.cos;
import static java.lang.Math.sqrt;
import static java.lang.Math.toDegrees;
import static java.lang.Math.toRadians;
import com.acmerobotics.dashboard.FtcDashboard;
import com.acmerobotics.dashboard.config.Config;
import com.acmerobotics.dashboard.FtcDashboard;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.IMU;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.Range;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
@Config
@TeleOp(name = "GG{GetthouGooder}")
public class manualTeleop extends LinearOpMode {
//these guys are for ftcdashboard
public static double dlOVERRIDE = 0;
public static double drOVERRIDE = 0;
@Override
public void runOpMode() throws InterruptedException {
//define motors as existing
DcMotorEx lr, rf, lf, rr, sl, sr, el, er;
IMU imu;
double heading;
double desiredHeading;
int timesAcrossZero;
double fixedHeading;
Orientation angles;
boolean dpadDebounce = false;
boolean justTurned = false;
double transientSpeed;
double transientTurn = 0;
double rampSpeed = 0.2; //Rate in percentage at which function ramps to speed
double turnOverride = 0.75; //at one turning can completely override drive, at zero turning has no effect
double turnRamp = 0.5; //this can be adjusted to slow down or speed up the turning acceleration
double headingCorrectionRange = 45; //this scales the reaction of the heading correction-
// smaller values encourage snappy reaction, but larger values decrease overshoot
double leftDiagPwr;
double rightDiagPwr;
Servo dl, dr, roll;
boolean fieldCentric = true;
boolean justTurbo = false;
//cool
imu = hardwareMap.get(IMU.class, "imu");
angles = imu.getRobotOrientation(AxesReference.EXTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES); //poll IMU
heading = Range.scale(angles.firstAngle, 0, 360, -1, 1); //extract heading
imu.resetYaw();
timesAcrossZero = 0;
transientSpeed = 0;
desiredHeading = 0;
//global hardware definitions
roll = hardwareMap.get(Servo.class, "rollServo");
dl = hardwareMap.get(Servo.class, "diffyLeft");
dr = hardwareMap.get(Servo.class, "diffyRight");
lf = hardwareMap.get(DcMotorEx.class, "frontLeft");
rf = hardwareMap.get(DcMotorEx.class, "frontRight");
lr = hardwareMap.get(DcMotorEx.class, "rearLeft");
rr = hardwareMap.get(DcMotorEx.class, "rearRight");
sl = hardwareMap.get(DcMotorEx.class, "shoulderLeft");
sr = hardwareMap.get(DcMotorEx.class, "shoulderRight");
el = hardwareMap.get(DcMotorEx.class, "elbowLeft");
er = hardwareMap.get(DcMotorEx.class, "elbowRight");
//Here you can edit the individual motor headings
lr.setDirection(DcMotorSimple.Direction.REVERSE);
rf.setDirection(DcMotorSimple.Direction.FORWARD);
lf.setDirection(DcMotorSimple.Direction.REVERSE);
rr.setDirection(DcMotorSimple.Direction.FORWARD);
double rollPwr = 0;
boolean clawState = false;
boolean rbDebounce = false;
double drPos = 0;
double dlPos = 0;
double clawPos = 0;
//FtcDashboard dashboard = FtcDashboard.getInstance();
waitForStart();
while(opModeIsActive()){
telemetry.update();
el.setPower(-gamepad1.right_stick_y);
er.setPower(-gamepad1.right_stick_y);
sl.setPower(-gamepad1.right_stick_x);
sr.setPower(gamepad1.right_stick_x);
if (gamepad1.x) {
rollPwr = 1;
} else if (gamepad1.y) {
rollPwr = -1;
}
roll.setPosition(rollPwr);
if (dlOVERRIDE == 0 && drOVERRIDE == 0) {
if (gamepad1.right_bumper && !rbDebounce) {
clawState = !clawState;
rbDebounce = true;
} else if (!gamepad1.right_bumper) {
rbDebounce = false;
}
if (clawState) {
drPos = 0.2;
dlPos = 0.2;
} else {
drPos = 0;
dlPos = 0;
}
if (gamepad1.a) {
clawPos = Range.clip(clawPos+0.01,0,1);
} else if (gamepad1.b) {
clawPos = Range.clip(clawPos-0.01,0,1);
}
drPos += 0.5+clawPos;
dlPos += 0.5-clawPos;
dr.setPosition(drPos);
dl.setPosition(dlPos);
} else {
dr.setPosition(drOVERRIDE);
dl.setPosition(dlOVERRIDE);
}
telemetry.addData("drPos", drPos);
telemetry.addData("dlPos", dlPos);
double xPwr = gamepad1.left_stick_x;
double yPwr = gamepad1.left_stick_y;
double desiredPower = Range.scale(sqrt(xPwr*xPwr+yPwr*yPwr), 0, sqrt(2), 0, 1); //convert the diagonal of the sticks to magnitude
double turn;
boolean turbo = (gamepad1.left_stick_button || gamepad1.right_stick_button);
float rotate = (gamepad1.right_trigger - gamepad1.left_trigger);//controller turn input
if (turbo){
desiredHeading += rotate*-4;
} else {
desiredHeading += rotate*-1;
}
if (transientSpeed >= desiredPower + 0.001) {
//transientSpeed -= (abs(desiredPower - transientSpeed))/5;
transientSpeed -= (transientSpeed - desiredPower)*rampSpeed;
} else if (transientSpeed < desiredPower - 0.001) {
transientSpeed += (desiredPower - transientSpeed)*rampSpeed;
}
double direction = (toDegrees(atan2(gamepad1.left_stick_x, gamepad1.left_stick_y)));
//All the field centric stuff is right here, copy/pasted from the last code but never really tested
//I HOPE THIS WORKS, CUZ I DIDNT VERIFY IT
//I guess we need a thing to track the jump from 360 degrees to zero and vice versa, cuz there probably isn't any negative degrees
double lastHeading = heading;
angles = imu.getRobotOrientation(AxesReference.EXTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES); //poll IMU
heading = angles.firstAngle; //extract heading
telemetry.addData("Heading:", heading);
//the below direction tracking code is inspired by VCSC
//I could make this dynamic, where the controller tracks the time between calls and calculates the max possible rotation in that time
if (heading - lastHeading > 270) {
timesAcrossZero--;
} else if (heading - lastHeading < -270){
timesAcrossZero++;
}
fixedHeading = heading + (timesAcrossZero * 360);
if (gamepad1.dpad_left && !dpadDebounce) {
fieldCentric = !fieldCentric;
dpadDebounce = true;
} else if (!gamepad1.dpad_left) {
dpadDebounce = false;
}
if (fieldCentric) { //when field centric, rotate control with IMU
direction = ((direction - fixedHeading) + 360) % 360; //augment joystick command with IMU direction, keeping within the range of 360
}
if (rotate == 0 && justTurned) {
desiredHeading = fixedHeading;
}
justTurned = rotate != 0;
turn = Range.clip(Range.scale(fixedHeading - desiredHeading, -headingCorrectionRange, headingCorrectionRange, -1, 1), -1, 1);
//the constants in the ramping functions below are tunable!
if (transientTurn > turn + 0.001) {
transientTurn = turn;
} else if (transientTurn < turn - 0.001) {
transientTurn += (turn - transientTurn)*turnRamp;
}
//Mecanum wheel diagonals spin the same direction, except when turning
//set the powers of rf and lf, and lr and lf
rightDiagPwr = (Range.clip((1.4*cos(toRadians(direction) + 3.1415/4)), -1, 1))*transientSpeed;
leftDiagPwr = (Range.clip((1.4*cos(toRadians(direction) - 3.1415/4)), -1, 1))*transientSpeed;
//what it does is scales down the drive power depending on how much it wants to turn, then adds turn to the scaled value
//recently added is when the voltage dips
double RFPwr = ((leftDiagPwr*(1-Range.clip(abs(transientTurn),0,turnOverride)))+transientTurn);
double lfPwr = ((rightDiagPwr*(1-Range.clip(abs(transientTurn),0,turnOverride)))-transientTurn);
double lrPwr = ((leftDiagPwr*(1-Range.clip(abs(transientTurn),0,turnOverride)))-transientTurn);
double RRPwr = ((rightDiagPwr*(1-Range.clip(abs(transientTurn),0,turnOverride)))+transientTurn);
//Alfly final values to motors, with power and turbo modifiers
if (!turbo) {
RFPwr /= 2;
lfPwr /= 2;
lrPwr /= 2;
RRPwr /= 2;
justTurbo = false;
} else {
if (!justTurbo) {
desiredHeading = heading + rotate;
}
justTurbo = true;
}
rf.setPower(RFPwr);
lf.setPower(lfPwr);
lr.setPower(lrPwr);
rr.setPower(RRPwr);
telemetry.addData("direction", direction);
telemetry.addData("desiredPower", desiredPower);
}
}
}