NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
maths.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight 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.
8  *
9  * Ninjaflight 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <stdint.h>
21 
22 #ifndef sq
23 #define sq(x) ((x)*(x))
24 #endif
25 
26 // Undefine this for use libc sinf/cosf. Keep this defined to use fast sin/cos approximations
27 #define FAST_TRIGONOMETRY // order 9 approximation
28 //#define EVEN_FASTER_TRIGONOMETRY // order 7 approximation
29 
30 // Use floating point M_PI instead explicitly.
31 #define M_PIf 3.14159265358979323846f
32 
33 #define RAD (M_PIf / 180.0f)
34 
35 #define MIN(a, b) ((a) < (b) ? (a) : (b))
36 #define MAX(a, b) ((a) > (b) ? (a) : (b))
37 #define ABS(x) ((x) > 0 ? (x) : -(x))
38 
39 typedef struct stdev_s
40 {
42  int m_n;
43 } stdev_t;
44 
45 // Floating point 3 vector.
46 typedef struct fp_vector {
47  float X;
48  float Y;
49  float Z;
51 
52 typedef union {
53  float A[3];
55 } t_fp_vector;
56 
57 // Floating point Euler angles.
58 // Be carefull, could be either of degrees or radians.
59 typedef struct fp_angles {
60  float roll;
61  float pitch;
62  float yaw;
64 
65 typedef union {
66  float raw[3];
68 } fp_angles_t;
69 
70 int32_t applyDeadband(int32_t value, int32_t deadband);
71 
72 int constrain(int amt, int low, int high);
73 float constrainf(float amt, float low, float high);
74 
75 void devClear(stdev_t *dev);
76 void devPush(stdev_t *dev, float x);
77 float devVariance(stdev_t *dev);
78 float devStandardDeviation(stdev_t *dev);
79 float degreesToRadians(int16_t degrees);
80 
81 int scaleRange(int x, int srcMin, int srcMax, int destMin, int destMax);
82 float scaleRangef(float x, float in_min, float in_max, float out_min, float out_max);
83 
84 void normalizeV(struct fp_vector *src, struct fp_vector *dest);
85 
86 void rotateV(struct fp_vector *v, fp_angles_t *delta);
87 void buildRotationMatrix(fp_angles_t *delta, float matrix[3][3]);
88 
89 int32_t quickMedianFilter3(int32_t * v);
90 int32_t quickMedianFilter5(int32_t * v);
91 int32_t quickMedianFilter7(int32_t * v);
92 int32_t quickMedianFilter9(int32_t * v);
93 
94 #if defined(FAST_MATH) || defined(VERY_FAST_MATH)
95 float sin_approx(float x);
96 float cos_approx(float x);
97 float atan2_approx(float y, float x);
98 float acos_approx(float x);
99 #define tan_approx(x) (sin_approx(x) / cos_approx(x))
100 #else
101 #define sin_approx(x) sinf(x)
102 #define cos_approx(x) cosf(x)
103 #define atan2_approx(y,x) atan2f(y,x)
104 #define acos_approx(x) acosf(x)
105 #define tan_approx(x) tanf(x)
106 #endif
107 
108 #define DEGREES_TO_DECIDEGREES(angle) (angle * 10)
109 #define DECIDEGREES_TO_DEGREES(angle) (angle / 10)
110 #define DECIDEGREES_TO_RADIANS(angle) ((angle / 10.0f) * 0.0174532925f)
111 #define DEGREES_TO_RADIANS(angle) ((angle) * 0.0174532925f)
112 
113 void arraySubInt32(int32_t *dest, int32_t *array1, int32_t *array2, int count);
114 
115 typedef float matrix_3x3_t[3][3];
117 
float roll
Definition: maths.h:60
float Z
Definition: maths.h:49
void normalizeV(struct fp_vector *src, struct fp_vector *dest)
Definition: maths.c:182
struct stdev_s stdev_t
float matrix_3x3_t[3][3]
Definition: maths.h:115
#define cos_approx(x)
Definition: maths.h:102
float scaleRangef(float x, float in_min, float in_max, float out_min, float out_max)
Definition: maths.c:177
int32_t quickMedianFilter5(int32_t *v)
Definition: maths.c:259
Definition: maths.h:59
void devPush(stdev_t *dev, float x)
Definition: maths.c:140
fp_angles_def angles
Definition: maths.h:67
float m_newM
Definition: maths.h:41
t_fp_vector_def V
Definition: maths.h:54
int32_t quickMedianFilter3(int32_t *v)
Definition: maths.c:250
float m_oldM
Definition: maths.h:41
void matrix_set_identity(matrix_3x3_t mat)
Definition: maths.c:194
int constrain(int amt, int low, int high)
Definition: maths.c:115
float m_newS
Definition: maths.h:41
int scaleRange(int x, int srcMin, int srcMax, int destMin, int destMax)
Definition: maths.c:169
#define sin_approx(x)
Definition: maths.h:101
int m_n
Definition: maths.h:42
Definition: maths.h:65
void arraySubInt32(int32_t *dest, int32_t *array1, int32_t *array2, int count)
Definition: maths.c:298
Definition: maths.h:46
float constrainf(float amt, float low, float high)
Definition: maths.c:125
float X
Definition: maths.h:47
float Y
Definition: maths.h:48
float devStandardDeviation(stdev_t *dev)
Definition: maths.c:159
float yaw
Definition: maths.h:62
int32_t quickMedianFilter9(int32_t *v)
Definition: maths.c:283
void devClear(stdev_t *dev)
Definition: maths.c:135
struct fp_vector t_fp_vector_def
struct fp_angles fp_angles_def
float pitch
Definition: maths.h:61
int32_t quickMedianFilter7(int32_t *v)
Definition: maths.c:270
#define atan2_approx(y, x)
Definition: maths.h:103
void buildRotationMatrix(fp_angles_t *delta, float matrix[3][3])
Definition: maths.c:201
float devVariance(stdev_t *dev)
Definition: maths.c:154
void rotateV(struct fp_vector *v, fp_angles_t *delta)
Definition: maths.c:230
float m_oldS
Definition: maths.h:41
int32_t applyDeadband(int32_t value, int32_t deadband)
Definition: maths.c:103
float degreesToRadians(int16_t degrees)
Definition: maths.c:164
int16_t raw[2]
Definition: accelerometer.h:51
Definition: maths.h:39
Definition: maths.h:52
#define acos_approx(x)
Definition: maths.h:104