Workaround: SERIAL_CHECK_TX is broken on F7 (#14052)
[betaflight.git] / src / main / common / maths.h
blobe0345fdf4966d12b8be427149bd6038dbf9a2333
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include <stdint.h>
25 #ifndef sq
26 #define sq(x) ((x)*(x))
27 #endif
28 #define power3(x) ((x)*(x)*(x))
29 #define power5(x) ((x)*(x)*(x)*(x)*(x))
31 // Undefine this for use libc sinf/cosf. Keep this defined to use fast sin/cos approximations
32 #define FAST_MATH // order 9 approximation
33 #define VERY_FAST_MATH // order 7 approximation
35 // Use floating point M_PI instead explicitly.
36 #define M_PIf 3.14159265358979323846f
37 #define M_EULERf 2.71828182845904523536f
39 #define RAD (M_PIf / 180.0f)
40 #define DEGREES_TO_DECIDEGREES(angle) ((angle) * 10)
41 #define DECIDEGREES_TO_DEGREES(angle) ((angle) / 10)
42 #define DECIDEGREES_TO_RADIANS(angle) ((angle) / 10.0f * 0.0174532925f)
43 #define DEGREES_TO_RADIANS(angle) ((angle) * RAD)
44 #define RADIANS_TO_DEGREES(angle) ((angle) / RAD)
46 #define CM_S_TO_KM_H(centimetersPerSecond) ((centimetersPerSecond) * 36 / 1000)
47 #define CM_S_TO_MPH(centimetersPerSecond) ((centimetersPerSecond) * 10000 / 5080 / 88)
49 #define MIN(a,b) \
50 __extension__ ({ __typeof__ (a) _a = (a); \
51 __typeof__ (b) _b = (b); \
52 _a < _b ? _a : _b; })
53 #define MAX(a,b) \
54 __extension__ ({ __typeof__ (a) _a = (a); \
55 __typeof__ (b) _b = (b); \
56 _a > _b ? _a : _b; })
57 #define ABS(x) \
58 __extension__ ({ __typeof__ (x) _x = (x); \
59 _x > 0 ? _x : -_x; })
60 #define SIGN(x) \
61 __extension__ ({ __typeof__ (x) _x = (x); \
62 (_x > 0) - (_x < 0); })
64 #define Q12 (1 << 12)
66 #define HZ_TO_INTERVAL(x) (1.0f / (x))
67 #define HZ_TO_INTERVAL_US(x) (1000000 / (x))
69 typedef int32_t fix12_t;
71 typedef struct stdev_s
73 float m_oldM, m_newM, m_oldS, m_newS;
74 int m_n;
75 } stdev_t;
77 // Floating point Euler angles.
78 // Be carefull, could be either of degrees or radians.
79 typedef struct fp_angles {
80 float roll;
81 float pitch;
82 float yaw;
83 } fp_angles_def;
85 typedef union {
86 float raw[3];
87 fp_angles_def angles;
88 } fp_angles_t;
90 int gcd(int num, int denom);
91 int32_t applyDeadband(int32_t value, int32_t deadband);
92 float fapplyDeadband(float value, float deadband);
94 void devClear(stdev_t *dev);
95 void devPush(stdev_t *dev, float x);
96 float devVariance(stdev_t *dev);
97 float devStandardDeviation(stdev_t *dev);
98 float degreesToRadians(int16_t degrees);
100 int scaleRange(int x, int srcFrom, int srcTo, int destFrom, int destTo);
101 float scaleRangef(float x, float srcFrom, float srcTo, float destFrom, float destTo);
103 int32_t quickMedianFilter3(const int32_t * v);
104 int32_t quickMedianFilter5(const int32_t * v);
105 int32_t quickMedianFilter7(const int32_t * v);
106 int32_t quickMedianFilter9(const int32_t * v);
108 float quickMedianFilter3f(const float * v);
109 float quickMedianFilter5f(const float * v);
110 float quickMedianFilter7f(const float * v);
111 float quickMedianFilter9f(const float * v);
113 #if defined(FAST_MATH) || defined(VERY_FAST_MATH)
114 float sin_approx(float x);
115 float cos_approx(float x);
116 float atan2_approx(float y, float x);
117 float acos_approx(float x);
118 float asin_approx(float x);
119 #define tan_approx(x) (sin_approx(x) / cos_approx(x))
120 float exp_approx(float val);
121 float log_approx(float val);
122 float pow_approx(float a, float b);
123 #else
124 #define sin_approx(x) sinf(x)
125 #define cos_approx(x) cosf(x)
126 #define atan2_approx(y,x) atan2f(y,x)
127 #define acos_approx(x) acosf(x)
128 #define tan_approx(x) tanf(x)
129 #define exp_approx(x) expf(x)
130 #define log_approx(x) logf(x)
131 #define pow_approx(a, b) powf(b, a)
132 #endif
134 void arraySubInt32(int32_t *dest, const int32_t *array1, const int32_t *array2, int count);
136 int16_t qPercent(fix12_t q);
137 int16_t qMultiply(fix12_t q, int16_t input);
138 fix12_t qConstruct(int16_t num, int16_t den);
140 float smoothStepUpTransition(const float x, const float center, const float width);
142 static inline int constrain(int amt, int low, int high)
144 if (amt < low)
145 return low;
146 else if (amt > high)
147 return high;
148 else
149 return amt;
152 static inline float constrainf(float amt, float low, float high)
154 if (amt < low)
155 return low;
156 else if (amt > high)
157 return high;
158 else
159 return amt;