-
Notifications
You must be signed in to change notification settings - Fork 0
/
HardwareOmnibotDrive.java
296 lines (250 loc) · 9.97 KB
/
HardwareOmnibotDrive.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
package org.firstinspires.ftc.teamcode.Mapsprogram;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.hardware.bosch.JustLoggingAccelerationIntegrator;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;
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 static java.lang.Math.abs;
import static java.lang.Math.atan2;
import static java.lang.Math.cos;
import static java.lang.Math.max;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static java.lang.Math.toRadians;
/**
*Created by 12090 STEM Punk
*/
public class HardwareOmnibotDrive
{
/* Public OpMode members. */
public final static double MIN_SPIN_RATE = 0.05;
public final static double MIN_DRIVE_RATE = 0.05;
// Robot Controller Config Strings
public final static String IMU = "imu";
// public final static String FRONT_LEFT_MOTOR = "FrontLeft";
// public final static String FRONT_RIGHT_MOTOR = "FrontRight";
// public final static String REAR_LEFT_MOTOR = "RearLeft";
// public final static String REAR_RIGHT_MOTOR = "RearRight";
public final static String FRONT_LEFT_MOTOR = "l1";
public final static String FRONT_RIGHT_MOTOR = "r1";
public final static String REAR_LEFT_MOTOR = "l2";
public final static String REAR_RIGHT_MOTOR = "r2";
// Hardware objects
protected DcMotor frontLeft = null;
protected DcMotor frontRight = null;
protected DcMotor rearLeft = null;
protected DcMotor rearRight = null;
protected BNO055IMU imu = null;
// Tracking variables
private static final int encoderClicksPerSecond = 2800;
protected double frontLeftMotorPower = 0.0;
protected double rearLeftMotorPower = 0.0;
protected double frontRightMotorPower = 0.0;
protected double rearRightMotorPower = 0.0;
private boolean inputShaping = true;
protected boolean imuRead = false;
protected double imuValue = 0.0;
/* local OpMode members. */
protected HardwareMap hwMap = null;
/* Constructor */
public HardwareOmnibotDrive(){
}
public void setInputShaping(boolean inputShapingEnabled) {
inputShaping = inputShapingEnabled;
}
public void initIMU()
{
// Init IMU code
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
parameters.calibrationDataFile = "BNO055IMUCalibration.json";
parameters.loggingEnabled = true;
parameters.loggingTag = "IMU";
parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
imu = hwMap.get(BNO055IMU.class, IMU);
imu.initialize(parameters);
}
public void resetReads() {
imuRead = false;
}
public double readIMU()
{
if(!imuRead) {
// Read IMU Code
Orientation angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
imuValue = (double)angles.firstAngle;
imuRead = true;
}
return imuValue;
}
public void setFrontLeftMotorPower(double power)
{
if(power != frontLeftMotorPower)
{
frontLeftMotorPower = power;
frontLeft.setPower(power);
}
}
public void setRearLeftMotorPower(double power)
{
if(power != rearLeftMotorPower)
{
rearLeftMotorPower = power;
rearLeft.setPower(power);
}
}
public void setFrontRightMotorPower(double power)
{
if(power != frontRightMotorPower)
{
frontRightMotorPower = power;
frontRight.setPower(power);
}
}
public void setRearRightMotorPower(double power)
{
if(power != rearRightMotorPower)
{
rearRightMotorPower = power;
rearRight.setPower(power);
}
}
public void setAllDrive(double power) {
setFrontLeftMotorPower(power);
setFrontRightMotorPower(power);
setRearRightMotorPower(power);
setRearLeftMotorPower(power);
}
public void setAllDriveZero()
{
setAllDrive(0.0);
}
/**
*
* @param xPower - -1.0 to 1.0 power in the X axis
* @param yPower - -1.0 to 1.0 power in the Y axis
* @param spin - -1.0 to 1.0 power to rotate the robot
* @param angleOffset - The offset from the gyro to run at, such as drive compensation
*/
public void drive(double xPower, double yPower, double spin, double angleOffset) {
double gyroAngle = readIMU() + angleOffset;
double leftFrontAngle = toRadians(45.0 + gyroAngle);
double rightFrontAngle = toRadians(-45.0 + gyroAngle);
double leftRearAngle = toRadians(135.0 + gyroAngle);
double rightRearAngle = toRadians(-135.0 + gyroAngle);
double joystickMagnitude = sqrt(xPower*xPower + yPower*yPower);
double joystickAngle = atan2(yPower, xPower);
double newPower = driverInputShaping(joystickMagnitude);
double newSpin = driverInputSpinShaping(spin);
double newXPower = newPower * cos(joystickAngle);
double newYPower = newPower * sin(joystickAngle);
double LFpower = newXPower * cos(leftFrontAngle) + newYPower * sin(leftFrontAngle) + newSpin;
double LRpower = newXPower * cos(leftRearAngle) + newYPower * sin(leftRearAngle) + newSpin;
double RFpower = newXPower * cos(rightFrontAngle) + newYPower * sin(rightFrontAngle) + newSpin;
double RRpower = newXPower * cos(rightRearAngle) + newYPower * sin(rightRearAngle) + newSpin;
double maxPower = max(1.0, max(max(abs(LFpower), abs(LRpower)),
max(abs(RFpower), abs(RRpower))));
if(maxPower > 1.0) {
LFpower /= maxPower;
RFpower /= maxPower;
RFpower /= maxPower;
RRpower /= maxPower;
}
setFrontLeftMotorPower(LFpower);
setFrontRightMotorPower(RFpower);
setRearRightMotorPower(RRpower);
setRearLeftMotorPower(LRpower);
}
protected double driverInputShaping( double valueIn) {
double valueOut = 0.0;
if(Math.abs(valueIn) < MIN_DRIVE_RATE) {
valueOut = 0.0;
} else {
if (inputShaping) {
if (valueIn > 0) {
valueOut = MIN_DRIVE_RATE + (1.0 - MIN_DRIVE_RATE) * valueIn;
} else {
valueOut = -MIN_DRIVE_RATE + (1.0 - MIN_DRIVE_RATE) * valueIn;
}
} else {
valueOut = valueIn;
}
}
return valueOut;
}
protected double driverInputSpinShaping( double valueIn) {
double valueOut = 0.0;
if(Math.abs(valueIn) < MIN_SPIN_RATE) {
valueOut = 0.0;
} else {
if (inputShaping) {
if (valueIn > 0) {
valueOut = (1.0 + MIN_SPIN_RATE) * valueIn - MIN_SPIN_RATE;
} else {
valueOut = (1.0 + MIN_SPIN_RATE) * valueIn + MIN_SPIN_RATE;
}
} else {
valueOut = valueIn;
}
}
return valueOut;
}
public void disableDriveEncoders()
{
frontLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
rearLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
rearRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
}
public void resetDriveEncoders()
{
int sleepTime = 0;
int encoderCount = frontLeft.getCurrentPosition();
frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rearLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rearRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
while((encoderCount != 0) && (sleepTime < 1000)) {
try {
Thread.sleep(10);
} catch (InterruptedException e) { break; }
sleepTime += 10;
encoderCount = frontLeft.getCurrentPosition();
}
frontLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rearLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rearRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Set the stop mode
frontLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
frontRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
rearLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
rearRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
}
/* Initialize standard Hardware interfaces */
public void init(HardwareMap ahwMap) {
// Save reference to Hardware map
hwMap = ahwMap;
// Define and Initialize Motors
frontLeft = hwMap.dcMotor.get(FRONT_LEFT_MOTOR);
frontRight = hwMap.dcMotor.get(FRONT_RIGHT_MOTOR);
rearLeft = hwMap.dcMotor.get(REAR_LEFT_MOTOR);
rearRight = hwMap.dcMotor.get(REAR_RIGHT_MOTOR);
frontLeft.setDirection(DcMotor.Direction.FORWARD);
frontRight.setDirection(DcMotor.Direction.FORWARD);
rearLeft.setDirection(DcMotor.Direction.FORWARD);
rearRight.setDirection(DcMotor.Direction.FORWARD);
// Set all motors to zero power
setAllDriveZero();
frontLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
rearLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
rearRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
resetDriveEncoders();
initIMU();
}
}