read_dual_acc_gyr_mag_RawData.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. clc
  2. close all;
  3. clear all;
  4. dt = 1;
  5. t=[0];
  6. % m=[0;0;0;0]
  7. m=[0;0;0;0;0;0;0;0;]
  8. p = plot(t,m)
  9. data = [];
  10. buf = [];
  11. x=0;
  12. %legend('左脚气压','右脚气压','r2','r3','b0','b1','b2','b3')
  13. legend('左脚气压','右脚气压')
  14. grid on;
  15. set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.7]);
  16. delete(instrfindall('Type','serial'));%清理串口
  17. object = serial('com3','BaudRate',115200);%配置串口
  18. fopen(object);
  19. state = 0;
  20. Len = 0;
  21. Lenf = 0;
  22. Zarr = [];
  23. Zarr(1) = 0;
  24. Zarr_en = 0;
  25. run = 0;
  26. left = 0;
  27. right = 0;
  28. acc3_b = 0;
  29. acc3_bb = 0;
  30. press_data = [];
  31. press_var = [];
  32. gyr_var = [];
  33. pos_data = [];
  34. imu_data = [];
  35. is_down = 0;
  36. is_up = 0;
  37. is_left = 0;
  38. is_right = 0;
  39. count = 0;
  40. mag_last = [0;0;0];
  41. transfer =[0 -1 0; -1 0 0; 0 0 1];
  42. press = zeros(1,18);
  43. gyr_data = zeros(1, 3);
  44. mag_data = zeros(1, 3);
  45. rotate_matrix = eye(3);
  46. left_acc = [0,0,0];
  47. left_gyr = [0,0,0];
  48. left_front_mag = [0,0,0];
  49. left_back_mag = [0,0,0];
  50. right_acc = [0,0,0];
  51. right_gyr = [0,0,0];
  52. right_front_mag = [0,0,0];
  53. right_back_mag = [0,0,0];
  54. down = 0;
  55. jump = 0;
  56. s_rssi = 0;
  57. rssi_buff =[];
  58. last_rssi = 0;
  59. press_buff = [];
  60. gyr_buff = [];
  61. h_acc_buff = [];
  62. h_acc_norm_buff = [];
  63. s_acc_buff = [];
  64. h_mag_buff = [];
  65. s_mag_buff = [];
  66. time_stamp = 0;
  67. relate_val = 0;
  68. buff_record = [];
  69. mag_norm = 0;
  70. h_mag_norm_buff = [];
  71. s_mag_norm_buff = [];
  72. % for i=1:shuliang
  73. while true
  74. data=fread(object);%读取数据
  75. while ~isempty(data)
  76. buf = [buf data(1)];
  77. data(1) = [];
  78. switch state
  79. case 0
  80. if length(buf)>=5
  81. if buf(1)==170
  82. Len = buf(2);
  83. Lenf = 255 - buf(2);
  84. if buf(3) == Lenf
  85. state = 1;
  86. else
  87. buf(1) = [];
  88. end
  89. else
  90. buf(1) = [];
  91. end
  92. end
  93. case 1
  94. if length(buf)>=Len
  95. ver = 0;
  96. for i=1:(Len-1)
  97. ver = ver + buf(i);
  98. end
  99. ver = mod(ver,256);
  100. % disp(ver);
  101. % disp(buf(Len));
  102. if ver==buf(Len) && buf(4) == 4
  103. left_acc(1) = double(bitshift(int16(buf(5)),8)+bitshift(int16(buf(6)),0)) ;
  104. left_acc(2) = double(bitshift(int16(buf(7)),8)+bitshift(int16(buf(8)),0)) ;
  105. left_acc(3) = double(bitshift(int16(buf(9)),8)+bitshift(int16(buf(10)),0)) ;
  106. left_gyr(1) = double(bitshift(int16(buf(11)),8)+bitshift(int16(buf(12)),0)) ;
  107. left_gyr(2) = double(bitshift(int16(buf(13)),8)+bitshift(int16(buf(14)),0)) ;
  108. left_gyr(3) = double(bitshift(int16(buf(15)),8)+bitshift(int16(buf(16)),0)) ;
  109. left_front_mag(1) = double(bitshift(int16(buf(17)),8)+bitshift(int16(buf(18)),0));
  110. left_front_mag(2) = double(bitshift(int16(buf(19)),8)+bitshift(int16(buf(20)),0));
  111. left_front_mag(3) = double(bitshift(int16(buf(21)),8)+bitshift(int16(buf(22)),0));
  112. left_front_mag_norm = norm(left_front_mag);
  113. left_back_mag(1) = double(bitshift(int16(buf(23)),8)+bitshift(int16(buf(24)),0))
  114. left_back_mag(2) = double(bitshift(int16(buf(25)),8)+bitshift(int16(buf(26)),0))
  115. left_back_mag(3) = double(bitshift(int16(buf(27)),8)+bitshift(int16(buf(28)),0))
  116. left_back_mag_norm = norm(left_back_mag);
  117. right_acc(1) = double(bitshift(int16(buf(29)),8)+bitshift(int16(buf(30)),0)) ;
  118. right_acc(2) = double(bitshift(int16(buf(31)),8)+bitshift(int16(buf(32)),0)) ;
  119. right_acc(3) = double(bitshift(int16(buf(33)),8)+bitshift(int16(buf(34)),0)) ;
  120. right_gyr(1) = double(bitshift(int16(buf(35)),8)+bitshift(int16(buf(36)),0)) ;
  121. right_gyr(2) = double(bitshift(int16(buf(37)),8)+bitshift(int16(buf(38)),0)) ;
  122. right_gyr(3) = double(bitshift(int16(buf(39)),8)+bitshift(int16(buf(40)),0)) ;
  123. right_front_mag(1) = double(bitshift(int16(buf(41)),8)+bitshift(int16(buf(42)),0));
  124. right_front_mag(2) = double(bitshift(int16(buf(43)),8)+bitshift(int16(buf(44)),0));
  125. right_front_mag(3) = double(bitshift(int16(buf(45)),8)+bitshift(int16(buf(46)),0));
  126. right_front_mag_norm = norm(right_front_mag);
  127. right_back_mag(1) = double(bitshift(int16(buf(47)),8)+bitshift(int16(buf(48)),0));
  128. right_back_mag(2) = double(bitshift(int16(buf(49)),8)+bitshift(int16(buf(50)),0));
  129. right_back_mag(3) = double(bitshift(int16(buf(51)),8)+bitshift(int16(buf(52)),0));
  130. right_back_mag_norm = norm(right_back_mag);
  131. buff_record = [buff_record;buf(1:Len)];
  132. dt = dt + 1;
  133. t=[t dt];
  134. if dt>250
  135. x=x+1;
  136. end
  137. % if((size(h_acc_norm_buff,2) > 9) && (max(h_acc_norm_buff(end-9:end)) - 1 < 0.1) && (min(h_acc_norm_buff(end-9:end)) - 1 > -0.1))
  138. % acc_zupt = 1;
  139. % else
  140. % acc_zupt = 0;
  141. % end
  142. % %测左右鞋地磁
  143. % m=[m, [ left_front_mag_norm;left_back_mag_norm; right_front_mag_norm ; right_back_mag_norm; 0; 0; 0; 0; ]];
  144. % set(p(1),'XData',t,'YData',m(1,:))
  145. % set(p(2),'XData',t,'YData',m(2,:))
  146. % set(p(3),'XData',t,'YData',m(3,:))
  147. % set(p(4),'XData',t,'YData',m(4,:))
  148. %
  149. % %测左右鞋地磁
  150. % m=[m, [ left_front_mag_norm;left_back_mag_norm; right_front_mag_norm ; right_back_mag_norm; 0; 0; 0; 0; ]];
  151. % set(p(1),'XData',t,'YData',m(1,:))
  152. % set(p(2),'XData',t,'YData',m(2,:))
  153. % set(p(3),'XData',t,'YData',m(3,:))
  154. % set(p(4),'XData',t,'YData',m(4,:))
  155. % %测左鞋前后地磁原始数据
  156. % m=[m, [ left_front_mag(1);left_front_mag(2); left_front_mag(3) ; left_back_mag(1); left_back_mag(2); left_back_mag(3); 0; 0; ]];
  157. % set(p(1),'XData',t,'YData',m(1,:),'Color','red')
  158. % set(p(2),'XData',t,'YData',m(2,:),'Color','green')
  159. % set(p(3),'XData',t,'YData',m(3,:),'Color','blue')
  160. % set(p(4),'XData',t,'YData',m(4,:),'Color','yellow')
  161. % set(p(5),'XData',t,'YData',m(5,:),'Color','black')
  162. % set(p(6),'XData',t,'YData',m(6,:),'Color','cyan')
  163. % %测右鞋前后地磁原始数据
  164. % m=[m, [ right_front_mag(1);right_front_mag(2); right_front_mag(3) ; right_back_mag(1); right_back_mag(2); right_back_mag(3); 0; 0; ]];
  165. % set(p(1),'XData',t,'YData',m(1,:))
  166. % set(p(2),'XData',t,'YData',m(2,:))
  167. % set(p(3),'XData',t,'YData',m(3,:))
  168. % set(p(4),'XData',t,'YData',m(4,:))
  169. % set(p(5),'XData',t,'YData',m(5,:))
  170. % set(p(6),'XData',t,'YData',m(6,:))
  171. %测左鞋六轴
  172. m=[m, [ left_acc(1);left_acc(2); left_acc(3) ; left_gyr(1); left_gyr(2); left_gyr(3); 0; 0; ]];
  173. set(p(1),'XData',t,'YData',m(1,:),'Color','red')
  174. set(p(2),'XData',t,'YData',m(2,:),'Color','green')
  175. set(p(3),'XData',t,'YData',m(3,:),'Color','blue')
  176. set(p(4),'XData',t,'YData',m(4,:),'Color','black')
  177. set(p(5),'XData',t,'YData',m(5,:),'Color','black')
  178. set(p(6),'XData',t,'YData',m(6,:),'Color','cyan')
  179. % %测右鞋六轴
  180. % m=[m, [ right_acc(1);right_acc(2); right_acc(3) ; right_gyr(1); right_gyr(2); right_gyr(3); 0; 0; ]];
  181. % set(p(1),'XData',t,'YData',m(1,:))
  182. % set(p(2),'XData',t,'YData',m(2,:))
  183. % set(p(3),'XData',t,'YData',m(3,:))
  184. % set(p(4),'XData',t,'YData',m(4,:))
  185. % set(p(5),'XData',t,'YData',m(5,:))
  186. % set(p(6),'XData',t,'YData',m(6,:))
  187. % set(p(6),'XData',t,'YData', m(6,:))
  188. % set(p(7),'XData',t,'YData',m(7,:))
  189. % set(p(8),'XData',t,'YData',m(8,:))
  190. % set(p(3),'XData',t,'YData',m(7,:))
  191. % set(p(8),'XData',t,'YData',m(8,:))
  192. % set(p(9),'XData',t,'YData',m(9,:))
  193. % set(p(10),'XData',t,'YData',m(10,:))
  194. buf = buf(Len:end);
  195. else
  196. buf(1) = [];
  197. end
  198. state = 0;
  199. end
  200. otherwise
  201. state = 0;
  202. end
  203. end
  204. drawnow limitrate nocallbacks
  205. % axis([x x+300 -0.1 0.1]);
  206. axis([x x+300 -1000 1000]);
  207. end
  208. fclose(object);
  209. delete(object);
  210. clear object;