From efcb985329b6f34bbaa2131e9be345d22af18c1d Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 11:52:10 -0400 Subject: [PATCH 01/11] chore(build): add WPILib math to dependencies --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 982e60b..669eb64 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,7 @@ dependencies { compileOnly "edu.wpi.first.wpilibj:wpilibj-java:2024.3.2" compileOnly "edu.wpi.first.wpiutil:wpiutil-java:2024.3.2" compileOnly "edu.wpi.first.wpilibj:commands:2024.3.2" + compileOnly "edu.wpi.first.wpimath:wpimath-java:2024.3.2" compileOnly "edu.wpi.first.wpilibNewCommands:wpilibNewCommands-java:2024.3.2" // PathPlannerLib From feed83024ffafce9e8aa512acbfee9e42b82aa2b Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 11:52:45 -0400 Subject: [PATCH 02/11] feat(math/curves): implement abstract Curve class and multiple different curve types. This commit adds the following curve types: - ExponentialCurve - y = (x/|x|) * ((1 + exaggeration)^|x| - 1) / exaggeration. - LimitedCurve - Limits another curve to a minimum and maximum value. - LinearCurve - y = mx + b - PiecewiseCurve - Piecewise functions using Java lambdas/functions for conditions - QuadraticCurve - Ax^2 + Bx + C - RadicalCurve - y = (x+C)^(1/A)+B - TimedCurve - An abstract curve that toggles between two outputs based on a timer. - NormalTimedCurve - A TimedCurve that toggles between x (when enabled) and curve(x) (when disabled). - DoubleTimedCurve - A TimedCurve that toggles between two curves. - ZeroTimedCurve - A TimedCurve that toggles between 0 (when disabled) and curve(x) (when enabled). --- .../frc5183/librobot/math/curve/Curve.java | 13 ++ .../librobot/math/curve/DoubleTimedCurve.java | 72 ++++++++++ .../librobot/math/curve/ExponentialCurve.java | 44 ++++++ .../librobot/math/curve/LimitedCurve.java | 87 ++++++++++++ .../librobot/math/curve/LinearCurve.java | 64 +++++++++ .../librobot/math/curve/NormalTimedCurve.java | 48 +++++++ .../librobot/math/curve/PiecewiseCurve.java | 127 ++++++++++++++++++ .../librobot/math/curve/QuadraticCurve.java | 87 ++++++++++++ .../librobot/math/curve/RadicalCurve.java | 87 ++++++++++++ .../librobot/math/curve/TimedCurve.java | 97 +++++++++++++ .../librobot/math/curve/ZeroTimedCurve.java | 48 +++++++ 11 files changed, 774 insertions(+) create mode 100644 src/main/java/net/frc5183/librobot/math/curve/Curve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java create mode 100644 src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java diff --git a/src/main/java/net/frc5183/librobot/math/curve/Curve.java b/src/main/java/net/frc5183/librobot/math/curve/Curve.java new file mode 100644 index 0000000..4e0130e --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/Curve.java @@ -0,0 +1,13 @@ +package net.frc5183.librobot.math.curve; + +/** + * An abstract class which represents a mathematical function/curve. + */ +public abstract class Curve { + /** + * Returns the value of the curve at the given x value. + * @param x The x value to evaluate the curve at. + * @return The value of the curve at the given x value. + */ + public abstract Double curve(Double x); +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java new file mode 100644 index 0000000..5d50753 --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java @@ -0,0 +1,72 @@ +package net.frc5183.librobot.math.curve; + +/** + * A {@link TimedCurve} that toggles between two curves. + */ +public class DoubleTimedCurve extends TimedCurve { + /** + * The curve to use when the curve is enabled. + */ + private Curve enabledCurve; + + /** + * The curve to use when the curve is disabled. + */ + private Curve disabledCurve; + + /** + * Creates a new {@link DoubleTimedCurve} with the given curves and delays. + * @param enabledCurve The curve to use when this curve is enabled. + * @param disabledCurve The curve to use when this curve is disabled. + * @param delayEnabled The delay in seconds before the curve is enabled. + * @param delayDisabled The delay in seconds before the curve is disabled. + * @see TimedCurve#TimedCurve(Double, Double) + */ + public DoubleTimedCurve(Curve enabledCurve, Curve disabledCurve, Double delayEnabled, Double delayDisabled) { + super(delayEnabled, delayDisabled); + this.enabledCurve = enabledCurve; + this.disabledCurve = disabledCurve; + } + + @Override + protected Double enabled(Double x) { + return enabledCurve.curve(x); + } + + @Override + protected Double disabled(Double x) { + return disabledCurve.curve(x); + } + + /** + * Returns the curve to use when this curve is enabled. + * @return The curve to use when this curve is enabled. + */ + public Curve getEnabledCurve() { + return enabledCurve; + } + + /** + * Sets the curve to use when this curve is enabled. + * @param enabledCurve The new curve to use when the curve is enabled. + */ + public void setEnabledCurve(Curve enabledCurve) { + this.enabledCurve = enabledCurve; + } + + /** + * Returns the curve to use when this curve is disabled. + * @return The curve to use when this curve is disabled. + */ + public Curve getDisabledCurve() { + return disabledCurve; + } + + /** + * Sets the curve to use when this curve is disabled. + * @param disabledCurve The new curve to use when the curve is disabled. + */ + public void setDisabledCurve(Curve disabledCurve) { + this.disabledCurve = disabledCurve; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java new file mode 100644 index 0000000..a7d362d --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java @@ -0,0 +1,44 @@ +package net.frc5183.librobot.math.curve; + +/** + * An {@link Curve} which represents an exponential function in the form y = (x/|x|) * ((1 + k)^|x| - 1) / k. + * Where k is the exaggeration of the curve. + */ +public class ExponentialCurve extends Curve { + /** + * The exaggeration of the curve. + */ + private double exaggeration; + + /** + * Creates a new {@link ExponentialCurve} with the given exaggeration. + * @param exaggeration The exaggeration of the curve. + */ + public ExponentialCurve(double exaggeration) { + this.exaggeration = exaggeration; + } + + @Override + public Double curve(Double x) { + if (x == 0) return 0d; + + // y = (x/|x|) * ((1 + exaggeration)^|x| - 1) / exaggeration + return (x / Math.abs(x)) * ((Math.pow(1 + exaggeration, Math.abs(x))) - 1) / exaggeration; + } + + /** + * Returns the exaggeration of the curve. + * @return The exaggeration of the curve. + */ + public double getExaggeration() { + return exaggeration; + } + + /** + * Sets the exaggeration of the curve. + * @param exaggeration The new exaggeration of the curve. + */ + public void setExaggeration(double exaggeration) { + this.exaggeration = exaggeration; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java new file mode 100644 index 0000000..66534ce --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java @@ -0,0 +1,87 @@ +package net.frc5183.librobot.math.curve; + +/** + * A {@link Curve} which limits the output of another curve to a certain range. + */ +public class LimitedCurve extends Curve { + /** + * The curve to limit. + */ + private Curve curve; + + /** + * The minimum value of the curve. + */ + private Double min; + + /** + * The maximum value of the curve. + */ + private Double max; + + /** + * Creates a new {@link LimitedCurve} with the given curve, minimum, and maximum values. + * @param curve The curve to limit. + * @param min The minimum value of the curve. + * @param max The maximum value of the curve. + */ + public LimitedCurve(Curve curve, Double min, Double max) { + this.curve = curve; + this.min = min; + this.max = max; + } + + @Override + public Double curve(Double x) { + Double y = curve.curve(x); + return Math.min(Math.max(y, min), max); + } + + /** + * Returns the curve to limit. + * @return The curve to limit. + */ + public Curve getCurve() { + return curve; + } + + /** + * Sets the curve to limit. + * @param curve The new curve to limit. + */ + public void setCurve(Curve curve) { + this.curve = curve; + } + + /** + * Returns the minimum value of the curve. + * @return The minimum value of the curve. + */ + public Double getMin() { + return min; + } + + /** + * Sets the minimum value of the curve. + * @param min The new minimum value of the curve. + */ + public void setMin(Double min) { + this.min = min; + } + + /** + * Returns the maximum value of the curve. + * @return The maximum value of the curve. + */ + public Double getMax() { + return max; + } + + /** + * Sets the maximum value of the curve. + * @param max The new maximum value of the curve. + */ + public void setMax(Double max) { + this.max = max; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java b/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java new file mode 100644 index 0000000..8f96bec --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java @@ -0,0 +1,64 @@ +package net.frc5183.librobot.math.curve; + +/** + * A {@link Curve} which represents a linear equation in the form y = mx + b. + */ +public class LinearCurve extends Curve { + /** + * The slope of the curve. + */ + private double slope; + + /** + * The y-intercept of the curve. + */ + private double yIntercept; + + /** + * Creates a new {@link LinearCurve} with the given slope and y-intercept. + * @param slope The slope of the curve. + * @param yIntercept The y-intercept of the curve. + */ + public LinearCurve(double slope, double yIntercept) { + this.slope = slope; + this.yIntercept = yIntercept; + } + + @Override + public Double curve(Double x) { + // y = mx + b + return slope * x + yIntercept; + } + + /** + * Returns the slope of the curve. + * @return The slope of the curve. + */ + public double getSlope() { + return slope; + } + + /** + * Sets the slope of the curve. + * @param slope The new slope of the curve. + */ + public void setSlope(double slope) { + this.slope = slope; + } + + /** + * Returns the y-intercept of the curve. + * @return The y-intercept of the curve. + */ + public double getYIntercept() { + return yIntercept; + } + + /** + * Sets the y-intercept of the curve. + * @param yIntercept The new y-intercept of the curve. + */ + public void setYIntercept(double yIntercept) { + this.yIntercept = yIntercept; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java new file mode 100644 index 0000000..d3fb978 --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java @@ -0,0 +1,48 @@ +package net.frc5183.librobot.math.curve; + +/** + * A @{link TimedCurve} that toggles between x (when disabled) and curve(x) (when enabled). + */ +public class NormalTimedCurve extends TimedCurve { + /** + * The curve to use when this curve is enabled. + */ + private Curve curve; + + /** + * Creates a new {@link NormalTimedCurve} with the given curve and delays. + * @param curve The curve to use when this curve is enabled. + * @param delayEnabled The delay in seconds before the curve is enabled. + * @param delayDisabled The delay in seconds before the curve is disabled. + */ + public NormalTimedCurve(Curve curve, Double delayEnabled, Double delayDisabled) { + super(delayEnabled, delayDisabled); + this.curve = curve; + } + + @Override + protected Double enabled(Double x) { + return curve(x); + } + + @Override + protected Double disabled(Double x) { + return x; + } + + /** + * Returns the curve to use when this curve is enabled. + * @return The curve to use when this curve is enabled. + */ + public Curve getCurve() { + return curve; + } + + /** + * Sets the curve to use when this curve is enabled. + * @param curve The new curve to use when the curve is enabled. + */ + public void setCurve(Curve curve) { + this.curve = curve; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java b/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java new file mode 100644 index 0000000..b27c9af --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java @@ -0,0 +1,127 @@ +package net.frc5183.librobot.math.curve; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * A {@link Curve} which represents a curve that is defined by multiple curves. + * The curve is determined by the first curve that returns true for its condition. + * If no conditions are met, the curve returns 0. + */ +public class PiecewiseCurve extends Curve { + /** + * A map of conditions to curves. + */ + private final Map, Curve> curves; + + /** + * Creates a new {@link PiecewiseCurve} with no curves. + */ + public PiecewiseCurve() { + this.curves = Map.of(); + } + + /** + * Creates a new {@link PiecewiseCurve} with the given condition and curve. + * @param condition The condition for the curve. + * @param curve The curve to use when the condition is true. + */ + public PiecewiseCurve(Function condition, Curve curve) { + this.curves = Map.of(condition, curve); + } + + /** + * Creates a new {@link PiecewiseCurve} with the given conditions and curves. + * @param curves The conditions and curves to use. + */ + @SafeVarargs + public PiecewiseCurve(Map.Entry, Curve>... curves) { + this.curves = Map.ofEntries(curves); + } + + /** + * Creates a new {@link PiecewiseCurve} with the given conditions and curves. + * @param curves The conditions and curves to use. + */ + public PiecewiseCurve(Map, Curve> curves) { + this.curves = curves; + } + + @Override + public Double curve(Double x) { + for (Map.Entry, Curve> entry : curves.entrySet()) { + if (entry.getKey().apply(x)) { + return entry.getValue().curve(x); + } + } + return 0d; + } + + /** + * Iterates over all the curves in this piecewise curve. + * @param consumer The consumer to apply to each curve. + */ + public void forEach(Consumer consumer) { + curves.values().forEach(consumer); + } + + /** + * Iterates over all the conditions in this piecewise curve. + * @param consumer The consumer to apply to each condition. + */ + public void forEachKey(Consumer> consumer) { + curves.keySet().forEach(consumer); + } + + /** + * Iterates over all the conditions and curves in this piecewise curve. + * @param consumer The consumer to apply to each condition and curve. + */ + public void forEachEntry(BiConsumer, Curve> consumer) { + curves.forEach(consumer); + } + + /** + * Replaces the curve with the given condition with the given curve. + * @param condition The condition to target when replacing. + * @param curve The curve to replace the condition with. + */ + public void replace(Function condition, Curve curve) { + curves.replace(condition, curve); + } + + /** + * Adds a new condition and curve to this piecewise curve. + * @param condition The condition for the curve. + * @param curve The curve to use when the condition is true. + */ + public void put(Function condition, Curve curve) { + curves.put(condition, curve); + } + + /** + * Gets the curve with the given condition. + * @param condition The condition to get the curve for. + * @return The curve with the given condition. + */ + public Curve get(Function condition) { + return curves.get(condition); + } + + /** + * Removes the curve with the given condition. + * @param condition The condition to remove the curve for. + */ + public void remove(Function condition) { + curves.remove(condition); + } + + /** + * Removes all curves from this piecewise curve. + */ + public void clear() { + curves.clear(); + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java b/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java new file mode 100644 index 0000000..075afc8 --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java @@ -0,0 +1,87 @@ +package net.frc5183.librobot.math.curve; + +/** + * A {@link Curve} which represents a quadratic equation in the form Ax^2 + Bx + C. + */ +public class QuadraticCurve extends Curve { + /** + * The "A" variable in the curve equation. + */ + private Double a; + + /** + * The "B" variable in the curve equation. + */ + private Double b; + + /** + * The "C" variable in the curve equation. + */ + private Double c; + + /** + * Creates a new {@link QuadraticCurve} with the given A, B, and C values. + * @param a The "A" variable in the curve equation. + * @param b The "B" variable in the curve equation. + * @param c The "C" variable in the curve equation. + */ + public QuadraticCurve(Double a, Double b, Double c) { + this.a = a; + this.b = b; + this.c = c; + } + + @Override + public Double curve(Double x) { + // y = Ax^2 + Bx + C + return (a * Math.pow(x, 2)) + (b * x) + c; + } + + /** + * Returns the "A" variable in the curve equation. + * @return The "A" variable in the curve equation. + */ + public Double getA() { + return a; + } + + /** + * Sets the "A" variable in the curve equation. + * @param a The new "A" variable in the curve equation. + */ + public void setA(Double a) { + this.a = a; + } + + /** + * Returns the "B" variable in the curve equation. + * @return The "B" variable in the curve equation. + */ + public Double getB() { + return b; + } + + /** + * Sets the "B" variable in the curve equation. + * @param b The new "B" variable in the curve equation. + */ + public void setB(Double b) { + this.b = b; + } + + /** + * Returns the "C" variable in the curve equation. + * @return The "C" variable in the curve equation. + */ + public Double getC() { + return c; + } + + /** + * Sets the "C" variable in the curve equation. + * @param c The new "C" variable in the curve equation. + */ + public void setC(Double c) { + this.c = c; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java b/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java new file mode 100644 index 0000000..86a0b2a --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java @@ -0,0 +1,87 @@ +package net.frc5183.librobot.math.curve; + +/** + * A {@link Curve} which represents a radical equation in the form y = (x+C)^(1/A)+B. + */ +public class RadicalCurve extends Curve { + /** + * The "A" variable in the curve equation. + */ + private Double a; + + /** + * The "B" variable in the curve equation. + */ + private Double b; + + /** + * The "C" variable in the curve equation. + */ + private Double c; + + /** + * Creates a new {@link RadicalCurve} with the given A, B, and C values. + * @param a The "A" variable in the curve equation. + * @param b The "B" variable in the curve equation. + * @param c The "C" variable in the curve equation. + */ + public RadicalCurve(Double a, Double b, Double c) { + this.a = a; + this.b = b; + this.c = c; + } + + @Override + public Double curve(Double x) { + // y = (x+C)^(1/A)+B + return Math.pow(x + c, 1 / a) + b; + } + + /** + * Returns the "A" variable in the curve equation. + * @return The "A" variable in the curve equation. + */ + public Double getA() { + return a; + } + + /** + * Sets the "A" variable in the curve equation. + * @param a The new "A" variable in the curve equation. + */ + public void setA(Double a) { + this.a = a; + } + + /** + * Returns the "B" variable in the curve equation. + * @return The "B" variable in the curve equation. + */ + public Double getB() { + return b; + } + + /** + * Sets the "B" variable in the curve equation. + * @param b The new "B" variable in the curve equation. + */ + public void setB(Double b) { + this.b = b; + } + + /** + * Returns the "C" variable in the curve equation. + * @return The "C" variable in the curve equation. + */ + public Double getC() { + return c; + } + + /** + * Sets the "C" variable in the curve equation. + * @param c The new "C" variable in the curve equation. + */ + public void setC(Double c) { + this.c = c; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java new file mode 100644 index 0000000..f30b673 --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java @@ -0,0 +1,97 @@ +package net.frc5183.librobot.math.curve; + +import edu.wpi.first.wpilibj.Timer; + +/** + * A {@link Curve} which toggles between two outputs based on a timer. + */ +public abstract class TimedCurve extends Curve { + /** + * The timer used to keep track of the time. + */ + private final Timer timer = new Timer(); + + /** + * Whether the curve is currently disabled. + */ + private boolean disabled = true; + + /** + * The delay in seconds before the curve is enabled. + */ + private Double delayEnabled; + + /** + * The delay in seconds before the curve is disabled. + */ + private Double delayDisabled; + + /** + * Creates a new {@link TimedCurve} with the given delays. + * @param delayEnabled The delay in seconds before the curve is enabled. + * @param delayDisabled The delay in seconds before the curve is disabled. + */ + public TimedCurve(Double delayEnabled, Double delayDisabled) { + this.delayEnabled = delayEnabled; + this.delayDisabled = delayDisabled; + } + + @Override + public Double curve(Double x) { + if (disabled && timer.hasElapsed(delayDisabled)) { + disabled = false; + timer.reset(); + } else if (!disabled && timer.hasElapsed(delayEnabled)) { + disabled = true; + timer.reset(); + } + + return disabled ? disabled(x) : enabled(x); + } + + /** + * Returns the value of the curve when it is enabled. + * @param x The x value to evaluate the curve at. + * @return The value of the curve when it is enabled. + */ + protected abstract Double enabled(Double x); + + /** + * Returns the value of the curve when it is disabled. + * @param x The x value to evaluate the curve at. + * @return The value of the curve when it is disabled. + */ + protected abstract Double disabled(Double x); + + /** + * Returns whether the curve is currently disabled. + * @return Whether the curve is currently disabled. + */ + public Double getDelayEnabled() { + return delayEnabled; + } + + /** + * Sets whether the curve is currently disabled. + * @param delayEnabled Whether the curve is currently disabled. + */ + public void setDelayEnabled(Double delayEnabled) { + this.delayEnabled = delayEnabled; + } + + /** + * Returns the delay in seconds before the curve is disabled. + * @return The delay in seconds before the curve is disabled. + */ + public Double getDelayDisabled() { + return delayDisabled; + } + + /** + * Sets the delay in seconds before the curve is disabled. + * @param delayDisabled The delay in seconds before the curve is disabled. + */ + public void setDelayDisabled(Double delayDisabled) { + this.delayDisabled = delayDisabled; + } +} \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java new file mode 100644 index 0000000..09a3cf3 --- /dev/null +++ b/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java @@ -0,0 +1,48 @@ +package net.frc5183.librobot.math.curve; + +/** + * A {@link TimedCurve} that toggles between 0 (when disabled) and curve(x) (when enabled); + */ +public class ZeroTimedCurve extends TimedCurve { + /** + * The curve to use when this curve is enabled. + */ + private Curve curve; + + /** + * Creates a new {@link ZeroTimedCurve} with the given curve and delays. + * @param curve The curve to use when this curve is enabled. + * @param delayEnabled The delay in seconds before the curve is enabled. + * @param delayDisabled The delay in seconds before the curve is disabled. + */ + public ZeroTimedCurve(Curve curve, Double delayEnabled, Double delayDisabled) { + super(delayEnabled, delayDisabled); + this.curve = curve; + } + + @Override + protected Double enabled(Double x) { + return curve.curve(x); + } + + @Override + protected Double disabled(Double x) { + return 0.0; + } + + /** + * Returns the curve to use when this curve is enabled. + * @return The curve to use when this curve is enabled. + */ + public Curve getCurve() { + return curve; + } + + /** + * Sets the curve to use when this curve is enabled. + * @param curve The new curve to use when the curve is enabled. + */ + public void setCurve(Curve curve) { + this.curve = curve; + } +} \ No newline at end of file From d23b0880fb1667fac89b6f39682ee939075f995b Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:00:26 -0400 Subject: [PATCH 03/11] style(math/curve/exponential): disable PMD check on line 26, as parentheses make the equation easier to read. --- .../java/net/frc5183/librobot/math/curve/ExponentialCurve.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java index a7d362d..4418c96 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java @@ -23,7 +23,7 @@ public Double curve(Double x) { if (x == 0) return 0d; // y = (x/|x|) * ((1 + exaggeration)^|x| - 1) / exaggeration - return (x / Math.abs(x)) * ((Math.pow(1 + exaggeration, Math.abs(x))) - 1) / exaggeration; + return (x / Math.abs(x)) * ((Math.pow(1 + exaggeration, Math.abs(x))) - 1) / exaggeration; // NOPMD - extra parentheses make the equation easier to read } /** From a1c367ffd1526f2dab311d6423235127b2eb6d2d Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:07:03 -0400 Subject: [PATCH 04/11] refactor(math/curves/exponential): validate input on ExponentialCurve.setExaggeration(double) --- .../net/frc5183/librobot/math/curve/ExponentialCurve.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java index 4418c96..f99d96f 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java @@ -39,6 +39,10 @@ public double getExaggeration() { * @param exaggeration The new exaggeration of the curve. */ public void setExaggeration(double exaggeration) { + if (exaggeration <= 0) { + throw new IllegalArgumentException("Exaggeration must be greater than 0."); + } + this.exaggeration = exaggeration; } } \ No newline at end of file From 0e5a435163aa0c84ed0c78df33a80c9c8e90181c Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:10:01 -0400 Subject: [PATCH 05/11] refactor(math/curves): only use Double where necessary, otherwise use primitive for null safety --- .../frc5183/librobot/math/curve/Curve.java | 2 +- .../librobot/math/curve/DoubleTimedCurve.java | 8 +++---- .../librobot/math/curve/ExponentialCurve.java | 2 +- .../librobot/math/curve/LimitedCurve.java | 18 +++++++-------- .../librobot/math/curve/LinearCurve.java | 2 +- .../librobot/math/curve/NormalTimedCurve.java | 6 ++--- .../librobot/math/curve/PiecewiseCurve.java | 2 +- .../librobot/math/curve/QuadraticCurve.java | 22 +++++++++---------- .../librobot/math/curve/RadicalCurve.java | 22 +++++++++---------- .../librobot/math/curve/TimedCurve.java | 20 ++++++++--------- .../librobot/math/curve/ZeroTimedCurve.java | 6 ++--- 11 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/main/java/net/frc5183/librobot/math/curve/Curve.java b/src/main/java/net/frc5183/librobot/math/curve/Curve.java index 4e0130e..378272f 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/Curve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/Curve.java @@ -9,5 +9,5 @@ public abstract class Curve { * @param x The x value to evaluate the curve at. * @return The value of the curve at the given x value. */ - public abstract Double curve(Double x); + public abstract double curve(double x); } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java index 5d50753..428dcde 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java @@ -20,21 +20,21 @@ public class DoubleTimedCurve extends TimedCurve { * @param disabledCurve The curve to use when this curve is disabled. * @param delayEnabled The delay in seconds before the curve is enabled. * @param delayDisabled The delay in seconds before the curve is disabled. - * @see TimedCurve#TimedCurve(Double, Double) + * @see TimedCurve#TimedCurve(double, double) */ - public DoubleTimedCurve(Curve enabledCurve, Curve disabledCurve, Double delayEnabled, Double delayDisabled) { + public DoubleTimedCurve(Curve enabledCurve, Curve disabledCurve, double delayEnabled, double delayDisabled) { super(delayEnabled, delayDisabled); this.enabledCurve = enabledCurve; this.disabledCurve = disabledCurve; } @Override - protected Double enabled(Double x) { + protected double enabled(double x) { return enabledCurve.curve(x); } @Override - protected Double disabled(Double x) { + protected double disabled(double x) { return disabledCurve.curve(x); } diff --git a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java index f99d96f..ee80f46 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java @@ -19,7 +19,7 @@ public ExponentialCurve(double exaggeration) { } @Override - public Double curve(Double x) { + public double curve(double x) { if (x == 0) return 0d; // y = (x/|x|) * ((1 + exaggeration)^|x| - 1) / exaggeration diff --git a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java index 66534ce..060d31c 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java @@ -12,12 +12,12 @@ public class LimitedCurve extends Curve { /** * The minimum value of the curve. */ - private Double min; + private double min; /** * The maximum value of the curve. */ - private Double max; + private double max; /** * Creates a new {@link LimitedCurve} with the given curve, minimum, and maximum values. @@ -25,15 +25,15 @@ public class LimitedCurve extends Curve { * @param min The minimum value of the curve. * @param max The maximum value of the curve. */ - public LimitedCurve(Curve curve, Double min, Double max) { + public LimitedCurve(Curve curve, double min, double max) { this.curve = curve; this.min = min; this.max = max; } @Override - public Double curve(Double x) { - Double y = curve.curve(x); + public double curve(double x) { + double y = curve.curve(x); return Math.min(Math.max(y, min), max); } @@ -57,7 +57,7 @@ public void setCurve(Curve curve) { * Returns the minimum value of the curve. * @return The minimum value of the curve. */ - public Double getMin() { + public double getMin() { return min; } @@ -65,7 +65,7 @@ public Double getMin() { * Sets the minimum value of the curve. * @param min The new minimum value of the curve. */ - public void setMin(Double min) { + public void setMin(double min) { this.min = min; } @@ -73,7 +73,7 @@ public void setMin(Double min) { * Returns the maximum value of the curve. * @return The maximum value of the curve. */ - public Double getMax() { + public double getMax() { return max; } @@ -81,7 +81,7 @@ public Double getMax() { * Sets the maximum value of the curve. * @param max The new maximum value of the curve. */ - public void setMax(Double max) { + public void setMax(double max) { this.max = max; } } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java b/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java index 8f96bec..8974f8c 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/LinearCurve.java @@ -25,7 +25,7 @@ public LinearCurve(double slope, double yIntercept) { } @Override - public Double curve(Double x) { + public double curve(double x) { // y = mx + b return slope * x + yIntercept; } diff --git a/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java index d3fb978..255c246 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java @@ -15,18 +15,18 @@ public class NormalTimedCurve extends TimedCurve { * @param delayEnabled The delay in seconds before the curve is enabled. * @param delayDisabled The delay in seconds before the curve is disabled. */ - public NormalTimedCurve(Curve curve, Double delayEnabled, Double delayDisabled) { + public NormalTimedCurve(Curve curve, double delayEnabled, double delayDisabled) { super(delayEnabled, delayDisabled); this.curve = curve; } @Override - protected Double enabled(Double x) { + protected double enabled(double x) { return curve(x); } @Override - protected Double disabled(Double x) { + protected double disabled(double x) { return x; } diff --git a/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java b/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java index b27c9af..18bd4b1 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/PiecewiseCurve.java @@ -50,7 +50,7 @@ public PiecewiseCurve(Map, Curve> curves) { } @Override - public Double curve(Double x) { + public double curve(double x) { for (Map.Entry, Curve> entry : curves.entrySet()) { if (entry.getKey().apply(x)) { return entry.getValue().curve(x); diff --git a/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java b/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java index 075afc8..e17fbfe 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/QuadraticCurve.java @@ -7,17 +7,17 @@ public class QuadraticCurve extends Curve { /** * The "A" variable in the curve equation. */ - private Double a; + private double a; /** * The "B" variable in the curve equation. */ - private Double b; + private double b; /** * The "C" variable in the curve equation. */ - private Double c; + private double c; /** * Creates a new {@link QuadraticCurve} with the given A, B, and C values. @@ -25,14 +25,14 @@ public class QuadraticCurve extends Curve { * @param b The "B" variable in the curve equation. * @param c The "C" variable in the curve equation. */ - public QuadraticCurve(Double a, Double b, Double c) { + public QuadraticCurve(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } @Override - public Double curve(Double x) { + public double curve(double x) { // y = Ax^2 + Bx + C return (a * Math.pow(x, 2)) + (b * x) + c; } @@ -41,7 +41,7 @@ public Double curve(Double x) { * Returns the "A" variable in the curve equation. * @return The "A" variable in the curve equation. */ - public Double getA() { + public double getA() { return a; } @@ -49,7 +49,7 @@ public Double getA() { * Sets the "A" variable in the curve equation. * @param a The new "A" variable in the curve equation. */ - public void setA(Double a) { + public void setA(double a) { this.a = a; } @@ -57,7 +57,7 @@ public void setA(Double a) { * Returns the "B" variable in the curve equation. * @return The "B" variable in the curve equation. */ - public Double getB() { + public double getB() { return b; } @@ -65,7 +65,7 @@ public Double getB() { * Sets the "B" variable in the curve equation. * @param b The new "B" variable in the curve equation. */ - public void setB(Double b) { + public void setB(double b) { this.b = b; } @@ -73,7 +73,7 @@ public void setB(Double b) { * Returns the "C" variable in the curve equation. * @return The "C" variable in the curve equation. */ - public Double getC() { + public double getC() { return c; } @@ -81,7 +81,7 @@ public Double getC() { * Sets the "C" variable in the curve equation. * @param c The new "C" variable in the curve equation. */ - public void setC(Double c) { + public void setC(double c) { this.c = c; } } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java b/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java index 86a0b2a..a0316ac 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java @@ -7,17 +7,17 @@ public class RadicalCurve extends Curve { /** * The "A" variable in the curve equation. */ - private Double a; + private double a; /** * The "B" variable in the curve equation. */ - private Double b; + private double b; /** * The "C" variable in the curve equation. */ - private Double c; + private double c; /** * Creates a new {@link RadicalCurve} with the given A, B, and C values. @@ -25,14 +25,14 @@ public class RadicalCurve extends Curve { * @param b The "B" variable in the curve equation. * @param c The "C" variable in the curve equation. */ - public RadicalCurve(Double a, Double b, Double c) { + public RadicalCurve(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } @Override - public Double curve(Double x) { + public double curve(double x) { // y = (x+C)^(1/A)+B return Math.pow(x + c, 1 / a) + b; } @@ -41,7 +41,7 @@ public Double curve(Double x) { * Returns the "A" variable in the curve equation. * @return The "A" variable in the curve equation. */ - public Double getA() { + public double getA() { return a; } @@ -49,7 +49,7 @@ public Double getA() { * Sets the "A" variable in the curve equation. * @param a The new "A" variable in the curve equation. */ - public void setA(Double a) { + public void setA(double a) { this.a = a; } @@ -57,7 +57,7 @@ public void setA(Double a) { * Returns the "B" variable in the curve equation. * @return The "B" variable in the curve equation. */ - public Double getB() { + public double getB() { return b; } @@ -65,7 +65,7 @@ public Double getB() { * Sets the "B" variable in the curve equation. * @param b The new "B" variable in the curve equation. */ - public void setB(Double b) { + public void setB(double b) { this.b = b; } @@ -73,7 +73,7 @@ public void setB(Double b) { * Returns the "C" variable in the curve equation. * @return The "C" variable in the curve equation. */ - public Double getC() { + public double getC() { return c; } @@ -81,7 +81,7 @@ public Double getC() { * Sets the "C" variable in the curve equation. * @param c The new "C" variable in the curve equation. */ - public void setC(Double c) { + public void setC(double c) { this.c = c; } } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java index f30b673..d2fe83c 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java @@ -19,25 +19,25 @@ public abstract class TimedCurve extends Curve { /** * The delay in seconds before the curve is enabled. */ - private Double delayEnabled; + private double delayEnabled; /** * The delay in seconds before the curve is disabled. */ - private Double delayDisabled; + private double delayDisabled; /** * Creates a new {@link TimedCurve} with the given delays. * @param delayEnabled The delay in seconds before the curve is enabled. * @param delayDisabled The delay in seconds before the curve is disabled. */ - public TimedCurve(Double delayEnabled, Double delayDisabled) { + public TimedCurve(double delayEnabled, double delayDisabled) { this.delayEnabled = delayEnabled; this.delayDisabled = delayDisabled; } @Override - public Double curve(Double x) { + public double curve(double x) { if (disabled && timer.hasElapsed(delayDisabled)) { disabled = false; timer.reset(); @@ -54,20 +54,20 @@ public Double curve(Double x) { * @param x The x value to evaluate the curve at. * @return The value of the curve when it is enabled. */ - protected abstract Double enabled(Double x); + protected abstract double enabled(double x); /** * Returns the value of the curve when it is disabled. * @param x The x value to evaluate the curve at. * @return The value of the curve when it is disabled. */ - protected abstract Double disabled(Double x); + protected abstract double disabled(double x); /** * Returns whether the curve is currently disabled. * @return Whether the curve is currently disabled. */ - public Double getDelayEnabled() { + public double getDelayEnabled() { return delayEnabled; } @@ -75,7 +75,7 @@ public Double getDelayEnabled() { * Sets whether the curve is currently disabled. * @param delayEnabled Whether the curve is currently disabled. */ - public void setDelayEnabled(Double delayEnabled) { + public void setDelayEnabled(double delayEnabled) { this.delayEnabled = delayEnabled; } @@ -83,7 +83,7 @@ public void setDelayEnabled(Double delayEnabled) { * Returns the delay in seconds before the curve is disabled. * @return The delay in seconds before the curve is disabled. */ - public Double getDelayDisabled() { + public double getDelayDisabled() { return delayDisabled; } @@ -91,7 +91,7 @@ public Double getDelayDisabled() { * Sets the delay in seconds before the curve is disabled. * @param delayDisabled The delay in seconds before the curve is disabled. */ - public void setDelayDisabled(Double delayDisabled) { + public void setDelayDisabled(double delayDisabled) { this.delayDisabled = delayDisabled; } } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java index 09a3cf3..8bf7b3b 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java @@ -15,18 +15,18 @@ public class ZeroTimedCurve extends TimedCurve { * @param delayEnabled The delay in seconds before the curve is enabled. * @param delayDisabled The delay in seconds before the curve is disabled. */ - public ZeroTimedCurve(Curve curve, Double delayEnabled, Double delayDisabled) { + public ZeroTimedCurve(Curve curve, double delayEnabled, double delayDisabled) { super(delayEnabled, delayDisabled); this.curve = curve; } @Override - protected Double enabled(Double x) { + protected double enabled(double x) { return curve.curve(x); } @Override - protected Double disabled(Double x) { + protected double disabled(double x) { return 0.0; } From 93589483014848146051711cfb30f5ef8641c054 Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:11:50 -0400 Subject: [PATCH 06/11] refactor(math/curves/exponential): also add exaggeration check to constructor of ExponentialCurve --- .../net/frc5183/librobot/math/curve/ExponentialCurve.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java index ee80f46..28a61d3 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java @@ -15,6 +15,10 @@ public class ExponentialCurve extends Curve { * @param exaggeration The exaggeration of the curve. */ public ExponentialCurve(double exaggeration) { + if (exaggeration <= 0) { + throw new IllegalArgumentException("Exaggeration must be greater than 0."); + } + this.exaggeration = exaggeration; } From a5f77d8733b65b06d1ed219382f364451cca547a Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:15:45 -0400 Subject: [PATCH 07/11] docs(math/curves/exponential): add proper documentation for the checks added in previous commits --- .../java/net/frc5183/librobot/math/curve/ExponentialCurve.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java index 28a61d3..4b9e8af 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ExponentialCurve.java @@ -13,6 +13,7 @@ public class ExponentialCurve extends Curve { /** * Creates a new {@link ExponentialCurve} with the given exaggeration. * @param exaggeration The exaggeration of the curve. + * @throws IllegalArgumentException If the exaggeration is less than or equal to 0. */ public ExponentialCurve(double exaggeration) { if (exaggeration <= 0) { @@ -41,6 +42,7 @@ public double getExaggeration() { /** * Sets the exaggeration of the curve. * @param exaggeration The new exaggeration of the curve. + * @throws IllegalArgumentException If the exaggeration is less than or equal to 0. */ public void setExaggeration(double exaggeration) { if (exaggeration <= 0) { From 0528588ed5ebfa514dd03544d755dfca6eb27130 Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:18:58 -0400 Subject: [PATCH 08/11] refactor(math/curve): add NotNull annotations where applicable --- .../librobot/math/curve/DoubleTimedCurve.java | 14 +++++++++----- .../frc5183/librobot/math/curve/LimitedCurve.java | 9 ++++++--- .../librobot/math/curve/NormalTimedCurve.java | 8 ++++++-- .../librobot/math/curve/ZeroTimedCurve.java | 8 ++++++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java index 428dcde..bcb95fd 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/DoubleTimedCurve.java @@ -1,5 +1,7 @@ package net.frc5183.librobot.math.curve; +import org.jetbrains.annotations.NotNull; + /** * A {@link TimedCurve} that toggles between two curves. */ @@ -7,11 +9,13 @@ public class DoubleTimedCurve extends TimedCurve { /** * The curve to use when the curve is enabled. */ + @NotNull private Curve enabledCurve; /** * The curve to use when the curve is disabled. */ + @NotNull private Curve disabledCurve; /** @@ -22,7 +26,7 @@ public class DoubleTimedCurve extends TimedCurve { * @param delayDisabled The delay in seconds before the curve is disabled. * @see TimedCurve#TimedCurve(double, double) */ - public DoubleTimedCurve(Curve enabledCurve, Curve disabledCurve, double delayEnabled, double delayDisabled) { + public DoubleTimedCurve(@NotNull Curve enabledCurve, @NotNull Curve disabledCurve, double delayEnabled, double delayDisabled) { super(delayEnabled, delayDisabled); this.enabledCurve = enabledCurve; this.disabledCurve = disabledCurve; @@ -42,7 +46,7 @@ protected double disabled(double x) { * Returns the curve to use when this curve is enabled. * @return The curve to use when this curve is enabled. */ - public Curve getEnabledCurve() { + public @NotNull Curve getEnabledCurve() { return enabledCurve; } @@ -50,7 +54,7 @@ public Curve getEnabledCurve() { * Sets the curve to use when this curve is enabled. * @param enabledCurve The new curve to use when the curve is enabled. */ - public void setEnabledCurve(Curve enabledCurve) { + public void setEnabledCurve(@NotNull Curve enabledCurve) { this.enabledCurve = enabledCurve; } @@ -58,7 +62,7 @@ public void setEnabledCurve(Curve enabledCurve) { * Returns the curve to use when this curve is disabled. * @return The curve to use when this curve is disabled. */ - public Curve getDisabledCurve() { + public @NotNull Curve getDisabledCurve() { return disabledCurve; } @@ -66,7 +70,7 @@ public Curve getDisabledCurve() { * Sets the curve to use when this curve is disabled. * @param disabledCurve The new curve to use when the curve is disabled. */ - public void setDisabledCurve(Curve disabledCurve) { + public void setDisabledCurve(@NotNull Curve disabledCurve) { this.disabledCurve = disabledCurve; } } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java index 060d31c..d356f90 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java @@ -1,5 +1,7 @@ package net.frc5183.librobot.math.curve; +import org.jetbrains.annotations.NotNull; + /** * A {@link Curve} which limits the output of another curve to a certain range. */ @@ -7,6 +9,7 @@ public class LimitedCurve extends Curve { /** * The curve to limit. */ + @NotNull private Curve curve; /** @@ -25,7 +28,7 @@ public class LimitedCurve extends Curve { * @param min The minimum value of the curve. * @param max The maximum value of the curve. */ - public LimitedCurve(Curve curve, double min, double max) { + public LimitedCurve(@NotNull Curve curve, double min, double max) { this.curve = curve; this.min = min; this.max = max; @@ -41,7 +44,7 @@ public double curve(double x) { * Returns the curve to limit. * @return The curve to limit. */ - public Curve getCurve() { + public @NotNull Curve getCurve() { return curve; } @@ -49,7 +52,7 @@ public Curve getCurve() { * Sets the curve to limit. * @param curve The new curve to limit. */ - public void setCurve(Curve curve) { + public void setCurve(@NotNull Curve curve) { this.curve = curve; } diff --git a/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java index 255c246..6df3ece 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/NormalTimedCurve.java @@ -1,5 +1,7 @@ package net.frc5183.librobot.math.curve; +import org.jetbrains.annotations.NotNull; + /** * A @{link TimedCurve} that toggles between x (when disabled) and curve(x) (when enabled). */ @@ -7,6 +9,7 @@ public class NormalTimedCurve extends TimedCurve { /** * The curve to use when this curve is enabled. */ + @NotNull private Curve curve; /** @@ -15,7 +18,7 @@ public class NormalTimedCurve extends TimedCurve { * @param delayEnabled The delay in seconds before the curve is enabled. * @param delayDisabled The delay in seconds before the curve is disabled. */ - public NormalTimedCurve(Curve curve, double delayEnabled, double delayDisabled) { + public NormalTimedCurve(@NotNull Curve curve, double delayEnabled, double delayDisabled) { super(delayEnabled, delayDisabled); this.curve = curve; } @@ -34,6 +37,7 @@ protected double disabled(double x) { * Returns the curve to use when this curve is enabled. * @return The curve to use when this curve is enabled. */ + @NotNull public Curve getCurve() { return curve; } @@ -42,7 +46,7 @@ public Curve getCurve() { * Sets the curve to use when this curve is enabled. * @param curve The new curve to use when the curve is enabled. */ - public void setCurve(Curve curve) { + public void setCurve(@NotNull Curve curve) { this.curve = curve; } } \ No newline at end of file diff --git a/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java index 8bf7b3b..6900ac6 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/ZeroTimedCurve.java @@ -1,5 +1,7 @@ package net.frc5183.librobot.math.curve; +import org.jetbrains.annotations.NotNull; + /** * A {@link TimedCurve} that toggles between 0 (when disabled) and curve(x) (when enabled); */ @@ -7,6 +9,7 @@ public class ZeroTimedCurve extends TimedCurve { /** * The curve to use when this curve is enabled. */ + @NotNull private Curve curve; /** @@ -15,7 +18,7 @@ public class ZeroTimedCurve extends TimedCurve { * @param delayEnabled The delay in seconds before the curve is enabled. * @param delayDisabled The delay in seconds before the curve is disabled. */ - public ZeroTimedCurve(Curve curve, double delayEnabled, double delayDisabled) { + public ZeroTimedCurve(@NotNull Curve curve, double delayEnabled, double delayDisabled) { super(delayEnabled, delayDisabled); this.curve = curve; } @@ -34,6 +37,7 @@ protected double disabled(double x) { * Returns the curve to use when this curve is enabled. * @return The curve to use when this curve is enabled. */ + @NotNull public Curve getCurve() { return curve; } @@ -42,7 +46,7 @@ public Curve getCurve() { * Sets the curve to use when this curve is enabled. * @param curve The new curve to use when the curve is enabled. */ - public void setCurve(Curve curve) { + public void setCurve(@NotNull Curve curve) { this.curve = curve; } } \ No newline at end of file From 16883ce4ca80a8ce1a810169c1d199a376ebe0bb Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:21:23 -0400 Subject: [PATCH 09/11] docs(math/curve/timed): fix documentation on TimedCurve.getDelayEnabled and TimedCurve.setDelayEnabled --- .../java/net/frc5183/librobot/math/curve/TimedCurve.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java index d2fe83c..9fdceb2 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/TimedCurve.java @@ -64,16 +64,16 @@ public double curve(double x) { protected abstract double disabled(double x); /** - * Returns whether the curve is currently disabled. - * @return Whether the curve is currently disabled. + * Returns the delay in seconds before the curve is enabled. + * @return The delay in seconds before the curve is enabled. */ public double getDelayEnabled() { return delayEnabled; } /** - * Sets whether the curve is currently disabled. - * @param delayEnabled Whether the curve is currently disabled. + * Sets the delay in seconds before the curve is enabled. + * @param delayEnabled The delay in seconds before the curve is enabled. */ public void setDelayEnabled(double delayEnabled) { this.delayEnabled = delayEnabled; From 046996fcbfacf3c4b5f0be2038c33c2f5e7b3675 Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:32:16 -0400 Subject: [PATCH 10/11] refactor(math/curves/limited): validate that minimum is not greater than maximum --- .../java/net/frc5183/librobot/math/curve/LimitedCurve.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java index d356f90..3df5158 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/LimitedCurve.java @@ -27,8 +27,11 @@ public class LimitedCurve extends Curve { * @param curve The curve to limit. * @param min The minimum value of the curve. * @param max The maximum value of the curve. + * @throws IllegalArgumentException If the minimum value is greater than the maximum value. */ public LimitedCurve(@NotNull Curve curve, double min, double max) { + if (min > max) throw new IllegalArgumentException("Minimum value cannot be greater than maximum value."); + this.curve = curve; this.min = min; this.max = max; @@ -67,8 +70,10 @@ public double getMin() { /** * Sets the minimum value of the curve. * @param min The new minimum value of the curve. + * @throws IllegalArgumentException If the minimum value is greater than the maximum value. */ public void setMin(double min) { + if (min > max) throw new IllegalArgumentException("Minimum value cannot be greater than maximum value."); this.min = min; } @@ -83,8 +88,10 @@ public double getMax() { /** * Sets the maximum value of the curve. * @param max The new maximum value of the curve. + * @throws IllegalArgumentException If the minimum value is greater than the maximum value. */ public void setMax(double max) { + if (min > max) throw new IllegalArgumentException("Minimum value cannot be greater than maximum value."); this.max = max; } } \ No newline at end of file From 20168b9514b6b2100d82edfa140f9d807e1391da Mon Sep 17 00:00:00 2001 From: Bacon Date: Mon, 30 Sep 2024 12:40:50 -0400 Subject: [PATCH 11/11] refactor(math/curves/radical): validate A != 0 --- .../java/net/frc5183/librobot/math/curve/RadicalCurve.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java b/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java index a0316ac..6446908 100644 --- a/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java +++ b/src/main/java/net/frc5183/librobot/math/curve/RadicalCurve.java @@ -24,8 +24,11 @@ public class RadicalCurve extends Curve { * @param a The "A" variable in the curve equation. * @param b The "B" variable in the curve equation. * @param c The "C" variable in the curve equation. + * @throws IllegalArgumentException If A is 0. */ public RadicalCurve(double a, double b, double c) { + if (a == 0) throw new IllegalArgumentException("A cannot be 0."); + this.a = a; this.b = b; this.c = c; @@ -48,8 +51,11 @@ public double getA() { /** * Sets the "A" variable in the curve equation. * @param a The new "A" variable in the curve equation. + * @throws IllegalArgumentException If A is 0. */ public void setA(double a) { + if (a == 0) throw new IllegalArgumentException("A cannot be 0."); + this.a = a; }