-
Notifications
You must be signed in to change notification settings - Fork 35
/
Drive.java
executable file
·546 lines (462 loc) · 21.6 KB
/
Drive.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package com.team254.frc2018.subsystems;
import com.ctre.phoenix.ErrorCode;
import com.ctre.phoenix.motorcontrol.*;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import com.ctre.phoenix.sensors.PigeonIMU;
import com.team254.frc2018.Constants;
import com.team254.frc2018.RobotState;
import com.team254.frc2018.loops.ILooper;
import com.team254.frc2018.loops.Loop;
import com.team254.frc2018.planners.DriveMotionPlanner;
import com.team254.lib.drivers.TalonSRXChecker;
import com.team254.lib.drivers.TalonSRXFactory;
import com.team254.lib.geometry.Pose2d;
import com.team254.lib.geometry.Pose2dWithCurvature;
import com.team254.lib.geometry.Rotation2d;
import com.team254.lib.trajectory.TrajectoryIterator;
import com.team254.lib.trajectory.timing.TimedState;
import com.team254.lib.util.DriveSignal;
import com.team254.lib.util.ReflectingCSVWriter;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.util.ArrayList;
public class Drive extends Subsystem {
private static final int kLowGearVelocityControlSlot = 0;
private static final int kHighGearVelocityControlSlot = 1;
private static final double DRIVE_ENCODER_PPR = 4096.;
private static Drive mInstance = new Drive();
// Hardware
private final TalonSRX mLeftMaster, mRightMaster, mLeftSlaveA, mRightSlaveA, mLeftSlaveB, mRightSlaveB;
private final Solenoid mShifter;
// Control states
private DriveControlState mDriveControlState;
private PigeonIMU mPigeon;
// Hardware states
private PeriodicIO mPeriodicIO;
private boolean mAutoShift;
private boolean mIsHighGear;
private boolean mIsBrakeMode;
private ReflectingCSVWriter<PeriodicIO> mCSVWriter = null;
private DriveMotionPlanner mMotionPlanner;
private Rotation2d mGyroOffset = Rotation2d.identity();
private boolean mOverrideTrajectory = false;
private final Loop mLoop = new Loop() {
@Override
public void onStart(double timestamp) {
synchronized (Drive.this) {
setOpenLoop(new DriveSignal(0.05, 0.05));
setBrakeMode(false);
// startLogging();
}
}
@Override
public void onLoop(double timestamp) {
synchronized (Drive.this) {
switch (mDriveControlState) {
case OPEN_LOOP:
break;
case PATH_FOLLOWING:
updatePathFollower();
break;
default:
System.out.println("Unexpected drive control state: " + mDriveControlState);
break;
}
/*
// TODO: fix this (tom)
if (mAutoShift) {
handleAutoShift();
} else */
{
setHighGear(false);
}
}
}
@Override
public void onStop(double timestamp) {
stop();
stopLogging();
}
};
private void configureMaster(TalonSRX talon, boolean left) {
talon.setStatusFramePeriod(StatusFrameEnhanced.Status_2_Feedback0, 5, 100);
final ErrorCode sensorPresent = talon.configSelectedFeedbackSensor(FeedbackDevice
.CTRE_MagEncoder_Relative, 0, 100); //primary closed-loop, 100 ms timeout
if (sensorPresent != ErrorCode.OK) {
DriverStation.reportError("Could not detect " + (left ? "left" : "right") + " encoder: " + sensorPresent, false);
}
talon.setInverted(!left);
talon.setSensorPhase(true);
talon.enableVoltageCompensation(true);
talon.configVoltageCompSaturation(12.0, Constants.kLongCANTimeoutMs);
talon.configVelocityMeasurementPeriod(VelocityMeasPeriod.Period_50Ms, Constants.kLongCANTimeoutMs);
talon.configVelocityMeasurementWindow(1, Constants.kLongCANTimeoutMs);
talon.configClosedloopRamp(Constants.kDriveVoltageRampRate, Constants.kLongCANTimeoutMs);
talon.configNeutralDeadband(0.04, 0);
}
private Drive() {
mPeriodicIO = new PeriodicIO();
// Start all Talons in open loop mode.
mLeftMaster = TalonSRXFactory.createDefaultTalon(Constants.kLeftDriveMasterId);
configureMaster(mLeftMaster, true);
mLeftSlaveA = TalonSRXFactory.createPermanentSlaveTalon(Constants.kLeftDriveSlaveAId,
Constants.kLeftDriveMasterId);
mLeftSlaveA.setInverted(false);
mLeftSlaveB = TalonSRXFactory.createPermanentSlaveTalon(Constants.kLeftDriveSlaveBId,
Constants.kLeftDriveMasterId);
mLeftSlaveB.setInverted(false);
mRightMaster = TalonSRXFactory.createDefaultTalon(Constants.kRightDriveMasterId);
configureMaster(mRightMaster, false);
mRightSlaveA = TalonSRXFactory.createPermanentSlaveTalon(Constants.kRightDriveSlaveAId,
Constants.kRightDriveMasterId);
mRightSlaveA.setInverted(true);
mRightSlaveB = TalonSRXFactory.createPermanentSlaveTalon(Constants.kRightDriveSlaveBId,
Constants.kRightDriveMasterId);
mRightSlaveB.setInverted(true);
mShifter = Constants.makeSolenoidForId(Constants.kShifterSolenoidId);
reloadGains();
mPigeon = new PigeonIMU(mLeftSlaveB);
mLeftSlaveB.setStatusFramePeriod(StatusFrameEnhanced.Status_11_UartGadgeteer, 10, 10);
// Force a solenoid message.
mIsHighGear = true;
setHighGear(false);
setOpenLoop(DriveSignal.NEUTRAL);
// Force a CAN message across.
mIsBrakeMode = true;
setBrakeMode(false);
mMotionPlanner = new DriveMotionPlanner();
}
public static Drive getInstance() {
return mInstance;
}
private static double rotationsToInches(double rotations) {
return rotations * (Constants.kDriveWheelDiameterInches * Math.PI);
}
private static double rpmToInchesPerSecond(double rpm) {
return rotationsToInches(rpm) / 60;
}
private static double inchesToRotations(double inches) {
return inches / (Constants.kDriveWheelDiameterInches * Math.PI);
}
private static double inchesPerSecondToRpm(double inches_per_second) {
return inchesToRotations(inches_per_second) * 60;
}
private static double radiansPerSecondToTicksPer100ms(double rad_s) {
return rad_s / (Math.PI * 2.0) * 4096.0 / 10.0;
}
@Override
public void registerEnabledLoops(ILooper in) {
in.register(mLoop);
}
/**
* Configure talons for open loop control
*/
public synchronized void setOpenLoop(DriveSignal signal) {
if (mDriveControlState != DriveControlState.OPEN_LOOP) {
setBrakeMode(false);
mAutoShift = true;
System.out.println("Switching to open loop");
System.out.println(signal);
mDriveControlState = DriveControlState.OPEN_LOOP;
mLeftMaster.configNeutralDeadband(0.04, 0);
mRightMaster.configNeutralDeadband(0.04, 0);
}
mPeriodicIO.left_demand = signal.getLeft();
mPeriodicIO.right_demand = signal.getRight();
mPeriodicIO.left_feedforward = 0.0;
mPeriodicIO.right_feedforward = 0.0;
}
/**
* Configures talons for velocity control
*/
public synchronized void setVelocity(DriveSignal signal, DriveSignal feedforward) {
if (mDriveControlState != DriveControlState.PATH_FOLLOWING) {
// We entered a velocity control state.
setBrakeMode(true);
mAutoShift = false;
mLeftMaster.selectProfileSlot(kLowGearVelocityControlSlot, 0);
mRightMaster.selectProfileSlot(kLowGearVelocityControlSlot, 0);
mLeftMaster.configNeutralDeadband(0.0, 0);
mRightMaster.configNeutralDeadband(0.0, 0);
mDriveControlState = DriveControlState.PATH_FOLLOWING;
}
mPeriodicIO.left_demand = signal.getLeft();
mPeriodicIO.right_demand = signal.getRight();
mPeriodicIO.left_feedforward = feedforward.getLeft();
mPeriodicIO.right_feedforward = feedforward.getRight();
}
public synchronized void setTrajectory(TrajectoryIterator<TimedState<Pose2dWithCurvature>> trajectory) {
if (mMotionPlanner != null) {
mOverrideTrajectory = false;
mMotionPlanner.reset();
mMotionPlanner.setTrajectory(trajectory);
mDriveControlState = DriveControlState.PATH_FOLLOWING;
}
}
public boolean isDoneWithTrajectory() {
if (mMotionPlanner == null || mDriveControlState != DriveControlState.PATH_FOLLOWING) {
return false;
}
return mMotionPlanner.isDone() || mOverrideTrajectory;
}
public boolean isHighGear() {
return mIsHighGear;
}
public synchronized void setHighGear(boolean wantsHighGear) {
if (wantsHighGear != mIsHighGear) {
mIsHighGear = wantsHighGear;
mShifter.set(wantsHighGear);
}
}
public boolean isBrakeMode() {
return mIsBrakeMode;
}
public synchronized void setBrakeMode(boolean on) {
if (mIsBrakeMode != on) {
mIsBrakeMode = on;
NeutralMode mode = on ? NeutralMode.Brake : NeutralMode.Coast;
mRightMaster.setNeutralMode(mode);
mRightSlaveA.setNeutralMode(mode);
mRightSlaveB.setNeutralMode(mode);
mLeftMaster.setNeutralMode(mode);
mLeftSlaveA.setNeutralMode(mode);
mLeftSlaveB.setNeutralMode(mode);
}
}
public synchronized Rotation2d getHeading() {
return mPeriodicIO.gyro_heading;
}
public synchronized void setHeading(Rotation2d heading) {
System.out.println("SET HEADING: " + heading.getDegrees());
mGyroOffset = heading.rotateBy(Rotation2d.fromDegrees(mPigeon.getFusedHeading()).inverse());
System.out.println("Gyro offset: " + mGyroOffset.getDegrees());
mPeriodicIO.gyro_heading = heading;
}
@Override
public synchronized void stop() {
setOpenLoop(DriveSignal.NEUTRAL);
}
@Override
public void outputTelemetry() {
SmartDashboard.putNumber("Right Drive Distance", mPeriodicIO.right_distance);
SmartDashboard.putNumber("Right Drive Ticks", mPeriodicIO.right_position_ticks);
SmartDashboard.putNumber("Left Drive Ticks", mPeriodicIO.left_position_ticks);
SmartDashboard.putNumber("Left Drive Distance", mPeriodicIO.left_distance);
SmartDashboard.putNumber("Right Linear Velocity", getRightLinearVelocity());
SmartDashboard.putNumber("Left Linear Velocity", getLeftLinearVelocity());
SmartDashboard.putNumber("x err", mPeriodicIO.error.getTranslation().x());
SmartDashboard.putNumber("y err", mPeriodicIO.error.getTranslation().y());
SmartDashboard.putNumber("theta err", mPeriodicIO.error.getRotation().getDegrees());
if (getHeading() != null) {
SmartDashboard.putNumber("Gyro Heading", getHeading().getDegrees());
}
if (mCSVWriter != null) {
mCSVWriter.write();
}
}
public synchronized void resetEncoders() {
mLeftMaster.setSelectedSensorPosition(0, 0, 0);
mRightMaster.setSelectedSensorPosition(0, 0, 0);
mPeriodicIO = new PeriodicIO();
}
@Override
public void zeroSensors() {
setHeading(Rotation2d.identity());
resetEncoders();
mAutoShift = true;
}
public double getLeftEncoderRotations() {
return mPeriodicIO.left_position_ticks / DRIVE_ENCODER_PPR;
}
public double getRightEncoderRotations() {
return mPeriodicIO.right_position_ticks / DRIVE_ENCODER_PPR;
}
public double getLeftEncoderDistance() {
return rotationsToInches(getLeftEncoderRotations());
}
public double getRightEncoderDistance() {
return rotationsToInches(getRightEncoderRotations());
}
public double getRightVelocityNativeUnits() {
return mPeriodicIO.right_velocity_ticks_per_100ms;
}
public double getRightLinearVelocity() {
return rotationsToInches(getRightVelocityNativeUnits() * 10.0 / DRIVE_ENCODER_PPR);
}
public double getLeftVelocityNativeUnits() {
return mPeriodicIO.left_velocity_ticks_per_100ms;
}
public double getLeftLinearVelocity() {
return rotationsToInches(getLeftVelocityNativeUnits() * 10.0 / DRIVE_ENCODER_PPR);
}
public double getLinearVelocity() {
return (getLeftLinearVelocity() + getRightLinearVelocity()) / 2.0;
}
public double getAngularVelocity() {
return (getRightLinearVelocity() - getLeftLinearVelocity()) / Constants.kDriveWheelTrackWidthInches;
}
public void overrideTrajectory(boolean value) {
mOverrideTrajectory = value;
}
private void updatePathFollower() {
if (mDriveControlState == DriveControlState.PATH_FOLLOWING) {
final double now = Timer.getFPGATimestamp();
DriveMotionPlanner.Output output = mMotionPlanner.update(now, RobotState.getInstance().getFieldToVehicle(now));
// DriveSignal signal = new DriveSignal(demand.left_feedforward_voltage / 12.0, demand.right_feedforward_voltage / 12.0);
mPeriodicIO.error = mMotionPlanner.error();
mPeriodicIO.path_setpoint = mMotionPlanner.setpoint();
if (!mOverrideTrajectory) {
setVelocity(new DriveSignal(radiansPerSecondToTicksPer100ms(output.left_velocity), radiansPerSecondToTicksPer100ms(output.right_velocity)),
new DriveSignal(output.left_feedforward_voltage / 12.0, output.right_feedforward_voltage / 12.0));
mPeriodicIO.left_accel = radiansPerSecondToTicksPer100ms(output.left_accel) / 1000.0;
mPeriodicIO.right_accel = radiansPerSecondToTicksPer100ms(output.right_accel) / 1000.0;
} else {
setVelocity(DriveSignal.BRAKE, DriveSignal.BRAKE);
mPeriodicIO.left_accel = mPeriodicIO.right_accel = 0.0;
}
} else {
DriverStation.reportError("Drive is not in path following state", false);
}
}
private void handleAutoShift() {
final double linear_velocity = Math.abs(getLinearVelocity());
final double angular_velocity = Math.abs(getAngularVelocity());
if (mIsHighGear && linear_velocity < Constants.kDriveDownShiftVelocity && angular_velocity < Constants
.kDriveDownShiftAngularVelocity) {
setHighGear(false);
} else if (!mIsHighGear && linear_velocity > Constants.kDriveUpShiftVelocity) {
setHighGear(true);
}
}
public synchronized void reloadGains() {
mLeftMaster.config_kP(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKp, Constants.kLongCANTimeoutMs);
mLeftMaster.config_kI(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKi, Constants.kLongCANTimeoutMs);
mLeftMaster.config_kD(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKd, Constants.kLongCANTimeoutMs);
mLeftMaster.config_kF(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKf, Constants.kLongCANTimeoutMs);
mLeftMaster.config_IntegralZone(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityIZone, Constants.kLongCANTimeoutMs);
mRightMaster.config_kP(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKp, Constants.kLongCANTimeoutMs);
mRightMaster.config_kI(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKi, Constants.kLongCANTimeoutMs);
mRightMaster.config_kD(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKd, Constants.kLongCANTimeoutMs);
mRightMaster.config_kF(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityKf, Constants.kLongCANTimeoutMs);
mRightMaster.config_IntegralZone(kLowGearVelocityControlSlot, Constants.kDriveLowGearVelocityIZone, Constants.kLongCANTimeoutMs);
}
@Override
public void writeToLog() {
}
@Override
public synchronized void readPeriodicInputs() {
double prevLeftTicks = mPeriodicIO.left_position_ticks;
double prevRightTicks = mPeriodicIO.right_position_ticks;
mPeriodicIO.left_position_ticks = mLeftMaster.getSelectedSensorPosition(0);
mPeriodicIO.right_position_ticks = mRightMaster.getSelectedSensorPosition(0);
mPeriodicIO.left_velocity_ticks_per_100ms = mLeftMaster.getSelectedSensorVelocity(0);
mPeriodicIO.right_velocity_ticks_per_100ms = mRightMaster.getSelectedSensorVelocity(0);
mPeriodicIO.gyro_heading = Rotation2d.fromDegrees(mPigeon.getFusedHeading()).rotateBy(mGyroOffset);
double deltaLeftTicks = ((mPeriodicIO.left_position_ticks - prevLeftTicks) / 4096.0) * Math.PI;
if (deltaLeftTicks > 0.0) {
mPeriodicIO.left_distance += deltaLeftTicks * Constants.kDriveWheelDiameterInches;
} else {
mPeriodicIO.left_distance += deltaLeftTicks * Constants.kDriveWheelDiameterInches;
}
double deltaRightTicks = ((mPeriodicIO.right_position_ticks - prevRightTicks) / 4096.0) * Math.PI;
if (deltaRightTicks > 0.0) {
mPeriodicIO.right_distance += deltaRightTicks * Constants.kDriveWheelDiameterInches;
} else {
mPeriodicIO.right_distance += deltaRightTicks * Constants.kDriveWheelDiameterInches;
}
if (mCSVWriter != null) {
mCSVWriter.add(mPeriodicIO);
}
// System.out.println("control state: " + mDriveControlState + ", left: " + mPeriodicIO.left_demand + ", right: " + mPeriodicIO.right_demand);
}
@Override
public synchronized void writePeriodicOutputs() {
if (mDriveControlState == DriveControlState.OPEN_LOOP) {
mLeftMaster.set(ControlMode.PercentOutput, mPeriodicIO.left_demand, DemandType.ArbitraryFeedForward, 0.0);
mRightMaster.set(ControlMode.PercentOutput, mPeriodicIO.right_demand, DemandType.ArbitraryFeedForward, 0.0);
} else {
mLeftMaster.set(ControlMode.Velocity, mPeriodicIO.left_demand, DemandType.ArbitraryFeedForward,
mPeriodicIO.left_feedforward + Constants.kDriveLowGearVelocityKd * mPeriodicIO.left_accel / 1023.0);
mRightMaster.set(ControlMode.Velocity, mPeriodicIO.right_demand, DemandType.ArbitraryFeedForward,
mPeriodicIO.right_feedforward + Constants.kDriveLowGearVelocityKd * mPeriodicIO.right_accel / 1023.0);
}
}
@Override
public boolean checkSystem() {
boolean leftSide = TalonSRXChecker.CheckTalons(this,
new ArrayList<TalonSRXChecker.TalonSRXConfig>() {
{
add(new TalonSRXChecker.TalonSRXConfig("left_master", mLeftMaster));
add(new TalonSRXChecker.TalonSRXConfig("left_slave", mLeftSlaveA));
add(new TalonSRXChecker.TalonSRXConfig("left_slave1", mLeftSlaveB));
}
}, new TalonSRXChecker.CheckerConfig() {
{
mCurrentFloor = 2;
mRPMFloor = 1500;
mCurrentEpsilon = 2.0;
mRPMEpsilon = 250;
mRPMSupplier = () -> mLeftMaster.getSelectedSensorVelocity(0);
}
});
boolean rightSide = TalonSRXChecker.CheckTalons(this,
new ArrayList<TalonSRXChecker.TalonSRXConfig>() {
{
add(new TalonSRXChecker.TalonSRXConfig("right_master", mRightMaster));
add(new TalonSRXChecker.TalonSRXConfig("right_slave", mRightSlaveA));
add(new TalonSRXChecker.TalonSRXConfig("right_slave1", mRightSlaveB));
}
}, new TalonSRXChecker.CheckerConfig() {
{
mCurrentFloor = 2;
mRPMFloor = 1500;
mCurrentEpsilon = 2.0;
mRPMEpsilon = 250;
mRPMSupplier = () -> mRightMaster.getSelectedSensorVelocity(0);
}
});
return leftSide && rightSide;
}
public synchronized void startLogging() {
if (mCSVWriter == null) {
mCSVWriter = new ReflectingCSVWriter<>("/home/lvuser/DRIVE-LOGS.csv", PeriodicIO.class);
}
}
public synchronized void stopLogging() {
if (mCSVWriter != null) {
mCSVWriter.flush();
mCSVWriter = null;
}
}
// The robot drivetrain's various states.
public enum DriveControlState {
OPEN_LOOP, // open loop voltage control
PATH_FOLLOWING, // velocity PID control
}
public enum ShifterState {
FORCE_LOW_GEAR,
FORCE_HIGH_GEAR,
AUTO_SHIFT
}
public static class PeriodicIO {
// INPUTS
public int left_position_ticks;
public int right_position_ticks;
public double left_distance;
public double right_distance;
public int left_velocity_ticks_per_100ms;
public int right_velocity_ticks_per_100ms;
public Rotation2d gyro_heading = Rotation2d.identity();
public Pose2d error = Pose2d.identity();
// OUTPUTS
public double left_demand;
public double right_demand;
public double left_accel;
public double right_accel;
public double left_feedforward;
public double right_feedforward;
public TimedState<Pose2dWithCurvature> path_setpoint = new TimedState<Pose2dWithCurvature>(Pose2dWithCurvature.identity());
}
}