Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / common / maths.h
blobb8d3803613f915c80438c29899ccd705fb62f877
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include <stdint.h>
21 #include <stdbool.h>
23 #ifndef sq
24 #define sq(x) ((x)*(x))
25 #endif
27 // Undefine this for use libc sinf/cosf. Keep this defined to use fast sin/cos approximations
28 #define FAST_MATH // order 9 approximation
29 //#define VERY_FAST_MATH // order 7 approximation
31 // Use floating point M_PI instead explicitly.
32 #define M_PIf 3.14159265358979323846f
33 #define M_LN2f 0.69314718055994530942f
34 #define M_Ef 2.71828182845904523536f
36 #define RAD (M_PIf / 180.0f)
38 #define DEGREES_TO_CENTIDEGREES(angle) ((angle) * 100)
39 #define CENTIDEGREES_TO_DEGREES(angle) ((angle) * 0.01f)
41 #define CENTIDEGREES_TO_DECIDEGREES(angle) ((angle) / 10.0f)
42 #define DECIDEGREES_TO_CENTIDEGREES(angle) ((angle) * 10)
44 #define DEGREES_TO_DECIDEGREES(angle) ((angle) * 10)
45 #define DECIDEGREES_TO_DEGREES(angle) ((angle) / 10.0f)
47 #define DEGREES_PER_DEKADEGREE 10
48 #define DEGREES_TO_DEKADEGREES(angle) ((angle) / DEGREES_PER_DEKADEGREE)
49 #define DEKADEGREES_TO_DEGREES(angle) ((angle) * DEGREES_PER_DEKADEGREE)
51 #define DEGREES_TO_RADIANS(angle) ((angle) * RAD)
52 #define RADIANS_TO_DEGREES(angle) ((angle) / RAD)
53 #define DECIDEGREES_TO_RADIANS(angle) (((angle) / 10.0f) * RAD)
54 #define RADIANS_TO_DECIDEGREES(angle) (((angle) * 10.0f) / RAD)
56 #define RADIANS_TO_CENTIDEGREES(angle) (((angle) * 100.0f) / RAD)
57 #define CENTIDEGREES_TO_RADIANS(angle) (((angle) * 0.01f) * RAD)
59 #define MILLIMETERS_TO_CENTIMETERS(mm) (mm / 10.0f)
61 #define CENTIMETERS_TO_CENTIFEET(cm) (cm / 0.3048f)
62 #define CENTIMETERS_TO_FEET(cm) (cm / 30.48f)
63 #define CENTIMETERS_TO_METERS(cm) (cm * 0.01f)
65 #define METERS_TO_CENTIMETERS(m) (m * 100)
66 #define METERS_TO_KILOMETERS(m) (m / 1000.0f)
67 #define METERS_TO_MILES(m) (m / 1609.344f)
68 #define METERS_TO_NAUTICALMILES(m) (m / 1852.00f)
70 #define MWH_TO_WH(mWh) (mWh / 1000.0f)
72 #define CMSEC_TO_CENTIMPH(cms) (cms * 2.2369363f)
73 #define CMSEC_TO_CENTIKPH(cms) (cms * 3.6f)
74 #define CMSEC_TO_CENTIKNOTS(cms) (cms * 1.943845f)
76 #define C_TO_KELVIN(temp) (temp + 273.15f)
78 // Standard Sea Level values
79 // Ref:https://en.wikipedia.org/wiki/Standard_sea_level
80 #define SSL_AIR_DENSITY 1.225f // kg/m^3
81 #define SSL_AIR_PRESSURE 101325.01576f // Pascal
82 #define SSL_AIR_TEMPERATURE 288.15f // K
84 // copied from https://code.google.com/p/cxutil/source/browse/include/cxutil/utility.h#70
85 #define _CHOOSE2(binoper, lexpr, lvar, rexpr, rvar) \
86 ( __extension__ ({ \
87 __typeof__(lexpr) lvar = (lexpr); \
88 __typeof__(rexpr) rvar = (rexpr); \
89 lvar binoper rvar ? lvar : rvar; \
90 }))
91 #define _CHOOSE_VAR2(prefix, unique) prefix##unique
92 #define _CHOOSE_VAR(prefix, unique) _CHOOSE_VAR2(prefix, unique)
93 #define _CHOOSE(binoper, lexpr, rexpr) \
94 _CHOOSE2( \
95 binoper, \
96 lexpr, _CHOOSE_VAR(_left, __COUNTER__), \
97 rexpr, _CHOOSE_VAR(_right, __COUNTER__) \
99 #define MIN(a, b) _CHOOSE(<, a, b)
100 #define MAX(a, b) _CHOOSE(>, a, b)
101 #define SIGN(a) ((a >= 0) ? 1 : -1)
103 #define _ABS_II(x, var) \
104 ( __extension__ ({ \
105 __typeof__(x) var = (x); \
106 var < 0 ? -var : var; \
108 #define _ABS_I(x, var) _ABS_II(x, var)
109 #define ABS(x) _ABS_I(x, _CHOOSE_VAR(_abs, __COUNTER__))
111 #define power3(x) ((x)*(x)*(x))
113 // Floating point Euler angles.
114 typedef struct fp_angles {
115 float roll;
116 float pitch;
117 float yaw;
118 } fp_angles_def;
120 typedef union {
121 float raw[3];
122 fp_angles_def angles;
123 } fp_angles_t;
125 typedef struct stdev_s {
126 float m_oldM, m_newM, m_oldS, m_newS;
127 int m_n;
128 } stdev_t;
130 typedef struct filterWithBufferSample_s {
131 float value;
132 uint32_t timestamp;
133 } filterWithBufferSample_t;
135 typedef struct filterWithBufferState_s {
136 uint16_t filter_size;
137 uint16_t sample_index;
138 filterWithBufferSample_t * samples;
139 } filterWithBufferState_t;
141 typedef struct {
142 float XtY[4];
143 float XtX[4][4];
144 } sensorCalibrationState_t;
146 void sensorCalibrationResetState(sensorCalibrationState_t * state);
147 void sensorCalibrationPushSampleForOffsetCalculation(sensorCalibrationState_t * state, float sample[3]);
148 void sensorCalibrationPushSampleForScaleCalculation(sensorCalibrationState_t * state, int axis, float sample[3], int target);
149 bool sensorCalibrationSolveForOffset(sensorCalibrationState_t * state, float result[3]);
150 bool sensorCalibrationSolveForScale(sensorCalibrationState_t * state, float result[3]);
152 int gcd(int num, int denom);
153 int32_t applyDeadband(int32_t value, int32_t deadband);
154 int32_t applyDeadbandRescaled(int32_t value, int32_t deadband, int32_t min, int32_t max);
156 int32_t constrain(int32_t amt, int32_t low, int32_t high);
157 float constrainf(float amt, float low, float high);
159 void devClear(stdev_t *dev);
160 void devPush(stdev_t *dev, float x);
161 float devVariance(stdev_t *dev);
162 float devStandardDeviation(stdev_t *dev);
163 float degreesToRadians(int16_t degrees);
165 int scaleRange(int x, int srcMin, int srcMax, int destMin, int destMax);
166 float scaleRangef(float x, float srcMin, float srcMax, float destMin, float destMax);
168 int32_t wrap_18000(int32_t angle);
169 int16_t wrap_180(int16_t angle);
170 int32_t wrap_36000(int32_t angle);
172 int32_t quickMedianFilter3(int32_t * v);
173 int32_t quickMedianFilter5(int32_t * v);
174 int32_t quickMedianFilter7(int32_t * v);
175 int32_t quickMedianFilter9(int32_t * v);
177 int16_t quickMedianFilter3_16(int16_t * v);
178 int16_t quickMedianFilter5_16(int16_t * v);
180 #if defined(FAST_MATH) || defined(VERY_FAST_MATH)
181 float sin_approx(float x);
182 float cos_approx(float x);
183 float atan2_approx(float y, float x);
184 float acos_approx(float x);
185 #define tan_approx(x) (sin_approx(x) / cos_approx(x))
186 #define asin_approx(x) (M_PIf / 2 - acos_approx(x))
187 #else
188 #define asin_approx(x) asinf(x)
189 #define sin_approx(x) sinf(x)
190 #define cos_approx(x) cosf(x)
191 #define atan2_approx(y,x) atan2f(y,x)
192 #define acos_approx(x) acosf(x)
193 #define tan_approx(x) tanf(x)
194 #endif
196 void arraySubInt32(int32_t *dest, int32_t *array1, int32_t *array2, int count);
198 float bellCurve(const float x, const float curveWidth);
199 float attenuation(const float input, const float width);
200 float gaussian(const float x, const float mu, const float sigma);
201 float fast_fsqrtf(const float value);
202 float calc_length_pythagorean_2D(const float firstElement, const float secondElement);
203 float calc_length_pythagorean_3D(const float firstElement, const float secondElement, const float thirdElement);
206 * The most significat byte is placed at the lowest address
207 * in other words, the most significant byte is "first", on even indexes
209 #define int16_val_big_endian(v, idx) ((int16_t)(((uint8_t)v[2 * idx] << 8) | v[2 * idx + 1]))
211 * The most significat byte is placed at the highest address
212 * in other words, the most significant byte is "last", on odd indexes
214 #define int16_val_little_endian(v, idx) ((int16_t)(((uint8_t)v[2 * idx + 1] << 8) | v[2 * idx]))
216 #ifdef SITL_BUILD
217 void arm_sub_f32(float * pSrcA, float * pSrcB, float * pDst, uint32_t blockSize);
218 void arm_scale_f32(float * pSrc, float scale, float * pDst, uint32_t blockSize);
219 void arm_mult_f32(float * pSrcA, float * pSrcB, float * pDst, uint32_t blockSize);
220 #endif