Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.

Commit 87d1e16

Browse files
committed
more kF implementation, PID input/output ranges, max high/low speed
implementation - RobotMap: set both VelocityPIDControllers' input ranges to +- Max High Speed, output ranges are +-1, and initial kF constants based on Max Low Speed (for more accuracy in Auto); set max output of robotDrive to Max High Speed - DT/DTInterface: new method getCurrentMaxSpeed() returns max speed based on if robot is in high or low gear; new method resetVelocityPIDkFConsts() that resets kF constants of both VPIDs based on return of getCurrentMaxSpeed and returns new kF value; updated method updatePidConstants() to call resetVelocityPIDkFConsts() instead of just passing in 1/Max Speed - PIDMove/Turn commands: when determining kF values for move/turn PIDs, called getCurrentMaxSpeed() instead of SD value Max Speed; set moveController input range to +- Max High Speed - ShiftHigh/LowGear commands: called resetVelocityPIDkFConsts() after SD value reset - NOTE: all Max Speeds should be in inches/per!!!
1 parent 5269634 commit 87d1e16

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

Robot2018/src/org/usfirst/frc/team199/Robot2018/RobotMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ public RobotMap() {
113113

114114
leftVelocityController = new VelocityPIDController(Robot.getConst("VelocityLeftkP", 1),
115115
Robot.getConst("VelocityLeftkI", 0), Robot.getConst("VelocityLeftkD", 0),
116-
1 / Robot.getConst("MaxSpeed", 204), leftEncRate, dtLeft);
116+
1 / Robot.getConst("Max Low Speed", 84), leftEncRate, dtLeft);
117117
leftVelocityController.enable();
118-
leftVelocityController.setInputRange(-Robot.getConst("Max Speed", 204), Robot.getConst("Max Speed", 204));
118+
leftVelocityController.setInputRange(-Robot.getConst("Max High Speed", 204),
119+
Robot.getConst("Max High Speed", 204));
119120
leftVelocityController.setOutputRange(-1.0, 1.0);
120121
leftVelocityController.setContinuous(false);
121122
leftVelocityController.setAbsoluteTolerance(Robot.getConst("VelocityToleranceLeft", 2));
@@ -134,15 +135,16 @@ public RobotMap() {
134135

135136
rightVelocityController = new VelocityPIDController(Robot.getConst("VelocityRightkP", 1),
136137
Robot.getConst("VelocityRightkI", 0), Robot.getConst("VelocityRightkD", 0),
137-
1 / Robot.getConst("MaxSpeed", 204), rightEncRate, dtRight);
138+
1 / Robot.getConst("Max Low Speed", 84), rightEncRate, dtRight);
138139
rightVelocityController.enable();
139-
rightVelocityController.setInputRange(-Robot.getConst("Max Speed", 204), Robot.getConst("Max Speed", 204));
140+
rightVelocityController.setInputRange(-Robot.getConst("Max High Speed", 204),
141+
Robot.getConst("Max High Speed", 204));
140142
rightVelocityController.setOutputRange(-1.0, 1.0);
141143
rightVelocityController.setContinuous(false);
142144
rightVelocityController.setAbsoluteTolerance(Robot.getConst("VelocityToleranceRight", 2));
143145

144146
robotDrive = new DifferentialDrive(leftVelocityController, rightVelocityController);
145-
robotDrive.setMaxOutput(Robot.getConst("Max Speed", 204));
147+
robotDrive.setMaxOutput(Robot.getConst("Max High Speed", 204));
146148

