Sylib
A C++ Library For V5
Loading...
Searching...
No Matches
math.hpp
1
10#pragma once
11#include "sylib/env.hpp"
12
13namespace sylib {
14using kv_fn_t = std::function<double(double)>;
15
19class EMAFilter {
20 private:
21 double inputkA;
22 double ema;
23
24 public:
29
42 double filter(double rawValue, double kA);
43
49 double getkA() const;
50
56 double getCurrentEMA() const;
57};
58
62class SMAFilter {
63 private:
64 std::queue<double> rawInputValues;
65 std::uint16_t sampleSize;
66 double meanValue;
67 double rawVelocityTotal;
68
69 public:
78 SMAFilter(int sampleSize);
79
88 double filter(double rawValue);
89
95 int getQueueLength() const;
96
104 int getQueueMaxLength() const;
105
111 double getCurrentTotal() const;
112
118 double getCurrentValue() const;
119};
120
125 private:
126 std::deque<double> rawInputValues;
127 int sampleSize;
128 int queueLength;
129 double medianValue;
130 int meanSizeEven;
131 int meanSizeOdd;
132
133 public:
148 MedianFilter(int sampleSize, int meanSizeEven, int meanSizeOdd);
149
158 double filter(double rawValue);
159
165 int getQueueLength() const;
166
172 int getQueueMaxLength() const;
173
179 int getEvenCenterSize() const;
180
186 int getOddCenterSize() const;
187
193 double getCurrentValue() const;
194};
195
200 private:
201 std::deque<double> rawInputValues;
202 double maxValue;
203 int queueLength;
204 int sampleSize;
205
206 public:
216 RangeExtremaFilter(int sampleSize);
217
226 double filter(double rawValue);
227
233 int getQueueLength() const;
234
240 int getQueueMaxLength() const;
241
247 double getCurrentValue() const;
248};
249
254 private:
255 double currentInputFunctionValue;
256 double previousInputFunctionValue;
257 double deltaInputFunctionValue;
258 uint32_t currentTime;
259 uint32_t previousTime;
260 uint32_t dT;
261 double derivativeFunctionValue;
262
263 public:
271
280 double solveDerivative(double input);
281
287 double getCurrentDerivative() const;
288
294 double getCurrentInputValue() const;
295};
296
301 private:
302 kv_fn_t kV;
303 double voltageEstimate;
304 double motorGearing;
305
306 public:
318 VoltageEstimation(kv_fn_t kV, double motorGearing = 200);
319
327 double estimate(double rpm);
328
334 kv_fn_t getKv() const;
335
341 double getMotorGearing() const;
342
350 void setkV(kv_fn_t value);
351
357 double getOutput() const;
358};
359
364 private:
365
366 double kP;
367 double motorGearing;
368 std::shared_ptr<double> error;
369 double maxRange;
370 bool maxRangeEnabled;
371 double kP2;
372 double proportional;
373
374
375
376 public:
401 ProportionalController(double kP, std::shared_ptr<double> error, double motorGearing = 200,
402 bool maxRangeEnabled = false, double kP2 = 0, double maxRange = 0);
403
409 double update();
410
416 double getkP() const;
417
423 double getOutput() const;
424
431 void setkP(double gain);
432
438 double operator*();
439
446 void setMaxRangeEnabled(bool enabled);
447
455 void setMaxRange(double range);
456
463 void setkP2(double gain);
464};
465
470 private:
471
472 double kI;
473 double motorGearing;
474 std::shared_ptr<double> error;
475 bool antiWindupEnabled;
476 double antiWindupRange;
477 double integral;
478 uint32_t currentTime;
479 uint32_t previousTime;
480 uint32_t dT;
481
482
483 public:
504 IntegralController(double kI, std::shared_ptr<double> error, double motorGearing = 200,
505 bool antiWindupEnabled = false, double antiWindupRange = 0);
506
512 double update();
513
519 double getkI() const;
520
525
531 double getOutput() const;
532
538 double getCurrentTime() const;
539
545 uint32_t getdT() const;
546
553 void setkI(double gain);
554
560 double operator*();
561
568 void setAntiWindupEnabled(bool enabled);
569
576 void setAntiWindupRange(double range);
577};
578
583 private:
584 double kD;
585 double motorGearing;
586 std::shared_ptr<double> error;
587 double derivative;
588 double currentInput;
589 double previousInput;
590 uint32_t currentTime;
591 uint32_t previousTime;
592 uint32_t dT;
593
594 public:
608 DerivativeController(double kD, std::shared_ptr<double> error, double motorGearing = 200);
609
615 double update();
616
622 double getkD() const;
623
629 double getOutput() const;
630
636 double getCurrentTime() const;
637
643 uint32_t getdT() const;
644
651 void setkD(double gain);
652
658 double operator*();
659};
660
665 private:
666 std::shared_ptr<double> error;
667 double kH;
668 double output;
669 double tbh;
670 double previousError;
671 uint32_t currentTime;
672 uint32_t previousTime;
673 uint32_t dT;
674
675 public:
685 TakeBackHalfController(double kH, std::shared_ptr<double> error);
686
692 double update();
693
699 double getOutput() const;
700
706 double getkH() const;
707
713 double getTBH() const;
714
721 void setkH(double gain);
722
728 double getCurrentTime() const;
729
735 double operator*() const;
736};
737
747 kv_fn_t kV = [](double rpm) { return 0; };
748
752 double kP = 0;
753
757 double kI = 0;
758
762 double kD = 0;
763
767 double kH = 0;
768
772 bool antiWindupEnabled = false;
773
777 double antiWindupRange = 0;
778
782 bool pRangeEnabled = false;
783
787 double pRange = 0;
788
792 double kP2 = 0;
793
797 double maxVoltageRange = 0;
798
802 bool coastDownEnabled = false;
803
808 double coastDownRange = 0;
809
814
816 kv_fn_t kV = [](double rpm) { return 0; }, double kP = 0, double kI = 0, double kD = 0,
817 double kH = 0, bool antiWindupEnabled = false, double antiWindupRange = 0,
818 bool pRangeEnabled = false, double pRange = 0, double kP2 = 0, double maxVoltageRange = 0,
819 bool coastDownEnabled = false, double coastDownRange = 0, double coastDownModifier = 1)
820 : kV(kV),
821 kP(kP),
822 kI(kI),
823 kD(kD),
824 kH(kH),
828 pRange(pRange),
829 kP2(kP2),
834};
835
836} // namespace sylib
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.
Median Filter.
Definition: math.hpp:124
int getQueueLength() const
ets the current length of the queue of previous values
MedianFilter(int sampleSize, int meanSizeEven, int meanSizeOdd)
Creates a Median filter.
double getCurrentValue() const
Gets the current filter output without providing a new input value.
int getOddCenterSize() const
Gets the number of median values to average when the sample size is odd.
int getEvenCenterSize() const
Gets the number of median values to average when the sample size is even.
double filter(double rawValue)
Filters an input value.
int getQueueMaxLength() const
Gets the current sum of queue values.
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