John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_MATH_H_ |
| 2 | #define AOS_MATH_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 4 | #include <cmath> |
| 5 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | namespace aos { |
| 7 | |
| 8 | // Clips a value so that it is in [min, max] |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 9 | static inline double Clip(double value, double min, double max) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | if (value > max) { |
| 11 | value = max; |
| 12 | } else if (value < min) { |
| 13 | value = min; |
| 14 | } |
| 15 | return value; |
| 16 | } |
| 17 | |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 18 | template <typename T> |
| 19 | static inline int sign(T val) { |
| 20 | if (val > T(0)) { |
| 21 | return 1; |
| 22 | } else { |
| 23 | return -1; |
| 24 | } |
| 25 | } |
| 26 | |
Sabina Davis | 92d2efa | 2017-11-04 22:35:25 -0700 | [diff] [blame] | 27 | // Adds deadband to provided value. deadband is the region close to the origin |
| 28 | // to add the deadband to, and max is the maximum input value used to re-scale |
| 29 | // the output after adding the deadband. |
| 30 | static inline double Deadband(double value, const double deadband, |
| 31 | const double max) { |
| 32 | if (::std::abs(value) < deadband) { |
| 33 | value = 0.0; |
| 34 | } else if (value > 0.0) { |
| 35 | value = (value - deadband) / (max - deadband); |
| 36 | } else { |
| 37 | value = (value + deadband) / (max - deadband); |
| 38 | } |
| 39 | return value; |
| 40 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | } // namespace aos |
| 42 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 43 | #endif // AOS_MATH_H_ |