aboutsummaryrefslogtreecommitdiff
path: root/apps/px4/attitude_estimator_bm/attitude_bm.c
blob: 29908ddd58ebf7bc4199ec41cc66b9fd6ec85164 (plain) (blame)
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
/*
 * attitude_bm.c
 *
 *  Created on: 21.12.2010
 *      Author: Laurens Mackay, Tobias Naegeli
 */

#include <math.h>
#include "attitude_bm.h"
#include "kalman.h"


#define TIME_STEP (1.0f / 500.0f)

static kalman_t attitude_blackmagic_kal;

void vect_norm(float_vect3 *vect)
{
	float length = sqrtf(
			       vect->x * vect->x + vect->y * vect->y + vect->z * vect->z);

	if (length != 0) {
		vect->x /= length;
		vect->y /= length;
		vect->z /= length;
	}
}


void vect_cross_product(const float_vect3 *a, const float_vect3 *b,
			float_vect3 *c)
{
	c->x = a->y * b->z - a->z * b->y;
	c->y = a->z * b->x - a->x * b->z;
	c->z = a->x * b->y - a->y * b->x;
}

void attitude_blackmagic_update_a(void)
{
	// for acc
	// Idendity matrix already in A.
	M(attitude_blackmagic_kal.a, 0, 1) = TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 11);
	M(attitude_blackmagic_kal.a, 0, 2) = -TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 10);

	M(attitude_blackmagic_kal.a, 1, 0) = -TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 11);
	M(attitude_blackmagic_kal.a, 1, 2) = TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 9);

	M(attitude_blackmagic_kal.a, 2, 0) = TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 10);
	M(attitude_blackmagic_kal.a, 2, 1) = -TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 9);

	// for mag
	// Idendity matrix already in A.
	M(attitude_blackmagic_kal.a, 3, 4) = TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 11);
	M(attitude_blackmagic_kal.a, 3, 5) = -TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 10);

	M(attitude_blackmagic_kal.a, 4, 3) = -TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 11);
	M(attitude_blackmagic_kal.a, 4, 5) = TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 9);

	M(attitude_blackmagic_kal.a, 5, 3) = TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 10);
	M(attitude_blackmagic_kal.a, 5, 4) = -TIME_STEP * kalman_get_state(
			&attitude_blackmagic_kal, 9);

}

void attitude_blackmagic_init(void)
{
	//X Kalmanfilter
	//initalize matrices

	static m_elem kal_a[12 * 12] = {
		1.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f
	};

	static m_elem kal_c[9 * 12] = {
		1.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 1.0f, 0, 0, 1.0f, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 1.0f, 0, 0, 1.0f, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0, 0, 1.0f
	};



#define FACTOR 0.5
#define FACTORstart 1


//	static m_elem kal_gain[12 * 9] =
//	{ 		0.004 , 0    ,   0    ,   0    ,   0    ,   0    ,   0   ,    0    ,   0,
//			0   ,    0.004 , 0   ,    0   ,    0   ,    0   ,    0   ,    0   ,    0,
//			0   ,    0    ,   0.004 , 0   ,    0   ,    0   ,    0   ,    0   ,    0,
//			0   ,    0    ,   0   ,    0.015, 	0   ,    0   ,    0   ,    0   ,    0,
//			0   ,    0   ,    0   ,    0    ,   0.015, 	 0   ,    0   ,    0   ,    0,
//			0   ,    0    ,   0   ,    0    ,   0   ,    0.015, 	  0   ,    0   ,    0,
//			0.0000 , +0.00002,0   ,    0 , 		0, 		 0,  	  0,  	   0    ,   0,
//			-0.00002,0    ,   0   ,    0 , 		0, 		 0,  	  0,  	   0, 	    0,
//			0,    	 0 ,	  0   ,    0,  	    0,		 0,  	  0,  	   0, 	    0,
//			0  ,     0    ,   0   ,    0   ,    0    ,   0   ,    0.4 ,   0   ,    0,
//			0   ,    0   ,    0   ,    0   ,    0    ,   0   ,    0    ,   0.4 ,   0,
//			0   ,    0   ,    0   ,    0   ,    0   ,    0   ,    0    ,   0    ,   0.4
//	};

	static m_elem kal_gain[12 * 9] = {
		0.001f , 0    ,   0    ,   0    ,   0    ,   0    ,   0   ,    0    ,   0,
		0   ,    0.001f , 0   ,    0   ,    0   ,    0   ,    0   ,    0   ,    0,
		0   ,    0    ,   0.001f , 0   ,    0   ,    0   ,    0   ,    0   ,    0,
		0   ,    0    ,   0   ,    0.015f, 	0   ,    0   ,    0   ,    0   ,    0,
		0   ,    0   ,    0   ,    0    ,   0.015f, 	 0   ,    0   ,    0   ,    0,
		0   ,    0    ,   0   ,    0    ,   0   ,    0.015f, 	  0   ,    0   ,    0,
		0.0000f , +0.00002f, 0   ,    0 , 		0, 		 0,  	  0,  	   0    ,   0,
		-0.00002f, 0    ,   0   ,    0 , 		0, 		 0,  	  0,  	   0, 	    0,
		0,    	 0 ,	  0   ,    0,  	    0,		 0,  	  0,  	   0, 	    0,
		0  ,     0    ,   0   ,    0   ,    0    ,   0   ,    0.6f ,   0   ,    0,
		0   ,    0   ,    0   ,    0   ,    0    ,   0   ,    0    ,   0.6f ,   0,
		0   ,    0   ,    0   ,    0   ,    0   ,    0   ,    0    ,   0    ,   0.6f
	};
	//offset update only correct if not upside down.

#define K (10.0f*TIME_STEP)

	static m_elem kal_gain_start[12 * 9] = {
		K, 0, 0, 0, 0, 0, 0, 0, 0,

		0, K, 0, 0, 0, 0, 0, 0, 0,

		0, 0, K, 0, 0, 0, 0, 0, 0,

		0, 0, 0, K, 0, 0, 0, 0, 0,

		0, 0, 0, 0, K, 0, 0, 0, 0,

		0, 0, 0, 0, 0, K, 0, 0, 0,

		0, 0, 0, 0, 0, 0, K, 0, 0,

		0, 0, 0, 0, 0, 0, 0, K, 0,

		0, 0, 0, 0, 0, 0, 0, 0, K,

		0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 0,

		0, 0, 0, 0, 0, 0, 0, 0, 0
	};



	static m_elem kal_x_apriori[12 * 1] =
		{  };


	//---> initial states sind aposteriori!? ---> fehler
	static m_elem kal_x_aposteriori[12 * 1] =
	{ 0.0f, 0.0f, -1.0f, 0.6f, 0.0f, 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };

	kalman_init(&attitude_blackmagic_kal, 12, 9, kal_a, kal_c,
		    kal_gain_start, kal_gain, kal_x_apriori, kal_x_aposteriori, 1000);

}

