savedata_uart_press.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. clc
  2. close all;
  3. clear all;
  4. dt = 1;
  5. t=[0];
  6. % m=[0;0;0;0]
  7. dis_m=[0;0;0;0;0;0]
  8. p = plot(t,dis_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. transfer =[0 -1 0; -1 0 0; 0 0 1];
  21. press_1 = zeros(1,18);
  22. press_2 = zeros(1,18);
  23. time_stamp = 0;
  24. time_stamp_last = 0;
  25. % for i=1:shuliang
  26. m = [0;0;]
  27. %算法变量
  28. F_val_now_back = 0;
  29. B_val_now_back = 0;
  30. F_val_now = 0;
  31. B_val_now = 0;
  32. F_rise_start = 0;
  33. F_val_old = 0;
  34. F_flag_step = 0;
  35. F_filter_val = 0;
  36. F_step_time_1 = 0;
  37. F_step_time_2 = 0;
  38. F_step_time_min = 0;
  39. F_step_time_max = 0;
  40. F_step_time_cnt = 0;
  41. F_step_zero = 0;
  42. while true
  43. data=fread(object,15);%读取数据
  44. while ~isempty(data)
  45. buf = [buf data(1)];
  46. data(1) = [];
  47. switch state
  48. case 0
  49. if length(buf)>=5
  50. if buf(1)==170
  51. Len = buf(2);
  52. Lenf = 255 - buf(2);
  53. if buf(3) == Lenf
  54. state = 1;
  55. else
  56. buf(1) = [];
  57. end
  58. else
  59. buf(1) = [];
  60. end
  61. end
  62. case 1
  63. if length(buf)>=Len
  64. ver = 0;
  65. for i=1:(Len-1)
  66. ver = ver + buf(i);
  67. end
  68. ver = mod(ver,256);
  69. % disp(ver);
  70. % disp(buf(Len));
  71. if ver==buf(Len)
  72. if buf(4) == 165
  73. % press_2 = double(bitshift(uint16(buf(6)),8)+bitshift(uint16(buf(7)),0));
  74. % press_1 = double(bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  75. F_val_now = 4096-double(bitshift(uint16(buf(6)),8)+bitshift(uint16(buf(7)),0));
  76. B_val_now = 4096-double(bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  77. F_val_now_back = 4096-double(bitshift(uint16(buf(10)),8)+bitshift(uint16(buf(11)),0));
  78. B_val_now_back = 4096-double(bitshift(uint16(buf(12)),8)+bitshift(uint16(buf(13)),0));
  79. F_val_sum = F_val_now + B_val_now;
  80. if F_val_sum>2080
  81. F_step_zero = 3000
  82. else
  83. F_step_zero = 1500
  84. end
  85. % F_val_now = (F_val_now+B_val_now);
  86. m = [m [F_val_now;B_val_now]];
  87. % if F_flag_step>0
  88. % F_flag_step = 0;
  89. % end
  90. %算法
  91. % F_filter_val = bitshift((bitshift(uint32(F_filter_val),1)-F_filter_val+F_val_now),-1);
  92. % if abs(F_filter_val-F_val_now)>200
  93. % F_filter_val = F_val_now;
  94. % end
  95. % F_filter_val = (F_filter_val*3-F_filter_val+F_val_now)/3;
  96. % disp(F_val_now);
  97. % disp(F_filter_val);
  98. % F_step_time_cnt = F_step_time_cnt + 1;
  99. %
  100. % if F_filter_val>=F_val_old
  101. % if F_rise_start==0
  102. % F_rise_start = F_val_old;
  103. % end
  104. % else
  105. % if F_rise_start>0
  106. % if F_val_old-F_rise_start>500
  107. % F_flag_step = 1000;
  108. % F_step_time_1 = F_step_time_2;
  109. % F_step_time_2 = F_step_time_cnt;
  110. % F_step_time_cnt = 0;
  111. % elseif F_val_old-F_rise_start>200
  112. % tempV = [F_step_time_1,F_step_time_2];
  113. % F_step_time_min = min(tempV);
  114. % F_step_time_max = max(tempV);
  115. % if F_step_time_max-F_step_time_min<3 && abs((F_step_time_max+F_step_time_min)/2-F_step_time_cnt)<5
  116. % F_flag_step = 500;
  117. % else
  118. % F_flag_step = 100;
  119. % end
  120. % F_step_time_1 = F_step_time_2;
  121. % F_step_time_2 = F_step_time_cnt;
  122. % F_step_time_cnt = 0;
  123. % end
  124. % F_rise_start = 0;
  125. % end
  126. % end
  127. % F_val_old = F_filter_val;
  128. %
  129. % time_stamp = bitshift(uint16(buf(10)),0);
  130. % % if time_stamp ~= (time_stamp_last+1)
  131. % % disp(['data loss:',num2str(time_stamp)]);
  132. % % end
  133. % time_stamp_last = time_stamp;
  134. % disPress(1) = press_1;
  135. % disPress(2) = press_2;
  136. % disPress(1) = time_stamp;
  137. % disp(disPress);
  138. dt = dt + 1;
  139. t=[t dt];
  140. if dt>90
  141. x=x+1;
  142. end
  143. dis_m =[dis_m [ F_val_now; B_val_now; F_val_sum;F_step_zero;F_val_now_back;B_val_now_back]];
  144. % m=[m [ mag_x; mag_y; mag_z;press ;0 ;0; 0; 0; 0;]];
  145. % else
  146. % tempbu = double(bitshift(uint16(buf(6)),24)+bitshift(uint16(buf(7)),16)+bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  147. % press_max = double(bitshift(uint16(buf(10)),8)+bitshift(uint16(buf(11)),0));
  148. % press_in = double(bitshift(uint16(buf(12)),8)+bitshift(uint16(buf(13)),0));
  149. % X = sprintf('press_max:%d,press_min:%d,step:%d',press_max,press_min,tempbu);
  150. %
  151. end
  152. set(p(1),'XData',t,'YData',dis_m(1,:))
  153. set(p(2),'XData',t,'YData',dis_m(2,:))
  154. set(p(3),'XData',t,'YData',dis_m(3,:))
  155. set(p(4),'XData',t,'YData',dis_m(4,:))
  156. % set(p(5),'XData',t,'YData',dis_m(5,:))
  157. % set(p(6),'XData',t,'YData',dis_m(6,:))
  158. buf = buf(Len:end);
  159. else
  160. buf(1) = [];
  161. end
  162. state = 0;
  163. end
  164. otherwise
  165. state = 0;
  166. end
  167. end
  168. drawnow limitrate nocallbacks
  169. axis([x x+100 800 4000]);
  170. end
  171. fclose(object);
  172. delete(object);
  173. clear object;