zupt_test.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. clear ;%清除工作区的缓存变量
  2. close all; % 关闭当前的所有窗口
  3. clc; %清除命令行的数据
  4. g = 9.7833;
  5. %尝试读取csv文件
  6. data_file = csvread('LoggedData2_CalInertialAndMag.csv',1 ,0);
  7. %把陀螺仪数据,加速度数据拿出来
  8. gyr_data = data_file(:, 2:4) *pi/180;
  9. acc_data = data_file(:, 5:7) * g;
  10. press_data = data_file(:,8:9)';
  11. %参数设置
  12. data_size = size(gyr_data,1);
  13. dt = 1/100;
  14. %当地重力加速度
  15. %设置过程噪声
  16. % 状态量为【姿态误差,速度误差,位置误差】
  17. % 相应的过程噪声协方差
  18. sigma_omega = 1e-2; sigma_a = 1e-2;
  19. % 观测噪声协方差,只观测速度误差
  20. sigma_v = 1e-2;
  21. R = diag([sigma_v sigma_v sigma_v*0.1]).^2;
  22. R_xy = diag([sigma_v sigma_v sigma_v sigma_v sigma_v sigma_v]).^2;
  23. %初始化姿态矩阵
  24. %先根据加速度计算俯仰角,翻滚角,航向角
  25. pitch = asin(-acc_data(1,1)/g);
  26. roll = atan2(acc_data(1,2), acc_data(1,3));
  27. yaw = 0;
  28. C_prev = [cos(yaw)*cos(pitch), -sin(yaw)*cos(roll)+cos(yaw)*sin(pitch)*sin(roll), sin(yaw)*sin(roll)+ cos(yaw)*sin(pitch)*cos(roll);
  29. sin(yaw)*cos(pitch), cos(yaw)*cos(roll)+sin(yaw)*sin(pitch)*sin(roll), -cos(yaw)*sin(roll)+sin(yaw)*sin(pitch)* cos(roll);
  30. -sin(pitch), cos(pitch)* sin(roll), cos(pitch)*cos(roll);];
  31. C = C_prev;
  32. acc_n = zeros(3, data_size);
  33. acc_n(:,1) = C*acc_data(1,:)'-[0,0,g]';
  34. vel_n = zeros(3, data_size);
  35. vel_n_norm = zeros(1,data_size);
  36. pos_n = zeros(3, data_size);
  37. P = zeros(9,9);
  38. H = [zeros(3,3), zeros(3,3), eye(3);
  39. ];
  40. H_xy= [zeros(3,3), zeros(3,3), eye(3);
  41. zeros(3,3),eye(3),zeros(3,3);
  42. ];
  43. gyr_norm = nan(1,data_size);
  44. acc_norm = nan(1,data_size);
  45. zupt = zeros(1,data_size);
  46. pitch_data = nan(1,data_size);
  47. roll_data = nan(1, data_size);
  48. yaw_data = nan(1, data_size);
  49. C_buff = zeros(3,3,data_size);
  50. C_buff(:,:,1) = C;
  51. last_zupt = 0;
  52. mini_update = 0;
  53. step_pos = zeros(3,1);
  54. last_step_xy = [0;0];
  55. last_step_xy_tmp = [0;0];
  56. pos_result = zeros(3,data_size);
  57. last_pos = [0,0,0]';
  58. last_zupt_index = 30;
  59. %以下为记录输出所用
  60. last_record = 0;
  61. output_signal = 0;
  62. output_return = 0;
  63. stand_num = 0;
  64. vel_local = zeros(2, data_size);
  65. last_running = 1;
  66. output_index = 1;
  67. maximum_window = [] ;
  68. window_size = 1;
  69. maximum_point = zeros(1,data_size);
  70. last_maximum_point = -1;
  71. for i = 3 : data_size
  72. %姿态矩阵更新, 其实有没有陀螺仪零片都没多大问题
  73. % w= gyrData - gyrBias
  74. w = gyr_data(i,:)';
  75. %陀螺仪K时刻求模,分析数据时候用
  76. gyr_norm(i) = norm(w);
  77. if(gyr_norm(i-2) < gyr_norm(i-1) && gyr_norm(i-1) > gyr_norm(i) && gyr_norm(i-1) > 1.5)
  78. % zupt(i-1) = 1.0;
  79. if(window_size == 1)
  80. maximum_window(window_size) = i - 1;
  81. window_size = window_size + 1;
  82. end
  83. if(window_size > 1)
  84. if(i - maximum_window(window_size - 1) > 10)
  85. maximum_window(window_size) = i-1;
  86. window_size = window_size + 1;
  87. elseif(gyr_norm(maximum_window(window_size - 1)) < gyr_norm(i-1) )
  88. maximum_window(window_size-1) = i-1;
  89. end
  90. end
  91. if(window_size > 4)
  92. if(gyr_norm(maximum_window(window_size-1)) > (gyr_norm(maximum_window(window_size-2)) + 2)...
  93. && gyr_norm(maximum_window(window_size-1)) > (gyr_norm(maximum_window(window_size-3)) + 2))
  94. maximum_point(i-1) = 1;
  95. last_maximum_point = i-1;
  96. end
  97. end
  98. end
  99. if(i > 10 )
  100. max_mum = max(gyr_norm(i-9:i));
  101. min_mum = min(gyr_norm(i-9:i));
  102. end
  103. if((i > 10 && i - last_maximum_point < 10 && max_mum - min_mum > 4.0 && gyr_norm(i) < 1.5) || ...
  104. (((gyr_norm(i)< 0.6) && (i > 15) && ((max_mum - gyr_norm(i)) < 0.6))))
  105. zupt(i) = 1.0;
  106. end
  107. end
  108. index = find(zupt > 0);
  109. maximum_index = find(maximum_point > 0);
  110. figure;
  111. plot(gyr_norm, 'r')
  112. grid on
  113. % hold on
  114. % plot(index, gyr_norm(index), 'k*')
  115. hold on
  116. plot(maximum_window(:), gyr_norm(maximum_window(:)), 'bo', 'LineWidth',3)