-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSkeletonEncoderGyro.java
More file actions
335 lines (273 loc) · 13.4 KB
/
AutoSkeletonEncoderGyro.java
File metadata and controls
335 lines (273 loc) · 13.4 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
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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.hardware.bosch.JustLoggingAccelerationIntegrator;
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.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import org.firstinspires.ftc.robotcore.external.ClassFactory;
import org.firstinspires.ftc.robotcore.external.Func;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.robotcore.external.navigation.Acceleration;
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 org.firstinspires.ftc.robotcore.external.navigation.Position;
import org.firstinspires.ftc.robotcore.external.navigation.Velocity;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
import java.util.List;
import java.util.Locale;
//All of the imports we need
@Autonomous(name = "AutoSkeletonEncoderGyro", group = "SkeletonCode") //Establishes name and group
@Disabled //Stops this from appearing, just comment it out to include
public class AutoSkeletonEncoderGyro extends LinearOpMode{
DcMotor FrontLeft;
DcMotor FrontRight;
DcMotor BackLeft;
DcMotor BackRight;
//Declares our drivetrain motors, make sure to add any extra motors used
BNO055IMU imu;
Orientation angles;
Acceleration gravity;
//Things for gyro turns, dont mess with
@Override
public void runOpMode() throws InterruptedException {
FrontLeft = hardwareMap.dcMotor.get("Front Left");
FrontRight = hardwareMap.dcMotor.get("Front Right");
BackLeft = hardwareMap.dcMotor.get("Back Left");
BackRight = hardwareMap.dcMotor.get("Back Right");
//Declares Hardwaremap stuff, make sure to add for extra motors
FrontLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
FrontRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
BackLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
BackRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
//Sets up Encoder usage, make sure to add for extra motors
BackRight.setDirection(DcMotorSimple.Direction.REVERSE);
FrontLeft.setDirection(DcMotorSimple.Direction.REVERSE);
//Correcting the direction of drivetrain motors, if auto doesnt move right check this first
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
parameters.calibrationDataFile = "BNO055IMUCalibration.json"; // see the calibration sample opmode
parameters.loggingEnabled = true;
parameters.loggingTag = "IMU";
parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
//Sets up Gyro stuff
imu = hardwareMap.get(BNO055IMU.class, "imu");
imu.initialize(parameters);
composeTelemetry();
telemetry.addData(">", "Press Play to start op mode");
telemetry.update();
//Finished Initialization
waitForStart();
/*
Here is where your auto code goes, use moveForward(inches), moveBackward(inches),
and turn(degrees) along with whatever other functions you add.
NOTES FOR GYRO TURN:
1)The code is set up for a control hub mounted horizontally
2)THE TURN ANGLES ARE GLOBALLY BASED
3)A counterclockwise turn is positive, clockwise is negative
4)Sometimes any variation of 0, 180, or 360 can break the turn, so try
to avoid those values
*/
}
double acceptableErrorMargin = 1;
public void moveBackward (double distance){ //537.7 PPR
FrontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
FrontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
BackLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
BackRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
double circumference = 3.14 * 5; //pi times wheel diameter
double rotationsneeded = distance / circumference; //length(in) divided by circumference
int encoderdrivingtarget = (int) (rotationsneeded * 538); //rotations needed times motor ticks per revolution
FrontLeft.setTargetPosition(-encoderdrivingtarget);
FrontRight.setTargetPosition(-encoderdrivingtarget);
BackLeft.setTargetPosition(-encoderdrivingtarget);
BackRight.setTargetPosition(-encoderdrivingtarget);
FrontLeft.setPower(.4);
FrontRight.setPower(.4);
BackLeft.setPower(.4);
BackRight.setPower(.4);
FrontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
FrontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
BackLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
BackRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
while(FrontRight.isBusy() || FrontLeft.isBusy()) {
}
FrontLeft.setPower(0);
FrontRight.setPower(0);
BackLeft.setPower(0);
BackRight.setPower(0);
//This function takes the target distance and uses math to convert to encoder ticks
}
public void moveForward (double distance) { //537.7 PPR
FrontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
FrontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
BackLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
BackRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
double circumference = 3.14 * 5; //pi times wheel diameter
double rotationsneeded = distance / circumference; //length(in) divided by circumference
int encoderdrivingtarget = (int) (rotationsneeded * 538); //rotations needed times motor ticks per revolution
FrontLeft.setTargetPosition(encoderdrivingtarget);
FrontRight.setTargetPosition(encoderdrivingtarget);
BackLeft.setTargetPosition(encoderdrivingtarget);
BackRight.setTargetPosition(encoderdrivingtarget);
FrontLeft.setPower(.4);
FrontRight.setPower(.4);
BackLeft.setPower(.4);
BackRight.setPower(.4);
FrontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
FrontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
BackLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
BackRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
while(FrontRight.isBusy() || FrontLeft.isBusy()) {
}
FrontLeft.setPower(0);
FrontRight.setPower(0);
BackLeft.setPower(0);
BackRight.setPower(0);
//This function takes the target distance and uses math to convert to encoder ticks
}
void turn(double target) {
//SETS MOTORS TO BE ABLE TO RUN WITHOUT ENCODER INPUTS
FrontRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
FrontLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
BackRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
BackLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
//TAKES INTIIAL ANGLE GROUP AND SETS ORIGIN TO THE Z ANGLE
angles = imu.getAngularOrientation(AxesReference.EXTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
double currentPos = angles.firstAngle;
boolean turnRight;
while (!(calculateError(target, currentPos) < acceptableErrorMargin)) {
angles = imu.getAngularOrientation(AxesReference.EXTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
currentPos = angles.firstAngle;
turnRight = directionCheck(target, currentPos);
twiddle(target, currentPos, turnRight);
}
}
void twiddle(double target, double origin, boolean turnRight) {
double currentPos = origin;
double turnPower = /*basePower * ((calculateError(target, currentPos))/30) + basePower; */ 1;
FrontLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
FrontRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
BackLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
BackRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
int i = 0;
if (turnRight) {
FrontLeft.setPower(turnPower);
FrontRight.setPower(-turnPower);
BackLeft.setPower(turnPower);
BackRight.setPower(-turnPower);
while (!(currentPos > target) && i % 5 == 0) {
angles = imu.getAngularOrientation(AxesReference.EXTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
currentPos = angles.firstAngle;
i++;
}
FrontLeft.setPower(0);
FrontRight.setPower(0);
BackLeft.setPower(0);
BackRight.setPower(0);
}
if (!turnRight) {
FrontLeft.setPower(-turnPower);
FrontRight.setPower(turnPower);
BackLeft.setPower(-turnPower);
BackRight.setPower(turnPower);
while (!(currentPos < target) && i % 5 == 0) {
angles = imu.getAngularOrientation(AxesReference.EXTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
currentPos = angles.firstAngle;
i++;
}
FrontLeft.setPower(0);
FrontRight.setPower(0);
BackLeft.setPower(0);
BackRight.setPower(0);
}
}
double calculateError(double target, double origin) {
if (target > origin && origin >= 0) {
return target - origin;
}
if (target > origin && origin <= 0) {
return target + Math.abs(origin);
}
if (origin > target && target >= 0) {
return origin - target;
}
if (origin > target && target <= 0) {
return origin + Math.abs(target);
}
return 0;
}
boolean directionCheck(double target, double origin) {
if (target > origin) {
return false;
}
return true;
}
void composeTelemetry() {
// At the beginning of each telemetry update, grab a bunch of data
// from the IMU that we will then display in separate lines.
telemetry.addAction(new Runnable() { @Override public void run()
{
// Acquiring the angles is relatively expensive; we don't want
// to do that in each of the three items that need that info, as that's
// three times the necessary expense.
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
gravity = imu.getGravity();
}
});
telemetry.addLine()
.addData("status", new Func<String>() {
@Override public String value() {
return imu.getSystemStatus().toShortString();
}
})
.addData("calib", new Func<String>() {
@Override public String value() {
return imu.getCalibrationStatus().toString();
}
});
telemetry.addLine()
.addData("heading", new Func<String>() {
@Override public String value() {
return formatAngle(angles.angleUnit, angles.firstAngle);
}
})
.addData("roll", new Func<String>() {
@Override public String value() {
return formatAngle(angles.angleUnit, angles.secondAngle);
}
})
.addData("pitch", new Func<String>() {
@Override public String value() {
return formatAngle(angles.angleUnit, angles.thirdAngle);
}
});
telemetry.addLine()
.addData("grvty", new Func<String>() {
@Override public String value() {
return gravity.toString();
}
})
.addData("mag", new Func<String>() {
@Override public String value() {
return String.format(Locale.getDefault(), "%.3f",
Math.sqrt(gravity.xAccel*gravity.xAccel
+ gravity.yAccel*gravity.yAccel
+ gravity.zAccel*gravity.zAccel));
}
});
}
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));
}
}