Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / common / maths.h
blobe0a655c04c66cd4862ef0fd55f1ad278466e4f32
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))
30 // Undefine this for use libc sinf/cosf. Keep this defined to use fast sin/cos approximations
31 #define FAST_MATH // order 9 approximation
32 #define VERY_FAST_MATH // order 7 approximation
34 // Use floating point M_PI instead explicitly.
35 #define M_PIf 3.14159265358979323846f
36 #define M_EULERf 2.71828182845904523536f
38 #define RAD (M_PIf / 180.0f)
39 #define DEGREES_TO_DECIDEGREES(angle) ((angle) * 10)
40 #define DECIDEGREES_TO_DEGREES(angle) ((angle) / 10)
41 #define DECIDEGREES_TO_RADIANS(angle) ((angle) / 10.0f * 0.0174532925f)
42 #define DEGREES_TO_RADIANS(angle) ((angle) * 0.0174532925f)
44 #define CM_S_TO_KM_H(centimetersPerSecond) ((centimetersPerSecond) * 36 / 1000)
45 #define CM_S_TO_MPH(centimetersPerSecond) ((centimetersPerSecond) * 10000 / 5080 / 88)
47 #define MIN(a,b) \
48 __extension__ ({ __typeof__ (a) _a = (a); \
49 __typeof__ (b) _b = (b); \
50 _a < _b ? _a : _b; })
51 #define MAX(a,b) \
52 __extension__ ({ __typeof__ (a) _a = (a); \
53 __typeof__ (b) _b = (b); \
54 _a > _b ? _a : _b; })
55 #define ABS(x) \
56 __extension__ ({ __typeof__ (x) _x = (x); \
57 _x > 0 ? _x : -_x; })
59 #define Q12 (1 << 12)
61 #define HZ_TO_INTERVAL(x) (1.0f / (x))
62 #define HZ_TO_INTERVAL_US(x) (1000000 / (x))
64 typedef int32_t fix12_t;
66 typedef struct stdev_s
68 float m_oldM, m_newM, m_oldS, m_newS;
69 int m_n;
70 } stdev_t;
72 // Floating point 3 vector.
73 typedef struct fp_vector {
74 float X;
75 float Y;
76 float Z;
77 } t_fp_vector_def;
79 typedef union u_fp_vector {
80 float A[3];
81 t_fp_vector_def V;
82 } t_fp_vector;
84 // Floating point Euler angles.
85 // Be carefull, could be either of degrees or radians.
86 typedef struct fp_angles {
87 float roll;
88 float pitch;
89 float yaw;
90 } fp_angles_def;
92 typedef union {
93 float raw[3];
94 fp_angles_def angles;
95 } fp_angles_t;
97 typedef struct fp_rotationMatrix_s {
98 float m[3][3]; // matrix
99 } fp_rotationMatrix_t;
101 int gcd(int num, int denom);
102 int32_t applyDeadband(int32_t value, int32_t deadband);
103 float fapplyDeadband(float value, float deadband);
105 void devClear(stdev_t *dev);
106 void devPush(stdev_t *dev, float x);
107 float devVariance(stdev_t *dev);
108 float devStandardDeviation(stdev_t *dev);
109 float degreesToRadians(int16_t degrees);
111 int scaleRange(int x, int srcFrom, int srcTo, int destFrom, int destTo);
112 float scaleRangef(float x, float srcFrom, float srcTo, float destFrom, float destTo);
114 void buildRotationMatrix(fp_angles_t *delta, fp_rotationMatrix_t *rotation);
115 void applyMatrixRotation(float *v, fp_rotationMatrix_t *rotationMatrix);
117 int32_t quickMedianFilter3(int32_t * v);
118 int32_t quickMedianFilter5(int32_t * v);
119 int32_t quickMedianFilter7(int32_t * v);
120 int32_t quickMedianFilter9(int32_t * v);
122 float quickMedianFilter3f(float * v);
123 float quickMedianFilter5f(float * v);
124 float quickMedianFilter7f(float * v);
125 float quickMedianFilter9f(float * v);
127 #if defined(FAST_MATH) || defined(VERY_FAST_MATH)
128 float sin_approx(float x);
129 float cos_approx(float x);
130 float atan2_approx(float y, float x);
131 float acos_approx(float x);
132 #define tan_approx(x) (sin_approx(x) / cos_approx(x))
133 float exp_approx(float val);
134 float log_approx(float val);
135 float pow_approx(float a, float b);
136 #else
137 #define sin_approx(x) sinf(x)
138 #define cos_approx(x) cosf(x)
139 #define atan2_approx(y,x) atan2f(y,x)
140 #define acos_approx(x) acosf(x)
141 #define tan_approx(x) tanf(x)
142 #define exp_approx(x) expf(x)
143 #define log_approx(x) logf(x)
144 #define pow_approx(a, b) powf(b, a)
145 #endif
147 void arraySubInt32(int32_t *dest, int32_t *array1, int32_t *array2, int count);
149 int16_t qPercent(fix12_t q);
150 int16_t qMultiply(fix12_t q, int16_t input);
151 fix12_t qConstruct(int16_t num, int16_t den);
153 static inline int constrain(int amt, int low, int high)
155 if (amt < low)
156 return low;
157 else if (amt > high)
158 return high;
159 else
160 return amt;
163 static inline float constrainf(float amt, float low, float high)
165 if (amt < low)
166 return low;
167 else if (amt > high)
168 return high;
169 else
170 return amt;