/sw/airborne/booz/test/test_mlkf.c

https://bitbucket.org/tamasszabo/paparazzi_mavlink · C · 116 lines · 96 code · 20 blank · 0 comment · 7 complexity · b79f884387735e865d58f91028eabe77 MD5 · raw file

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "math/pprz_algebra_double.h"
  4. #include "subsystems/imu.h"
  5. #include "subsystems/ahrs.h"
  6. #include "ahrs/ahrs_mlkf.h"
  7. static void read_data(const char* filename);
  8. static void feed_imu(int i);
  9. static void store_filter_output(int i);
  10. static void dump_output(const char* filename);
  11. #define IN_FILE "../../../simulator/scilab/q6d/data/stop_stop_state_sensors.txt"
  12. #define OUT_FILE "./out.txt"
  13. #define MAX_SAMPLE 15000
  14. struct test_sample {
  15. double time;
  16. struct DoubleQuat quat_true;
  17. struct DoubleRates omega_true;
  18. struct DoubleRates gyro;
  19. struct DoubleVect3 accel;
  20. struct DoubleVect3 mag;
  21. };
  22. static struct test_sample samples[MAX_SAMPLE];
  23. static int nb_samples;
  24. struct test_output {
  25. struct FloatQuat quat_est;
  26. struct FloatRates bias_est;
  27. struct FloatRates rate_est;
  28. float P[6][6];
  29. };
  30. static struct test_output output[MAX_SAMPLE];
  31. int main(int argc, char** argv) {
  32. read_data(IN_FILE);
  33. imu_init();
  34. ahrs_init();
  35. for (int i=0; i<nb_samples; i++) {
  36. feed_imu(i);
  37. ahrs_propagate();
  38. ahrs_update_accel();
  39. ahrs_update_mag();
  40. store_filter_output(i);
  41. }
  42. dump_output(OUT_FILE);
  43. return 0;
  44. }
  45. static void read_data(const char* filename) {
  46. FILE* fd = fopen(filename, "r");
  47. nb_samples = 0;
  48. int ret = 0;
  49. do {
  50. struct test_sample* s = &samples[nb_samples];
  51. ret = fscanf(fd, "%lf [%lf %lf %lf %lf] [%lf %lf %lf] [%lf %lf %lf] [%lf %lf %lf] [%lf %lf %lf]",
  52. &s->time,
  53. &s->quat_true.qi, &s->quat_true.qx, &s->quat_true.qy, &s->quat_true.qz,
  54. &s->omega_true.p, &s->omega_true.q, &s->omega_true.r,
  55. &s->gyro.p, &s->gyro.q, &s->gyro.r,
  56. &s->accel.x, &s->accel.y, &s->accel.z,
  57. &s->mag.x, &s->mag.y, &s->mag.z );
  58. nb_samples++;
  59. }
  60. while (ret == 17 && nb_samples < MAX_SAMPLE);
  61. nb_samples--;
  62. fclose(fd);
  63. printf("read %d points in file %s\n", nb_samples, filename);
  64. }
  65. static void feed_imu(int i) {
  66. if (i>0) {
  67. RATES_COPY(imu.gyro_prev, imu.gyro);
  68. }
  69. else {
  70. RATES_BFP_OF_REAL(imu.gyro_prev, samples[0].gyro);
  71. }
  72. RATES_BFP_OF_REAL(imu.gyro, samples[i].gyro);
  73. ACCELS_BFP_OF_REAL(imu.accel, samples[i].accel);
  74. MAGS_BFP_OF_REAL(imu.mag, samples[i].mag);
  75. }
  76. static void store_filter_output(int i) {
  77. QUAT_COPY(output[i].quat_est, ahrs_float.ltp_to_imu_quat);
  78. RATES_COPY(output[i].bias_est, ahrs_mlkf.gyro_bias);
  79. RATES_COPY(output[i].rate_est, ahrs_float.imu_rate);
  80. memcpy(output[i].P, ahrs_mlkf.P, sizeof(ahrs_mlkf.P));
  81. }
  82. static void dump_output(const char* filename) {
  83. FILE* fd = fopen(filename, "w");
  84. int i;
  85. for (i=0; i<nb_samples; i++) {
  86. fprintf(fd, "%.16f [%.16f %.16f %.16f %.16f] [%.16f %.16f %.16f] [%.16f %.16f %.16f] [%.16f %.16f %.16f %.16f %.16f %.16f]\n",
  87. samples[i].time,
  88. output[i].quat_est.qi, output[i].quat_est.qx, output[i].quat_est.qy, output[i].quat_est.qz, // quaternion
  89. output[i].rate_est.p, output[i].rate_est.q, output[i].rate_est.r, // omega
  90. output[i].bias_est.p, output[i].bias_est.q, output[i].bias_est.r, // bias
  91. output[i].P[0][0], output[i].P[1][1], output[i].P[2][2], // covariance
  92. output[i].P[3][3], output[i].P[4][4], output[i].P[5][5] );
  93. }
  94. fclose(fd);
  95. printf("wrote %d points in file %s\n", nb_samples, filename);
  96. }