forked from frc971/971-Robot-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonmath.h
43 lines (37 loc) · 993 Bytes
/
commonmath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef AOS_MATH_H_
#define AOS_MATH_H_
#include <cmath>
namespace aos {
// Clips a value so that it is in [min, max]
static inline double Clip(double value, double min, double max) {
if (value > max) {
value = max;
} else if (value < min) {
value = min;
}
return value;
}
template <typename T>
static inline int sign(T val) {
if (val > T(0)) {
return 1;
} else {
return -1;
}
}
// Adds deadband to provided value. deadband is the region close to the origin
// to add the deadband to, and max is the maximum input value used to re-scale
// the output after adding the deadband.
static inline double Deadband(double value, const double deadband,
const double max) {
if (::std::abs(value) < deadband) {
value = 0.0;
} else if (value > 0.0) {
value = (value - deadband) / (max - deadband);
} else {
value = (value + deadband) / (max - deadband);
}
return value;
}
} // namespace aos
#endif // AOS_MATH_H_