-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoCorrectGyro.java
364 lines (308 loc) · 16.2 KB
/
AutoCorrectGyro.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
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="AutoCorrectGyro")
public class AutoCorrectGyro extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime();
RRBotHardware robot = new RRBotHardware();
private static double orientation = 0; //Orientation of 0 means that the front of the robot is facing the midline of the field (throught the neutral bridge)
private static double correctnessThreshold=0.2;
private static double maxTime = 5000; //Milliseconds allotted for angle autocorrect
private static float maxTurnPower = 1f;
private static float xPosition;
private static float yPosition;
private static long startMS;
//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);
orientation = 0; //Starting Orientation
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)
startMS = System.currentTimeMillis();
while (opModeIsActive()) {
DriveDirection(0.5,90,5);
}
}
public static float map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
public void updatePosition(){
}
public void goToPosition(){
}
public void AutoCorrect(){
double prevPowerRR = robot.rearRightMotor.getPower();
double prevPowerRL = robot.rearLeftMotor.getPower();
double prevPowerFR = robot.frontRightMotor.getPower();
double prevPowerFL = robot.frontLeftMotor.getPower();
int dirTurn = 1;
double traveled=0;
double leftmost=0;
double rightmost=0;
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
float startHeading = angles.firstAngle;
long doneTime = 0;
boolean timerStart=false;
long startTime = System.currentTimeMillis();
while((!timerStart || Math.abs(startHeading-orientation)>correctnessThreshold || System.currentTimeMillis()-doneTime<100) && System.currentTimeMillis()-startTime < maxTime && System.currentTimeMillis()-startMS<29500){
if(Math.abs(startHeading-orientation)<=correctnessThreshold && !timerStart){
doneTime=System.currentTimeMillis();
timerStart=true;
}else if(Math.abs(startHeading-orientation)>correctnessThreshold){
doneTime=0;
timerStart=false;
}
startHeading = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES).firstAngle;
if(startHeading>orientation && startHeading-orientation<180) dirTurn=-1;
else if(startHeading<orientation && orientation-startHeading>180) dirTurn=-1;
else dirTurn=1;
leftmost = startHeading>orientation ? startHeading : orientation;
rightmost = startHeading+orientation-leftmost;
if(leftmost<0 != rightmost<0){
traveled = leftmost+Math.abs(rightmost)<90 ? leftmost+Math.abs(rightmost) : (180-leftmost)+180-Math.abs(rightmost);
}else{
traveled = leftmost>0 ? leftmost-rightmost : Math.abs(rightmost-leftmost);
}
double turnPower = map((float)traveled, 0,30,maxTurnPower/10,maxTurnPower);
if(turnPower>maxTurnPower) turnPower=maxTurnPower;
robot.rearRightMotor.setPower(prevPowerRR+(turnPower*dirTurn));
robot.rearLeftMotor.setPower(prevPowerRL+(-turnPower*dirTurn));
robot.frontRightMotor.setPower(prevPowerFR+(turnPower*dirTurn));
robot.frontLeftMotor.setPower(prevPowerFL+(-turnPower*dirTurn));
}
robot.rearRightMotor.setPower(prevPowerRR);
robot.rearLeftMotor.setPower(prevPowerRL);
robot.frontRightMotor.setPower(prevPowerFR);
robot.frontLeftMotor.setPower(prevPowerFL);
}
public void DriveDirection(double speed, double angle, 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);
double jx = Math.cos(Math.toRadians(angle));
double jy = Math.sin(Math.toRadians(angle));
if (opModeIsActive())
{
runtime.reset();
drive.setMotorPower(jx*speed,jy*speed,0,0,true);
while(opModeIsActive() &&
(runtime.seconds() < timeoutS))
{
AutoCorrect();
}
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()))
{
AutoCorrect();
}
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 setAngle(double orientation){
this.orientation = orientation;
AutoCorrect();
}
public void turnAngle(String direction, double angle){
if(direction=="left") this.orientation += angle;
else if(direction=="right") this.orientation -= angle;
AutoCorrect();
}
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();
}
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);
}
}
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);
}
}