read_dual_acc_gyr_mag_RawData.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 && Len>52
  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_acc_norm = norm(left_acc);
  107. left_gyr(1) = double(bitshift(int16(buf(11)),8)+bitshift(int16(buf(12)),0)) ;
  108. left_gyr(2) = double(bitshift(int16(buf(13)),8)+bitshift(int16(buf(14)),0)) ;
  109. left_gyr(3) = double(bitshift(int16(buf(15)),8)+bitshift(int16(buf(16)),0)) ;
  110. left_gyr_norm = norm(left_gyr);
  111. left_front_mag(1) = double(bitshift(int16(buf(17)),8)+bitshift(int16(buf(18)),0));
  112. left_front_mag(2) = double(bitshift(int16(buf(19)),8)+bitshift(int16(buf(20)),0));
  113. left_front_mag(3) = double(bitshift(int16(buf(21)),8)+bitshift(int16(buf(22)),0));
  114. left_front_mag_norm = norm(left_front_mag);
  115. left_back_mag(1) = double(bitshift(int16(buf(23)),8)+bitshift(int16(buf(24)),0))
  116. left_back_mag(2) = double(bitshift(int16(buf(25)),8)+bitshift(int16(buf(26)),0));
  117. left_back_mag(3) = double(bitshift(int16(buf(27)),8)+bitshift(int16(buf(28)),0));
  118. left_back_mag_norm = norm(left_back_mag);
  119. right_acc(1) = double(bitshift(int16(buf(29)),8)+bitshift(int16(buf(30)),0)) ;
  120. right_acc(2) = double(bitshift(int16(buf(31)),8)+bitshift(int16(buf(32)),0)) ;
  121. right_acc(3) = double(bitshift(int16(buf(33)),8)+bitshift(int16(buf(34)),0)) ;
  122. right_acc_norm = norm(right_acc);
  123. right_gyr(1) = double(bitshift(int16(buf(35)),8)+bitshift(int16(buf(36)),0)) ;
  124. right_gyr(2) = double(bitshift(int16(buf(37)),8)+bitshift(int16(buf(38)),0)) ;
  125. right_gyr(3) = double(bitshift(int16(buf(39)),8)+bitshift(int16(buf(40)),0)) ;
  126. right_gyr_norm = norm(right_gyr);
  127. right_front_mag(1) = double(bitshift(int16(buf(41)),8)+bitshift(int16(buf(42)),0));
  128. right_front_mag(2) = double(bitshift(int16(buf(43)),8)+bitshift(int16(buf(44)),0));
  129. right_front_mag(3) = double(bitshift(int16(buf(45)),8)+bitshift(int16(buf(46)),0));
  130. right_front_mag_norm = norm(right_front_mag);
  131. right_back_mag(1) = double(bitshift(int16(buf(47)),8)+bitshift(int16(buf(48)),0));
  132. right_back_mag(2) = double(bitshift(int16(buf(49)),8)+bitshift(int16(buf(50)),0));
  133. right_back_mag(3) = double(bitshift(int16(buf(51)),8)+bitshift(int16(buf(52)),0));
  134. right_back_mag_norm = norm(right_back_mag);
  135. buff_record = [buff_record;buf(1:Len)];
  136. dt = dt + 1;
  137. t=[t dt];
  138. if dt>250
  139. x=x+1;
  140. end
  141. % 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))
  142. % acc_zupt = 1;
  143. % else
  144. % acc_zupt = 0;
  145. % end
  146. % %测左右鞋地磁
  147. % m=[m, [ left_front_mag_norm;left_back_mag_norm; right_front_mag_norm ; right_back_mag_norm; 0; 0; 0; 0; ]];
  148. % set(p(1),'XData',t,'YData',m(1,:))
  149. % set(p(2),'XData',t,'YData',m(2,:))
  150. % set(p(3),'XData',t,'YData',m(3,:))
  151. % set(p(4),'XData',t,'YData',m(4,:))
  152. %
  153. % %测左右鞋地磁
  154. % m=[m, [ left_front_mag_norm;left_back_mag_norm; right_front_mag_norm ; right_back_mag_norm; 0; 0; 0; 0; ]];
  155. % set(p(1),'XData',t,'YData',m(1,:))
  156. % set(p(2),'XData',t,'YData',m(2,:))
  157. % set(p(3),'XData',t,'YData',m(3,:))
  158. % set(p(4),'XData',t,'YData',m(4,:))
  159. % %测左鞋前后地磁原始数据
  160. % 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; ]];
  161. % set(p(1),'XData',t,'YData',m(1,:),'Color','red')
  162. % set(p(2),'XData',t,'YData',m(2,:),'Color','green')
  163. % set(p(3),'XData',t,'YData',m(3,:),'Color','blue')
  164. % set(p(4),'XData',t,'YData',m(4,:),'Color','yellow')
  165. % set(p(5),'XData',t,'YData',m(5,:),'Color','black')
  166. % set(p(6),'XData',t,'YData',m(6,:),'Color','cyan')
  167. % %测右鞋前后地磁原始数据
  168. % 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; ]];
  169. % set(p(1),'XData',t,'YData',m(1,:))
  170. % set(p(2),'XData',t,'YData',m(2,:))
  171. % set(p(3),'XData',t,'YData',m(3,:))
  172. % set(p(4),'XData',t,'YData',m(4,:))
  173. % set(p(5),'XData',t,'YData',m(5,:))
  174. % set(p(6),'XData',t,'YData',m(6,:))
  175. %测左鞋六轴
  176. m=[m, [ left_acc_norm+4000;left_gyr_norm+3000; left_front_mag_norm+2000 ; left_back_mag_norm+1000;right_acc_norm-1000;right_gyr_norm-2000; right_front_mag_norm-3000 ; right_back_mag_norm-4000; ]];
  177. set(p(1),'XData',t,'YData',m(1,:),'Color','red')
  178. set(p(2),'XData',t,'YData',m(2,:),'Color','green')
  179. set(p(3),'XData',t,'YData',m(3,:),'Color','blue')
  180. set(p(4),'XData',t,'YData',m(4,:),'Color','black')
  181. set(p(5),'XData',t,'YData',m(5,:),'Color','red')
  182. set(p(6),'XData',t,'YData',m(6,:),'Color','green')
  183. set(p(7),'XData',t,'YData',m(7,:),'Color','blue')
  184. set(p(8),'XData',t,'YData',m(8,:),'Color','black')
  185. % m=[m, [ right_back_mag(1);left_back_mag(1); 0 ; 0; 0;0; 0; 0; ]];
  186. % set(p(1),'XData',t,'YData',m(1,:))
  187. % set(p(2),'XData',t,'YData',m(2,:))
  188. % %测右鞋六轴
  189. % m=[m, [ right_acc(1);right_acc(2); right_acc(3) ; right_gyr(1); right_gyr(2); right_gyr(3); 0; 0; ]];
  190. % set(p(1),'XData',t,'YData',m(1,:))
  191. % set(p(2),'XData',t,'YData',m(2,:))
  192. % set(p(3),'XData',t,'YData',m(3,:))
  193. % set(p(4),'XData',t,'YData',m(4,:))
  194. % set(p(5),'XData',t,'YData',m(5,:))
  195. % set(p(6),'XData',t,'YData',m(6,:))
  196. % set(p(6),'XData',t,'YData', m(6,:))
  197. % set(p(7),'XData',t,'YData',m(7,:))
  198. % set(p(8),'XData',t,'YData',m(8,:))
  199. % set(p(3),'XData',t,'YData',m(7,:))
  200. % set(p(8),'XData',t,'YData',m(8,:))
  201. % set(p(9),'XData',t,'YData',m(9,:))
  202. % set(p(10),'XData',t,'YData',m(10,:))
  203. buf = buf(Len:end);
  204. else
  205. buf(1) = [];
  206. end
  207. state = 0;
  208. end
  209. otherwise
  210. state = 0;
  211. end
  212. end
  213. drawnow limitrate nocallbacks
  214. % axis([x x+300 -0.1 0.1]);
  215. axis([x x+300 -10000 10000]);
  216. end
  217. fclose(object);
  218. delete(object);
  219. clear object;