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

Commit 385e1fb

Browse files
Merge branch 'master' of
https://github.com/theEgotisticalGiraffe/RobotCode2018.git into vel-pid Conflicts: Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDMove.java
2 parents 1b992ff + 46f773c commit 385e1fb

5 files changed

Lines changed: 126 additions & 40 deletions

File tree

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,44 @@ public double calcDefkD(double maxSpeed) {
239239
* be converted from rpm to radians per second, so divide by 60 and multiply to
240240
* get radians.
241241
*/
242-
double gearReduction = Robot.getBool("High Gear", false) ? Robot.getConst("High Gear Gear Reduction", 5.392)
243-
: Robot.getConst("Low Gear Gear Reduction", 12.255);
244-
double radius = Robot.getConst("Radius of Drivetrain Wheel", 0.0635);
245-
double timeConstant = Robot.getConst("Omega Max", 5330) / gearReduction / 60 * 2 * Math.PI
246-
* convertNtokG(Robot.getConst("Weight of Robot", 342)) / 2 * radius * radius
247-
/ (Robot.getConst("Stall Torque", 2.41) * gearReduction * 2);
248-
double cycleTime = Robot.getConst("Code cycle time", 0.05);
242+
double gearReduction = getGearRatio();
243+
double radius = getRadius();
244+
double timeConstant = getOmegaMax() / gearReduction / 60 * 2 * Math.PI
245+
* convertNtokG(getWeight()) / 2 * radius * radius
246+
/ (getStallTorque() * gearReduction * 2);
247+
double cycleTime = getCycleTime();
249248
/*
250249
* The denominator of kD is 1-(e ^ -cycleTime / timeConstant). The numerator is
251250
* one.
252251
*/
253252
double denominator = Math.pow(Math.E, 1 * cycleTime / timeConstant) - 1;
254253
return 1 / denominator / maxSpeed;
255254
}
255+
256+
public double getGearRatio() {
257+
return Robot.getBool("High Gear", false) ? Robot.getConst("High Gear Gear Reduction", 5.392)
258+
: Robot.getConst("Low Gear Gear Reduction", 12.255);
259+
}
260+
261+
public double getRadius() {
262+
return Robot.getConst("Radius of Drivetrain Wheel", 0.0635);
263+
}
264+
265+
public double getOmegaMax() {
266+
return Robot.getConst("Omega Max", 5330);
267+
}
268+
269+
public double getWeight() {
270+
return Robot.getConst("Weight of Robot", 342);
271+
}
272+
273+
public double getCycleTime() {
274+
return Robot.getConst("Code cycle time", 0.05);
275+
}
276+
277+
public double getStallTorque() {
278+
return Robot.getConst("Stall Torque", 2.41);
279+
}
256280

257281
private double convertLbsTokG(double lbs) {
258282
// number from google ;)

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,18 @@ public PIDMove(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
5151
}
5252
double kf = 1 / (dt.getCurrentMaxSpeed() * sd.getConst("Default PID Update Time", 0.05));
5353
moveController = new PIDController(sd.getConst("MovekP", 0.1), sd.getConst("MovekI", 0),
54-
sd.getConst("MovekD", 0), kf, avg, this);
54+
sd.getConst("MovekD", 0), kf, avg, this) {
55+
/**
56+
* Move Velocity: V = sqrt(8TGd) / (R*m) where T = max torque of wheels G = gear
57+
* ratio d = distance remaining R = radius of wheels m = mass
58+
*/
59+
@Override
60+
protected double calculateFeedForward() {
61+
double feedForwardConst = dt.getPIDMoveConstant();
62+
double setPt = getSetpoint();
63+
return (setPt / Math.abs(setPt)) * feedForwardConst * Math.sqrt(Math.abs(setPt));
64+
}
65+
};
5566
sd.putData("Move PID", moveController);
5667
}
5768

