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

Commit 13c3180

Browse files
author
Zhang
committed
in the middle of a change
1 parent cf3cca3 commit 13c3180

4 files changed

Lines changed: 76 additions & 39 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
@@ -241,20 +241,44 @@ public double calcDefkD(double maxSpeed) {
241241
* be converted from rpm to radians per second, so divide by 60 and multiply to
242242
* get radians.
243243
*/
244-
double gearReduction = Robot.getBool("High Gear", false) ? Robot.getConst("High Gear Gear Reduction", 5.392)
245-
: Robot.getConst("Low Gear Gear Reduction", 12.255);
246-
double radius = Robot.getConst("Radius of Drivetrain Wheel", 0.0635);
247-
double timeConstant = Robot.getConst("Omega Max", 5330) / gearReduction / 60 * 2 * Math.PI
248-
* convertNtokG(Robot.getConst("Weight of Robot", 342)) / 2 * radius * radius
249-
/ (Robot.getConst("Stall Torque", 2.41) * gearReduction * 2);
250-
double cycleTime = Robot.getConst("Code cycle time", 0.05);
244+
double gearReduction = getGearRatio();
245+
double radius = getRadius();
246+
double timeConstant = getOmegaMax() / gearReduction / 60 * 2 * Math.PI
247+
* convertNtokG(getWeight()) / 2 * radius * radius
248+
/ (getStallTorque() * gearReduction * 2);
249+
double cycleTime = getCycleTime();
251250
/*
252251
* The denominator of kD is 1-(e ^ -cycleTime / timeConstant). The numerator is
253252
* one.
254253
*/
255254
double denominator = Math.pow(Math.E, 1 * cycleTime / timeConstant) - 1;
256255
return 1 / denominator / maxSpeed;
257256
}
257+
258+
public double getGearRatio() {
259+
return Robot.getBool("High Gear", false) ? Robot.getConst("High Gear Gear Reduction", 5.392)
260+
: Robot.getConst("Low Gear Gear Reduction", 12.255);
261+
}
262+
263+
public double getRadius() {
264+
return Robot.getConst("Radius of Drivetrain Wheel", 0.0635);
265+
}
266+
267+
public double getOmegaMax() {
268+
return Robot.getConst("Omega Max", 5330);
269+
}
270+
271+
public double getWeight() {
272+
return Robot.getConst("Weight of Robot", 342);
273+
}
274+
275+
public double getCycleTime() {
276+
return Robot.getConst("Code cycle time", 0.05);
277+
}
278+
279+
public double getStallTorque() {
280+
return Robot.getConst("Stall Torque", 2.41);
281+
}
258282

259283
private double convertLbsTokG(double lbs) {
260284
// number from google ;)

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,25 @@ public PIDMove(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
4545
}
4646
double kf = 1 / (dt.getCurrentMaxSpeed() * sd.getConst("Default PID Update Time", 0.05));
4747
moveController = new PIDController(sd.getConst("MovekP", 0.1), sd.getConst("MovekI", 0),
48-
sd.getConst("MovekD", 0), kf, avg, this);
48+
sd.getConst("MovekD", 0), kf, avg, this) {
49+
/**
50+
* Move Velocity: V = sqrt(8TGd) / (R*m))
51+
* where T = max torque of wheels
52+
* G = gear ratio
53+
* d = distance remaining
54+
* R = radius of wheels
55+
* m = mass
56+
*/
57+
@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);
65+
}
66+
};
4967
}
5068

5169
/**

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,27 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
5656
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * sd.getConst("Distance Between Wheels", 26.25));
5757
double kf = 1 / (maxTurnSpeed * sd.getConst("Default PID Update Time", 0.05));
5858
turnController = new PIDController(sd.getConst("TurnkP", 1), sd.getConst("TurnkI", 0), sd.getConst("TurnkD", 0),
59-
kf, ahrs, this);
59+
kf, ahrs, this) {
60+
/**
61+
* Turn Velocity: V = 4r sqrt((T*G*theta) / (R*m))
62+
* where r = half of' distance between wheels
63+
* T = max torque of wheels
64+
* G = gear ratio
65+
* theta = rotational distance to end of turn
66+
* R = radius of wheels
67+
* m = mass
68+
*/
69+
@Override
70+
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));
78+
}
79+
};
6080
// tim = new Timer();
6181
}
6282

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

Lines changed: 5 additions & 30 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,8 @@ 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;
431-
}
432-
406+
407+
433408
private double convertNtokG(double newtons) {
434409
// weight / accel due to grav = kg
435410
return newtons / 9.81;

0 commit comments

Comments
 (0)