-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriveCommands.java
More file actions
389 lines (337 loc) · 15 KB
/
DriveCommands.java
File metadata and controls
389 lines (337 loc) · 15 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
// Copyright (c) 2025 FRC 6328
// http://github.com/Mechanical-Advantage
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file at
// the root directory of this project.
package frc.robot.commands;
import static frc.robot.subsystems.drive.DriveConstants.DRIVE_CONFIG;
import static frc.robot.subsystems.drive.DriveConstants.ROTATION_CONTROLLER_CONSTANTS;
import static frc.robot.subsystems.drive.DriveConstants.ROTATION_TOLERANCE;
import com.pathplanner.lib.auto.AutoBuilder;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import frc.robot.commands.controllers.SimpleDriveController;
import frc.robot.commands.controllers.SpeedLevelController;
import frc.robot.subsystems.drive.Drive;
import frc.robot.utility.AllianceFlipUtil;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
public class DriveCommands {
private static final double FF_START_DELAY_SECONDS = 2.0; // Secs
private static final double FF_RAMP_RATE = 0.1; // Volts/Sec
private static final double WHEEL_RADIUS_MAX_VELOCITY = 0.25; // Rad/Sec
private static final double WHEEL_RADIUS_RAMP_RATE = 0.05; // Rad/Sec^2
public static Command joystickDrive(
Drive drive,
Supplier<Translation2d> translationSupplier,
DoubleSupplier omegaSupplier,
Supplier<SpeedLevelController.SpeedLevel> speedLevelSupplier,
BooleanSupplier useFieldRelative) {
return drive
.run(
() -> {
Translation2d translation = translationSupplier.get();
double omega = omegaSupplier.getAsDouble();
ChassisSpeeds speeds =
SpeedLevelController.apply(
new ChassisSpeeds(translation.getX(), translation.getY(), omega),
speedLevelSupplier.get());
drive.setRobotSpeeds(speeds, useFieldRelative.getAsBoolean());
})
.finallyDo(drive::stop);
}
public static Command joystickHeadingDrive(
Drive drive,
Supplier<Translation2d> translationSupplier,
Supplier<Optional<Rotation2d>> headingSupplier,
Supplier<SpeedLevelController.SpeedLevel> speedLevelSupplier,
BooleanSupplier useFieldRelative) {
ProfiledPIDController controller =
new ProfiledPIDController(
ROTATION_CONTROLLER_CONSTANTS.kP(),
ROTATION_CONTROLLER_CONSTANTS.kI(),
ROTATION_CONTROLLER_CONSTANTS.kD(),
DRIVE_CONFIG.getAngularConstraints());
controller.setTolerance(ROTATION_TOLERANCE.getRadians());
controller.enableContinuousInput(0, Units.rotationsToRadians(1));
return drive
.run(
() -> {
Translation2d translation = translationSupplier.get();
Optional<Rotation2d> heading = headingSupplier.get();
if (heading.isPresent()) {
controller.setGoal(heading.get().getRadians());
}
double omega =
controller.calculate(
AllianceFlipUtil.apply(drive.getRobotPose().getRotation()).getRadians());
ChassisSpeeds speeds =
SpeedLevelController.apply(
new ChassisSpeeds(translation.getX(), translation.getY(), 0),
speedLevelSupplier.get());
if (!controller.atGoal()) {
speeds.omegaRadiansPerSecond = omega;
}
drive.setRobotSpeeds(speeds, useFieldRelative.getAsBoolean());
})
.beforeStarting(
() -> {
controller.setGoal(
AllianceFlipUtil.apply(drive.getRobotPose().getRotation()).getRadians());
controller.reset(
drive.getRobotPose().getRotation().getRadians(),
drive.getRobotSpeeds().omegaRadiansPerSecond);
})
.finallyDo(drive::stop);
}
public static Command joystickDriveWithSlowdown(
Drive drivetrain,
Supplier<Translation2d> translationSupplier,
DoubleSupplier omegaSupplier,
DoubleSupplier elevatorHeightSupplier,
BooleanSupplier useFieldRelativeSupplier) {
Runnable drive =
() -> {
Translation2d translation = translationSupplier.get();
double elevatorHeight = elevatorHeightSupplier.getAsDouble();
boolean fieldRelative = useFieldRelativeSupplier.getAsBoolean();
double multiplier = getMultiplier(elevatorHeight);
double dx = translation.getX() * multiplier;
double dy = translation.getY() * multiplier;
double omega = omegaSupplier.getAsDouble();
drivetrain.setRobotSpeeds(new ChassisSpeeds(dx, dy, omega), fieldRelative);
};
return drivetrain.run(drive).finallyDo(drivetrain::stop);
}
public static Command joystickHeadingDriveWithSlowdown(
Drive drivetrain,
Supplier<Translation2d> translationSupplier,
Supplier<Optional<Rotation2d>> headingSupplier,
DoubleSupplier elevatorHeightSupplier,
BooleanSupplier useFieldRelativeSupplier) {
ProfiledPIDController controller =
new ProfiledPIDController(
ROTATION_CONTROLLER_CONSTANTS.kP(),
ROTATION_CONTROLLER_CONSTANTS.kI(),
ROTATION_CONTROLLER_CONSTANTS.kD(),
DRIVE_CONFIG.getAngularConstraints());
controller.setTolerance(ROTATION_TOLERANCE.getRadians());
controller.enableContinuousInput(0, Units.rotationsToRadians(1));
Runnable setup =
() -> {
Rotation2d robotRotation = drivetrain.getRobotPose().getRotation();
double goal = AllianceFlipUtil.apply(robotRotation).getRadians();
controller.setGoal(goal);
controller.reset(
robotRotation.getRadians(), drivetrain.getRobotSpeeds().omegaRadiansPerSecond);
};
Runnable drive =
() -> {
Translation2d translation = translationSupplier.get();
Optional<Rotation2d> heading = headingSupplier.get();
if (heading.isPresent()) {
controller.setGoal(heading.get().getRadians());
}
double multiplier = getMultiplier(elevatorHeightSupplier.getAsDouble());
double controllerOutput =
controller.calculate(
AllianceFlipUtil.apply(drivetrain.getRobotPose().getRotation()).getRadians());
double dx = translation.getX() * multiplier;
double dy = translation.getY() * multiplier;
double omega = controller.atGoal() ? 0 : controllerOutput;
drivetrain.setRobotSpeeds(new ChassisSpeeds(dx, dy, omega));
};
return drivetrain.run(drive).beforeStarting(setup).finallyDo(drivetrain::stop);
}
/** Joystick drive */
public static Command joystickDriveSmartAngleLock(
Drive drive,
Supplier<Translation2d> translationSupplier,
DoubleSupplier omegaSupplier,
Supplier<SpeedLevelController.SpeedLevel> speedLevelSupplier,
BooleanSupplier useFieldRelative) {
return new SmartJoystickDriveAngleLock(
drive, translationSupplier, omegaSupplier, speedLevelSupplier, useFieldRelative);
}
/** Drive to a pose, more precise */
public static Command driveToPoseSimple(Drive drive, Pose2d desiredPose) {
SimpleDriveController controller = new SimpleDriveController();
return drive
.run(
() ->
drive.setRobotSpeeds(
controller.calculate(
drive.getRobotPose(), desiredPose, 0, desiredPose.getRotation())))
.until(controller::atReference)
.beforeStarting(
() -> {
controller.getXController().reset();
controller.getYController().reset();
controller
.getThetaController()
.reset(
drive.getRobotPose().getRotation().getRadians(),
drive.getRobotSpeeds().omegaRadiansPerSecond);
})
.finallyDo(drive::stop);
}
public static Command holdPositionCommand(Drive drive) {
SimpleDriveController controller = new SimpleDriveController();
return Commands.either(
drive.run(drive::stopUsingBrakeArrangement),
drive.run(() -> drive.setRobotSpeeds(controller.calculate(drive.getRobotPose()))),
controller::atReference)
.beforeStarting(
() -> {
controller.reset(drive.getRobotPose());
controller.setSetpoint(drive.getRobotPose());
})
.finallyDo(drive::stopUsingForwardArrangement);
}
/** Pathfind to a pose with pathplanner, only gets you roughly to target pose. */
public static Command pathfindToPoseCommand(
Drive drive, Pose2d desiredPose, double speedMultiplier, double goalEndVelocity) {
return AutoBuilder.pathfindToPose(
desiredPose, DRIVE_CONFIG.getPathConstraints(speedMultiplier), goalEndVelocity);
}
/** Estimated feed forward Ks and Kv by driving robot forward, control motors by voltage */
public static Command feedforwardCharacterization(Drive drive) {
List<Double> velocitySamples = new LinkedList<>();
List<Double> voltageSamples = new LinkedList<>();
Timer timer = new Timer();
return Commands.sequence(
// Reset data
Commands.runOnce(
() -> {
velocitySamples.clear();
voltageSamples.clear();
}),
// Allow modules to orient
Commands.run(
() -> {
drive.runCharacterization(0.0);
},
drive)
.withTimeout(FF_START_DELAY_SECONDS),
// Start timer
Commands.runOnce(timer::restart),
// Accelerate and gather data
Commands.run(
() -> {
double voltage = timer.get() * FF_RAMP_RATE;
drive.runCharacterization(voltage);
velocitySamples.add(drive.getFFCharacterizationVelocity());
voltageSamples.add(voltage);
},
drive)
// When cancelled, calculate and print results
.finallyDo(
() -> {
int n = velocitySamples.size();
double sumX = 0.0;
double sumY = 0.0;
double sumXY = 0.0;
double sumX2 = 0.0;
for (int i = 0; i < n; i++) {
sumX += velocitySamples.get(i);
sumY += voltageSamples.get(i);
sumXY += velocitySamples.get(i) * voltageSamples.get(i);
sumX2 += velocitySamples.get(i) * velocitySamples.get(i);
}
double kS = (sumY * sumX2 - sumX * sumXY) / (n * sumX2 - sumX * sumX);
double kV = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.println("********** Drive FF Characterization Results **********");
System.out.println("\tkS: " + formatter.format(kS));
System.out.println("\tkV: " + formatter.format(kV));
}));
}
/** Measures the robot's wheel radius by spinning in a circle. */
public static Command wheelRadiusCharacterization(Drive drive) {
SlewRateLimiter limiter = new SlewRateLimiter(WHEEL_RADIUS_RAMP_RATE);
WheelRadiusCharacterizationState state = new WheelRadiusCharacterizationState();
return Commands.parallel(
// Drive control sequence
Commands.sequence(
// Reset acceleration limiter
Commands.runOnce(
() -> {
limiter.reset(0.0);
}),
// Turn in place, accelerating up to full speed
Commands.run(
() -> {
double speed = limiter.calculate(WHEEL_RADIUS_MAX_VELOCITY);
drive.setRobotSpeeds(new ChassisSpeeds(0.0, 0.0, speed));
},
drive)),
// Measurement sequence
Commands.sequence(
// Wait for modules to fully orient before starting measurement
Commands.waitSeconds(1.0),
// Record starting measurement
Commands.runOnce(
() -> {
state.positions = drive.getWheelRadiusCharacterizationPositions();
state.lastAngle = drive.getRawGyroRotation();
state.gyroDelta = 0.0;
}),
// Update gyro delta
Commands.run(
() -> {
Rotation2d rotation = drive.getRawGyroRotation();
state.gyroDelta += Math.abs(rotation.minus(state.lastAngle).getRadians());
state.lastAngle = rotation;
})
// When cancelled, calculate and print results
.finallyDo(
() -> {
double[] positions = drive.getWheelRadiusCharacterizationPositions();
double wheelDelta = 0.0;
for (int i = 0; i < 4; i++) {
wheelDelta += Math.abs(positions[i] - state.positions[i]) / 4.0;
}
double wheelRadius =
(state.gyroDelta * DRIVE_CONFIG.driveBaseRadius()) / wheelDelta;
NumberFormat formatter = new DecimalFormat("#0.000");
System.out.println(
"********** Wheel Radius Characterization Results **********");
System.out.println(
"\tWheel Delta: " + formatter.format(wheelDelta) + " radians");
System.out.println(
"\tGyro Delta: " + formatter.format(state.gyroDelta) + " radians");
System.out.println(
"\tWheel Radius: "
+ formatter.format(wheelRadius)
+ " meters, "
+ formatter.format(Units.metersToInches(wheelRadius))
+ " inches");
})));
}
private static class WheelRadiusCharacterizationState {
double[] positions = new double[4];
Rotation2d lastAngle = new Rotation2d();
double gyroDelta = 0.0;
}
private static double getMultiplier(double elevatorHeight) {
return getMultiplier(elevatorHeight, 0.5);
}
private static double getMultiplier(double elevatorHeight, double multiplier) {
return 1 - (elevatorHeight * multiplier);
}
}