8
8
import edu .wpi .first .wpilibj .*;
9
9
import com .ctre .phoenix .motorcontrol .*;
10
10
import com .ctre .phoenix .motorcontrol .ControlMode ;
11
+ import com .fasterxml .jackson .annotation .JacksonInject .Value ;
11
12
12
13
//import edu.wpi.first.wpilibj.command.Subsystem;
13
14
import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
@@ -29,9 +30,9 @@ public class Drive extends SubsystemBase {
29
30
public static final boolean COAST_MODE = false ;
30
31
31
32
// PID loop variables
32
- private PIDController m_HeadingPid ;
33
- private PIDHeadingError m_HeadingError ;
34
- private PIDHeadingCorrection m_HeadingCorrection ;
33
+ private final PIDController m_HeadingPid ;
34
+ private final PIDHeadingError m_HeadingError ;
35
+ private final PIDHeadingCorrection m_HeadingCorrection ;
35
36
private boolean m_HeadingPidPreventWindup = false ;
36
37
private static final int LOOPS_GYRO_DELAY = 10 ;
37
38
@@ -45,21 +46,22 @@ public class Drive extends SubsystemBase {
45
46
private AHRS Navx ;
46
47
47
48
// Driving mode
48
- private boolean m_speedRacerDriveMode = true ; // set by default
49
+ private final boolean m_speedRacerDriveMode = true ; // set by default
49
50
50
51
// NavX parameters
51
52
private double m_desiredHeading = 0.0 ;
52
53
private boolean m_useHeadingCorrection = true ;
53
- private static final double HEADING_PID_P = 0.007 ; // was 0.030 in 2019 for HIGH_GEAR
54
+ private static final double HEADING_PID_P = 0.007 ; // was 0.030 in 2019 for HIGH_GEAR
54
55
private static final double kToleranceDegreesPIDControl = 0.2 ;
55
56
56
57
// Drive parameters
57
58
// pi * diameter * (pulley ratios) / (counts per rev * gearbox reduction)
58
- public static final double DISTANCE_PER_PULSE_IN_INCHES = 3.14 * 5.75 * 36.0 / 42.0 / (2048.0 * 7.56 ); // corrected for 2020
59
+ public static final double DISTANCE_PER_PULSE_IN_INCHES = 3.14 * 5.75 * 36.0 / 42.0 / (2048.0 * 7.56 ); // corrected
60
+ // for 2020
59
61
60
62
private boolean m_closedLoopMode = false ;
61
- private double m_maxWheelSpeed = 1.0 ; // should be maximum wheel speed in native units
62
- private static final double CLOSED_LOOP_RAMP_RATE = 0.1 ; // time from neutral to full in seconds
63
+ private final double m_maxWheelSpeed = 1.0 ; // should be maximum wheel speed in native units
64
+ private static final double CLOSED_LOOP_RAMP_RATE = 0.1 ; // time from neutral to full in seconds
63
65
64
66
private double m_initialWheelDistance = 0.0 ;
65
67
private int m_iterationsSinceRotationCommanded = 0 ;
@@ -84,7 +86,7 @@ public Drive() {
84
86
*/
85
87
Navx = new AHRS (SPI .Port .kMXP );
86
88
Navx .reset ();
87
- } catch (RuntimeException ex ) {
89
+ } catch (final RuntimeException ex ) {
88
90
DriverStation .reportError ("Error instantiating navX MXP: " + ex .getMessage (), true );
89
91
System .out .println ("Error loading navx." );
90
92
}
@@ -128,7 +130,7 @@ public void init() {
128
130
configureDriveTalon (rightFrontTalon );
129
131
}
130
132
131
- public void zeroHeadingGyro (double headingOffset ) {
133
+ public void zeroHeadingGyro (final double headingOffset ) {
132
134
Navx .zeroYaw ();
133
135
setHeadingOffset (headingOffset );
134
136
@@ -146,22 +148,22 @@ public void initDefaultCommand() {
146
148
// setDefaultCommand(new SpeedRacerDrive());
147
149
}
148
150
149
- private void configureDriveTalon (MayhemTalonSRX talon ) {
150
- double wheelP = 1.5 ;
151
- double wheelI = 0.0 ;
152
- double wheelD = 0.0 ;
153
- double wheelF = 1.0 ;
151
+ private void configureDriveTalon (final MayhemTalonSRX talon ) {
152
+ final double wheelP = 1.5 ;
153
+ final double wheelI = 0.0 ;
154
+ final double wheelD = 0.0 ;
155
+ final double wheelF = 1.0 ;
154
156
155
157
talon .setFeedbackDevice (FeedbackDevice .IntegratedSensor );
156
-
158
+
157
159
talon .configNominalOutputVoltage (+0.0f , -0.0f );
158
160
talon .configPeakOutputVoltage (+12.0 , -12.0 );
159
161
160
162
talon .config_kP (0 , wheelP );
161
163
talon .config_kI (0 , wheelI );
162
164
talon .config_kD (0 , wheelD );
163
165
talon .config_kF (0 , wheelF );
164
- talon .configClosedloopRamp (CLOSED_LOOP_RAMP_RATE ); // specify minimum time for neutral to full in seconds
166
+ talon .configClosedloopRamp (CLOSED_LOOP_RAMP_RATE ); // specify minimum time for neutral to full in seconds
165
167
166
168
DriverStation .reportError ("setWheelPIDF: " + wheelP + " " + wheelI + " " + wheelD + " " + wheelF + "\n " , false );
167
169
}
@@ -172,9 +174,9 @@ private void configureDriveTalon(MayhemTalonSRX talon) {
172
174
* @param brakeMode - true for "brake in neutral" and false for "coast in
173
175
* neutral"
174
176
*/
175
- public void setBrakeMode (boolean brakeMode ) {
177
+ public void setBrakeMode (final boolean brakeMode ) {
176
178
177
- NeutralMode mode = (brakeMode ) ? NeutralMode .Brake : NeutralMode .Coast ;
179
+ final NeutralMode mode = (brakeMode ) ? NeutralMode .Brake : NeutralMode .Coast ;
178
180
179
181
leftFrontTalon .setNeutralMode (mode );
180
182
leftRearTalon .setNeutralMode (mode );
@@ -221,8 +223,8 @@ public double getLeftSpeed() {
221
223
222
224
// *************************** GYRO *******************************************
223
225
224
- public double calculateHeadingError (double Target ) {
225
- double currentHeading = getHeading ();
226
+ public double calculateHeadingError (final double Target ) {
227
+ final double currentHeading = getHeading ();
226
228
double error = Target - currentHeading ;
227
229
error = error % 360.0 ;
228
230
if (error > 180.0 ) {
@@ -235,7 +237,7 @@ public boolean getHeadingCorrectionMode() {
235
237
return m_useHeadingCorrection ;
236
238
}
237
239
238
- public void setHeadingCorrectionMode (boolean useHeadingCorrection ) {
240
+ public void setHeadingCorrectionMode (final boolean useHeadingCorrection ) {
239
241
m_useHeadingCorrection = useHeadingCorrection ;
240
242
}
241
243
@@ -244,8 +246,8 @@ private void resetAndEnableHeadingPID() {
244
246
m_HeadingPid .setP (HEADING_PID_P );
245
247
// } else
246
248
// {
247
- // low gear
248
- // m_HeadingPid.setP(HEADING_PID_P_FOR_LOW_GEAR);
249
+ // low gear
250
+ // m_HeadingPid.setP(HEADING_PID_P_FOR_LOW_GEAR);
249
251
// }
250
252
m_HeadingPid .reset ();
251
253
m_HeadingPid .enable ();
@@ -256,11 +258,11 @@ private void resetAndEnableHeadingPID() {
256
258
static private double m_prevRightDistance = 0.0 ;
257
259
258
260
public boolean isStationary () {
259
- double leftDistance = getLeftEncoder ();
260
- double rightDistance = getRightEncoder ();
261
+ final double leftDistance = getLeftEncoder ();
262
+ final double rightDistance = getRightEncoder ();
261
263
262
- double leftDelta = Math .abs (leftDistance - m_prevLeftDistance );
263
- double rightDelta = Math .abs (rightDistance - m_prevRightDistance );
264
+ final double leftDelta = Math .abs (leftDistance - m_prevLeftDistance );
265
+ final double rightDelta = Math .abs (rightDistance - m_prevRightDistance );
264
266
265
267
m_prevLeftDistance = leftDistance ;
266
268
m_prevRightDistance = rightDistance ;
@@ -279,7 +281,7 @@ public void displayGyroInfo() {
279
281
280
282
private double m_headingOffset = 0.0 ;
281
283
282
- public void setHeadingOffset (double arg_offset ) {
284
+ public void setHeadingOffset (final double arg_offset ) {
283
285
m_headingOffset = arg_offset ;
284
286
}
285
287
@@ -346,7 +348,7 @@ public void updateSdbPdp() {
346
348
double rf ;
347
349
double lb ;
348
350
double rb ;
349
- double fudgeFactor = 0.0 ;
351
+ final double fudgeFactor = 0.0 ;
350
352
351
353
lf = pdp .getCurrent (Constants .PDP .DRIVE_LEFT_A ) - fudgeFactor ;
352
354
rf = pdp .getCurrent (Constants .PDP .DRIVE_LEFT_B ) - fudgeFactor ;
@@ -357,6 +359,7 @@ public void updateSdbPdp() {
357
359
SmartDashboard .putNumber ("Right Front I" , rf );
358
360
SmartDashboard .putNumber ("Left Back I" , lb );
359
361
SmartDashboard .putNumber ("Right Back I" , rb );
362
+
360
363
}
361
364
362
365
/*
@@ -394,12 +397,11 @@ public void setAutoAlignFalse() {
394
397
// Robot.lights.set(LedPatternFactory.autoAlignGotIt);
395
398
}
396
399
397
- public void speedRacerDrive (double throttle , double rawSteeringX , boolean quickTurn ) {
400
+ public void speedRacerDrive (final double throttle , final double rawSteeringX , final boolean quickTurn ) {
398
401
speedRacerDriveNew (throttle , rawSteeringX , quickTurn );
399
402
}
400
403
401
-
402
- public void speedRacerDriveNew (double throttle , double rawSteeringX , boolean quickTurn ) {
404
+ public void speedRacerDriveNew (final double throttle , final double rawSteeringX , final boolean quickTurn ) {
403
405
double leftPower , rightPower ;
404
406
double rotation = 0 ;
405
407
final double QUICK_TURN_GAIN = 0.55 ; // 2019: .75. 2020: .75 was too fast.
@@ -411,7 +413,8 @@ public void speedRacerDriveNew(double throttle, double rawSteeringX, boolean qui
411
413
throttleSign = -1 ;
412
414
}
413
415
414
- // check for if steering input is essentially zero for "DriveStraight" functionality
416
+ // check for if steering input is essentially zero for "DriveStraight"
417
+ // functionality
415
418
if ((-0.01 < rawSteeringX ) && (rawSteeringX < 0.01 )) {
416
419
// no turn being commanded, drive straight or stay still
417
420
m_iterationsSinceRotationCommanded ++;
@@ -476,11 +479,10 @@ public void speedRacerDriveNew(double throttle, double rawSteeringX, boolean qui
476
479
setMotorPower (leftPower , rightPower );
477
480
}
478
481
479
-
480
- public void speedRacerDriveOld (double throttle , double rawSteeringX , boolean quickTurn ) {
482
+ public void speedRacerDriveOld (final double throttle , final double rawSteeringX , final boolean quickTurn ) {
481
483
double leftPower , rightPower ;
482
484
double rotation = 0 ;
483
- double adjustedSteeringX = rawSteeringX * throttle ;
485
+ final double adjustedSteeringX = rawSteeringX * throttle ;
484
486
final double QUICK_TURN_GAIN = 0.55 ; // 2019: .75. 2020: .75 was too fast.
485
487
final double STD_TURN_GAIN = 0.9 ; // 2019: 1.5. 2020: 1.5 was too fast// the driver wants the non-quick turn
486
488
// turning a little more responsive.
@@ -611,7 +613,7 @@ private double maintainHeading() {
611
613
return headingCorrection ;
612
614
}
613
615
614
- public void rotate (double RotateX ) {
616
+ public void rotate (final double RotateX ) {
615
617
m_desiredHeading += RotateX ;
616
618
if (m_desiredHeading > 180 ) {
617
619
m_desiredHeading -= 360 ;
@@ -623,7 +625,7 @@ public void rotate(double RotateX) {
623
625
m_iterationsSinceMovementCommanded = 0 ;
624
626
}
625
627
626
- public void rotateToHeading (double desiredHeading ) {
628
+ public void rotateToHeading (final double desiredHeading ) {
627
629
m_desiredHeading = desiredHeading ;
628
630
}
629
631
@@ -643,6 +645,9 @@ public void updateSmartDashboard() {
643
645
// ***** KBS: Uncommenting below, as it takes a LONG time to get PDP values
644
646
// updateSdbPdp();
645
647
648
+ int matchnumber = DriverStation .getInstance ().getMatchNumber ();
649
+ DriverStation .MatchType MatchType = DriverStation .getInstance ().getMatchType ();
650
+ SmartDashboard .putString ("matchInfo" , "" + MatchType + '_' + matchnumber );
646
651
SmartDashboard .putNumber ("Left Front Encoder Counts" , leftFrontTalon .getSelectedSensorPosition (0 ));
647
652
SmartDashboard .putNumber ("Right Front Encoder Counts" , rightFrontTalon .getSelectedSensorPosition (0 ));
648
653
SmartDashboard .putNumber ("Left Rear Encoder Counts" , leftRearTalon .getSelectedSensorPosition (0 ));
@@ -687,13 +692,13 @@ public void updateSmartDashboard() {
687
692
private static final double CAMERA_LAG = 0.150 ; // was .200; changing to .150 at CMP
688
693
689
694
public void updateHistory () {
690
- double now = Timer .getFPGATimestamp ();
695
+ final double now = Timer .getFPGATimestamp ();
691
696
headingHistory .add (now , getHeading ());
692
697
}
693
698
694
699
public double getHeadingForCapturedImage () {
695
- double now = Timer .getFPGATimestamp ();
696
- double indexTime = now - CAMERA_LAG ;
700
+ final double now = Timer .getFPGATimestamp ();
701
+ final double indexTime = now - CAMERA_LAG ;
697
702
return headingHistory .getAzForTime (indexTime );
698
703
}
699
704
@@ -712,12 +717,12 @@ public void saveInitialWheelDistance() {
712
717
* @return
713
718
*/
714
719
public double getWheelDistance () {
715
- double dist = (getLeftEncoder () + getRightEncoder ()) / 2 ;
720
+ final double dist = (getLeftEncoder () + getRightEncoder ()) / 2 ;
716
721
return dist - m_initialWheelDistance ;
717
722
}
718
723
719
724
// NOTE the difference between rotateToHeading(...) and goToHeading(...)
720
- public void setDesiredHeading (double desiredHeading ) {
725
+ public void setDesiredHeading (final double desiredHeading ) {
721
726
m_desiredHeading = desiredHeading ;
722
727
m_iterationsSinceRotationCommanded = LOOPS_GYRO_DELAY + 1 ;
723
728
m_iterationsSinceMovementCommanded = 0 ;
0 commit comments