Skip to content

Commit 5932991

Browse files
authored
2.0.1
Fix Lazy Interpolation Gen, Add Filter Interface, and Pose#Mirror for Decode Autos
2 parents ebcefec + 12f27ec commit 5932991

17 files changed

+418
-73
lines changed

core/src/main/java/com/pedropathing/VectorCalculator.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ public Vector getDriveVector() {
182182
if (Math.abs(driveError) < drivePIDFSwitch && useSecondaryDrivePID) {
183183
secondaryDrivePIDF.updateFeedForwardInput(Math.signum(driveError));
184184
secondaryDrivePIDF.updateError(driveError);
185-
driveVector = new Vector(MathFunctions.clamp(secondaryDrivePIDF.runPIDF(), -maxPowerScaling, maxPowerScaling), tangent.getTheta());
185+
driveVector = new Vector(MathFunctions.clamp(secondaryDrivePIDF.run(), -maxPowerScaling, maxPowerScaling), tangent.getTheta());
186186
return driveVector.copy();
187187
}
188188

189189
drivePIDF.updateFeedForwardInput(Math.signum(driveError));
190190
drivePIDF.updateError(driveError);
191-
driveVector = new Vector(MathFunctions.clamp(drivePIDF.runPIDF(), -maxPowerScaling, maxPowerScaling), tangent.getTheta());
191+
driveVector = new Vector(MathFunctions.clamp(drivePIDF.run(), -maxPowerScaling, maxPowerScaling), tangent.getTheta());
192192
return driveVector.copy();
193193
}
194194

@@ -207,12 +207,12 @@ public Vector getHeadingVector() {
207207
if (Math.abs(headingError) < headingPIDFSwitch && useSecondaryHeadingPID) {
208208
secondaryHeadingPIDF.updateFeedForwardInput(MathFunctions.getTurnDirection(currentPose.getHeading(), headingGoal));
209209
secondaryHeadingPIDF.updateError(headingError);
210-
headingVector = new Vector(MathFunctions.clamp(secondaryHeadingPIDF.runPIDF(), -maxPowerScaling, maxPowerScaling), currentPose.getHeading());
210+
headingVector = new Vector(MathFunctions.clamp(secondaryHeadingPIDF.run(), -maxPowerScaling, maxPowerScaling), currentPose.getHeading());
211211
return headingVector.copy();
212212
}
213213
headingPIDF.updateFeedForwardInput(MathFunctions.getTurnDirection(currentPose.getHeading(), headingGoal));
214214
headingPIDF.updateError(headingError);
215-
headingVector = new Vector(MathFunctions.clamp(headingPIDF.runPIDF(), -maxPowerScaling, maxPowerScaling), currentPose.getHeading());
215+
headingVector = new Vector(MathFunctions.clamp(headingPIDF.run(), -maxPowerScaling, maxPowerScaling), currentPose.getHeading());
216216
return headingVector.copy();
217217
}
218218

