-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRBotAuto8.java
395 lines (332 loc) · 17.4 KB
/
RRBotAuto8.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
import com.qualcomm.hardware.bosch.BNO055IMU;
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;
import java.util.Locale;
@Autonomous(name="RedRepoPausePark", group="Red")
public class RRBotAuto8 extends LinearOpMode {
// Declare OpMode members.
private ElapsedTime runtime = new ElapsedTime();
RRBotHardware robot = new RRBotHardware();
//gyro variables
private BNO055IMU imu;
private Orientation angles;
//Motor Definitions
private final double COUNTS_PER_MOTOR_REV = 537.6; // Andymark 20:1 gearmotor
private final double DRIVE_GEAR_REDUCTION = 1.0; // This is < 1.0 if geared UP
private final double WHEEL_DIAMETER_INCHES = 4.0; // For figuring circumference
private final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCHES * 3.1415);
//construct drive class
RRBotMecanumDrive drive = new RRBotMecanumDrive(robot);
@Override
public void runOpMode() {
telemetry.addData("Status", "Initialized");
telemetry.update();
robot.init(hardwareMap);
initGyro();
// Send telemetry message to indicate successful Encoder reset
telemetry.addData("Path0", "Starting at %7d :%7d",
robot.rearRightMotor.getCurrentPosition(),
robot.rearLeftMotor.getCurrentPosition(),
robot.frontRightMotor.getCurrentPosition(),
robot.frontLeftMotor.getCurrentPosition());
telemetry.update();
// Wait for the game to start (driver presses PLAY)
waitForStart();
runtime.reset();
// run until the end of the match (driver presses STOP)
while (opModeIsActive()) {
long start = System.currentTimeMillis();
EncoderDriveSideways(Constants.autoSpeed,-8.5,10);//strafe 20 inches to the left
EncoderDriveTank(Constants.autoSpeed,-33,-33,10); //run to foundation
robot.trayPullerLeft.setPosition(0);//Grasp foundation with servos
robot.trayPullerRight.setPosition(1);//^^^
sleep(800);//Wait for servos
EncoderDriveTank(Constants.autoSpeed,32,32,10);//bring foundation back to wall
robot.trayPullerLeft.setPosition(1);//Release servos
robot.trayPullerRight.setPosition(0);//^^^
sleep(800);//Wait for servos
long delay = 25000 - (System.currentTimeMillis()-start);
sleep(delay);
EncoderDriveSideways(Constants.autoSpeed*2,40,10);
requestOpModeStop();
}
}
public void initGyro()
{
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
parameters.calibrationDataFile = "BNO055IMUCalibration.json";
parameters.loggingEnabled = true;
parameters.loggingTag = "IMU";
imu = hardwareMap.get(BNO055IMU.class, "imu");
imu.initialize(parameters);
}
public void EncoderDriveSideways(double speed, double distance, double timeoutS)
{
//reset encoders
robot.rearLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
idle();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
int newRearLeftTarget;
int newRearRightTarget;
int newFrontLeftTarget;
int newFrontRightTarget;
//ensure that the opmode is still active
if(opModeIsActive())
{
//calculate target positions, negative for two motors so the robot strafes
newRearLeftTarget = robot.rearLeftMotor.getCurrentPosition() + (int) (-distance * Math.sqrt(2) * COUNTS_PER_INCH);
newRearRightTarget = robot.rearRightMotor.getCurrentPosition() + (int) (distance * Math.sqrt(2) * COUNTS_PER_INCH);
newFrontLeftTarget = robot.frontLeftMotor.getCurrentPosition() + (int) (distance * Math.sqrt(2) * COUNTS_PER_INCH);
newFrontRightTarget = robot.frontRightMotor.getCurrentPosition() + (int) (-distance * Math.sqrt(2) * COUNTS_PER_INCH);
robot.rearLeftMotor.setTargetPosition(newRearLeftTarget);
robot.rearRightMotor.setTargetPosition(newRearRightTarget);
robot.frontLeftMotor.setTargetPosition(newFrontLeftTarget);
robot.frontRightMotor.setTargetPosition(newFrontRightTarget);
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
runtime.reset();
robot.rearLeftMotor.setPower(Math.abs(speed));
robot.rearRightMotor.setPower(Math.abs(speed));
robot.frontLeftMotor.setPower(Math.abs(speed));
robot.frontRightMotor.setPower(Math.abs(speed));
//keep looping until one of the motors finished its movement
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(robot.rearLeftMotor.isBusy() && robot.rearRightMotor.isBusy() && robot.frontLeftMotor.isBusy() && robot.frontRightMotor.isBusy()))
{
//report current and target positions to driver station
telemetry.addData("Path1", "Running to %7d :%7d", newRearLeftTarget, newRearRightTarget, newFrontLeftTarget, newFrontRightTarget);
telemetry.addData("Path2", "Running at %7d :%7d",
robot.rearLeftMotor.getCurrentPosition(),
robot.rearRightMotor.getCurrentPosition(),
robot.frontLeftMotor.getCurrentPosition(),
robot.frontRightMotor.getCurrentPosition());
telemetry.update();
}
TurnOffMotors();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}
public double[] calcVelocities(double leftX, double leftY)
{
double moveY1 = leftY;
double turn = leftX;
//remap input values using a function
/*if(doFunction)
{
moveX = inputFunction(moveX);
moveY1 = inputFunction(moveY1);
turn = inputFunction(turn);
moveY2 = inputFunction(moveY2);
}*/
double v1 = moveY1 + turn;
double v2 = moveY1 - turn;
double v3 = moveY1 - turn;
double v4 = moveY1 + turn;
double max = Math.abs(v1);
if(Math.abs(v2) > max)
max = Math.abs(v2);
if(Math.abs(v3) > max)
max = Math.abs(v3);
if(Math.abs(v4) > max)
max = Math.abs(v4);
if(max > 1)
{
v1 /= max;
v2 /= max;
v3 /= max;
v4 /= max;
}
double[] velocities = {v1, v2, v3, v4};
return velocities;
}
public void setMotorPower(double x, double y)
{
//calculate the velocities
double[] velocities = calcVelocities(x, y);
//set the motor power
robot.frontLeftMotor.setPower(velocities[0]);
robot.frontRightMotor.setPower(velocities[1]);
robot.rearLeftMotor.setPower(velocities[2]);
robot.rearRightMotor.setPower(velocities[3]);
}
//make another version of the method where you give it inches sideways and inches forward and it goes in a straight line there
public void DriveDirection(double speed, double angle, double inches, double timeoutS){ //0 Degrees is strafing directly to the right
robot.rearLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
idle();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
//sin(x−(1/4)π) - front right & back left (power, -1 to 1 where negative is back and positive is forward)
//sin(x+(1/4)π) - front left & back right
//double inchesfwd = inches*Math.sin(angle);
//int rearLeftTarget = robot.rearLeftMotor.getCurrentPosition() + (int) (inchesfwd * COUNTS_PER_INCH);
//int rearRightTarget = robot.rearRightMotor.getCurrentPosition() + (int) (inchesfwd * COUNTS_PER_INCH);
//int frontLeftTarget = robot.frontLeftMotor.getCurrentPosition() + (int) (inchesfwd * COUNTS_PER_INCH);
//int frontRightTarget = robot.frontRightMotor.getCurrentPosition() + (int) (inchesfwd * COUNTS_PER_INCH);
double jx = Math.cos(angle);
double jy = Math.sin(angle);
if (opModeIsActive())
{
/*rearLeftTarget *= Math.abs(rearLeftPower);
rearRightTarget *= Math.abs(rearRightPower);
frontLeftTarget *= Math.abs(frontLeftPower);
frontRightTarget *= Math.abs(frontRightPower);*/
// reset the timeout time and start motion.
// Turn On RUN_TO_POSITION
/*robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);*/
runtime.reset();
//public void setMotorPower(double x, double y)
setMotorPower(jx*speed,jy*speed);
// keep looping while we are still active, and there is time left, and both motors are running.
while(opModeIsActive() &&
(runtime.seconds() < timeoutS))
{
// Display it for the driver.
//telemetry.addData("Path1", "Running to %7d :%7d", newRearLeftTarget, newRearRightTarget, newFrontLeftTarget, newFrontRightTarget);
telemetry.addData("Path2", "Running at %7d :%7d",
robot.rearLeftMotor.getCurrentPosition(),
robot.rearRightMotor.getCurrentPosition(),
robot.frontLeftMotor.getCurrentPosition(),
robot.frontRightMotor.getCurrentPosition());
telemetry.update();
}
TurnOffMotors();
// Turn off RUN_TO_POSITION
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}
public void EncoderDriveTank(double speed, double leftInches, double rightInches, double timeoutS)
{
robot.rearLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
idle();
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
int newRearLeftTarget;
int newRearRightTarget;
int newFrontLeftTarget;
int newFrontRightTarget;
// Ensure that the opmode is still active
if (opModeIsActive())
{
// Determine new target position, and pass to motor controller
newRearLeftTarget = robot.rearLeftMotor.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
newRearRightTarget = robot.rearRightMotor.getCurrentPosition() + (int) (rightInches * COUNTS_PER_INCH);
newFrontLeftTarget = robot.frontLeftMotor.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
newFrontRightTarget = robot.frontRightMotor.getCurrentPosition() + (int) (rightInches * COUNTS_PER_INCH);
robot.rearLeftMotor.setTargetPosition(newRearLeftTarget);
robot.rearRightMotor.setTargetPosition(newRearRightTarget);
robot.frontLeftMotor.setTargetPosition(newFrontLeftTarget);
robot.frontRightMotor.setTargetPosition(newFrontRightTarget);
// Turn On RUN_TO_POSITION
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
robot.rearLeftMotor.setPower(Math.abs(speed));
robot.rearRightMotor.setPower(Math.abs(speed));
robot.frontLeftMotor.setPower(Math.abs(speed));
robot.frontRightMotor.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
while(opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(robot.rearLeftMotor.isBusy() && robot.rearRightMotor.isBusy() && robot.frontLeftMotor.isBusy() && robot.frontRightMotor.isBusy()))
{
// Display it for the driver.
telemetry.addData("Path1", "Running to %7d :%7d", newRearLeftTarget, newRearRightTarget, newFrontLeftTarget, newFrontRightTarget);
telemetry.addData("Path2", "Running at %7d :%7d",
robot.rearLeftMotor.getCurrentPosition(),
robot.rearRightMotor.getCurrentPosition(),
robot.frontLeftMotor.getCurrentPosition(),
robot.frontRightMotor.getCurrentPosition());
telemetry.update();
}
TurnOffMotors();
// Turn off RUN_TO_POSITION
robot.rearLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.rearRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
robot.frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}
public void TurnByGyro(String direction, int angle, double speed)
{
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
float startHeading = angles.firstAngle;
if(direction.equals("left"))
{
robot.rearRightMotor.setPower(speed);
robot.rearLeftMotor.setPower(-speed);
robot.frontRightMotor.setPower(speed);
robot.frontLeftMotor.setPower(-speed);
}
else if(direction.equals("right"))
{
robot.rearRightMotor.setPower(-speed);
robot.rearLeftMotor.setPower(speed);
robot.frontRightMotor.setPower(-speed);
robot.frontLeftMotor.setPower(speed);
}
while(opModeIsActive() && Math.abs(angles.firstAngle - startHeading) < angle)
{
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
telemetry.addData("heading", formatAngle(AngleUnit.DEGREES, angles.firstAngle));
telemetry.update();
}
TurnOffMotors();
}
String formatAngle(AngleUnit angleUnit, double angle)
{
return formatDegrees(AngleUnit.DEGREES.fromUnit(angleUnit, angle));
}
String formatDegrees(double degrees)
{
return String.format(Locale.getDefault(), "%.1f", AngleUnit.DEGREES.normalize(degrees));
}
public void TurnOffMotors()
{
robot.rearRightMotor.setPower(0);
robot.rearLeftMotor.setPower(0);
robot.frontRightMotor.setPower(0);
robot.frontLeftMotor.setPower(0);
}
}