@@ -86,6 +97,20 @@ public PIDMove(double[] point, DrivetrainInterface dt, SmartDashboardInterface s
8697
moveController = new PIDController(sd.getConst("MovekP", 1), sd.getConst("MovekI", 0), sd.getConst("MovekD", 0),
8798
kf, avg, this);
8899
sd.putData("Move PID", moveController);
100+
moveController = new PIDController(sd.getConst("MovekP", 0.1), sd.getConst("MovekI", 0),
101+
sd.getConst("MovekD", 0), kf, avg, this) {
102+
/**
103+
* Move Velocity: V = sqrt(8TGd) / (R*m) where T = max torque of wheels G = gear
104+
* ratio d = distance remaining R = radius of wheels m = mass
105+
*/
106+
@Override
107+
protected double calculateFeedForward() {
108+
double feedForwardConst = dt.getPIDMoveConstant();
109+
double setPt = getSetpoint();
110+
return (setPt / Math.abs(setPt)) * feedForwardConst * Math.sqrt(Math.abs(setPt));
111+
}
112+
};
113+
sd.putData("Move PID", moveController);
89114
}
90115

91116
/**

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
6868
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * sd.getConst("Distance Between Wheels", 26.25));
6969
double kf = 1 / (maxTurnSpeed * sd.getConst("Default PID Update Time", 0.05));
7070
turnController = new PIDController(sd.getConst("TurnkP", 1), sd.getConst("TurnkI", 0), sd.getConst("TurnkD", 0),
71-
kf, ahrs, this);
71+
kf, ahrs, this) {
72+
/**
73+
* Turn Velocity: V = 4r sqrt((T*G*theta) / (R*m)) where r = half of distance
74+
* between wheels T = max torque of wheels G = gear ratio theta = rotational
75+
* distance to end of turn R = radius of wheels m = mass
76+
*/
77+
@Override
78+
protected double calculateFeedForward() {
79+
double feedForwardConst = dt.getPIDTurnConstant();
80+
double setpt = getSetpoint();
81+
return feedForwardConst * (getDistanceBetweenWheels() / 2) * (setpt / Math.abs(setpt))
82+
* Math.sqrt(Math.abs(setpt));
83+
}
84+
};
7285
// tim = new Timer();
7386
}
7487