@@ -259,21 +259,21 @@ public Vector getTranslationalCorrection() {
259259

260260
if (currentPose.distanceFrom(closestPose) < translationalPIDFSwitch && useSecondaryTranslationalPID) {
261261
secondaryTranslationalIntegral.updateError(translationalVector.getMagnitude());
262-
secondaryTranslationalIntegralVector = secondaryTranslationalIntegralVector.plus(new Vector(secondaryTranslationalIntegral.runPIDF() - previousSecondaryTranslationalIntegral, translationalVector.getTheta()));
263-
previousSecondaryTranslationalIntegral = secondaryTranslationalIntegral.runPIDF();
262+
secondaryTranslationalIntegralVector = secondaryTranslationalIntegralVector.plus(new Vector(secondaryTranslationalIntegral.run() - previousSecondaryTranslationalIntegral, translationalVector.getTheta()));
263+
previousSecondaryTranslationalIntegral = secondaryTranslationalIntegral.run();
264264

265265
secondaryTranslationalPIDF.updateFeedForwardInput(1);
266266
secondaryTranslationalPIDF.updateError(translationalVector.getMagnitude());
267-
translationalVector.setMagnitude(secondaryTranslationalPIDF.runPIDF());
267+
translationalVector.setMagnitude(secondaryTranslationalPIDF.run());
268268
translationalVector = translationalVector.plus(secondaryTranslationalIntegralVector);
269269
} else {
270270
translationalIntegral.updateError(translationalVector.getMagnitude());
271-
translationalIntegralVector = translationalIntegralVector.plus(new Vector(translationalIntegral.runPIDF() - previousTranslationalIntegral, translationalVector.getTheta()));
272-
previousTranslationalIntegral = translationalIntegral.runPIDF();
271+
translationalIntegralVector = translationalIntegralVector.plus(new Vector(translationalIntegral.run() - previousTranslationalIntegral, translationalVector.getTheta()));
272+
previousTranslationalIntegral = translationalIntegral.run();
273273

274274
translationalPIDF.updateFeedForwardInput(1);
275275
translationalPIDF.updateError(translationalVector.getMagnitude());
276-
translationalVector.setMagnitude(translationalPIDF.runPIDF());
276+
translationalVector.setMagnitude(translationalPIDF.run());
277277
translationalVector = translationalVector.plus(translationalIntegralVector);
278278
}
279279

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.pedropathing.control;
2+
3+
public interface Controller {
4+
double run();
5+
void reset();
6+
void updateError(double error);
7+
void updatePosition(double position);
8+
}

core/src/main/java/com/pedropathing/control/FilteredPIDFController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author Anyi Lin - 10158 Scott's Bots
99
* @version 1.0, 7/15/2024
1010
*/
11-
public class FilteredPIDFController {
11+
public class FilteredPIDFController implements Controller{
1212
private FilteredPIDFCoefficients coefficients;
1313

1414
private double previousError;
@@ -39,7 +39,7 @@ public FilteredPIDFController(FilteredPIDFCoefficients set) {
3939
*
4040
* @return this returns the value of the filtered PIDF from the current error.
4141
*/
42-
public double runPIDF() {
42+
public double run() {
4343
return error * P() + filteredDerivative * D() + errorIntegral * I() + F() * feedForwardInput;
4444
}
4545

core/src/main/java/com/pedropathing/control/KalmanFilter.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,32 @@
66
* @author Anyi Lin - 10158 Scott's Bots
77
* @version 1.0, 7/17/2024
88
*/
9-
public class KalmanFilter {
9+
public class KalmanFilter implements NoiseFilter {
1010
private KalmanFilterParameters parameters;
1111
private double state;
1212
private double variance;
1313
private double kalmanGain;
1414
private double previousState;
1515
private double previousVariance;
1616

17+
/**
18+
* This creates a new KalmanFilter from a set of KalmanFilterParameters.
19+
* @param parameters the parameters to use.
20+
*/
1721
public KalmanFilter(KalmanFilterParameters parameters) {
1822
this.parameters = parameters;
1923
reset();
2024
}
2125

26+
/**
27+
* This creates a new KalmanFilter from a set of KalmanFilterParameters, a starting state,
28+
* a starting variance, and a starting Kalman gain.
29+
*
30+
* @param parameters the parameters to use.
31+
* @param startState the starting state.
32+
* @param startVariance the starting variance.
33+
* @param startGain the starting Kalman gain.
34+
*/
2235
public KalmanFilter(KalmanFilterParameters parameters, double startState, double startVariance, double startGain) {
2336
this.parameters = parameters;
2437
reset(startState, startVariance, startGain);
@@ -50,6 +63,10 @@ public double getState() {
5063
return state;
5164
}
5265

66+
/**
67+
* This method outputs the current state, variance, and Kalman gain of the filter as a string array.
68+
* @return A string array containing the current state, variance, and Kalman gain.
69+
*/
5370
public String[] output() {
5471
return new String[]{
5572
"State: " + state,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.pedropathing.control;
2+
3+
/**
4+
* This is the LowPassFilter class. This class implements a simple low-pass filter to smooth out noisy data.
5+
*
6+
* @author Havish Sripada - 12808 RevAmped Robotics
7+
*/
8+
public class LowPassFilter implements NoiseFilter {
9+
private final double alpha;
10+
private double state;
11+
private boolean initialized;
12+
13+
/**
14+
* Constructs a LowPassFilter with the specified alpha value.
15+
* @param alpha The smoothing factor (0 [less than] alpha [less than] 1). A higher alpha means more smoothing.
16+
*/
17+
public LowPassFilter(double alpha) {
18+
if (alpha <= 0 || alpha >= 1) {
19+
throw new IllegalArgumentException("Alpha must be between 0 and 1 (exclusive).");
20+
}
21+
this.alpha = alpha;
22+
this.initialized = false;
23+
}
24+
25+
/**
26+
* Updates the filter with new data.
27+
* @param updateData The new data to update the filter with.
28+
* @param updateProjection The projection of the new data (not used in LowPassFilter).
29+
*/
30+
@Override
31+
public void update(double updateData, double updateProjection) {
32+
if (!initialized) {
33+
state = updateData;
34+
initialized = true;
35+
} else {
36+
state = alpha * updateData + (1 - alpha) * state;
37+
}
38+
}
39+
40+
/**
41+
* Gets the current state of the filter.
42+
* @return The current state of the filter.
43+
*/
44+
@Override
45+
public double getState() {
46+
return state;
47+
}
48+
49+
/**
50+
* Resets the filter to a specified state.
51+
* @param startState The state to reset the filter to.
52+
* @param startVariance The variance to reset the filter to (not used in LowPassFilter).
53+
* @param startGain The gain to reset the filter to (not used in LowPassFilter).
54+
*/
55+
@Override
56+
public void reset(double startState, double startVariance, double startGain) {
57+
this.state = startState;
58+
this.initialized = true;
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.pedropathing.control;
2+
3+
/**
4+
* This is the NoiseFilter interface. This interface defines the methods that any noise filter should implement.
5+
*
6+
* @author Havish Sripada - 12808 RevAmped Robotics
7+
*/
8+
public interface NoiseFilter {
9+
/**
10+
* Gets the current state of the filter.
11+
* @return the current state of the filter
12+
*/
13+
double getState();
14+
15+
/**
16+
* Updates the filter with new data.
17+
* @param updateData the new data to update the filter with
18+
* @param updateProjection the projection of the new data
19+
*/
20+
void update(double updateData, double updateProjection);
21+
22+
/**
23+
* Resets the filter to its initial state.
24+
*/
25+
default void reset() {
26+
reset(0, 1, 1);
27+
};
28+
29+
/**
30+
* Resets the filter to a specified state.
31+
* @param startState the state to reset the filter to
32+
* @param startVariance the variance to reset the filter to
33+
* @param startGain the gain to reset the filter to
34+
*/
35+
void reset(double startState, double startVariance, double startGain);
36+
}

core/src/main/java/com/pedropathing/control/PIDFController.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @author Harrison Womack - 10158 Scott's Bots
1313
* @version 1.0, 3/5/2024
1414
*/
15-
public class PIDFController {
15+
public class PIDFController implements Controller {
1616
private PIDFCoefficients coefficients;
1717

1818
private double previousError;
@@ -41,7 +41,7 @@ public PIDFController(PIDFCoefficients set) {
4141
*
4242
* @return this returns the value of the PIDF from the current error.
4343
*/
44-
public double runPIDF() {
44+
public double run() {
4545
return error * P() + errorDerivative * D() + errorIntegral * I() + feedForwardInput * F();
4646
}
4747

@@ -50,12 +50,12 @@ public double runPIDF() {
5050
* a target position to calculate error. This will update the error from the current position to
5151
* the target position specified.
5252
*
53-
* @param update This is the current position.
53+
* @param position This is the current position.
5454
*/
55-
public void updatePosition(double update) {
56-
position = update;
55+
public void updatePosition(double position) {
56+
this.position = position;
5757
previousError = error;
58-
error = targetPosition - position;
58+
error = targetPosition - this.position;
5959

6060
deltaTimeNano = System.nanoTime() - previousUpdateTimeNano;
6161
previousUpdateTimeNano = System.nanoTime();
@@ -221,4 +221,12 @@ public double F() {
221221
public double getError() {
222222
return error;
223223
}
224+
225+
/**
226+
* This returns the current derivative of the error.
227+
* @return the derivative
228+
*/
229+
public double getErrorDerivative() {
230+
return errorDerivative;
231+
}
224232
}

core/src/main/java/com/pedropathing/follower/Follower.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,4 +1040,8 @@ private void setPath(Path path) {
10401040
public PathPoint getPreviousClosestPose() {
10411041
return previousClosestPose;
10421042
}
1043+
1044+
public double getHeading() {
1045+
return getPose().getHeading();
1046+
}
10431047
}

core/src/main/java/com/pedropathing/geometry/BezierCurve.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @author Anyi Lin - 10158 Scott's Bots
2121
* @author Aaron Yang - 10158 Scott's Bots
2222
* @author Harrison Womack - 10158 Scott's Bots
23+
* @author William Phomphakdee - 7462 Not to Scale Alumni
2324
* @version 1.0, 3/5/2024
2425
*/
2526
public class BezierCurve implements Curve {

0 commit comments

Comments
 (0)