savedata_uart.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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;]
  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. %炬랬긴좆
  27. F_val_now = 0;
  28. B_val_now = 0;
  29. F_rise_start = 0;
  30. F_val_old = 0;
  31. F_flag_step = 0;
  32. F_filter_val = 0;
  33. m = [0;0;]
  34. while true
  35. data=fread(object,30);%뗍혤鑒앴
  36. while ~isempty(data)
  37. buf = [buf data(1)];
  38. data(1) = [];
  39. switch state
  40. case 0
  41. if length(buf)>=5
  42. if buf(1)==170
  43. Len = buf(2);
  44. Lenf = 255 - buf(2);
  45. if buf(3) == Lenf
  46. state = 1;
  47. else
  48. buf(1) = [];
  49. end
  50. else
  51. buf(1) = [];
  52. end
  53. end
  54. case 1
  55. if length(buf)>=Len
  56. ver = 0;
  57. for i=1:(Len-1)
  58. ver = ver + buf(i);
  59. end
  60. ver = mod(ver,256);
  61. % disp(ver);
  62. % disp(buf(Len));
  63. if ver==buf(Len)
  64. if buf(4) == 165
  65. % press_2 = double(bitshift(uint16(buf(6)),8)+bitshift(uint16(buf(7)),0));
  66. % press_1 = double(bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  67. B_val_now = double(bitshift(uint16(buf(6)),8)+bitshift(uint16(buf(7)),0));
  68. F_val_now = double(bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  69. m = [m [F_val_now;B_val_now]];
  70. if F_flag_step>0
  71. F_flag_step = 0;
  72. end
  73. %炬랬
  74. F_filter_val = bitshift((bitshift(uint32(F_filter_val),1)-F_filter_val+F_val_now),-1);
  75. % F_filter_val = (F_filter_val*3-F_filter_val+F_val_now)/3;
  76. disp(F_val_now);
  77. disp(F_filter_val);
  78. if F_filter_val>=F_val_old
  79. if F_rise_start==0
  80. F_rise_start = F_val_old;
  81. end
  82. else
  83. if F_rise_start>0
  84. if F_val_old-F_rise_start>500
  85. F_flag_step = F_rise_start;
  86. end
  87. F_rise_start = 0;
  88. end
  89. end
  90. F_val_old = F_filter_val;
  91. time_stamp = bitshift(uint16(buf(10)),0);
  92. if time_stamp ~= (time_stamp_last+1)
  93. disp(['data loss:',num2str(time_stamp)]);
  94. end
  95. time_stamp_last = time_stamp;
  96. % disPress(1) = press_1;
  97. % disPress(2) = press_2;
  98. % disPress(1) = time_stamp;
  99. % disp(disPress);
  100. dt = dt + 1;
  101. t=[t dt];
  102. if dt>250
  103. x=x+1;
  104. end
  105. dis_m =[dis_m [ F_val_now; F_filter_val; F_flag_step]];
  106. % m=[m [ mag_x; mag_y; mag_z;press ;0 ;0; 0; 0; 0;]];
  107. % else
  108. % tempbu = double(bitshift(uint16(buf(6)),24)+bitshift(uint16(buf(7)),16)+bitshift(uint16(buf(8)),8)+bitshift(uint16(buf(9)),0));
  109. % press_max = double(bitshift(uint16(buf(10)),8)+bitshift(uint16(buf(11)),0));
  110. % press_in = double(bitshift(uint16(buf(12)),8)+bitshift(uint16(buf(13)),0));
  111. % X = sprintf('press_max:%d,press_min:%d,step:%d',press_max,press_min,tempbu);
  112. %
  113. end
  114. set(p(1),'XData',t,'YData',dis_m(1,:))
  115. set(p(2),'XData',t,'YData',dis_m(2,:))
  116. set(p(3),'XData',t,'YData',dis_m(3,:))
  117. buf = buf(Len:end);
  118. else
  119. buf(1) = [];
  120. end
  121. state = 0;
  122. end
  123. otherwise
  124. state = 0;
  125. end
  126. end
  127. drawnow limitrate nocallbacks
  128. axis([x x+300 -500 6000]);
  129. end
  130. fclose(object);
  131. delete(object);
  132. clear object;