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

Commit 785296d

Browse files
committed
Override calculateFeedForward methods of PIDControllers in PIDMove and PIDTurn
1 parent 13c3180 commit 785296d

4 files changed

Lines changed: 44 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,17 @@ public PIDMove(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
4747
moveController = new PIDController(sd.getConst("MovekP", 0.1), sd.getConst("MovekI", 0),
4848
sd.getConst("MovekD", 0), kf, avg, this) {
4949
/**
50-
* Move Velocity: V = sqrt(8TGd) / (R*m))
50+
* Move Velocity: V = sqrt(8TGd) / (R*m)
5151
* where T = max torque of wheels
5252
* G = gear ratio
5353
* d = distance remaining
5454
* R = radius of wheels
5555
* m = mass
5656
*/
5757
@Override
58-
protected double calculateFeedForward() {
59-
double T = SmartDashboard.getNumber(,);
60-
double G = SmartDashboard.getNumber(,);
61-
double d = SmartDashboard.getNumber(,);
62-
double R = SmartDashboard.getNumber(,);
63-
double m = SmartDashboard.getNumber(,);
64-
return (d / Math.abs(d)) * Math.sqrt(8*T*G*Math.abs(d)) / (R*m);
58+
protected double calculateFeedForward() {
59+
double feedForwardConst = dt.getPIDMoveConstant();
60+
return (targ / Math.abs(targ)) * feedForwardConst * Math.sqrt(Math.abs(targ));
6561
}
6662
};
6763
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class PIDTurn extends Command implements PIDOutput {
1919

2020
private double target;
2121
private DrivetrainInterface dt;
22+
private SmartDashboardInterface sd;
2223
private PIDController turnController;
2324
private PIDSource ahrs;
2425
private Timer tim;
@@ -46,20 +47,21 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
4647
// Use requires() here to declare subsystem dependencies
4748
target = targ;
4849
this.dt = dt;
50+
this.sd = sd;
4951
this.ahrs = ahrs;
5052

5153
if (Robot.dt != null) {
5254
requires(Robot.dt);
5355
}
5456
// calculates the maximum turning speed in degrees/sec based on the max linear
5557
// speed in inches/s and the distance (inches) between sides of the DT
56-
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * sd.getConst("Distance Between Wheels", 26.25));
58+
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * getDistanceBetweenWheels());
5759
double kf = 1 / (maxTurnSpeed * sd.getConst("Default PID Update Time", 0.05));
5860
turnController = new PIDController(sd.getConst("TurnkP", 1), sd.getConst("TurnkI", 0), sd.getConst("TurnkD", 0),
5961
kf, ahrs, this) {
6062
/**
6163
* Turn Velocity: V = 4r sqrt((T*G*theta) / (R*m))
62-
* where r = half of' distance between wheels
64+
* where r = half of distance between wheels
6365
* T = max torque of wheels
6466
* G = gear ratio
6567
* theta = rotational distance to end of turn
@@ -68,13 +70,8 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
6870
*/
6971
@Override
7072
protected double calculateFeedForward() {
71-
double r =
72-
double T = SmartDashboard.getNumber(,);
73-
double G = SmartDashboard.getNumber(,);
74-
double theta = SmartDashboard.getNumber(,);
75-
double R = SmartDashboard.getNumber(,);
76-
double m = SmartDashboard.getNumber(,);
77-
return 4*r*(theta / Math.abs(theta)) * Math.sqrt((T*G*theta) / (R*m));
73+
double feedForwardConst = dt.getPIDTurnConstant();
74+
return feedForwardConst * (getDistanceBetweenWheels()/2) * (targ / Math.abs(targ)) * Math.sqrt(Math.abs(targ));
7875
}
7976
};
8077
// tim = new Timer();
@@ -183,4 +180,12 @@ public void pidWrite(double output) {
183180
dt.arcadeDrive(0, output);
184181
SmartDashboard.putNumber("Turn PID Output", output);
185182
}
183+
184+
/**
185+
* Gets the distance between the two middle wheels.
186+
* @return that distance
187+
*/
188+
private double getDistanceBetweenWheels() {
189+
return sd.getConst("Distance Between Wheels", 26.25);
190+
}
186191
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,21 @@ public void putVelocityControllersToDashboard() {
403403
SmartDashboard.putData("Right PID Controller", rightVelocityController);
404404
}
405405

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));
412+
}
406413

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+
}
407421

408422
private double convertNtokG(double newtons) {
409423
// weight / accel due to grav = kg

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)