11#include "sylib/env.hpp"
14using kv_fn_t = std::function<double(
double)>;
42 double filter(
double rawValue,
double kA);
64 std::queue<double> rawInputValues;
65 std::uint16_t sampleSize;
67 double rawVelocityTotal;
126 std::deque<double> rawInputValues;
201 std::deque<double> rawInputValues;
255 double currentInputFunctionValue;
256 double previousInputFunctionValue;
257 double deltaInputFunctionValue;
258 uint32_t currentTime;
259 uint32_t previousTime;
261 double derivativeFunctionValue;
303 double voltageEstimate;
368 std::shared_ptr<double> error;
370 bool maxRangeEnabled;
402 bool maxRangeEnabled =
false,
double kP2 = 0,
double maxRange = 0);
474 std::shared_ptr<double> error;
475 bool antiWindupEnabled;
476 double antiWindupRange;
478 uint32_t currentTime;
479 uint32_t previousTime;
505 bool antiWindupEnabled =
false,
double antiWindupRange = 0);
586 std::shared_ptr<double> error;
589 double previousInput;
590 uint32_t currentTime;
591 uint32_t previousTime;
666 std::shared_ptr<double> error;
670 double previousError;
671 uint32_t currentTime;
672 uint32_t previousTime;
816 kv_fn_t kV = [](
double rpm) {
return 0; },
double kP = 0,
double kI = 0,
double kD = 0,
D Controller.
Definition: math.hpp:582
void setkD(double gain)
Sets the kD constant.
DerivativeController(double kD, std::shared_ptr< double > error, double motorGearing=200)
Creates a D controller for a V5 motor.
double getCurrentTime() const
Gets the current time the controller is using.
double operator*()
Operator overload for getting the current filter ouput.
uint32_t getdT() const
Gets the most recent time difference between updates.
double getkD() const
Gets the kD constant.
double update()
Calculates controller output based on the current error value.
double getOutput() const
Gets the current stored output value without calculating a new value.
Exponential Moving Average Filter.
Definition: math.hpp:19
double getCurrentEMA() const
Gets the current filter output.
double filter(double rawValue, double kA)
Filters an input value.
double getkA() const
Gets the most recently used kA value.
EMAFilter()
Creates an Exponential Moving Average filter.
I Controller.
Definition: math.hpp:469
uint32_t getdT() const
Gets the most recent time difference between updates.
double getCurrentTime() const
Gets the current time the controller is using.
void resetValue()
Resets the integral value to 0.
void setAntiWindupRange(double range)
Sets the error range from the target value that is used for anti-windup.
IntegralController(double kI, std::shared_ptr< double > error, double motorGearing=200, bool antiWindupEnabled=false, double antiWindupRange=0)
Creates an I controller for a V5 motor.
double operator*()
Operator overload for getting the current filter ouput.
double getOutput() const
Gets the current stored output value without calculating a new value.
void setAntiWindupEnabled(bool enabled)
Sets whether the antiWindupEnabled flag is on or off.
double getkI() const
Gets the kI constant.
void setkI(double gain)
Sets the kI constant.
double update()
Calculates controller output based on the current error value.
P Controller.
Definition: math.hpp:363
ProportionalController(double kP, std::shared_ptr< double > error, double motorGearing=200, bool maxRangeEnabled=false, double kP2=0, double maxRange=0)
Creates a P controller for a V5 motor.
double update()
Calculates controller output based on the current error value.
void setMaxRangeEnabled(bool enabled)
Sets whether the maxRangeEnabled flag is on or off.
double getkP() const
Gets the kP constant.
double operator*()
Operator overload for getting the current filter ouput.
void setMaxRange(double range)
Sets the error range from the target value that is used for switching between kP and kP2.
void setkP(double gain)
Sets the kP constant.
double getOutput() const
Gets the current stored output value without calculating a new value.
void setkP2(double gain)
Sets the secondary kP constant.
Extrema Filter.
Definition: math.hpp:199
int getQueueLength() const
Gets the current length of the queue of previous values.
int getQueueMaxLength() const
Gets the number of median values to average when the sample size is even.
RangeExtremaFilter(int sampleSize)
Creates an Extrema filter that returns the largest absolute value of recent inputs.
double filter(double rawValue)
Filters an input value.
double getCurrentValue() const
Gets the current filter output without providing a new input value.
Simple Moving Average Filter.
Definition: math.hpp:62
double getCurrentTotal() const
Gets the current sum of queue values.
SMAFilter(int sampleSize)
Creates a Simple Moving Average filter.
int getQueueMaxLength() const
Gets the maximum length of the queue of previous values.
double getCurrentValue() const
Gets the current filter output without providing a new input value.
double filter(double rawValue)
Filters an input value.
int getQueueLength() const
Gets the current length of the queue of previous values.
Generic Derivative Solver.
Definition: math.hpp:253
double solveDerivative(double input)
Calculates the slope between the last input value and the current input value.
double getCurrentDerivative() const
Gets the current stored output value without adding any new input.
SympleDerivativeSolver()
Creates a generic derivative solver that outputs the dX/dT, where X is the input value provided,...
double getCurrentInputValue() const
Gets the most recent input value.
TBH Controller.
Definition: math.hpp:664
double getTBH() const
Gets the current stored TBH value.
TakeBackHalfController(double kH, std::shared_ptr< double > error)
Creates an Take Back Half controller for a V5 motor.
double operator*() const
Operator overload for getting the current filter ouput.
void setkH(double gain)
Sets the kH constant.
double getCurrentTime() const
Gets the current time the controller is using.
double getOutput() const
Gets the current stored output value without calculating a new value.
double update()
Calculates controller output based on the current error value.
double getkH() const
Gets the kH constant.
Feedforward Controller.
Definition: math.hpp:300
void setkV(kv_fn_t value)
Sets the kV function.
double getOutput() const
Gets the current stored output value without adding any new input.
VoltageEstimation(kv_fn_t kV, double motorGearing=200)
Creates a simple feedforward controller for a V5 motor.
kv_fn_t getKv() const
Gets the current kV function.
double estimate(double rpm)
Outputs an estimated voltage value for acheiving the desired RPM based on the kV function supplied in...
double getMotorGearing() const
Gets the set motor gearing for the estimator.
Definition: addrled.hpp:20
std::function< double(double)> kv_fn_t
Definition: math.hpp:14
Custom Velocity Controller.
Definition: math.hpp:743
double antiWindupRange
Range to use for the anti-windup on the I controller, if enabled.
Definition: math.hpp:777
kv_fn_t kV
Lambda function to use for feedforward kV calculation.
Definition: math.hpp:747
bool coastDownEnabled
Whether or not to enable coast-down to more quickly move to a lower velocity.
Definition: math.hpp:802
double coastDownModifier
Constant to multiple the applied voltage by if error is below the coast-down range.
Definition: math.hpp:813
double maxVoltageRange
Positive range from target above which the motor applies max voltage outside of.
Definition: math.hpp:797
double kI
I controller gain.
Definition: math.hpp:757
double kH
TBH controller gain.
Definition: math.hpp:767
bool antiWindupEnabled
Whether or not to enable anti-windup on the I controller.
Definition: math.hpp:772
bool pRangeEnabled
Whether or not to change the P controller gain within a certain target range.
Definition: math.hpp:782
double kP
P controller gain.
Definition: math.hpp:752
double kP2
Secondary kP value that the P controller uses when inside the target range, if enabled.
Definition: math.hpp:792
SpeedControllerInfo(kv_fn_t kV=[](double rpm) { return 0;}, double kP=0, double kI=0, double kD=0, double kH=0, bool antiWindupEnabled=false, double antiWindupRange=0, bool pRangeEnabled=false, double pRange=0, double kP2=0, double maxVoltageRange=0, bool coastDownEnabled=false, double coastDownRange=0, double coastDownModifier=1)
Definition: math.hpp:815
double pRange
Range to use for changing P controller gain, if enabled.
Definition: math.hpp:787
double kD
D controller gain.
Definition: math.hpp:762
double coastDownRange
Negative range from target below which the applied voltage is multiplied by the coast-down modifier.
Definition: math.hpp:808