147149
distEncAvg = new PIDSourceAverage(leftEncDist, rightEncDist);
148150
fancyGyro = new AHRS(SerialPort.Port.kMXP);

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDMove.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public PIDMove(double targ, DrivetrainInterface dt, PIDSourceAverage avg) {
2323
target = targ;
2424
this.dt = dt;
2525
requires(Robot.dt);
26-
double kf = 1 / (Robot.getConst("Max Speed", 204) * Robot.getConst("Default PID Update Time", 0.05));
26+
double kf = 1 / (dt.getCurrentMaxSpeed() * Robot.getConst("Default PID Update Time", 0.05));
2727
moveController = new PIDController(Robot.getConst("MovekP", 1), Robot.getConst("MovekI", 0),
2828
Robot.getConst("MovekD", 0), kf, avg, this);
2929
}
@@ -33,7 +33,7 @@ public PIDMove(double targ, DrivetrainInterface dt, PIDSourceAverage avg) {
3333
public void initialize() {
3434
dt.resetDistEncs();
3535
// input is in inches
36-
moveController.setInputRange(-Double.MAX_VALUE, Double.MAX_VALUE);
36+
moveController.setInputRange(-Robot.getConst("Max High Speed", 204), Robot.getConst("Max High Speed", 204));
3737
// output in "motor units" (arcade and tank only accept values [-1, 1]
3838
moveController.setOutputRange(-1.0, 1.0);
3939
moveController.setContinuous(false);

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDTurn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public PIDTurn(double targ, DrivetrainInterface dt, AHRS ahrs) {
2626
requires(Robot.dt);
2727
// calculates the maximum turning speed in degrees/sec based on the max linear
2828
// speed in inches/s and the distance (inches) between sides of the DT
29-
double maxTurnSpeed = Robot.getConst("Max Speed", 204) * 360
29+
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360
3030
/ (Math.PI * Robot.getConst("Distance Between Wheels", 26.25));
3131
double kf = 1 / (maxTurnSpeed * Robot.getConst("Default PID Update Time", 0.05));
3232
turnController = new PIDController(Robot.getConst("TurnkP", 1), Robot.getConst("TurnkI", 0),

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/ShiftHighGear.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ public ShiftHighGear() {
2121
protected void initialize() {
2222
Robot.dt.shiftGears(true);
2323
SmartDashboard.putBoolean("High Gear", true);
24+
Robot.dt.resetVelocityPIDkFConsts();
2425
}
2526
}

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/ShiftLowGear.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ public ShiftLowGear() {
2121
protected void initialize() {
2222
Robot.dt.shiftGears(false);
2323
SmartDashboard.putBoolean("High Gear", false);
24+
Robot.dt.resetVelocityPIDkFConsts();
2425
}
2526
}

Robot2018/src/org/usfirst/frc/team199/Robot2018/subsystems/Drivetrain.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ public double getDtRightSpeed() {
111111
*/
112112
@Override
113113
public void updatePidConstants() {
114-
leftVelocityController.setPID(Robot.getConst("MoveLeftkP", 1), Robot.getConst("MoveLeftkI", 0),
115-
Robot.getConst("MoveLeftkD", 0), 1 / Robot.getConst("MaxSpeed", 17));
116-
rightVelocityController.setPID(Robot.getConst("MoveRightkP", 1), Robot.getConst("MoveRightkI", 0),
117-
Robot.getConst("MoveRightkD", 0), 1 / Robot.getConst("MaxSpeed", 17));
114+
leftVelocityController.setPID(Robot.getConst("VelocityLeftkP", 1), Robot.getConst("VelocityLeftkI", 0),
115+
Robot.getConst("VelocityLeftkD", 0));
116+
rightVelocityController.setPID(Robot.getConst("VelocityRightkP", 1), Robot.getConst("VelocityRightkI", 0),
117+
Robot.getConst("VelocityRightkD", 0));
118+
resetVelocityPIDkFConsts();
118119
}
119120

120121
/**
@@ -209,7 +210,7 @@ public double getRightEncDist() {
209210
}
210211

211212
/**
212-
* Activates the solenoid to push the drivetrain into high or low gear
213+
* Activates the solenoid to push the drivetrain into high or low gear.
213214
*
214215
* @param highGear
215216
* If the solenoid is to be pushed into high gear (true, kForward) or
@@ -231,4 +232,35 @@ public void shiftGears(boolean highGear) {
231232
public void shiftGearSolenoidOff() {
232233
dtGear.set(DoubleSolenoid.Value.kOff);
233234
}
235+
236+
/**
237+
* Reset the kf constants for both VelocityPIDControllers based on current DT
238+
* gearing (high or low gear).
239+
*
240+
* @param newKF
241+
* the new kF constant based on high and low gear max speeds; should
242+
* be 1 / max speed
243+
* @return the new kF value as 1 / correct max speed
244+
*/
245+
@Override
246+
public double resetVelocityPIDkFConsts() {
247+
double newKF = 1 / getCurrentMaxSpeed();
248+
leftVelocityController.setF(newKF);
249+
rightVelocityController.setF(newKF);
250+
return newKF;
251+
}
252+
253+
/**
254+
* Gets the current max speed of the DT based on gearing (high or low gear)
255+
*
256+
* @return the current max speed of the DT in inches/second
257+
*/
258+
@Override
259+
public double getCurrentMaxSpeed() {
260+
if (Robot.getBool("High Gear", true)) {
261+
return Robot.getConst("Max High Speed", 204);
262+
} else {
263+
return Robot.getConst("Max Low Speed", 84);
264+
}
265+
}
234266
}

Robot2018/src/org/usfirst/frc/team199/Robot2018/subsystems/DrivetrainInterface.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ public interface DrivetrainInterface {
124124
* Stops the solenoid that pushes the drivetrain into low or high gear
125125
*/
126126
public void shiftGearSolenoidOff();
127+
128+
/**
129+
* Reset the kf constants for both VelocityPIDControllers based on current DT
130+
* gearing (high or low gear).
131+
*
132+
* @param newKF
133+
* the new kF constant based on high and low gear max speeds; should
134+
* be 1 / max speed
135+
* @return the new kF value as 1 / correct max speed
136+
*/
137+
public double resetVelocityPIDkFConsts();
138+
139+
/**
140+
* Gets the current max speed of the DT based on gearing (high or low gear)
141+
*
142+
* @return the current max speed of the DT in inches/second
143+
*/
144+
public double getCurrentMaxSpeed();
127145
}

0 commit comments

Comments
 (0)