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

Commit a8b8730

Browse files
happy VPID!
VPID is tuned and working in both high and low gears
1 parent 2c2738c commit a8b8730

4 files changed

Lines changed: 47 additions & 17 deletions

File tree

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public RobotMap() {
135135
dtLeft.setInverted(true);
136136

137137
leftVelocityController = new VelocityPIDController(Robot.getConst("VelocityLeftkI", 0), 0,
138-
Robot.getConst("VelocityLeftkD", calcDefkD()), 1 / Robot.getConst("Max Low Speed", 84), leftEncRate,
139-
dtLeft);
138+
Robot.getConst("VelocityLeftkD", calcDefkD(Robot.getConst("Max Low Speed", 84))),
139+
1 / Robot.getConst("Max Low Speed", 84), leftEncRate, dtLeft);
140140
leftVelocityController.setInputRange(-Robot.getConst("Max High Speed", 204),
141141
Robot.getConst("Max High Speed", 204));
142142
leftVelocityController.setOutputRange(-1.0, 1.0);
@@ -152,6 +152,7 @@ public RobotMap() {
152152
rightEncRate.setPIDSourceType(PIDSourceType.kRate);
153153
rightEncDist.setDistancePerPulse(Robot.getConst("DPP", DIST_PER_PULSE_RATIO));
154154
rightEncRate.setDistancePerPulse(Robot.getConst("DPP", DIST_PER_PULSE_RATIO));
155+
rightEncRate.setReverseDirection(true);
155156

156157
dtRightMaster = new WPI_TalonSRX(getPort("RightTalonSRXMaster", 4));
157158
configSRX(dtRightMaster);
@@ -162,18 +163,17 @@ public RobotMap() {
162163
dtRight.setInverted(true);
163164

164165
rightVelocityController = new VelocityPIDController(Robot.getConst("VelocityRightkI", 0), 0,
165-
Robot.getConst("VelocityRightkD", calcDefkD()), 1 / Robot.getConst("Max Low Speed", 84), rightEncRate,
166-
dtRight);
166+
Robot.getConst("VelocityRightkD", calcDefkD(Robot.getConst("Max Low Speed", 84))),
167+
1 / Robot.getConst("Max Low Speed", 84), rightEncRate, dtRight);
167168
rightVelocityController.setInputRange(-Robot.getConst("Max High Speed", 204),
168169
Robot.getConst("Max High Speed", 204));
169170
rightVelocityController.setOutputRange(-1.0, 1.0);
170171
rightVelocityController.setContinuous(false);
171172
rightVelocityController.setAbsoluteTolerance(Robot.getConst("VelocityToleranceRight", 2));
172173

173-
// robotDrive = new DifferentialDrive(leftVelocityController,
174-
// rightVelocityController);
175-
// robotDrive.setMaxOutput(Robot.getConst("Max High Speed", 204));
176-
robotDrive = new DifferentialDrive(dtLeft, dtRight);
174+
robotDrive = new DifferentialDrive(leftVelocityController, rightVelocityController);
175+
robotDrive.setMaxOutput(Robot.getConst("Max High Speed", 204));
176+
// robotDrive = new DifferentialDrive(dtLeft, dtRight);
177177

178178
distEncAvg = new PIDSourceAverage(leftEncDist, rightEncDist);
179179
fancyGyro = new AHRS(SerialPort.Port.kMXP);
@@ -203,7 +203,7 @@ public int getPort(String key, int def) {
203203
/**
204204
* Uses SmartDashboard and math to calculate a *great* default kD
205205
*/
206-
public double calcDefkD() {
206+
public double calcDefkD(double maxSpeed) {
207207
/*
208208
* timeConstant is proportional to max speed of the shaft (which is the max
209209
* speed of the cim divided by the gear reduction), half the mass (because the
@@ -224,7 +224,7 @@ public double calcDefkD() {
224224
* one.
225225
*/
226226
double denominator = Math.pow(Math.E, 1 * cycleTime / timeConstant) - 1;
227-
return 1 / denominator;
227+
return 1 / denominator / maxSpeed;
228228
}
229229

230230
private double convertLbsTokG(double lbs) {

Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/VelocityPIDController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void set(double speed) {
6464
*/
6565
@Override
6666
public double get() {
67-
return getSetpoint();
67+
return out.get();
6868
}
6969

7070
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public TeleopDrive() {
2323
// Called just before this Command runs the first time
2424
@Override
2525
protected void initialize() {
26-
// Robot.dt.enableVelocityPIDs();
26+
Robot.dt.enableVelocityPIDs();
2727
}
2828

2929
// Called repeatedly when this Command is scheduled to run
3030
@Override
3131
protected void execute() {
3232
Robot.dt.resetVPIDInputRanges();
33-
// Robot.dt.updatePidConstants();
33+
Robot.dt.updatePidConstants();
3434
Robot.dt.teleopDrive();
3535
}
3636

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,8 @@ public double getDtRightSpeed() {
195195
*/
196196
@Override
197197
public void updatePidConstants() {
198-
leftVelocityController.setPID(Robot.getConst("VelocityLeftkI", 0), Robot.getConst("VelocityLeftkI", 0),
199-
Robot.getConst("VelocityLeftkD", 0));
200-
rightVelocityController.setPID(Robot.getConst("VelocityRightkI", 0), Robot.getConst("VelocityRightkI", 0),
201-
Robot.getConst("VelocityRightkD", 0));
198+
leftVelocityController.setPID(Robot.getConst("VelocityLeftkI", 0), 0, calcDefkD(getCurrentMaxSpeed()));
199+
rightVelocityController.setPID(Robot.getConst("VelocityRightkI", 0), 0, calcDefkD(getCurrentMaxSpeed()));
202200
resetVelocityPIDkFConsts();
203201
}
204202

@@ -366,4 +364,36 @@ public void putVelocityControllersToDashboard() {
366364
SmartDashboard.putData("Left PID Controller", leftVelocityController);
367365
SmartDashboard.putData("Right PID Controller", rightVelocityController);
368366
}
367+
368+
/**
369+
* Uses SmartDashboard and math to calculate a *great* default kD
370+
*/
371+
public double calcDefkD(double maxSpeed) {
372+
/*
373+
* timeConstant is proportional to max speed of the shaft (which is the max
374+
* speed of the cim divided by the gear reduction), half the mass (because the
375+
* half of the drivetrain only has to support half of the robot), and radius of
376+
* the drivetrain wheels squared. It's inversely proportional to the stall
377+
* torque of the shaft, which is found by multiplying the stall torque of the
378+
* motor with the gear reduction by the amount of motors.
379+
*/
380+
double gearReduction = Robot.getBool("High Gear", false) ? Robot.getConst("High Gear Gear Reduction", 5.392)
381+
: Robot.getConst("Low Gear Gear Reduction", 12.255);
382+
double radius = Robot.getConst("Radius of Drivetrain Wheel", 0.0635);
383+
double timeConstant = Robot.getConst("Omega Max", 5330) / gearReduction / 60 * 2 * Math.PI
384+
* convertNtokG(Robot.getConst("Weight of Robot", 342)) / 2 * radius * radius
385+
/ (Robot.getConst("Stall Torque", 2.41) * gearReduction * 2);
386+
double cycleTime = Robot.getConst("Code cycle time", 0.05);
387+
/*
388+
* The denominator of kD is 1-(e ^ -cycleTime / timeConstant). The numerator is
389+
* one.
390+
*/
391+
double denominator = Math.pow(Math.E, 1 * cycleTime / timeConstant) - 1;
392+
return 1 / denominator / maxSpeed;
393+
}
394+
395+
private double convertNtokG(double newtons) {
396+
// weight / accel due to grav = kg
397+
return newtons / 9.81;
398+
}
369399
}

0 commit comments

Comments
 (0)