@@ -114,6 +127,7 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
114127
*/
115128
public PIDTurn(double[] point, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource ahrs) {
116129
this.dt = dt;
130+
this.sd = sd;
117131
this.ahrs = ahrs;
118132
this.sd = sd;
119133

@@ -127,10 +141,23 @@ public PIDTurn(double[] point, DrivetrainInterface dt, SmartDashboardInterface s
127141
}
128142
// calculates the maximum turning speed in degrees/sec based on the max linear
129143
// speed in inches/s and the distance (inches) between sides of the DT
130-
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * sd.getConst("Distance Between Wheels", 26.25));
144+
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * getDistanceBetweenWheels());
131145
double kf = 1 / (maxTurnSpeed * sd.getConst("Default PID Update Time", 0.05));
132146
turnController = new PIDController(sd.getConst("TurnkP", 1), sd.getConst("TurnkI", 0), sd.getConst("TurnkD", 0),
133-
kf, ahrs, this);
147+
kf, ahrs, this) {
148+
/**
149+
* Turn Velocity: V = 4r sqrt((T*G*theta) / (R*m)) where r = half of distance
150+
* between wheels T = max torque of wheels G = gear ratio theta = rotational
151+
* distance to end of turn R = radius of wheels m = mass
152+
*/
153+
@Override
154+
protected double calculateFeedForward() {
155+
double feedForwardConst = dt.getPIDTurnConstant();
156+
double setpt = getSetpoint();
157+
return feedForwardConst * (getDistanceBetweenWheels() / 2) * (setpt / Math.abs(setpt))
158+
* Math.sqrt(Math.abs(setpt));
159+
}
160+
};
134161
// tim = new Timer();
135162
sd.putData("Turn PID", turnController);
136163
}
@@ -259,4 +286,13 @@ public void pidWrite(double output) {
259286
dt.arcadeDrive(0, output);
260287
SmartDashboard.putNumber("Turn PID Output", output);
261288
}
289+
290+
/**
291+
* Gets the distance between the two middle wheels.
292+
*
293+
* @return that distance
294+
*/
295+
private double getDistanceBetweenWheels() {
296+
return sd.getConst("Distance Between Wheels", 26.25);
297+
}
262298
}

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class Drivetrain extends Subsystem implements DrivetrainInterface {
4444
private final DifferentialDrive robotDrive = RobotMap.robotDrive;
4545
private final VelocityPIDController leftVelocityController = RobotMap.leftVelocityController;
4646
private final VelocityPIDController rightVelocityController = RobotMap.rightVelocityController;
47-
47+
4848
private final AHRS fancyGyro = RobotMap.fancyGyro;
4949
private final DoubleSolenoid dtGear = RobotMap.dtGear;
5050

@@ -222,8 +222,8 @@ public double getDtRightSpeed() {
222222
*/
223223
@Override
224224
public void updatePidConstants() {
225-
leftVelocityController.setPID(Robot.getConst("VelocityLeftkI", 0), 0, calcDefkD(getCurrentMaxSpeed()));
226-
rightVelocityController.setPID(Robot.getConst("VelocityRightkI", 0), 0, calcDefkD(getCurrentMaxSpeed()));
225+
leftVelocityController.setPID(Robot.getConst("VelocityLeftkI", 0), 0, Robot.rmap.calcDefkD(getCurrentMaxSpeed()));
226+
rightVelocityController.setPID(Robot.getConst("VelocityRightkI", 0), 0, Robot.rmap.calcDefkD(getCurrentMaxSpeed()));
227227
resetVelocityPIDkFConsts();
228228
}
229229

@@ -403,33 +403,22 @@ public void putVelocityControllersToDashboard() {
403403
SmartDashboard.putData("Right PID Controller", rightVelocityController);
404404
}
405405

406-
/**
407-
* Uses SmartDashboard and math to calculate a *great* default kD
408-
*/
409-
public double calcDefkD(double maxSpeed) {
410-
/*
411-
* timeConstant is proportional to max speed of the shaft (which is the max
412-
* speed of the cim divided by the gear reduction), half the mass (because the
413-
* half of the drivetrain only has to support half of the robot), and radius of
414-
* the drivetrain wheels squared. It's inversely proportional to the stall
415-
* torque of the shaft, which is found by multiplying the stall torque of the
416-
* motor with the gear reduction by the amount of motors.
417-
*/
418-
double gearReduction = Robot.getBool("High Gear", false) ? Robot.getConst("High Gear Gear Reduction", 5.392)
419-
: Robot.getConst("Low Gear Gear Reduction", 12.255);
420-
double radius = Robot.getConst("Radius of Drivetrain Wheel", 0.0635);
421-
double timeConstant = Robot.getConst("Omega Max", 5330) / gearReduction / 60 * 2 * Math.PI
422-
* convertNtokG(Robot.getConst("Weight of Robot", 342)) / 2 * radius * radius
423-
/ (Robot.getConst("Stall Torque", 2.41) * gearReduction * 2);
424-
double cycleTime = Robot.getConst("Code cycle time", 0.05);
425-
/*
426-
* The denominator of kD is 1-(e ^ -cycleTime / timeConstant). The numerator is
427-
* one.
428-
*/
429-
double denominator = Math.pow(Math.E, 1 * cycleTime / timeConstant) - 1;
430-
return 1 / denominator / maxSpeed;
406+
public double getPIDMoveConstant() {
407+
double G = Robot.rmap.getGearRatio();
408+
double T = Robot.rmap.getStallTorque();
409+
double R = Robot.rmap.getRadius();
410+
double M = Robot.rmap.getWeight();
411+
return Math.sqrt((8*T*G)/(R*M));
431412
}
432-
413+
414+
public double getPIDTurnConstant() {
415+
double G = Robot.rmap.getGearRatio();
416+
double T = Robot.rmap.getStallTorque();
417+
double R = Robot.rmap.getRadius();
418+
double M = Robot.rmap.getWeight();
419+
return 4 * Math.sqrt((T*G) / (R*M));
420+
}
421+
433422
private double convertNtokG(double newtons) {
434423
// weight / accel due to grav = kg
435424
return newtons / 9.81;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,16 @@ public interface DrivetrainInterface {
172172
* Put left and right velocity controllers (PID) on SmartDashboard.
173173
*/
174174
public void putVelocityControllersToDashboard();
175+
176+
/**
177+
* Calculates a constant for calculating feed forward in PIDMove
178+
* @return the constant
179+
*/
180+
public double getPIDMoveConstant();
181+
182+
/**
183+
* Calculates a constant for calculating feed forward in PIDTurn
184+
* @return the constant
185+
*/
186+
public double getPIDTurnConstant();
175187
}

0 commit comments

Comments
 (0)