forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AQMath.h
372 lines (309 loc) · 11.2 KB
/
AQMath.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
AeroQuad v2.3 - March 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_2_MPS2(g) (g * 9.80665)
#define MPS2_2_G(m) (m * 0.10197162)
// Low pass filter, kept as regular C function for speed
float filterSmooth(float currentData, float previousData, float smoothFactor) {
if (smoothFactor != 1.0) //only apply time compensated filter if smoothFactor is applied
return (previousData * (1.0 - smoothFactor) + (currentData * smoothFactor));
else
return currentData; //if smoothFactor == 1.0, do not calculate, just bypass!
}
float filterSmoothWithTime(float currentData, float previousData, float smoothFactor, float dT_scaledAroundOne) { //time scale factor
if (smoothFactor != 1.0) //only apply time compensated filter if smoothFactor is applied
return (previousData * (1.0 - (smoothFactor * dT_scaledAroundOne)) + (currentData * (smoothFactor * dT_scaledAroundOne)));
else
return currentData; //if smoothFactor == 1.0, do not calculate, just bypass!
}
// ***********************************************************************
// *********************** Median Filter Class ***************************
// ***********************************************************************
// Median filter currently not used, but kept if needed for the future
// To declare use: MedianFilter filterSomething;
#define DATASIZE 25
class MedianFilter {
public:
float data[DATASIZE], sortData[DATASIZE];
int dataIndex;
MedianFilter(void) {}
void initialize(void) {
for (int index = 0; index < DATASIZE; index++) {
data[index] = 0;
sortData[index] = 0;
}
dataIndex = 0;
}
const float filter(float newData) {
int temp, j; // used to sort array
// Insert new data into raw data array round robin style
data[dataIndex] = newData;
if (dataIndex < (DATASIZE-1))
dataIndex++;
else
dataIndex = 0;
// Copy raw data to sort data array
memcpy(sortData, data, sizeof(data));
// Insertion Sort
for(int i=1; i<=(DATASIZE-1); i++) {
temp = sortData[i];
j = i-1;
while(temp<sortData[j] && j>=0) {
sortData[j+1] = sortData[j];
j = j-1;
}
sortData[j+1] = temp;
}
return data[(DATASIZE)>>1]; // return data value in middle of sorted array
}
};
////////////////////////////////////////////////////////////////////////////////
// Vector Dot Product
// Return the Dot product of vectors a and b with length m
//
// Call as: vectorDotProduct(m, a, b)
////////////////////////////////////////////////////////////////////////////////
float vectorDotProduct(int length, float vector1[], float vector2[])
{
float dotProduct = 0;
//int i;
for (int i = 0; i < length; i++)
{
dotProduct += vector1[i] * vector2[i];
}
return dotProduct;
}
////////////////////////////////////////////////////////////////////////////////
// Vector Cross Product
// Compute the cross product of vectors a and b with length 3
// Place result in vector C
//
// Call as: vectorDotProduct(c, a, b)
////////////////////////////////////////////////////////////////////////////////
void vectorCrossProduct(float vectorC[3], float vectorA[3], float vectorB[3])
{
vectorC[0] = (vectorA[1] * vectorB[2]) - (vectorA[2] * vectorB[1]);
vectorC[1] = (vectorA[2] * vectorB[0]) - (vectorA[0] * vectorB[2]);
vectorC[2] = (vectorA[0] * vectorB[1]) - (vectorA[1] * vectorB[0]);
}
////////////////////////////////////////////////////////////////////////////////
// Multiply a vector by a scalar
// Mulitply vector a with length m by a scalar
// Place result in vector b
//
// Call as: vectorScale(m, b, a, scalar)
////////////////////////////////////////////////////////////////////////////////
void vectorScale(int length, float scaledVector[], float inputVector[], float scalar)
{
//int i;
for (int i = 0; i < length; i++)
{
scaledVector[i] = inputVector[i] * scalar;
}
}
////////////////////////////////////////////////////////////////////////////////
// Compute sum of 2 vectors
// Add vector a to vector b, both of length m
// Place result in vector c
//
// Call as: vectorAdd(m, c, b, a)
////////////////////////////////////////////////////////////////////////////////
void vectorAdd(int length, float vectorC[], float vectorA[], float vectorB[])
{
//int i;
for(int i = 0; i < length; i++)
{
vectorC[i] = vectorA[i] + vectorB[i];
}
}
////////////////////////////////////////////////////////////////////////////////
// Compute difference of 2 vectors
// Subtract vector a from vector b, both of length m
// Place result in vector c
//
// Call as: vectorSubtract(m, c, b, a)
////////////////////////////////////////////////////////////////////////////////
void vectorSubtract(int length, float vectorC[], float vectorA[], float vectorB[])
{
//int i;
for(int i = 0; i < length; i++)
{
vectorC[i] = vectorA[i] - vectorB[i];
}
}
////////////////////////////////////////////////////////////////////////////////
// Matrix Multiply
// Multiply matrix A times matrix B, matrix A dimension m x n, matrix B dimension n x p
// Result placed in matrix C, dimension m x p
//
// Call as: matrixMultiply(m, n, p, C, A, B)
////////////////////////////////////////////////////////////////////////////////
void matrixMultiply(int aRows, int aCols_bRows, int bCols, float matrixC[], float matrixA[], float matrixB[])
{
//int i, j, k;
for (int i = 0; i < aRows * bCols; i++)
{
matrixC[i] = 0.0;
}
for (int i = 0; i < aRows; i++)
{
for(int j = 0; j < aCols_bRows; j++)
{
for(int k = 0; k < bCols; k++)
{
matrixC[i * bCols + k] += matrixA[i * aCols_bRows + j] * matrixB[j * bCols + k];
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Matrix Addition
// Add matrix A to matrix B, dimensions m x n
// Result placed in matrix C, dimension m x n
//
// Call as: matrixAdd(m, n, C, A, B)
////////////////////////////////////////////////////////////////////////////////
void matrixAdd(int rows, int cols, float matrixC[], float matrixA[], float matrixB[])
{
//int i;
for (int i = 0; i < rows * cols; i++)
{
matrixC[i] = matrixA[i] + matrixB[i];
}
}
////////////////////////////////////////////////////////////////////////////////
// Matrix Subtraction
// Subtract matrix A from matrix B, dimensions m x n
// Result placed in matrix C, dimension m x n
//
// Call as: matrixSubtract(m, n, C, A, B)
////////////////////////////////////////////////////////////////////////////////
void matrixSubtract(int rows, int cols, float matrixC[], float matrixA[], float matrixB[])
{
//int i;
for (int i = 0; i < rows * cols; i++)
{
matrixC[i] = matrixA[i] - matrixB[i];
}
}
////////////////////////////////////////////////////////////////////////////////
// Matrix Scaling
// Scale matrix A, dimensions m x n, by a scaler, S
// Result placed in matrix C, dimension m x n
//
// Call as: matrixScale(m, n, C, S, B)
////////////////////////////////////////////////////////////////////////////////
void matrixScale(int rows, int cols, float matrixC[], float scaler, float matrixA[])
{
//int i;
for (int i = 0; i < rows * cols; i++)
{
matrixC[i] = scaler * matrixA[i];
}
}
////////////////////////////////////////////////////////////////////////////////
// 3 x 3 Matrix Transpose
// Compute 3 x 3 Transpose of A
// Result placed in matrix C, dimension 3 x 3
//
// Call as: Transpose3x3(C, A)
////////////////////////////////////////////////////////////////////////////////
void matrixTranspose3x3(float matrixC[9], float matrixA[9])
{
matrixC[0] = matrixA[0];
matrixC[1] = matrixA[3];
matrixC[2] = matrixA[6];
matrixC[3] = matrixA[1];
matrixC[4] = matrixA[4];
matrixC[5] = matrixA[7];
matrixC[6] = matrixA[2];
matrixC[7] = matrixA[5];
matrixC[8] = matrixA[8];
}
////////////////////////////////////////////////////////////////////////////////
// 3 x 3 Matrix Inverse
// Compute 3 x 3 Inverse of A
// Result placed in matrix C, dimension 3 x 3
//
// Call as: Inverse3x3(C, A)
////////////////////////////////////////////////////////////////////////////////
void matrixInverse3x3(float matrixC[9], float matrixA[9])
{
float det;
float transposeA[9];
float minors[9];
float transposeMinors[9];
det = matrixA[0] * (matrixA[4] * matrixA[8] - matrixA[5] * matrixA[7]) -
matrixA[1] * (matrixA[3] * matrixA[8] - matrixA[5] * matrixA[6]) +
matrixA[2] * (matrixA[3] * matrixA[7] - matrixA[4] * matrixA[6]);
matrixTranspose3x3(transposeA, matrixA);
minors[0] = matrixA[4] * matrixA[8] - matrixA[5] * matrixA[7];
minors[1] = matrixA[5] * matrixA[6] - matrixA[3] * matrixA[8];
minors[2] = matrixA[3] * matrixA[7] - matrixA[4] * matrixA[6];
minors[3] = matrixA[2] * matrixA[7] - matrixA[1] * matrixA[8];
minors[4] = matrixA[0] * matrixA[8] - matrixA[2] * matrixA[6];
minors[5] = matrixA[1] * matrixA[6] - matrixA[0] * matrixA[7];
minors[6] = matrixA[1] * matrixA[5] - matrixA[2] * matrixA[4];
minors[7] = matrixA[2] * matrixA[3] - matrixA[0] * matrixA[5];
minors[8] = matrixA[0] * matrixA[4] - matrixA[1] * matrixA[3];
matrixTranspose3x3(transposeMinors, minors);
det = 1/det;
matrixScale(3,3, matrixC, det, transposeMinors);
}
// Alternate method to calculate arctangent from: http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm
float arctan2(float y, float x) {
float coeff_1 = PI/4;
float coeff_2 = 3*coeff_1;
float abs_y = abs(y)+1e-10; // kludge to prevent 0/0 condition
float r, angle;
if (x >= 0) {
r = (x - abs_y) / (x + abs_y);
angle = coeff_1 - coeff_1 * r;
}
else {
r = (x + abs_y) / (abs_y - x);
angle = coeff_2 - coeff_1 * r;
}
if (y < 0)
return(-angle); // negate if in quad III or IV
else
return(angle);
}
// Used for sensor calibration
// Takes the median of 50 results as zero
#if defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
float findMedian(float *data, int arraySize) {
float temp;
#else
int findMedian(int *data, int arraySize) { //Thanks ala42! Post: http://aeroquad.com/showthread.php?1369-The-big-enhancement-addition-to-2.0-code/page5
int temp;
#endif
boolean done = 0;
//byte i;
// Sorts numbers from lowest to highest
while (done != 1) {
done = 1;
for (byte i = 0; i<(arraySize-1); i++) {
if (data[i] > data[i+1]) { // numbers are out of order - swap
temp = data[i+1];
data[i+1] = data[i];
data[i] = temp;
done = 0;
}
}
}
return data[arraySize/2]; // return the median value
}