void attitude_blackmagic(const float_vect3 *accel, const float_vect3 *mag, const float_vect3 *gyro)
{
	// Kalman Filter

	//Calculate new linearized A matrix
	attitude_blackmagic_update_a();

	kalman_predict(&attitude_blackmagic_kal);

	//correction update

	m_elem measurement[9] =
		{ };
	m_elem mask[9] =
	{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };

	// XXX Hack - stop updating accel if upside down

	if (accel->z > 0) {
		mask[0] = 0.0f;
		mask[1] = 0.0f;
		mask[2] = 0.0f;
	} else {
		mask[0] = 1.0f;
		mask[1] = 1.0f;
		mask[2] = 1.0f;
	}

	measurement[0] = accel->x;
	measurement[1] = accel->y;
	measurement[2] = accel->z;

	measurement[3] = mag->x;
	measurement[4] = mag->y;
	measurement[5] = mag->z;

	measurement[6] = gyro->x;
	measurement[7] = gyro->y;
	measurement[8] = gyro->z;

	//Put measurements into filter


//	static int j = 0;
//	if (j >= 3)
//	{
//		j = 0;
//
//		mask[3]=1;
//		mask[4]=1;
//		mask[5]=1;
//		j=0;
//
//	}else{
//		j++;}

	kalman_correct(&attitude_blackmagic_kal, measurement, mask);

}
void attitude_blackmagic_get_all(float_vect3 *euler, float_vect3 *rates, float_vect3 *x_n_b, float_vect3 *y_n_b, float_vect3 *z_n_b)
{
	//debug

	// save outputs
	float_vect3 kal_acc;
	float_vect3 kal_mag;
//	float_vect3 kal_w0, kal_w;

	kal_acc.x = kalman_get_state(&attitude_blackmagic_kal, 0);
	kal_acc.y = kalman_get_state(&attitude_blackmagic_kal, 1);
	kal_acc.z = kalman_get_state(&attitude_blackmagic_kal, 2);

	kal_mag.x = kalman_get_state(&attitude_blackmagic_kal, 3);
	kal_mag.y = kalman_get_state(&attitude_blackmagic_kal, 4);
	kal_mag.z = kalman_get_state(&attitude_blackmagic_kal, 5);

//	kal_w0.x = kalman_get_state(&attitude_blackmagic_kal, 6);
//	kal_w0.y = kalman_get_state(&attitude_blackmagic_kal, 7);
//	kal_w0.z = kalman_get_state(&attitude_blackmagic_kal, 8);
//
//	kal_w.x = kalman_get_state(&attitude_blackmagic_kal, 9);
//	kal_w.y = kalman_get_state(&attitude_blackmagic_kal, 10);
//	kal_w.z = kalman_get_state(&attitude_blackmagic_kal, 11);

	rates->x = kalman_get_state(&attitude_blackmagic_kal, 9);
	rates->y = kalman_get_state(&attitude_blackmagic_kal, 10);
	rates->z = kalman_get_state(&attitude_blackmagic_kal, 11);



//	kal_w = kal_w;		// XXX hack to silence compiler warning
//	kal_w0 = kal_w0;	// XXX hack to silence compiler warning



	//debug_vect("magn", mag);

	//float_vect3 x_n_b, y_n_b, z_n_b;
	z_n_b->x = -kal_acc.x;
	z_n_b->y = -kal_acc.y;
	z_n_b->z = -kal_acc.z;
	vect_norm(z_n_b);
	vect_cross_product(z_n_b, &kal_mag, y_n_b);
	vect_norm(y_n_b);

	vect_cross_product(y_n_b, z_n_b, x_n_b);



	//save euler angles
	euler->x = atan2f(z_n_b->y, z_n_b->z);
	euler->y = -asinf(z_n_b->x);
	euler->z = atan2f(y_n_b->x, x_n_b->x);

}