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

Commit 1f762ad

Browse files
Fixes to FindTurnTimeConstant and PIDTurn.
1 parent 2979aee commit 1f762ad

3 files changed

Lines changed: 48 additions & 28 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ public void teleopPeriodic() {
245245
// SmartDashboard.putNumber("Avg Enc Dist", dt.getEncAvgDist());
246246
//
247247

248-
System.out.printf("Left: %1$5.2f; Right: %2$5.2f\n", RobotMap.dtLeft.get(), RobotMap.dtRight.get());
248+
// System.out.printf("Left: %1$5.2f; Right: %2$5.2f\n", RobotMap.dtLeft.get(),
249+
// RobotMap.dtRight.get());
249250

250251
SmartDashboard.putNumber("Angle", dt.getAHRSAngle());
251252
SmartDashboard.putNumber("Left Current draw", rmap.pdp.getCurrent(4));

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ public class FindTurnTimeConstant extends Command {
2020
private SmartDashboardInterface sd;
2121
private AHRS gyro;
2222
private Robot robot;
23-
private Timer tim = new Timer();
24-
private ArrayList<DataPoint> dataPoints = new ArrayList<DataPoint>();
23+
private Timer tim;
24+
private ArrayList<DataPoint> dataPoints;
2525

2626
private class DataPoint {
2727
public double seconds;
28-
public double degreesPerSecond;
28+
public double radiansPerSecond;
2929

30-
public DataPoint(double seconds, double degreesPerSecond) {
30+
public DataPoint(double seconds, double radiansPerSecond) {
3131
this.seconds = seconds;
32-
this.degreesPerSecond = degreesPerSecond;
32+
this.radiansPerSecond = radiansPerSecond;
3333
}
3434

3535
public String toString() {
36-
return "Time: " + seconds + " seconds; rotational velocity: " + degreesPerSecond + " degrees per second";
36+
return "Time: " + seconds + " seconds; rotational velocity: " + radiansPerSecond + " radians per second";
3737
}
3838
}
3939

@@ -53,12 +53,18 @@ public FindTurnTimeConstant(Robot robot, DrivetrainInterface dt, AHRS gyro, Smar
5353
// Called just before this Command runs the first time
5454
protected void initialize() {
5555
System.out.println("FindTurnTimeConstant is in init()");
56-
if (!robot.isTest()) {
57-
System.out.println("but you're not in test mode.");
58-
return;
59-
}
56+
// if (!robot.isTest()) {
57+
// System.out.println("but you're not in test mode.");
58+
// return;
59+
// }
60+
61+
// this should be done in low gear
62+
Robot.dt.shiftGears(false);
63+
64+
dataPoints = new ArrayList<DataPoint>();
6065

6166
System.out.println("and you're in test mode");
67+
tim = new Timer();
6268
tim.start();
6369
// spin at full speed
6470
dt.arcadeDrive(0, 1);
@@ -70,6 +76,7 @@ protected void execute() {
7076
DataPoint newMeasurement = new DataPoint(tim.get(), gyro.getRate());
7177
System.out.println(newMeasurement);
7278
dataPoints.add(newMeasurement);
79+
dt.arcadeDrive(0, 1);
7380
}
7481

7582
// Make this return true when this Command no longer needs to run execute()
@@ -78,26 +85,34 @@ protected boolean isFinished() {
7885
// true if the last and second-to-last rotational velocities measured are within
7986
// 5 degrees per second squared of each other. or if it's not a test, finish
8087
// immediately
81-
if (numberOfDataPoints < 2) {
88+
89+
// if we're still in the first 51 cycles (don't have enough data), then not
90+
// finished. also, we're probably still accelerating if we haven't reached 3
91+
// radians per second (the max will be around 5).
92+
if (numberOfDataPoints < 51 || Math.abs(dataPoints.get(numberOfDataPoints - 1).radiansPerSecond) < 3.0) {
8293
return false;
8394
}
84-
return (Math.abs(dataPoints.get(numberOfDataPoints - 1).degreesPerSecond
85-
- dataPoints.get(numberOfDataPoints - 2).degreesPerSecond) < 5) || !robot.isTest();
95+
96+
return (Math.abs(dataPoints.get(numberOfDataPoints - 1).radiansPerSecond
97+
- dataPoints.get(numberOfDataPoints - 51).radiansPerSecond) < 0.02);
8698
}
8799

88100
// Called once after isFinished returns true
89101
protected void end() {
90102
// stop moving
91103
dt.arcadeDrive(0, 0);
92104

93-
DataPoint lastDataPoint = dataPoints.get(dataPoints.size() - 1);
94-
System.out.println("Finished when " + lastDataPoint);
95-
// the rotvel of the data point we're looking for to find the time constant
96-
double magicValue = lastDataPoint.degreesPerSecond * (1 - (1 / Math.E));
105+
double lastRadiansPerSecond = dataPoints.get(dataPoints.size() - 1).radiansPerSecond;
106+
System.out.println("Finished at " + lastRadiansPerSecond + " radians per second");
107+
Robot.putConst("Max Turn Radians Per Second", lastRadiansPerSecond);
108+
109+
// the radians per second of the data point we're looking for to find the time
110+
// constant
111+
double magicValue = lastRadiansPerSecond * (1 - (1 / Math.E));
97112

98113
for (DataPoint datum : dataPoints) {
99114
// if we've past the magic value, we're done
100-
if (Math.abs(datum.degreesPerSecond) > Math.abs(magicValue)) {
115+
if (Math.abs(datum.radiansPerSecond) > Math.abs(magicValue)) {
101116
System.out.println("The magic value is " + datum);
102117
Robot.putConst("TurnTimeConstant", datum.seconds);
103118
// stop checking data

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,7 @@ public PIDTurn(double target, double[] point, DrivetrainInterface dt, SmartDashb
6868
if (Robot.dt != null) {
6969
requires(Robot.dt);
7070
}
71-
// calculates the maximum turning speed in degrees/sec based on the max linear
72-
// speed in inches/s and the distance (inches) between sides of the DT
73-
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * getDistanceBetweenWheels());
74-
double r = Robot.getConst("TurnPidR", 3.0);
75-
double kP = r / Robot.rmap.getDrivetrainTimeConstant() / maxTurnSpeed;
76-
double kI = 0;
77-
double kD = r / maxTurnSpeed;
78-
double kF = 1 / (maxTurnSpeed * sd.getConst("Default PID Update Time", 0.05));
79-
turnController = new PIDController(kP, kI, kD, kF, ahrs, this);
71+
turnController = new PIDController(0, 0, 0, 0, ahrs, this);
8072
// tim = new Timer();
8173
sd.putData("Turn PID", turnController);
8274
}
@@ -156,6 +148,18 @@ public PIDTurn(double[] point, DrivetrainInterface dt, SmartDashboardInterface s
156148
*/
157149
@Override
158150
protected void initialize() {
151+
// calculate pid constants
152+
153+
// max turn speed from FindTurnTimeConstant, converted to degrees
154+
double maxTurnSpeed = Robot.getConst("Max Turn Radians Per Second", 4.0) * 180 / Math.PI;
155+
double updateTime = sd.getConst("Default PID Update Time", 0.05);
156+
double r = Robot.getConst("TurnPidR", 3.0);
157+
158+
double kP = r / Robot.getConst("TurnTimeConstant", 0.2) / maxTurnSpeed;
159+
double kI = 0;
160+
double kD = r / (maxTurnSpeed * updateTime);
161+
double kF = 1 / (maxTurnSpeed * updateTime);
162+
turnController.setPID(kP, kI, kD, kF);
159163

160164
if (!turnToPoint) {
161165
if (absoluteRotation) {

0 commit comments

Comments
 (0)