-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSimplifiedOdometryRobot.java
More file actions
452 lines (381 loc) · 19.5 KB
/
SimplifiedOdometryRobot.java
File metadata and controls
452 lines (381 loc) · 19.5 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* Created by Phil Malone. 2023.
This class illustrates my simplified Odometry Strategy.
It implements basic straight line motions but with heading and drift controls to limit drift.
See the readme for a link to a video tutorial explaining the operation and limitations of the code.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.lynx.LynxModule;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.IMU;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AngularVelocity;
import org.firstinspires.ftc.robotcore.external.navigation.YawPitchRollAngles;
import java.util.List;
public class SimplifiedOdometryRobot {
// Adjust these numbers to suit your robot.
private final double ODOM_INCHES_PER_COUNT = 0.002969; // GoBilda Odometry Pod (1/226.8)
private final boolean INVERT_DRIVE_ODOMETRY = true; // When driving FORWARD, the odometry value MUST increase. If it does not, flip the value of this constant.
private final boolean INVERT_STRAFE_ODOMETRY = true; // When strafing to the LEFT, the odometry value MUST increase. If it does not, flip the value of this constant.
private static final double DRIVE_GAIN = 0.03; // Strength of axial position control
private static final double DRIVE_ACCEL = 2.0; // Acceleration limit. Percent Power change per second. 1.0 = 0-100% power in 1 sec.
private static final double DRIVE_TOLERANCE = 0.5; // Controller is is "inPosition" if position error is < +/- this amount
private static final double DRIVE_DEADBAND = 0.2; // Error less than this causes zero output. Must be smaller than DRIVE_TOLERANCE
private static final double DRIVE_MAX_AUTO = 0.6; // "default" Maximum Axial power limit during autonomous
private static final double STRAFE_GAIN = 0.03; // Strength of lateral position control
private static final double STRAFE_ACCEL = 1.5; // Acceleration limit. Percent Power change per second. 1.0 = 0-100% power in 1 sec.
private static final double STRAFE_TOLERANCE = 0.5; // Controller is is "inPosition" if position error is < +/- this amount
private static final double STRAFE_DEADBAND = 0.2; // Error less than this causes zero output. Must be smaller than DRIVE_TOLERANCE
private static final double STRAFE_MAX_AUTO = 0.6; // "default" Maximum Lateral power limit during autonomous
private static final double YAW_GAIN = 0.018; // Strength of Yaw position control
private static final double YAW_ACCEL = 3.0; // Acceleration limit. Percent Power change per second. 1.0 = 0-100% power in 1 sec.
private static final double YAW_TOLERANCE = 1.0; // Controller is is "inPosition" if position error is < +/- this amount
private static final double YAW_DEADBAND = 0.25; // Error less than this causes zero output. Must be smaller than DRIVE_TOLERANCE
private static final double YAW_MAX_AUTO = 0.6; // "default" Maximum Yaw power limit during autonomous
// Public Members
public double driveDistance = 0; // scaled axial distance (+ = forward)
public double strafeDistance = 0; // scaled lateral distance (+ = left)
public double heading = 0; // Latest Robot heading from IMU
// Establish a proportional controller for each axis to calculate the required power to achieve a setpoint.
public ProportionalControl driveController = new ProportionalControl(DRIVE_GAIN, DRIVE_ACCEL, DRIVE_MAX_AUTO, DRIVE_TOLERANCE, DRIVE_DEADBAND, false);
public ProportionalControl strafeController = new ProportionalControl(STRAFE_GAIN, STRAFE_ACCEL, STRAFE_MAX_AUTO, STRAFE_TOLERANCE, STRAFE_DEADBAND, false);
public ProportionalControl yawController = new ProportionalControl(YAW_GAIN, YAW_ACCEL, YAW_MAX_AUTO, YAW_TOLERANCE,YAW_DEADBAND, true);
// --- Private Members
// Hardware interface Objects
private DcMotor leftFrontDrive; // control the left front drive wheel
private DcMotor rightFrontDrive; // control the right front drive wheel
private DcMotor leftBackDrive; // control the left back drive wheel
private DcMotor rightBackDrive; // control the right back drive wheel
private DcMotor driveEncoder; // the Axial (front/back) Odometry Module (may overlap with motor, or may not)
private DcMotor strafeEncoder; // the Lateral (left/right) Odometry Module (may overlap with motor, or may not)
private LinearOpMode myOpMode;
private IMU imu;
private ElapsedTime holdTimer = new ElapsedTime(); // User for any motion requiring a hold time or timeout.
private int rawDriveOdometer = 0; // Unmodified axial odometer count
private int driveOdometerOffset = 0; // Used to offset axial odometer
private int rawStrafeOdometer = 0; // Unmodified lateral odometer count
private int strafeOdometerOffset= 0; // Used to offset lateral odometer
private double rawHeading = 0; // Unmodified heading (degrees)
private double headingOffset = 0; // Used to offset heading
private double turnRate = 0; // Latest Robot Turn Rate from IMU
private boolean showTelemetry = false;
// Robot Constructor
public SimplifiedOdometryRobot(LinearOpMode opmode) {
myOpMode = opmode;
}
/**
* Robot Initialization:
* Use the hardware map to Connect to devices.
* Perform any set-up all the hardware devices.
* @param showTelemetry Set to true if you want telemetry to be displayed by the robot sensor/drive functions.
*/
public void initialize(boolean showTelemetry)
{
// Initialize the hardware variables. Note that the strings used to 'get' each
// motor/device must match the names assigned during the robot configuration.
// !!! Set the drive direction to ensure positive power drives each wheel forward.
leftFrontDrive = setupDriveMotor("leftfront_drive", DcMotor.Direction.REVERSE);
rightFrontDrive = setupDriveMotor("rightfront_drive", DcMotor.Direction.FORWARD);
leftBackDrive = setupDriveMotor( "leftback_drive", DcMotor.Direction.REVERSE);
rightBackDrive = setupDriveMotor( "rightback_drive",DcMotor.Direction.FORWARD);
imu = myOpMode.hardwareMap.get(IMU.class, "imu");
// Connect to the encoder channels using the name of that channel.
driveEncoder = myOpMode.hardwareMap.get(DcMotor.class, "axial");
strafeEncoder = myOpMode.hardwareMap.get(DcMotor.class, "lateral");
// Set all hubs to use the AUTO Bulk Caching mode for faster encoder reads
List<LynxModule> allHubs = myOpMode.hardwareMap.getAll(LynxModule.class);
for (LynxModule module : allHubs) {
module.setBulkCachingMode(LynxModule.BulkCachingMode.AUTO);
}
// Tell the software how the Control Hub is mounted on the robot to align the IMU XYZ axes correctly
RevHubOrientationOnRobot orientationOnRobot =
new RevHubOrientationOnRobot(RevHubOrientationOnRobot.LogoFacingDirection.UP,
RevHubOrientationOnRobot.UsbFacingDirection.FORWARD);
imu.initialize(new IMU.Parameters(orientationOnRobot));
// zero out all the odometry readings.
resetOdometry();
// Set the desired telemetry state
this.showTelemetry = showTelemetry;
}
/**
* Setup a drive motor with passed parameters. Ensure encoder is reset.
* @param deviceName Text name associated with motor in Robot Configuration
* @param direction Desired direction to make the wheel run FORWARD with positive power input
* @return the DcMotor object
*/
private DcMotor setupDriveMotor(String deviceName, DcMotor.Direction direction) {
DcMotor aMotor = myOpMode.hardwareMap.get(DcMotor.class, deviceName);
aMotor.setDirection(direction);
aMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); // Reset Encoders to zero
aMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
aMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); // Requires motor encoder cables to be hooked up.
return aMotor;
}
/**
* Read all input devices to determine the robot's motion
* always return true so this can be used in "while" loop conditions
* @return true
*/
public boolean readSensors() {
rawDriveOdometer = driveEncoder.getCurrentPosition() * (INVERT_DRIVE_ODOMETRY ? -1 : 1);
rawStrafeOdometer = strafeEncoder.getCurrentPosition() * (INVERT_STRAFE_ODOMETRY ? -1 : 1);
driveDistance = (rawDriveOdometer - driveOdometerOffset) * ODOM_INCHES_PER_COUNT;
strafeDistance = (rawStrafeOdometer - strafeOdometerOffset) * ODOM_INCHES_PER_COUNT;
YawPitchRollAngles orientation = imu.getRobotYawPitchRollAngles();
AngularVelocity angularVelocity = imu.getRobotAngularVelocity(AngleUnit.DEGREES);
rawHeading = orientation.getYaw(AngleUnit.DEGREES);
heading = rawHeading - headingOffset;
turnRate = angularVelocity.zRotationRate;
if (showTelemetry) {
myOpMode.telemetry.addData("Odom Ax:Lat", "%6d %6d", rawDriveOdometer - driveOdometerOffset, rawStrafeOdometer - strafeOdometerOffset);
myOpMode.telemetry.addData("Dist Ax:Lat", "%5.2f %5.2f", driveDistance, strafeDistance);
myOpMode.telemetry.addData("Head Deg:Rate", "%5.2f %5.2f", heading, turnRate);
}
return true; // do this so this function can be included in the condition for a while loop to keep values fresh.
}
// ######################## Mid level control functions. #############################3#
/**
* Drive in the axial (forward/reverse) direction, maintain the current heading and don't drift sideways
* @param distanceInches Distance to travel. +ve = forward, -ve = reverse.
* @param power Maximum power to apply. This number should always be positive.
* @param holdTime Minimum time (sec) required to hold the final position. 0 = no hold.
*/
public void drive(double distanceInches, double power, double holdTime) {
resetOdometry();
driveController.reset(distanceInches, power); // achieve desired drive distance
strafeController.reset(0); // Maintain zero strafe drift
yawController.reset(); // Maintain last turn heading
holdTimer.reset();
while (myOpMode.opModeIsActive() && readSensors()){
// implement desired axis powers
moveRobot(driveController.getOutput(driveDistance), strafeController.getOutput(strafeDistance), yawController.getOutput(heading));
// Time to exit?
if (driveController.inPosition() && yawController.inPosition()) {
if (holdTimer.time() > holdTime) {
break; // Exit loop if we are in position, and have been there long enough.
}
} else {
holdTimer.reset();
}
myOpMode.sleep(10);
}
stopRobot();
}
/**
* Strafe in the lateral (left/right) direction, maintain the current heading and don't drift fwd/bwd
* @param distanceInches Distance to travel. +ve = left, -ve = right.
* @param power Maximum power to apply. This number should always be positive.
* @param holdTime Minimum time (sec) required to hold the final position. 0 = no hold.
*/
public void strafe(double distanceInches, double power, double holdTime) {
resetOdometry();
driveController.reset(0.0); // Maintain zero drive drift
strafeController.reset(distanceInches, power); // Achieve desired Strafe distance
yawController.reset(); // Maintain last turn angle
holdTimer.reset();
while (myOpMode.opModeIsActive() && readSensors()){
// implement desired axis powers
moveRobot(driveController.getOutput(driveDistance), strafeController.getOutput(strafeDistance), yawController.getOutput(heading));
// Time to exit?
if (strafeController.inPosition() && yawController.inPosition()) {
if (holdTimer.time() > holdTime) {
break; // Exit loop if we are in position, and have been there long enough.
}
} else {
holdTimer.reset();
}
myOpMode.sleep(10);
}
stopRobot();
}
/**
* Rotate to an absolute heading/direction
* @param headingDeg Heading to obtain. +ve = CCW, -ve = CW.
* @param power Maximum power to apply. This number should always be positive.
* @param holdTime Minimum time (sec) required to hold the final position. 0 = no hold.
*/
public void turnTo(double headingDeg, double power, double holdTime) {
yawController.reset(headingDeg, power);
while (myOpMode.opModeIsActive() && readSensors()) {
// implement desired axis powers
moveRobot(0, 0, yawController.getOutput(heading));
// Time to exit?
if (yawController.inPosition()) {
if (holdTimer.time() > holdTime) {
break; // Exit loop if we are in position, and have been there long enough.
}
} else {
holdTimer.reset();
}
myOpMode.sleep(10);
}
stopRobot();
}
// ######################## Low level control functions. ###############################
/**
* Drive the wheel motors to obtain the requested axes motions
* @param drive Fwd/Rev axis power
* @param strafe Left/Right axis power
* @param yaw Yaw axis power
*/
public void moveRobot(double drive, double strafe, double yaw){
double lF = drive - strafe - yaw;
double rF = drive + strafe + yaw;
double lB = drive + strafe - yaw;
double rB = drive - strafe + yaw;
double max = Math.max(Math.abs(lF), Math.abs(rF));
max = Math.max(max, Math.abs(lB));
max = Math.max(max, Math.abs(rB));
//normalize the motor values
if (max > 1.0) {
lF /= max;
rF /= max;
lB /= max;
rB /= max;
}
//send power to the motors
leftFrontDrive.setPower(lF);
rightFrontDrive.setPower(rF);
leftBackDrive.setPower(lB);
rightBackDrive.setPower(rB);
if (showTelemetry) {
myOpMode.telemetry.addData("Axes D:S:Y", "%5.2f %5.2f %5.2f", drive, strafe, yaw);
myOpMode.telemetry.addData("Wheels lf:rf:lb:rb", "%5.2f %5.2f %5.2f %5.2f", lF, rF, lB, rB);
myOpMode.telemetry.update(); // Assume this is the last thing done in the loop.
}
}
/**
* Stop all motors.
*/
public void stopRobot() {
moveRobot(0,0,0);
}
/**
* Set odometry counts and distances to zero.
*/
public void resetOdometry() {
readSensors();
driveOdometerOffset = rawDriveOdometer;
driveDistance = 0.0;
driveController.reset(0);
strafeOdometerOffset = rawStrafeOdometer;
strafeDistance = 0.0;
strafeController.reset(0);
}
/**
* Reset the robot heading to zero degrees, and also lock that heading into heading controller.
*/
public void resetHeading() {
readSensors();
headingOffset = rawHeading;
yawController.reset(0);
heading = 0;
}
public double getHeading() {return heading;}
public double getTurnRate() {return turnRate;}
/**
* Set the drive telemetry on or off
*/
public void showTelemetry(boolean show){
showTelemetry = show;
}
}
//****************************************************************************************************
//****************************************************************************************************
/***
* This class is used to implement a proportional controller which can calculate the desired output power
* to get an axis to the desired setpoint value.
* It also implements an acceleration limit, and a max power output.
*/
class ProportionalControl {
double lastOutput;
double gain;
double accelLimit;
double defaultOutputLimit;
double liveOutputLimit;
double setPoint;
double tolerance;
double deadband;
boolean circular;
boolean inPosition;
ElapsedTime cycleTime = new ElapsedTime();
public ProportionalControl(double gain, double accelLimit, double outputLimit, double tolerance, double deadband, boolean circular) {
this.gain = gain;
this.accelLimit = accelLimit;
this.defaultOutputLimit = outputLimit;
this.liveOutputLimit = outputLimit;
this.tolerance = tolerance;
this.deadband = deadband;
this.circular = circular;
reset(0.0);
}
/**
* Determines power required to obtain the desired setpoint value based on new input value.
* Uses proportional gain, and limits rate of change of output, as well as max output.
* @param input Current live control input value (from sensors)
* @return desired output power.
*/
public double getOutput(double input) {
double error = setPoint - input;
double dV = cycleTime.seconds() * accelLimit;
double output;
// normalize to +/- 180 if we are controlling heading
if (circular) {
while (error > 180) error -= 360;
while (error <= -180) error += 360;
}
inPosition = (Math.abs(error) < tolerance);
// Prevent any very slow motor output accumulation
if (Math.abs(error) <= deadband) {
output = 0;
} else {
// calculate output power using gain and clip it to the limits
output = (error * gain);
output = Range.clip(output, -liveOutputLimit, liveOutputLimit);
// Now limit rate of change of output (acceleration)
if ((output - lastOutput) > dV) {
output = lastOutput + dV;
} else if ((output - lastOutput) < -dV) {
output = lastOutput - dV;
}
}
lastOutput = output;
cycleTime.reset();
return output;
}
public boolean inPosition(){
return inPosition;
}
public double getSetpoint() {return setPoint;}
/**
* Saves a new setpoint and resets the output power history.
* This call allows a temporary power limit to be set to override the default.
* @param setPoint
* @param powerLimit
*/
public void reset(double setPoint, double powerLimit) {
liveOutputLimit = Math.abs(powerLimit);
this.setPoint = setPoint;
reset();
}
/**
* Saves a new setpoint and resets the output power history.
* @param setPoint
*/
public void reset(double setPoint) {
liveOutputLimit = defaultOutputLimit;
this.setPoint = setPoint;
reset();
}
/**
* Leave everything else the same, Just restart the acceleration timer and set output to 0
*/
public void reset() {
cycleTime.reset();
inPosition = false;
lastOutput = 0.0;
}
}