blob: 0130992d57edea3e664556bbf19b318b3bf02a5f [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_MATH_H_
2#define AOS_MATH_H_
brians343bc112013-02-10 01:53:46 +00003
Sabina Davis92d2efa2017-11-04 22:35:25 -07004#include <cmath>
5
brians343bc112013-02-10 01:53:46 +00006namespace aos {
7
8// Clips a value so that it is in [min, max]
Brian Silvermanad9e0002014-04-13 14:55:57 -07009static inline double Clip(double value, double min, double max) {
brians343bc112013-02-10 01:53:46 +000010 if (value > max) {
11 value = max;
12 } else if (value < min) {
13 value = min;
14 }
15 return value;
16}
17
Brian Silvermanad9e0002014-04-13 14:55:57 -070018template <typename T>
19static inline int sign(T val) {
20 if (val > T(0)) {
21 return 1;
22 } else {
23 return -1;
24 }
25}
26
Sabina Davis92d2efa2017-11-04 22:35:25 -070027// 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.
30static 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}
brians343bc112013-02-10 01:53:46 +000041} // namespace aos
42
John Park33858a32018-09-28 23:05:48 -070043#endif // AOS_MATH_H_