Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
ins_ext_pose.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 MAVLab
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
28 #include <time.h>
29 
30 #include "ins_ext_pose.h"
31 #include "state.h"
33 #include "modules/imu/imu.h"
34 #include "modules/ins/ins.h"
35 #include "generated/flight_plan.h"
36 
37 #include "modules/core/abi.h"
38 
39 #if 0
40 #include <stdio.h>
41 #define DEBUG_PRINT(...) printf(__VA_ARGS__)
42 #else
43 #define DEBUG_PRINT(...) {}
44 #endif
45 
48 struct InsExtPose {
49  /* Inputs */
50  struct FloatRates gyros_f;
51  struct FloatVect3 accels_f;
54 
55  struct FloatVect3 ev_pos;
56  struct FloatVect3 ev_vel;
57  struct FloatEulers ev_att;
58  struct FloatQuat ev_quat;
60  float ev_time;
61 
62  /* Origin */
63  struct LtpDef_i ltp_def;
64 
65  /* output LTP NED */
66  struct NedCoor_i ltp_pos;
67  struct NedCoor_i ltp_speed;
68  struct NedCoor_i ltp_accel;
69 };
70 struct InsExtPose ins_ext_pos;
71 
72 
74 {
75 
76  struct LlaCoor_i llh_nav0; /* Height above the ellipsoid */
77  llh_nav0.lat = NAV_LAT0;
78  llh_nav0.lon = NAV_LON0;
79  /* NAV_ALT0 = ground alt above msl, NAV_MSL0 = geoid-height (msl) over ellipsoid */
80  llh_nav0.alt = NAV_ALT0 + NAV_MSL0;
81 
82  struct EcefCoor_i ecef_nav0;
83  ecef_of_lla_i(&ecef_nav0, &llh_nav0);
84 
86  ins_ext_pos.ltp_def.hmsl = NAV_ALT0;
88  /* update local ENU coordinates of global waypoints */
90 }
91 
92 
96 #if PERIODIC_TELEMETRY
98 
99 static void send_ins(struct transport_tx *trans, struct link_device *dev)
100 {
101  pprz_msg_send_INS(trans, dev, AC_ID,
105 }
106 
107 static void send_ins_z(struct transport_tx *trans, struct link_device *dev)
108 {
109  static float fake_baro_z = 0.0;
110  pprz_msg_send_INS_Z(trans, dev, AC_ID,
111  (float *)&fake_baro_z, &ins_ext_pos.ltp_pos.z,
113 }
114 
115 static void send_ins_ref(struct transport_tx *trans, struct link_device *dev)
116 {
117  static float fake_qfe = 0.0;
118  pprz_msg_send_INS_REF(trans, dev, AC_ID,
121  &ins_ext_pos.ltp_def.hmsl, (float *)&fake_qfe);
122 }
123 
124 static void send_external_pose_down(struct transport_tx *trans, struct link_device *dev)
125 {
126  pprz_msg_send_EXTERNAL_POSE_DOWN(trans, dev, AC_ID,
128  &ins_ext_pos.ev_pos.x,
129  &ins_ext_pos.ev_pos.y,
131  &ins_ext_pos.ev_vel.x,
132  &ins_ext_pos.ev_vel.y,
133  &ins_ext_pos.ev_vel.z,
138 }
139 static void send_ahrs_bias(struct transport_tx *trans, struct link_device *dev)
140 {
141  float dummy0 = 0.0;
142  pprz_msg_send_AHRS_BIAS(trans, dev, AC_ID,
143  &ekf_X[9],
144  &ekf_X[10],
145  &ekf_X[11],
146  &ekf_X[12],
147  &ekf_X[13],
148  &ekf_X[14],
149  &dummy0,
150  &dummy0,
151  &dummy0);
152 }
153 #endif
154 
155 
160 #ifndef INS_EXT_POSE_IMU_ID
161 #define INS_EXT_POSE_IMU_ID ABI_BROADCAST
162 #endif
163 PRINT_CONFIG_VAR(INS_EXT_POSE_IMU_ID)
164 
167 
168 static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel);
169 static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro);
170 
171 
172 
173 static void gyro_cb(uint8_t sender_id __attribute__((unused)),
174  uint32_t stamp __attribute__((unused)),
175  struct Int32Rates *gyro)
176 {
178  ins_ext_pos.has_new_gyro = true;
179 }
180 
181 static void accel_cb(uint8_t sender_id __attribute__((unused)),
182  uint32_t stamp __attribute__((unused)),
183  struct Int32Vect3 *accel)
184 {
186  ins_ext_pos.has_new_acc = true;
187 }
188 
189 
195 {
196  if (DL_EXTERNAL_POSE_ac_id(buf) != AC_ID) { return; } // not for this aircraft
197 
198  float enu_x = DL_EXTERNAL_POSE_enu_x(buf);
199  float enu_y = DL_EXTERNAL_POSE_enu_y(buf);
200  float enu_z = DL_EXTERNAL_POSE_enu_z(buf);
201  float enu_xd = DL_EXTERNAL_POSE_enu_xd(buf);
202  float enu_yd = DL_EXTERNAL_POSE_enu_yd(buf);
203  float enu_zd = DL_EXTERNAL_POSE_enu_zd(buf);
204  float quat_i = DL_EXTERNAL_POSE_body_qi(buf);
205  float quat_x = DL_EXTERNAL_POSE_body_qx(buf);
206  float quat_y = DL_EXTERNAL_POSE_body_qy(buf);
207  float quat_z = DL_EXTERNAL_POSE_body_qz(buf);
208 
209  DEBUG_PRINT("EXT_UPDATE\n");
210 
211  struct FloatQuat orient;
212  struct FloatEulers orient_eulers;
213 
214  // Transformation of External Pose. Optitrack motive 2.X Yup
215  orient.qi = quat_i ;
216  orient.qx = quat_y ;
217  orient.qy = quat_x ;
218  orient.qz = -quat_z;
219 
220  float_eulers_of_quat(&orient_eulers, &orient);
221 
223  ins_ext_pos.ev_pos.x = enu_y;
224  ins_ext_pos.ev_pos.y = enu_x;
225  ins_ext_pos.ev_pos.z = -enu_z;
226  ins_ext_pos.ev_vel.x = enu_yd;
227  ins_ext_pos.ev_vel.y = enu_xd;
228  ins_ext_pos.ev_vel.z = -enu_zd;
229  ins_ext_pos.ev_att.phi = orient_eulers.phi;
230  ins_ext_pos.ev_att.theta = orient_eulers.theta;
231  ins_ext_pos.ev_att.psi = orient_eulers.psi;
232  ins_ext_pos.ev_quat.qi = orient.qi;
233  ins_ext_pos.ev_quat.qx = orient.qx;
234  ins_ext_pos.ev_quat.qy = orient.qy;
235  ins_ext_pos.ev_quat.qz = orient.qz;
236 
238 
240 }
241 
243 {
244  // Ext pos does not allow geoinit: FP origin only
245 }
246 
248 {
249  // Ext pos does not allow geoinit: FP origin only
250 }
251 
252 
256 static inline void ekf_init(void);
257 static inline void ekf_run(void);
258 
264 {
265 
266  // Initialize inputs
267  ins_ext_pos.has_new_acc = false;
268  ins_ext_pos.has_new_gyro = false;
270 
271  // Get External Pose Origin From Flightplan
273 
274  // Provide telemetry
275 #if PERIODIC_TELEMETRY
279  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_EXTERNAL_POSE_DOWN, send_external_pose_down);
281 #endif
282 
283  // Get IMU through ABI
284  AbiBindMsgIMU_ACCEL(INS_EXT_POSE_IMU_ID, &accel_ev, accel_cb);
285  AbiBindMsgIMU_GYRO(INS_EXT_POSE_IMU_ID, &gyro_ev, gyro_cb);
286  // Get External Pose through datalink message: setup in xml
287 
288  // Initialize EKF
289  ekf_init();
290 }
291 
293 {
294  ekf_run();
295 }
296 
297 
298 
299 
300 /***************************************************
301  * Kalman Filter.
302  */
303 
304 
305 
306 static inline void ekf_f(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], float out[EKF_NUM_STATES]);
307 static inline void ekf_F(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS],
308  float out[EKF_NUM_STATES][EKF_NUM_STATES]);
309 static inline void ekf_L(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS],
310  float out[EKF_NUM_STATES][EKF_NUM_INPUTS]);
311 
312 static inline void ekf_f_rk4(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], const float dt,
313  float out[EKF_NUM_STATES]);
314 
315 static inline void ekf_step(const float U[EKF_NUM_INPUTS], const float Z[EKF_NUM_OUTPUTS], const float dt);
316 static inline void ekf_prediction_step(const float U[EKF_NUM_INPUTS], const float dt);
317 static inline void ekf_measurement_step(const float Z[EKF_NUM_OUTPUTS]);
318 
319 
320 
327 
328 float ekf_H[EKF_NUM_OUTPUTS][EKF_NUM_STATES] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}};
329 
330 
331 float t0;
332 float t1;
333 
334 void ekf_set_diag(float **a, float *b, int n);
335 void ekf_set_diag(float **a, float *b, int n)
336 {
337  int i, j;
338  for (i = 0 ; i < n; i++) {
339  for (j = 0 ; j < n; j++) {
340  if (i == j) {
341  a[i][j] = b[i];
342  } else {
343  a[i][j] = 0.0;
344  }
345  }
346  }
347 }
348 
349 
350 
351 static inline void ekf_init(void)
352 {
353 
354  DEBUG_PRINT("ekf init");
355  float X0[EKF_NUM_STATES] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
356  float U0[EKF_NUM_INPUTS] = {0, 0, 0, 0, 0, 0};
357  float Z0[EKF_NUM_OUTPUTS] = {0, 0, 0, 0, 0, 0};
358 
359  float Pdiag[EKF_NUM_STATES] = {1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.};
360  float Qdiag[EKF_NUM_INPUTS] = {1.0, 1.0, 1.0, 0.0173, 4.878e-4, 3.547e-4};//{0.0325, 0.4494, 0.5087, 0.0173, 4.878e-4, 3.547e-4};
361 
362  float Rdiag[EKF_NUM_OUTPUTS] = {8.372e-6, 3.832e-6, 4.761e-6, 2.830e-4, 8.684e-6, 7.013e-6};
363 
367 
368  ekf_set_diag(ekf_P_, Pdiag, EKF_NUM_STATES);
369  ekf_set_diag(ekf_Q_, Qdiag, EKF_NUM_INPUTS);
370  ekf_set_diag(ekf_R_, Rdiag, EKF_NUM_OUTPUTS);
374 }
375 
376 static inline void ekf_f(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], float out[EKF_NUM_STATES])
377 {
378  float x0 = cos(X[8]);
379  float x1 = U[0] - X[9];
380  float x2 = cos(X[7]);
381  float x3 = x1 * x2;
382  float x4 = U[2] - X[11];
383  float x5 = sin(X[6]);
384  float x6 = sin(X[8]);
385  float x7 = x5 * x6;
386  float x8 = sin(X[7]);
387  float x9 = cos(X[6]);
388  float x10 = x0 * x9;
389  float x11 = U[1] - X[10];
390  float x12 = x6 * x9;
391  float x13 = x0 * x5;
392  float x14 = tan(X[7]);
393  float x15 = U[4] - X[13];
394  float x16 = x15 * x5;
395  float x17 = U[5] - X[14];
396  float x18 = x17 * x9;
397  float x19 = 1.0 / x2;
398  out[0] = X[3];
399  out[1] = X[4];
400  out[2] = X[5];
401  out[3] = x0 * x3 + x11 * (-x12 + x13 * x8) + x4 * (x10 * x8 + x7);
402  out[4] = x11 * (x10 + x7 * x8) + x3 * x6 + x4 * (x12 * x8 - x13);
403  out[5] = -x1 * x8 + x11 * x2 * x5 + x2 * x4 * x9 + 9.8100000000000005;
404  out[6] = U[3] - X[12] + x14 * x16 + x14 * x18;
405  out[7] = x15 * x9 - x17 * x5;
406  out[8] = x16 * x19 + x18 * x19;
407  out[9] = 0;
408  out[10] = 0;
409  out[11] = 0;
410  out[12] = 0;
411  out[13] = 0;
412  out[14] = 0;
413 }
414 
415 static inline void ekf_F(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS],
416  float out[EKF_NUM_STATES][EKF_NUM_STATES])
417 {
418  float x0 = U[1] - X[10];
419  float x1 = sin(X[6]);
420  float x2 = sin(X[8]);
421  float x3 = x1 * x2;
422  float x4 = sin(X[7]);
423  float x5 = cos(X[6]);
424  float x6 = cos(X[8]);
425  float x7 = x5 * x6;
426  float x8 = x4 * x7;
427  float x9 = x3 + x8;
428  float x10 = U[2] - X[11];
429  float x11 = x2 * x5;
430  float x12 = x1 * x6;
431  float x13 = x12 * x4;
432  float x14 = x11 - x13;
433  float x15 = U[0] - X[9];
434  float x16 = x15 * x4;
435  float x17 = cos(X[7]);
436  float x18 = x0 * x17;
437  float x19 = x10 * x17;
438  float x20 = x17 * x2;
439  float x21 = x11 * x4;
440  float x22 = x12 - x21;
441  float x23 = -x3 * x4 - x7;
442  float x24 = x17 * x6;
443  float x25 = x17 * x5;
444  float x26 = x1 * x17;
445  float x27 = x4 * x5;
446  float x28 = U[4] - X[13];
447  float x29 = tan(X[7]);
448  float x30 = x29 * x5;
449  float x31 = U[5] - X[14];
450  float x32 = x1 * x29;
451  float x33 = pow(x29, 2) + 1;
452  float x34 = x1 * x28;
453  float x35 = 1.0 / x17;
454  float x36 = x35 * x5;
455  float x37 = x1 * x35;
456  float x38 = pow(x17, -2);
457  out[0][0] = 0;
458  out[0][1] = 0;
459  out[0][2] = 0;
460  out[0][3] = 1;
461  out[0][4] = 0;
462  out[0][5] = 0;
463  out[0][6] = 0;
464  out[0][7] = 0;
465  out[0][8] = 0;
466  out[0][9] = 0;
467  out[0][10] = 0;
468  out[0][11] = 0;
469  out[0][12] = 0;
470  out[0][13] = 0;
471  out[0][14] = 0;
472  out[1][0] = 0;
473  out[1][1] = 0;
474  out[1][2] = 0;
475  out[1][3] = 0;
476  out[1][4] = 1;
477  out[1][5] = 0;
478  out[1][6] = 0;
479  out[1][7] = 0;
480  out[1][8] = 0;
481  out[1][9] = 0;
482  out[1][10] = 0;
483  out[1][11] = 0;
484  out[1][12] = 0;
485  out[1][13] = 0;
486  out[1][14] = 0;
487  out[2][0] = 0;
488  out[2][1] = 0;
489  out[2][2] = 0;
490  out[2][3] = 0;
491  out[2][4] = 0;
492  out[2][5] = 1;
493  out[2][6] = 0;
494  out[2][7] = 0;
495  out[2][8] = 0;
496  out[2][9] = 0;
497  out[2][10] = 0;
498  out[2][11] = 0;
499  out[2][12] = 0;
500  out[2][13] = 0;
501  out[2][14] = 0;
502  out[3][0] = 0;
503  out[3][1] = 0;
504  out[3][2] = 0;
505  out[3][3] = 0;
506  out[3][4] = 0;
507  out[3][5] = 0;
508  out[3][6] = x0 * x9 + x10 * x14;
509  out[3][7] = x12 * x18 - x16 * x6 + x19 * x7;
510  out[3][8] = x0 * x23 + x10 * x22 - x15 * x20;
511  out[3][9] = -x24;
512  out[3][10] = x14;
513  out[3][11] = -x3 - x8;
514  out[3][12] = 0;
515  out[3][13] = 0;
516  out[3][14] = 0;
517  out[4][0] = 0;
518  out[4][1] = 0;
519  out[4][2] = 0;
520  out[4][3] = 0;
521  out[4][4] = 0;
522  out[4][5] = 0;
523  out[4][6] = x0 * (-x12 + x21) + x10 * x23;
524  out[4][7] = x11 * x19 - x16 * x2 + x18 * x3;
525  out[4][8] = x0 * (-x11 + x13) + x10 * x9 + x15 * x24;
526  out[4][9] = -x20;
527  out[4][10] = x23;
528  out[4][11] = x22;
529  out[4][12] = 0;
530  out[4][13] = 0;
531  out[4][14] = 0;
532  out[5][0] = 0;
533  out[5][1] = 0;
534  out[5][2] = 0;
535  out[5][3] = 0;
536  out[5][4] = 0;
537  out[5][5] = 0;
538  out[5][6] = x0 * x25 - x10 * x26;
539  out[5][7] = -x0 * x1 * x4 - x10 * x27 + x17 * (-U[0] + X[9]);
540  out[5][8] = 0;
541  out[5][9] = x4;
542  out[5][10] = -x26;
543  out[5][11] = -x25;
544  out[5][12] = 0;
545  out[5][13] = 0;
546  out[5][14] = 0;
547  out[6][0] = 0;
548  out[6][1] = 0;
549  out[6][2] = 0;
550  out[6][3] = 0;
551  out[6][4] = 0;
552  out[6][5] = 0;
553  out[6][6] = x28 * x30 - x31 * x32;
554  out[6][7] = x31 * x33 * x5 + x33 * x34;
555  out[6][8] = 0;
556  out[6][9] = 0;
557  out[6][10] = 0;
558  out[6][11] = 0;
559  out[6][12] = -1;
560  out[6][13] = -x32;
561  out[6][14] = -x30;
562  out[7][0] = 0;
563  out[7][1] = 0;
564  out[7][2] = 0;
565  out[7][3] = 0;
566  out[7][4] = 0;
567  out[7][5] = 0;
568  out[7][6] = -x34 + x5 * (-U[5] + X[14]);
569  out[7][7] = 0;
570  out[7][8] = 0;
571  out[7][9] = 0;
572  out[7][10] = 0;
573  out[7][11] = 0;
574  out[7][12] = 0;
575  out[7][13] = -x5;
576  out[7][14] = x1;
577  out[8][0] = 0;
578  out[8][1] = 0;
579  out[8][2] = 0;
580  out[8][3] = 0;
581  out[8][4] = 0;
582  out[8][5] = 0;
583  out[8][6] = x28 * x36 - x31 * x37;
584  out[8][7] = x27 * x31 * x38 + x34 * x38 * x4;
585  out[8][8] = 0;
586  out[8][9] = 0;
587  out[8][10] = 0;
588  out[8][11] = 0;
589  out[8][12] = 0;
590  out[8][13] = -x37;
591  out[8][14] = -x36;
592  out[9][0] = 0;
593  out[9][1] = 0;
594  out[9][2] = 0;
595  out[9][3] = 0;
596  out[9][4] = 0;
597  out[9][5] = 0;
598  out[9][6] = 0;
599  out[9][7] = 0;
600  out[9][8] = 0;
601  out[9][9] = 0;
602  out[9][10] = 0;
603  out[9][11] = 0;
604  out[9][12] = 0;
605  out[9][13] = 0;
606  out[9][14] = 0;
607  out[10][0] = 0;
608  out[10][1] = 0;
609  out[10][2] = 0;
610  out[10][3] = 0;
611  out[10][4] = 0;
612  out[10][5] = 0;
613  out[10][6] = 0;
614  out[10][7] = 0;
615  out[10][8] = 0;
616  out[10][9] = 0;
617  out[10][10] = 0;
618  out[10][11] = 0;
619  out[10][12] = 0;
620  out[10][13] = 0;
621  out[10][14] = 0;
622  out[11][0] = 0;
623  out[11][1] = 0;
624  out[11][2] = 0;
625  out[11][3] = 0;
626  out[11][4] = 0;
627  out[11][5] = 0;
628  out[11][6] = 0;
629  out[11][7] = 0;
630  out[11][8] = 0;
631  out[11][9] = 0;
632  out[11][10] = 0;
633  out[11][11] = 0;
634  out[11][12] = 0;
635  out[11][13] = 0;
636  out[11][14] = 0;
637  out[12][0] = 0;
638  out[12][1] = 0;
639  out[12][2] = 0;
640  out[12][3] = 0;
641  out[12][4] = 0;
642  out[12][5] = 0;
643  out[12][6] = 0;
644  out[12][7] = 0;
645  out[12][8] = 0;
646  out[12][9] = 0;
647  out[12][10] = 0;
648  out[12][11] = 0;
649  out[12][12] = 0;
650  out[12][13] = 0;
651  out[12][14] = 0;
652  out[13][0] = 0;
653  out[13][1] = 0;
654  out[13][2] = 0;
655  out[13][3] = 0;
656  out[13][4] = 0;
657  out[13][5] = 0;
658  out[13][6] = 0;
659  out[13][7] = 0;
660  out[13][8] = 0;
661  out[13][9] = 0;
662  out[13][10] = 0;
663  out[13][11] = 0;
664  out[13][12] = 0;
665  out[13][13] = 0;
666  out[13][14] = 0;
667  out[14][0] = 0;
668  out[14][1] = 0;
669  out[14][2] = 0;
670  out[14][3] = 0;
671  out[14][4] = 0;
672  out[14][5] = 0;
673  out[14][6] = 0;
674  out[14][7] = 0;
675  out[14][8] = 0;
676  out[14][9] = 0;
677  out[14][10] = 0;
678  out[14][11] = 0;
679  out[14][12] = 0;
680  out[14][13] = 0;
681  out[14][14] = 0;
682 }
683 
684 static inline void ekf_L(const float X[EKF_NUM_STATES], __attribute__((unused)) const float U[EKF_NUM_INPUTS],
685  float out[EKF_NUM_STATES][EKF_NUM_INPUTS])
686 {
687  float x0 = cos(X[7]);
688  float x1 = cos(X[8]);
689  float x2 = sin(X[8]);
690  float x3 = cos(X[6]);
691  float x4 = x2 * x3;
692  float x5 = sin(X[7]);
693  float x6 = sin(X[6]);
694  float x7 = x1 * x6;
695  float x8 = x2 * x6;
696  float x9 = x1 * x3;
697  float x10 = tan(X[7]);
698  float x11 = 1.0 / x0;
699  out[0][0] = 0;
700  out[0][1] = 0;
701  out[0][2] = 0;
702  out[0][3] = 0;
703  out[0][4] = 0;
704  out[0][5] = 0;
705  out[1][0] = 0;
706  out[1][1] = 0;
707  out[1][2] = 0;
708  out[1][3] = 0;
709  out[1][4] = 0;
710  out[1][5] = 0;
711  out[2][0] = 0;
712  out[2][1] = 0;
713  out[2][2] = 0;
714  out[2][3] = 0;
715  out[2][4] = 0;
716  out[2][5] = 0;
717  out[3][0] = -x0 * x1;
718  out[3][1] = x4 - x5 * x7;
719  out[3][2] = -x5 * x9 - x8;
720  out[3][3] = 0;
721  out[3][4] = 0;
722  out[3][5] = 0;
723  out[4][0] = -x0 * x2;
724  out[4][1] = -x5 * x8 - x9;
725  out[4][2] = -x4 * x5 + x7;
726  out[4][3] = 0;
727  out[4][4] = 0;
728  out[4][5] = 0;
729  out[5][0] = x5;
730  out[5][1] = -x0 * x6;
731  out[5][2] = -x0 * x3;
732  out[5][3] = 0;
733  out[5][4] = 0;
734  out[5][5] = 0;
735  out[6][0] = 0;
736  out[6][1] = 0;
737  out[6][2] = 0;
738  out[6][3] = -1;
739  out[6][4] = -x10 * x6;
740  out[6][5] = -x10 * x3;
741  out[7][0] = 0;
742  out[7][1] = 0;
743  out[7][2] = 0;
744  out[7][3] = 0;
745  out[7][4] = -x3;
746  out[7][5] = x6;
747  out[8][0] = 0;
748  out[8][1] = 0;
749  out[8][2] = 0;
750  out[8][3] = 0;
751  out[8][4] = -x11 * x6;
752  out[8][5] = -x11 * x3;
753  out[9][0] = 0;
754  out[9][1] = 0;
755  out[9][2] = 0;
756  out[9][3] = 0;
757  out[9][4] = 0;
758  out[9][5] = 0;
759  out[10][0] = 0;
760  out[10][1] = 0;
761  out[10][2] = 0;
762  out[10][3] = 0;
763  out[10][4] = 0;
764  out[10][5] = 0;
765  out[11][0] = 0;
766  out[11][1] = 0;
767  out[11][2] = 0;
768  out[11][3] = 0;
769  out[11][4] = 0;
770  out[11][5] = 0;
771  out[12][0] = 0;
772  out[12][1] = 0;
773  out[12][2] = 0;
774  out[12][3] = 0;
775  out[12][4] = 0;
776  out[12][5] = 0;
777  out[13][0] = 0;
778  out[13][1] = 0;
779  out[13][2] = 0;
780  out[13][3] = 0;
781  out[13][4] = 0;
782  out[13][5] = 0;
783  out[14][0] = 0;
784  out[14][1] = 0;
785  out[14][2] = 0;
786  out[14][3] = 0;
787  out[14][4] = 0;
788  out[14][5] = 0;
789 }
790 
791 
792 
793 static inline void ekf_f_rk4(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], const float dt,
794  float out[EKF_NUM_STATES])
795 {
796  float k1[EKF_NUM_STATES];
797  float k2[EKF_NUM_STATES];
798  float k3[EKF_NUM_STATES];
799  float k4[EKF_NUM_STATES];
800 
801  float Xtmp[EKF_NUM_STATES];
802 
803  // k1 = f(X,U)
804  ekf_f(X, U, k1);
805 
806  // Xtmp = X+dt*k1/2
807  float_vect_smul(Xtmp, k1, dt / 2, EKF_NUM_STATES);
809 
810  // k2 = f(Xtmp,U)
811  ekf_f(Xtmp, U, k2);
812 
813  // Xtmp = X+dt*k2/2
814  float_vect_smul(Xtmp, k2, dt / 2, EKF_NUM_STATES);
816 
817  // k3 = f(Xtmp,U)
818  ekf_f(Xtmp, U, k3);
819 
820  // Xtmp = X+dt*k3
821  float_vect_smul(Xtmp, k3, dt, EKF_NUM_STATES);
823 
824  // k4 = f(Xtmp,U)
825  ekf_f(Xtmp, U, k4);
826 
827  // out = k2+k3
828  float_vect_sum(out, k2, k3, EKF_NUM_STATES);
829  // out *= 2
831  // out += k1
832  float_vect_add(out, k1, EKF_NUM_STATES);
833  // out += k4
834  float_vect_add(out, k4, EKF_NUM_STATES);
835  // out *= dt/6
836  float_vect_scale(out, dt / 6, EKF_NUM_STATES);
837  // out += X
839 }
840 
841 
842 static inline void ekf_step(const float U[EKF_NUM_INPUTS], const float Z[EKF_NUM_OUTPUTS], const float dt)
843 {
844  // [1] Predicted (a priori) state estimate:
845  float Xkk_1[EKF_NUM_STATES];
846  ekf_f_rk4(ekf_X, U, dt, Xkk_1);
847 
848 
849  // [2] Get matrices
850  float F[EKF_NUM_STATES][EKF_NUM_STATES];
851  float L[EKF_NUM_STATES][EKF_NUM_INPUTS];
852  ekf_F(ekf_X, U, F);
853  ekf_L(ekf_X, U, L);
854 
855 
856  // [3] Continuous to discrete
857  // Fd = eye(N) + F*dt + F*F*dt**2/2 = I + [I+F*dt/2]*F*dt
858  // Ld = L*dt+F*L*dt**2/2 = [I+F*dt/2]*L*dt
859  float Fd[EKF_NUM_STATES][EKF_NUM_STATES];
860  float Ld[EKF_NUM_STATES][EKF_NUM_INPUTS];
861  float tmp[EKF_NUM_STATES][EKF_NUM_STATES];
862 
867  MAKE_MATRIX_PTR(tmp_, tmp, EKF_NUM_STATES);
868 
869  // tmp = I+F*dt/2
872 
873  // Ld = tmp*L*dt
876 
877  // Fd = tmp*F*dt
880 
881  // Fd += I
882  int i;
883  for (i = 0; i < EKF_NUM_STATES; i++) {
884  Fd[i][i] += 1;
885  }
886 
887 
888  // [4] Predicted covariance estimate:
889  // Pkk_1 = Fd*P*Fd.T + Ld*Q*Ld.T
890  float Pkk_1[EKF_NUM_STATES][EKF_NUM_STATES];
891  float LdT[EKF_NUM_INPUTS][EKF_NUM_STATES];
892  float QLdT[EKF_NUM_INPUTS][EKF_NUM_STATES];
893 
894  MAKE_MATRIX_PTR(Pkk_1_, Pkk_1, EKF_NUM_STATES);
897  MAKE_MATRIX_PTR(LdT_, LdT, EKF_NUM_INPUTS);
898  MAKE_MATRIX_PTR(QLdT_, QLdT, EKF_NUM_INPUTS);
899 
900  // Fd = Fd.T
902 
903  // tmp = P*Fd
905 
906  // Fd = Fd.T
908 
909  // Pkk_1 = Fd*tmp
911 
912  // LdT = Ld.T
914 
915  // QLdT = Q*LdT
917 
918  // tmp = Ld*QLdT
920 
921  // Pkk_1 += tmp
923 
924 
925  // [5] Measurement residual:
926  // yk = Z - H*Xkk_1
927  float yk[EKF_NUM_OUTPUTS];
928 
930 
934 
935 
936  // [6] Residual covariance:
937  // Sk = H*Pkk_1*H.T + R
938  float Sk[EKF_NUM_OUTPUTS][EKF_NUM_OUTPUTS];
939  float PHT[EKF_NUM_STATES][EKF_NUM_OUTPUTS];
940 
942  MAKE_MATRIX_PTR(PHT_, PHT, EKF_NUM_STATES);
944 
945  // PHT = Pkk_1*H.T
948 
949  // Sk = H*PHT
951 
952  // Sk += R
954 
955 
956  // [7] Near-optimal Kalman gain:
957  // K = Pkk_1*H.T*inv(Sk)
958  float Sk_inv[EKF_NUM_OUTPUTS][EKF_NUM_OUTPUTS];
960 
961  MAKE_MATRIX_PTR(Sk_inv_, Sk_inv, EKF_NUM_OUTPUTS);
963 
964  // Sk_inv = inv(Sk)
965  float_mat_invert(Sk_inv_, Sk_, EKF_NUM_OUTPUTS);
966 
967  // K = PHT*Sk_inv
969 
970 
971  // [8] Updated state estimate
972  // Xkk = Xkk_1 + K*yk
975 
976 
977  // [9] Updated covariance estimate:
978  // Pkk = (I - K*H)*Pkk_1
979 
980  // tmp = K*H
982 
983  // tmp *= -1
985 
986  // tmp += I
987  for (i = 0; i < EKF_NUM_STATES; i++) {
988  tmp_[i][i] += 1;
989  }
990  // P = tmp*Pkk_1
992 }
993 
994 static inline void ekf_prediction_step(const float U[EKF_NUM_INPUTS], const float dt)
995 {
996  // [1] Predicted (a priori) state estimate:
997  float Xkk_1[EKF_NUM_STATES];
998  // Xkk_1 = f(X,U)
999  ekf_f(ekf_X, U, Xkk_1);
1000  // Xkk_1 *= dt
1001  float_vect_scale(Xkk_1, dt, EKF_NUM_STATES);
1002  // Xkk_1 += X
1004 
1005 
1006  // [2] Get matrices
1007  float F[EKF_NUM_STATES][EKF_NUM_STATES];
1008  float Ld[EKF_NUM_STATES][EKF_NUM_INPUTS];
1009  ekf_F(ekf_X, U, F);
1010  ekf_L(ekf_X, U, Ld);
1011 
1012 
1013  // [3] Continuous to discrete
1014  // Fd = eye(N) + F*dt
1015  // Ld = L*dt
1016  float Fd[EKF_NUM_STATES][EKF_NUM_STATES];
1017 
1019  MAKE_MATRIX_PTR(Fd_, Fd, EKF_NUM_STATES);
1020  MAKE_MATRIX_PTR(Ld_, Ld, EKF_NUM_STATES);
1021 
1022  // Fd = I+F*dt/2
1025 
1026  // Ld = Ld*dt
1028 
1029 
1030  // [4] Predicted covariance estimate:
1031  // Pkk_1 = Fd*P*Fd.T + Ld*Q*Ld.T
1032  float Pkk_1[EKF_NUM_STATES][EKF_NUM_STATES];
1033  float LdT[EKF_NUM_INPUTS][EKF_NUM_STATES];
1034  float QLdT[EKF_NUM_INPUTS][EKF_NUM_STATES];
1035  float tmp[EKF_NUM_STATES][EKF_NUM_STATES];
1036 
1037  MAKE_MATRIX_PTR(Pkk_1_, Pkk_1, EKF_NUM_STATES);
1040  MAKE_MATRIX_PTR(LdT_, LdT, EKF_NUM_INPUTS);
1041  MAKE_MATRIX_PTR(QLdT_, QLdT, EKF_NUM_INPUTS);
1042  MAKE_MATRIX_PTR(tmp_, tmp, EKF_NUM_STATES);
1043 
1044  // Fd = Fd.T
1046 
1047  // tmp = P*Fd
1049 
1050  // Fd = Fd.T
1052 
1053  // Pkk_1 = Fd*tmp
1055 
1056  // LdT = Ld.T
1058 
1059  // QLdT = Q*LdT
1061 
1062  // tmp = Ld*QLdT
1064 
1065  // Pkk_1 += tmp
1067 
1068  // X = Xkk_1
1070 
1071  // P = Pkk_1
1072  float_mat_copy(ekf_P_, Pkk_1_, EKF_NUM_STATES, EKF_NUM_STATES);
1073 }
1074 
1075 static inline void ekf_measurement_step(const float Z[EKF_NUM_OUTPUTS])
1076 {
1077  // Xkk_1 = X
1078  float Xkk_1[EKF_NUM_STATES];
1080 
1081  // Pkk_1 = P
1082  float Pkk_1[EKF_NUM_STATES][EKF_NUM_STATES];
1083  MAKE_MATRIX_PTR(Pkk_1_, Pkk_1, EKF_NUM_STATES);
1085  float_mat_copy(Pkk_1_, ekf_P_, EKF_NUM_STATES, EKF_NUM_STATES);
1086 
1087  // [5] Measurement residual:
1088  // yk = Z - H*Xkk_1
1089  float yk[EKF_NUM_OUTPUTS];
1090 
1092 
1093  float_mat_vect_mul(yk, ekf_H_, Xkk_1, EKF_NUM_OUTPUTS, EKF_NUM_STATES);
1096 
1097 
1098  // [6] Residual covariance:
1099  // Sk = H*Pkk_1*H.T + R
1100  float Sk[EKF_NUM_OUTPUTS][EKF_NUM_OUTPUTS];
1101  float PHT[EKF_NUM_STATES][EKF_NUM_OUTPUTS];
1102 
1103  MAKE_MATRIX_PTR(Sk_, Sk, EKF_NUM_OUTPUTS);
1104  MAKE_MATRIX_PTR(PHT_, PHT, EKF_NUM_STATES);
1106 
1107  // PHT = Pkk_1*H.T
1110 
1111  // Sk = H*PHT
1113 
1114  // Sk += R
1116 
1117 
1118  // [7] Near-optimal Kalman gain:
1119  // K = Pkk_1*H.T*inv(Sk)
1120  float Sk_inv[EKF_NUM_OUTPUTS][EKF_NUM_OUTPUTS];
1122 
1123  MAKE_MATRIX_PTR(Sk_inv_, Sk_inv, EKF_NUM_OUTPUTS);
1125 
1126  // Sk_inv = inv(Sk)
1127  float_mat_invert(Sk_inv_, Sk_, EKF_NUM_OUTPUTS);
1128 
1129  // K = PHT*Sk_inv
1131 
1132 
1133  // [8] Updated state estimate
1134  // Xkk = Xkk_1 + K*yk
1137 
1138 
1139  // [9] Updated covariance estimate:
1140  // Pkk = (I - K*H)*Pkk_1
1141  float tmp[EKF_NUM_STATES][EKF_NUM_STATES];
1142  MAKE_MATRIX_PTR(tmp_, tmp, EKF_NUM_STATES);
1143 
1144  // tmp = K*H
1146 
1147  // tmp *= -1
1149 
1150  // tmp += I
1151  int i;
1152  for (i = 0; i < EKF_NUM_STATES; i++) {
1153  tmp_[i][i] += 1;
1154  }
1155  // P = tmp*Pkk_1
1156  float_mat_mul(ekf_P_, tmp_, Pkk_1_, EKF_NUM_STATES, EKF_NUM_STATES, EKF_NUM_STATES);
1157 }
1158 
1159 
1160 
1161 
1162 
1163 static inline void ekf_run(void)
1164 {
1165  static bool start = false;
1166 
1167 
1168  // Time
1169  t1 = get_sys_time_float();
1170  float dt = t1 - t0;
1171  t0 = t1;
1172 
1173  // Only Start If External Pose is Available
1174  if (!start) {
1175  // ekf starts at the first ev update
1177  start = true;
1178 
1179  // initial guess
1180  ekf_X[0] = ins_ext_pos.ev_pos.x;
1181  ekf_X[1] = ins_ext_pos.ev_pos.y;
1182  ekf_X[2] = ins_ext_pos.ev_pos.z;
1183  ekf_X[6] = ins_ext_pos.ev_att.phi;
1185  ekf_X[8] = ins_ext_pos.ev_att.psi;
1186  }
1187  }
1188 
1189  // set input values
1190  if (ins_ext_pos.has_new_acc) {
1191  ekf_U[0] = ins_ext_pos.accels_f.x;
1192  ekf_U[1] = ins_ext_pos.accels_f.y;
1193  ekf_U[2] = ins_ext_pos.accels_f.z;
1194  ins_ext_pos.has_new_acc = false;
1195  } else {
1196  DEBUG_PRINT("ekf missing acc\n");
1197  }
1198  if (ins_ext_pos.has_new_gyro) {
1199  ekf_U[3] = ins_ext_pos.gyros_f.p;
1200  ekf_U[4] = ins_ext_pos.gyros_f.q;
1201  ekf_U[5] = ins_ext_pos.gyros_f.r;
1202  ins_ext_pos.has_new_gyro = false;
1203  } else {
1204  DEBUG_PRINT("ekf missing gyro\n");
1205  }
1206 
1207  if (start) {
1208 
1209  // prediction step
1210  DEBUG_PRINT("ekf prediction step U = %f, %f, %f, %f, %f, %f dt = %f \n", ekf_U[0], ekf_U[1], ekf_U[2], ekf_U[3],
1211  ekf_U[4], ekf_U[5], dt);
1213 
1214  // measurement step
1216 
1217  //fix psi
1218  static float last_psi = 0;
1219  float delta_psi = ins_ext_pos.ev_att.psi - last_psi;
1220  last_psi = ins_ext_pos.ev_att.psi;
1221 
1222  if (delta_psi > M_PI) {
1223  delta_psi -= 2 * M_PI;
1224  } else if (delta_psi < -M_PI) {
1225  delta_psi += 2 * M_PI;
1226  }
1227 
1228 
1229  ekf_Z[0] = ins_ext_pos.ev_pos.x;
1230  ekf_Z[1] = ins_ext_pos.ev_pos.y;
1231  ekf_Z[2] = ins_ext_pos.ev_pos.z;
1232  ekf_Z[3] = ins_ext_pos.ev_att.phi;
1234  ekf_Z[5] += delta_psi;
1235  ins_ext_pos.has_new_ext_pose = false;
1236 
1237  DEBUG_PRINT("ekf measurement step Z = %f, %f, %f, %f \n", ekf_Z[0], ekf_Z[1], ekf_Z[2], ekf_Z[3]);
1239  }
1240  }
1241 
1242  // Export Results
1243  struct NedCoor_f ned_pos;
1244  ned_pos.x = ekf_X[0];
1245  ned_pos.y = ekf_X[1];
1246  ned_pos.z = ekf_X[2];
1247 
1248  struct NedCoor_f ned_speed;
1249  ned_speed.x = ekf_X[3];
1250  ned_speed.y = ekf_X[4];
1251  ned_speed.z = ekf_X[5];
1252 
1253  struct FloatEulers ned_to_body_eulers;
1254  ned_to_body_eulers.phi = ekf_X[6];
1255  ned_to_body_eulers.theta = ekf_X[7];
1256  ned_to_body_eulers.psi = ekf_X[8];
1257 
1258  struct FloatRates rates = { ekf_U[3] - ekf_X[12], ekf_U[4] - ekf_X[13], ekf_U[5] - ekf_X[14] };
1259 
1260  struct FloatVect3 accel;
1261  struct FloatVect3 accel_ned_f;
1262  accel.x = ekf_U[0] - ekf_X[9];
1263  accel.y = ekf_U[1] - ekf_X[10];
1264  accel.z = ekf_U[2] - ekf_X[11];
1265 
1266  // Export Body Accelerations (without bias)
1267  struct Int32Vect3 accel_i;
1268  ACCELS_BFP_OF_REAL(accel_i, accel);
1269  stateSetAccelBody_i(&accel_i);
1270 
1271 
1272  struct FloatRMat *ned_to_body_rmat_f = stateGetNedToBodyRMat_f();
1273  float_rmat_transp_vmult(&accel_ned_f, ned_to_body_rmat_f, &accel);
1274  accel_ned_f.z += 9.81;
1275 
1276  stateSetPositionNed_f(&ned_pos);
1277  stateSetSpeedNed_f(&ned_speed);
1278  stateSetNedToBodyEulers_f(&ned_to_body_eulers);
1279  stateSetBodyRates_f(&rates);
1280  stateSetAccelNed_f((struct NedCoor_f *)&accel_ned_f);
1281 
1282 }
1283 
1284 
1285 
1290 void ins_ext_pos_log_header(FILE *file)
1291 {
1292  fprintf(file,
1293  "ekf_X1,ekf_X2,ekf_X3,ekf_X4,ekf_X5,ekf_X6,ekf_X7,ekf_X8,ekf_X9,ekf_X10,ekf_X11,ekf_X12,ekf_X13,ekf_X14,ekf_X15,");
1294  fprintf(file, "ekf_U1,ekf_U2,ekf_U3,ekf_U4,ekf_U5,ekf_U6,");
1295  fprintf(file, "ekf_Z1,ekf_Z2,ekf_Z3,ekf_Z4,");
1296 }
1297 
1298 void ins_ext_pos_log_data(FILE *file)
1299 {
1300  fprintf(file, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,", ekf_X[0], ekf_X[1], ekf_X[2], ekf_X[3], ekf_X[4],
1301  ekf_X[5], ekf_X[6], ekf_X[7], ekf_X[8], ekf_X[9], ekf_X[10], ekf_X[11], ekf_X[12], ekf_X[13], ekf_X[14]);
1302  fprintf(file, "%f,%f,%f,%f,%f,%f,", ekf_U[0], ekf_U[1], ekf_U[2], ekf_U[3], ekf_U[4], ekf_U[5]);
1303  fprintf(file, "%f,%f,%f,%f,", ekf_Z[0], ekf_Z[1], ekf_Z[2], ekf_Z[3]);
1304 }
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
float q
in rad/s
float phi
in radians
float p
in rad/s
float r
in rad/s
float theta
in radians
float psi
in radians
static void float_vect_add(float *a, const float *b, const int n)
a += b
static void float_vect_sum(float *o, const float *a, const float *b, const int n)
o = a + b
static void float_vect_smul(float *o, const float *a, const float s, const int n)
o = a * s
static void float_mat_vect_mul(float *o, float **a, float *b, int m, int n)
o = a * b
static void float_vect_scale(float *a, const float s, const int n)
a *= s
static void float_mat_sum_scaled(float **a, float **b, float k, int m, int n)
a += k*b, where k is a scalar value
static void float_mat_mul_copy(float **o, float **a, float **b, int m, int n, int l)
o = a * b
static void float_mat_copy(float **a, float **b, int m, int n)
a = b
static void float_vect_copy(float *a, const float *b, const int n)
a = b
static void float_mat_mul(float **o, float **a, float **b, int m, int n, int l)
o = a * b
static void float_mat_transpose(float **o, float **a, int n, int m)
transpose non-square matrix
#define MAKE_MATRIX_PTR(_ptr, _mat, _rows)
Make a pointer to a matrix of _rows lines.
void float_mat_invert(float **o, float **mat, int n)
Calculate inverse of any n x n matrix (passed as C array) o = mat^-1 Algorithm verified with Matlab.
void float_rmat_transp_vmult(struct FloatVect3 *vb, struct FloatRMat *m_b2a, struct FloatVect3 *va)
rotate 3D vector by transposed rotation matrix.
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
euler rotation 'ZYX'
static void float_mat_transpose_square(float **a, int n)
transpose square matrix
static void float_mat_diagonal_scal(float **o, float v, int n)
Make an n x n identity matrix (for matrix passed as array)
static void float_mat_scale(float **a, float k, int m, int n)
a *= k, where k is a scalar value
euler angles
Roation quaternion.
rotation matrix
angular rates
#define ACCELS_BFP_OF_REAL(_ef, _ei)
Definition: pprz_algebra.h:801
#define RATES_FLOAT_OF_BFP(_rf, _ri)
Definition: pprz_algebra.h:759
#define ACCELS_FLOAT_OF_BFP(_ef, _ei)
Definition: pprz_algebra.h:795
angular rates
int32_t lat
in degrees*1e7
int32_t hmsl
Height above mean sea level in mm.
int32_t alt
in millimeters above WGS84 reference ellipsoid
int32_t z
Down.
int32_t z
in centimeters
struct LlaCoor_i lla
Reference point in lla.
int32_t x
in centimeters
int32_t y
East.
struct EcefCoor_i ecef
Reference point in ecef.
int32_t y
in centimeters
int32_t lon
in degrees*1e7
int32_t x
North.
void ecef_of_lla_i(struct EcefCoor_i *out, struct LlaCoor_i *in)
Convert a LLA to ECEF.
void ltp_def_from_ecef_i(struct LtpDef_i *def, struct EcefCoor_i *ecef)
vector in EarthCenteredEarthFixed coordinates
vector in Latitude, Longitude and Altitude
definition of the local (flat earth) coordinate system
vector in North East Down coordinates
static void stateSetAccelNed_f(struct NedCoor_f *ned_accel)
Set acceleration in NED coordinates (float).
Definition: state.h:1002
static struct FloatRMat * stateGetNedToBodyRMat_f(void)
Get vehicle body attitude rotation matrix (float).
Definition: state.h:1137
static void stateSetNedToBodyEulers_f(struct FloatEulers *ned_to_body_eulers)
Set vehicle body attitude from euler angles (float).
Definition: state.h:1105
static void stateSetPositionNed_f(struct NedCoor_f *ned_pos)
Set position from local NED coordinates (float).
Definition: state.h:598
static void stateSetLocalOrigin_i(struct LtpDef_i *ltp_def)
Set the local (flat earth) coordinate frame origin (int).
Definition: state.h:457
static void stateSetBodyRates_f(struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1181
static void stateSetAccelBody_i(struct Int32Vect3 *body_accel)
Set acceleration in Body coordinates (int).
Definition: state.h:855
static void stateSetSpeedNed_f(struct NedCoor_f *ned_speed)
Set ground speed in local NED coordinates (float).
Definition: state.h:809
Inertial Measurement Unit interface.
Integrated Navigation System interface.
static void ekf_L(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], float out[EKF_NUM_STATES][EKF_NUM_INPUTS])
Definition: ins_ext_pose.c:684
struct InsExtPose ins_ext_pos
Definition: ins_ext_pose.c:70
struct FloatRates gyros_f
Definition: ins_ext_pose.c:50
struct FloatEulers ev_att
Definition: ins_ext_pose.c:57
void ins_reset_local_origin(void)
INS local origin reset.
Definition: ins_ext_pose.c:242
static void ekf_f_rk4(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], const float dt, float out[EKF_NUM_STATES])
Definition: ins_ext_pose.c:793
struct FloatVect3 ev_vel
Definition: ins_ext_pose.c:56
static void send_ins(struct transport_tx *trans, struct link_device *dev)
Provide telemetry.
Definition: ins_ext_pose.c:99
void ins_ext_pose_msg_update(uint8_t *buf)
Import External Pose Message.
Definition: ins_ext_pose.c:194
static void ekf_F(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], float out[EKF_NUM_STATES][EKF_NUM_STATES])
Definition: ins_ext_pose.c:415
void ins_ext_pos_log_data(FILE *file)
float ekf_P[EKF_NUM_STATES][EKF_NUM_STATES]
Definition: ins_ext_pose.c:324
float ekf_R[EKF_NUM_OUTPUTS][EKF_NUM_OUTPUTS]
Definition: ins_ext_pose.c:326
struct LtpDef_i ltp_def
Definition: ins_ext_pose.c:63
static abi_event accel_ev
Definition: ins_ext_pose.c:165
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
Definition: ins_ext_pose.c:173
struct FloatQuat ev_quat
Definition: ins_ext_pose.c:58
struct NedCoor_i ltp_pos
Definition: ins_ext_pose.c:66
static void ekf_init(void)
EKF protos.
Definition: ins_ext_pose.c:351
struct FloatVect3 accels_f
Definition: ins_ext_pose.c:51
static void ins_ext_pose_init_from_flightplan(void)
Definition: ins_ext_pose.c:73
bool has_new_gyro
Definition: ins_ext_pose.c:52
float ekf_U[EKF_NUM_INPUTS]
Definition: ins_ext_pose.c:322
void ekf_set_diag(float **a, float *b, int n)
Definition: ins_ext_pose.c:335
float t0
Definition: ins_ext_pose.c:331
void ins_ext_pose_init(void)
Module.
Definition: ins_ext_pose.c:263
struct NedCoor_i ltp_speed
Definition: ins_ext_pose.c:67
static void ekf_measurement_step(const float Z[EKF_NUM_OUTPUTS])
static abi_event gyro_ev
Definition: ins_ext_pose.c:166
static void send_ins_ref(struct transport_tx *trans, struct link_device *dev)
Definition: ins_ext_pose.c:115
bool has_new_ext_pose
Definition: ins_ext_pose.c:59
struct FloatVect3 ev_pos
Definition: ins_ext_pose.c:55
struct NedCoor_i ltp_accel
Definition: ins_ext_pose.c:68
static void send_ins_z(struct transport_tx *trans, struct link_device *dev)
Definition: ins_ext_pose.c:107
float ekf_Q[EKF_NUM_INPUTS][EKF_NUM_INPUTS]
Definition: ins_ext_pose.c:325
bool has_new_acc
Definition: ins_ext_pose.c:53
#define DEBUG_PRINT(...)
Definition: ins_ext_pose.c:43
void ins_ext_pose_run(void)
Definition: ins_ext_pose.c:292
void ins_reset_altitude_ref(void)
INS altitude reference reset.
Definition: ins_ext_pose.c:247
#define INS_EXT_POSE_IMU_ID
Import Gyro and Acc from ABI.
Definition: ins_ext_pose.c:161
float ev_time
Definition: ins_ext_pose.c:60
static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
Definition: ins_ext_pose.c:181
float t1
Definition: ins_ext_pose.c:332
static void ekf_step(const float U[EKF_NUM_INPUTS], const float Z[EKF_NUM_OUTPUTS], const float dt)
Definition: ins_ext_pose.c:842
void ins_ext_pos_log_header(FILE *file)
Logging.
float ekf_Z[EKF_NUM_OUTPUTS]
Definition: ins_ext_pose.c:323
static void ekf_prediction_step(const float U[EKF_NUM_INPUTS], const float dt)
Definition: ins_ext_pose.c:994
static void send_external_pose_down(struct transport_tx *trans, struct link_device *dev)
Definition: ins_ext_pose.c:124
static void ekf_f(const float X[EKF_NUM_STATES], const float U[EKF_NUM_INPUTS], float out[EKF_NUM_STATES])
Definition: ins_ext_pose.c:376
static void ekf_run(void)
float ekf_X[EKF_NUM_STATES]
Definition: ins_ext_pose.c:321
float ekf_H[EKF_NUM_OUTPUTS][EKF_NUM_STATES]
Definition: ins_ext_pose.c:328
static void send_ahrs_bias(struct transport_tx *trans, struct link_device *dev)
Definition: ins_ext_pose.c:139
Data for telemetry and LTP origin.
Definition: ins_ext_pose.c:48
Integrated Navigation System interface.
#define EKF_NUM_OUTPUTS
Definition: ins_ext_pose.h:37
#define EKF_NUM_INPUTS
Definition: ins_ext_pose.h:36
#define EKF_NUM_STATES
Definition: ins_ext_pose.h:35
void waypoints_localize_all(void)
update local ENU coordinates of global waypoints
Definition: waypoints.c:357
Paparazzi floating point algebra.
float z
in meters
float x
in meters
float y
in meters
vector in North East Down coordinates Units: meters
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
static float K[9]
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
float b
Definition: wedgebug.c:202