-
Notifications
You must be signed in to change notification settings - Fork 1
/
TeleOp_Methods.java
385 lines (330 loc) · 14.1 KB
/
TeleOp_Methods.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
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 org.firstinspires.ftc.robotcore.external.navigation.Orientation;
public class TeleOp_Methods extends TeleOp_Members {
/**
*copy of runUsingIMUAuto methods
*
*/
public void resetAngle()
{
lastAngles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
globalAngle = 0;
}
/**
* Get current cumulative angle rotation from last reset.
* @return Angle in degrees. + = left, - = right.
*/
public double getAngle ()
{
// We experimentally determined the Z axis is the axis we want to use for heading angle.
// We have to process the angle because the imu works in euler angles so the Z axis is
// returned as 0 to +180 or 0 to -180 rolling back to -179 or +179 when rotation passes
// 180 degrees. We detect this transition and track the total cumulative angle of rotation.
Orientation angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
double deltaAngle = angles.firstAngle - lastAngles.firstAngle;
if (globalAngle < -180)
globalAngle += 360;
else if (globalAngle > 180)
globalAngle -= 360;
globalAngle += deltaAngle;
lastAngles = angles;
return globalAngle;
}
/**
* See if we are moving in a straight line and if not return a power correction value.
* @return Power adjustment, + is adjust left - is adjust right.
*/
public double checkDirection()
{
// The gain value determines how sensitive the correction is to direction changes.
// You will have to experiment with your robot to get small smooth direction changes
// to stay on a straight line.
double correction, angle, gain = .10;
angle = getAngle();
if (angle == 0)
correction = 0; // no adjustment.
else
correction = -angle; // reverse sign of angle for correction.
correction = correction * gain;
return correction;
}
/**
* Rotate left or right the number of degrees. Does not support turning more than 180 degrees.
* @param degrees Degrees to turn, + is left - is right
*/
public void rotate(double degrees, double power)
{
double leftPower, rightPower;
leftBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// restart imu movement tracking.
resetAngle();
// getAngle() returns + when rotating counter clockwise (left) and - when rotating
// clockwise (right).
power = -power;
if (degrees < 0)
{ // turn right.
leftPower = power;
rightPower = -power;
}
else if (degrees > 0)
{ // turn left.
leftPower = -power;
rightPower = power;
}
else return;
// set power to rotate.
//leftMotor.setPower(leftPower);
//rightMotor.setPower(rightPower);
leftBackDrive.setPower(leftPower);
leftFrontDrive.setPower(leftPower);
rightBackDrive.setPower(rightPower);
rightFrontDrive.setPower(rightPower);
// rotate until turn is completed.
if (degrees < 0)
{
// On right turn we have to get off zero first.
while (opModeIsActive() && getAngle() == 0) {}
while (opModeIsActive() && getAngle() > degrees + 3.5) {}
}
else // left turn.
while (opModeIsActive() && getAngle() < degrees - 3.5) {}
// turn the motors off.
//rightMotor.setPower(0);
//leftMotor.setPower(0);
leftBackDrive.setPower(0);
leftFrontDrive.setPower(0);
rightBackDrive.setPower(0);
rightFrontDrive.setPower(0);
// wait for rotation to stop.
//sleep(50);
// reset angle tracking on new heading.
resetAngle();
}
/**
* Rotate left or right the number of degrees. Does not support turning more than 359 degrees.
* @param degrees Degrees to turn, + is left - is right
*/
private void PIDrotate(int degrees, double power)
{
// restart imu angle tracking.
resetAngle();
// if degrees > 359 we cap at 359 with same sign as original degrees.
if (Math.abs(degrees) > 359) degrees = (int) Math.copySign(359, degrees);
// start pid controller. PID controller will monitor the turn angle with respect to the
// target angle and reduce power as we approach the target angle. This is to prevent the
// robots momentum from overshooting the turn after we turn off the power. The PID controller
// reports onTarget() = true when the difference between turn angle and target angle is within
// 1% of target (tolerance) which is about 1 degree. This helps prevent overshoot. Overshoot is
// dependant on the motor and gearing configuration, starting power, weight of the robot and the
// on target tolerance. If the controller overshoots, it will reverse the sign of the output
// turning the robot back toward the setpoint value.
pidRotate.reset();
pidRotate.setSetpoint(degrees);
pidRotate.setInputRange(0, degrees);
pidRotate.setOutputRange(0, power);
pidRotate.setTolerance(1);
pidRotate.enable();
// getAngle() returns + when rotating counter clockwise (left) and - when rotating
// clockwise (right).
// rotate until turn is completed.
if (degrees < 0)
{
// On right turn we have to get off zero first.
while (opModeIsActive() && getAngle() == 0)
{
leftBackDrive.setPower(power);
rightBackDrive.setPower(-power);
sleep(100);
}
do
{
power = pidRotate.performPID(getAngle()); // power will be - on right turn.
leftBackDrive.setPower(-power);
rightBackDrive.setPower(power);
} while (opModeIsActive() && !pidRotate.onTarget());
}
else // left turn.
do
{
power = pidRotate.performPID(getAngle()); // power will be + on left turn.
leftBackDrive.setPower(-power);
rightBackDrive.setPower(power);
} while (opModeIsActive() && !pidRotate.onTarget());
// turn the motors off.
leftBackDrive.setPower(0);
rightBackDrive.setPower(0);
rotation = getAngle();
// wait for rotation to stop.
sleep(500);
// reset angle tracking on new heading.
resetAngle();
}
/**
* check method for teleOp and possibly autonomous
* @param power increase power the first check but decrease power the second
* add sleep of 50 in between
*/
public void check (double power)
{
if (getAngle() == 0) {
rotate(0,power);
}
else if (getAngle() > 0) {
rotate(-getAngle(), power);
}
else if (getAngle() < 0) {
rotate(-getAngle(), power);
}
//sleep(50);
}
//
//
//COMPLETELY USELESS ENCODER MOVEMENTS EXCEPT FOR ENDGAME AUTOMATED POWER SHOTS
//
//
/**
* Forward is never used but is here to test for correction
* @param speed
* @param distance
*/
public void forward(double speed, double distance)
{
resetAngle();
leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
int counts = (int) ((distance / (4 * Math.PI)) * 1075);
leftBackDrive.setTargetPosition(counts);
leftFrontDrive.setTargetPosition(counts);
rightBackDrive.setTargetPosition(counts);
rightFrontDrive.setTargetPosition(counts);
//setting all motors to go forward (positive)
leftBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBackDrive.setPower(speed);
leftFrontDrive.setPower(speed);
rightBackDrive.setPower(speed);
rightFrontDrive.setPower(speed);
while (opModeIsActive() && leftBackDrive.isBusy() && leftFrontDrive.isBusy() && rightBackDrive.isBusy() && rightFrontDrive.isBusy()) {
double correction = checkDirection();
leftBackDrive.setPower(speed - correction);
//leftFrontDrive.setPower(speed - correction);
rightBackDrive.setPower(speed + correction);
//rightFrontDrive.setPower(speed + correction);
}
//setting all motor powers to 0 (stopping)
leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
}
/**
* Need this method for Power Shot Automation
* @param speed
* @param distance
*/
public void strafeRight(double speed, double distance)
{
resetAngle();
leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
int counts = (int) ((distance / (4 * Math.PI)) * 1075);
leftBackDrive.setTargetPosition(counts);
leftFrontDrive.setTargetPosition(-counts);
rightBackDrive.setTargetPosition(-counts);
rightFrontDrive.setTargetPosition(counts);
//setting all motors to go forward (positive)
leftBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBackDrive.setPower(speed);
leftFrontDrive.setPower(-speed);
rightBackDrive.setPower(-speed);
rightFrontDrive.setPower(speed);
while (opModeIsActive() && leftBackDrive.isBusy() && leftFrontDrive.isBusy() && rightBackDrive.isBusy() && rightFrontDrive.isBusy()) {
double correction = checkDirection();
leftBackDrive.setPower(speed - correction);
leftFrontDrive.setPower(speed + correction);
rightBackDrive.setPower(speed - correction);
rightFrontDrive.setPower(speed + correction);
}
//setting all motor powers to 0 (stopping)
leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
}
/**
* Automated movement of arm to designated parallel position
* @param counts
* @param power
* @param original
*/
public void testArm(int counts, double power, int original)
{
wobbleArm.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
wobbleArm.setPower(-0.2);
if(wobbleArm.getCurrentPosition() <= original + 10 && wobbleArm.getCurrentPosition() >= original - 10){
wobbleArm.setPower(0);
sleep(1000);
}
wobbleArm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
wobbleArm.setTargetPosition(counts);
wobbleArm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
wobbleArm.setPower(power);
}
/**
* Automated 3 power shots from Wall
*/
public void automatedPowerShots()
{
frontShooter.setPower(0.55);
sleep(2000);
strafeRight(12.5, 0.1);
trigger.setPosition(shootPosition);
sleep(100);
trigger.setPosition(readyPosition);
strafeRight(4.5, 0.1);
trigger.setPosition(shootPosition);
sleep(100);
trigger.setPosition(readyPosition);
strafeRight(4.5, 0.1);
trigger.setPosition(shootPosition);
sleep(100);
trigger.setPosition(readyPosition);
sleep(1000);
frontShooter.setPower(0);
}
/**
* to return all drive base motors to RUN USING ENCODERS so it works in teleOp
*/
public void returnToTeleOp()
{
leftBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
/**
* @throws InterruptedException
*/
@Override
public void runOpMode() throws InterruptedException {
}
}