savedata_phlex.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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;0;]
  8. p = plot(t,m)
  9. data = [];
  10. buf = [];
  11. x=0;
  12. %legend('左脚气压','右脚气压','r2','r3','b0','b1','b2','b3')
  13. legend('phlex_0','phlex_1')
  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. right_pos_data = [0,0,0];
  47. right_att_data = [0,0,0];
  48. left_pos_data = [0,0,0];
  49. left_att_data = [0,0,0];
  50. down = 0;
  51. jump = 0;
  52. s_rssi = 0;
  53. rssi_buff =[];
  54. last_rssi = 0;
  55. press_buff = [];
  56. gyr_buff = [];
  57. acc_buff = [];
  58. time_stamp = 0;
  59. relate_val = 0;
  60. tempbu =0;
  61. press_max=0;
  62. press_min =0;
  63. pressFlag =0;
  64. press_base_noone =0;
  65. press_lvbo =0;
  66. mag_x =0;
  67. mag_y =0;
  68. mag_z =0;
  69. phlex_0 = 0;
  70. phlex_1 = 0;
  71. % for i=1:shuliang
  72. while true
  73. data=fread(object,30);%读取数据
  74. while ~isempty(data)
  75. buf = [buf data(1)];
  76. data(1) = [];
  77. switch state
  78. case 0
  79. if length(buf)>=5
  80. if buf(1)==170
  81. Len = buf(2);
  82. Lenf = 255 - buf(2);
  83. if buf(3) == Lenf
  84. state = 1;
  85. else
  86. buf(1) = [];
  87. end
  88. else
  89. buf(1) = [];
  90. end
  91. end
  92. case 1
  93. if length(buf)>=Len
  94. ver = 0;
  95. for i=1:(Len-1)
  96. ver = ver + buf(i);
  97. end
  98. ver = mod(ver,256);
  99. % disp(ver);
  100. % disp(buf(Len));
  101. if ver==buf(Len)
  102. if buf(4) == 136
  103. % tempbu = double(bitshift(uint32(buf(6)),24)+bitshift(uint32(buf(7)),16)+bitshift(uint32(buf(8)),8)+bitshift(uint32(buf(9)),0));
  104. % press_max = double(bitshift(uint16(buf(10)),8)+bitshift(uint16(buf(11)),0));
  105. % press_min = double(bitshift(uint16(buf(12)),8)+bitshift(uint16(buf(13)),0));
  106. % press_base = double(bitshift(uint16(buf(14)),8)+bitshift(uint16(buf(15)),0));
  107. % press = double(bitshift(uint16(buf(16)),8)+bitshift(uint16(buf(17)),0));
  108. % press_base_noone = double(bitshift(uint16(buf(18)),8)+bitshift(uint16(buf(19)),0));
  109. % pressFlag = double(bitshift(uint16(buf(20)),8)+bitshift(uint16(buf(21)),0));
  110. % press_lvbo = double(bitshift(uint16(buf(22)),8)+bitshift(uint16(buf(23)),0));
  111. %
  112. % mag_x = double(bitshift(int16(buf(24)),8)+bitshift(int16(buf(25)),0));
  113. % mag_y = double(bitshift(int16(buf(26)),8)+bitshift(int16(buf(27)),0));
  114. % mag_z = double(bitshift(int16(buf(28)),8)+bitshift(int16(buf(29)),0));
  115. %
  116. % if mag_x <0
  117. % mag_x = -(32768+mag_x);
  118. % end
  119. % if mag_y <0
  120. % mag_y = -(32768+mag_y);
  121. % end
  122. % if mag_z <0
  123. % mag_z = -(32768+mag_z);
  124. % end
  125. %
  126. % if pressFlag ==0
  127. % pressFlag=0;
  128. % else
  129. % pressFlag =1000;
  130. % end
  131. % disPress(1) = mag_x;
  132. % disPress(2) = mag_y;
  133. % disPress(3) = mag_z;
  134. % disp(disPress);
  135. phlex_0 = double(bitshift(uint32(buf(5)),8)+bitshift(uint32(buf(6)),0));
  136. phlex_1 = double(bitshift(uint32(buf(7)),8)+bitshift(uint32(buf(8)),0));
  137. disPress(1) = phlex_0;
  138. disPress(2) = phlex_1;
  139. disp(disPress);
  140. dt = dt + 1;
  141. t=[t dt];
  142. if dt>250
  143. x=x+1;
  144. end
  145. % if press_base == 65535
  146. % press_base = 100;
  147. % end
  148. % press_lvbo =press_lvbo -3000;
  149. % m=[m [ press_max; press_min; press_base; press; press_base_noone;pressFlag; mag_x; mag_y; mag_z;]];
  150. % m=[m [ mag_x; mag_y; mag_z;0 ;0 ;0; 0; 0; 0;]];
  151. m=[m [ phlex_0; phlex_1; 0;0 ;0 ;0; 0; 0; 0;]];
  152. % else
  153. % tempbu = double(bitshift(uint16(buf(6)),24)+bitshift(uint16(buf(7)),16)+bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  154. % press_max = double(bitshift(uint16(buf(10)),8)+bitshift(uint16(buf(11)),0));
  155. % press_in = double(bitshift(uint16(buf(12)),8)+bitshift(uint16(buf(13)),0));
  156. % X = sprintf('press_max:%d,press_min:%d,step:%d',press_max,press_min,tempbu);
  157. %
  158. end
  159. set(p(1),'XData',t,'YData',m(1,:))
  160. set(p(2),'XData',t,'YData',m(2,:))
  161. % set(p(3),'XData',t,'YData',m(3,:))
  162. % set(p(4),'XData',t,'YData',m(4,:))
  163. % set(p(5),'XData',t,'YData',m(5,:))
  164. % set(p(6),'XData',t,'YData',m(6,:))
  165. %
  166. % set(p(7),'XData',t,'YData',m(7,:))
  167. % set(p(8),'XData',t,'YData',m(8,:))
  168. % set(p(9),'XData',t,'YData',m(9,:))
  169. buf = buf(Len:end);
  170. else
  171. buf(1) = [];
  172. end
  173. state = 0;
  174. end
  175. otherwise
  176. state = 0;
  177. end
  178. end
  179. drawnow limitrate nocallbacks
  180. axis([x x+300 500 2000]);
  181. end
  182. fclose(object);
  183. delete(object);
  184. clear object;