PER Firmware
Loading...
Searching...
No Matches
linear_algebra.h
Go to the documentation of this file.
1#ifndef LINEAR_ALGEBRA_H
2#define LINEAR_ALGEBRA_H
3
13#include <math.h>
14
15typedef struct {
16 float x;
17 float y;
18 float z;
19} vector3_t;
20
21typedef struct {
22 float data[3][3];
24
25typedef struct {
26 float roll;
27 float pitch;
28 float yaw;
30
37static inline float vector3_magnitude(const vector3_t vec) {
38 return sqrtf(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
39}
40
47static inline vector3_t vector3_normalize(const vector3_t vec) {
48 float mag = vector3_magnitude(vec);
49 if (mag < 0.0001f) {
50 return (vector3_t){0, 0, 0};
51 }
52
53 return (vector3_t){
54 vec.x / mag,
55 vec.y / mag,
56 vec.z / mag
57 };
58}
59
67static inline vector3_t matrix_multiply_vector3(const matrix3x3_t *mat, const vector3_t *in) {
68 vector3_t out;
69 out.x = mat->data[0][0] * in->x + mat->data[0][1] * in->y + mat->data[0][2] * in->z;
70 out.y = mat->data[1][0] * in->x + mat->data[1][1] * in->y + mat->data[1][2] * in->z;
71 out.z = mat->data[2][0] * in->x + mat->data[2][1] * in->y + mat->data[2][2] * in->z;
72 return out;
73}
74
82static inline matrix3x3_t matrix_multiply_matrix3x3(const matrix3x3_t *a, const matrix3x3_t *b) {
83 matrix3x3_t result;
84 for (int i = 0; i < 3; i++) {
85 for (int j = 0; j < 3; j++) {
86 result.data[i][j] = a->data[i][0] * b->data[0][j] +
87 a->data[i][1] * b->data[1][j] +
88 a->data[i][2] * b->data[2][j];
89 }
90 }
91 return result;
92}
93
100static inline matrix3x3_t tait_bryan(const euler_angles_t angles) {
101 matrix3x3_t rot;
102 float cos_roll = cosf(angles.roll);
103 float sin_roll = sinf(angles.roll);
104 float cos_pitch = cosf(angles.pitch);
105 float sin_pitch = sinf(angles.pitch);
106 float cos_yaw = cosf(angles.yaw);
107 float sin_yaw = sinf(angles.yaw);
108
109 // ZYX Euler sequence (Yaw -> Pitch -> Roll)
110 // R = Rz(yaw) * Ry(pitch) * Rx(roll)
111 rot.data[0][0] = cos_pitch * cos_yaw;
112 rot.data[0][1] = cos_yaw * sin_pitch * sin_roll - cos_roll * sin_yaw;
113 rot.data[0][2] = sin_roll * sin_yaw + cos_roll * cos_yaw * sin_pitch;
114
115 rot.data[1][0] = cos_pitch * sin_yaw;
116 rot.data[1][1] = cos_roll * cos_yaw + sin_roll * sin_pitch * sin_yaw;
117 rot.data[1][2] = cos_roll * sin_pitch * sin_yaw - cos_yaw * sin_roll;
118
119 rot.data[2][0] = -sin_pitch;
120 rot.data[2][1] = cos_pitch * sin_roll;
121 rot.data[2][2] = cos_pitch * cos_roll;
122
123 return rot;
124}
125
126#endif // LINEAR_ALGEBRA_H
Definition linear_algebra.h:25
Definition linear_algebra.h:21
Definition linear_algebra.h:15