-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.ino
94 lines (81 loc) · 2.23 KB
/
Math.ino
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
/* This file is part of the Razor AHRS Firmware */
// Computes the dot product of two vectors
float Vector_Dot_Product(const float v1[3], const float v2[3])
{
float result = 0;
for(int c = 0; c < 3; c++)
{
result += v1[c] * v2[c];
}
return result;
}
// Computes the cross product of two vectors
// out has to different from v1 and v2 (no in-place)!
void Vector_Cross_Product(float out[3], const float v1[3], const float v2[3])
{
out[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
out[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
out[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
}
// Multiply the vector by a scalar
void Vector_Scale(float out[3], const float v[3], float scale)
{
for(int c = 0; c < 3; c++)
{
out[c] = v[c] * scale;
}
}
// Adds two vectors
void Vector_Add(float out[3], const float v1[3], const float v2[3])
{
for(int c = 0; c < 3; c++)
{
out[c] = v1[c] + v2[c];
}
}
// Multiply two 3x3 matrices: out = a * b
// out has to different from a and b (no in-place)!
void Matrix_Multiply(const float a[3][3], const float b[3][3], float out[3][3])
{
for(int x = 0; x < 3; x++) // rows
{
for(int y = 0; y < 3; y++) // columns
{
out[x][y] = a[x][0] * b[0][y] + a[x][1] * b[1][y] + a[x][2] * b[2][y];
}
}
}
// Multiply 3x3 matrix with vector: out = a * b
// out has to different from b (no in-place)!
void Matrix_Vector_Multiply(const float a[3][3], const float b[3], float out[3])
{
for(int x = 0; x < 3; x++)
{
out[x] = a[x][0] * b[0] + a[x][1] * b[1] + a[x][2] * b[2];
}
}
// Init rotation matrix using euler angles
void init_rotation_matrix(float m[3][3], float yaw, float pitch, float roll)
{
float c1 = cos(roll);
float s1 = sin(roll);
float c2 = cos(pitch);
float s2 = sin(pitch);
float c3 = cos(yaw);
float s3 = sin(yaw);
// Euler angles, right-handed, intrinsic, XYZ convention
// (which means: rotate around body axes Z, Y', X'')
m[0][0] = c2 * c3;
m[0][1] = c3 * s1 * s2 - c1 * s3;
m[0][2] = s1 * s3 + c1 * c3 * s2;
m[1][0] = c2 * s3;
m[1][1] = c1 * c3 + s1 * s2 * s3;
m[1][2] = c1 * s2 * s3 - c3 * s1;
m[2][0] = -s2;
m[2][1] = c2 * s1;
m[2][2] = c1 * c2;
}
float vector_module(float v[3])
{
return sqrt(pow(v[0], 2)+pow(v[1], 2)+pow(v[2], 2));
}