uart.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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]
  8. p = plot(t,m)
  9. data = [];
  10. buf = [];
  11. x=0;
  12. legend('x','y','z','压力')
  13. grid on;
  14. set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.7]);
  15. delete(instrfindall('Type','serial'));%清理串口
  16. object = serial('com3','BaudRate',115200);%配置串口
  17. fopen(object);
  18. state = 0;
  19. Len = 0;
  20. Lenf = 0;
  21. Zarr = [];
  22. Zarr(1) = 0;
  23. Zarr_en = 0;
  24. run = 0;
  25. left = 0;
  26. right = 0;
  27. acc3_b = 0;
  28. acc3_bb = 0;
  29. press_data = [];
  30. press_var = [];
  31. gyr_var = [];
  32. pos_data = [];
  33. imu_data = [];
  34. % for i=1:shuliang
  35. while true
  36. data=fread(object);%读取数据
  37. while ~isempty(data)
  38. buf = [buf data(1)];
  39. data(1) = [];
  40. switch state
  41. case 0
  42. if length(buf)>=5
  43. if buf(1)==170 && buf(2)==187 && buf(3)==204
  44. Len = buf(4);
  45. Lenf = 255 - buf(5);
  46. if Len==Lenf
  47. state = 1;
  48. else
  49. buf(1) = [];
  50. end
  51. else
  52. buf(1) = [];
  53. end
  54. end
  55. case 1
  56. if length(buf)>=Len
  57. ver = 0;
  58. for i=1:(Len-1)
  59. ver = ver + buf(i);
  60. end
  61. ver = mod(ver,256);
  62. % disp(ver);
  63. % disp(buf(Len));
  64. if ver==buf(Len)
  65. if buf(6)==0 && buf(7)==0
  66. % acc(1)=double(bitshift(int32(buf(8)),24)+bitshift(int32(buf(9)),16)+bitshift(int32(buf(10)),8)+int32(buf(11)));
  67. % acc(2)=double(bitshift(int32(buf(12)),24)+bitshift(int32(buf(13)),16)+bitshift(int32(buf(14)),8)+int32(buf(15)));
  68. % acc(3)=double(bitshift(int32(buf(16)),24)+bitshift(int32(buf(17)),16)+bitshift(int32(buf(18)),8)+int32(buf(19)));
  69. % acc(1)=double(bitshift(int16(buf(8)),8)+int16(buf(9)));
  70. % acc(2)=double(bitshift(int16(buf(10)),8)+int16(buf(11)));
  71. % acc(3)=double(bitshift(int16(buf(12)),8)+int16(buf(13)));
  72. % disp(acc);
  73. gry(1)=double(bitshift(int16(buf(8)),8)+int16(buf(9)));
  74. gry(2)=double(bitshift(int16(buf(10)),8)+int16(buf(11)));
  75. gry(3)=double(bitshift(int16(buf(12)),8)+int16(buf(13)));
  76. % gyr_norm = [gyr_norm norm([gry(1),gry(2),gry(3)])./16.384*pi/180];
  77. acc(1)=double(bitshift(int16(buf(14)),8)+int16(buf(15)));
  78. acc(2)=double(bitshift(int16(buf(16)),8)+int16(buf(17)));
  79. acc(3)=double(bitshift(int16(buf(18)),8)+int16(buf(19)));
  80. % disp(gry);
  81. press(1)=double(bitshift(int32(buf(20)),24)+bitshift(int32(buf(21)),16)+bitshift(int32(buf(22)),8)+int32(buf(23)));
  82. press_data = [press_data press(1)];
  83. % disp(press(1));
  84. pos_res(1) = double(bitshift(int16(buf(24)),8)+int16(buf(25)));
  85. pos_res(2) = double(bitshift(int16(buf(26)),8)+int16(buf(27)));
  86. pos_res(3) = double(bitshift(int16(buf(28)),8)+int16(buf(29)));
  87. signal_out = int16(buf(30));
  88. if(signal_out == 2)
  89. disp("MOTION_JUMP")
  90. elseif(signal_out == 3)
  91. disp("MOTION_DOWN")
  92. max(press_data(end-6:end)) - min(press_data(end-6:end))
  93. elseif(signal_out == 4)
  94. disp("MOTION_LEFT")
  95. elseif(signal_out == 5)
  96. disp("MOTION_RIGHT")
  97. end
  98. % if(norm(pos_data(1:2,end)) > 0.3)
  99. % % pca_filter(pos_data(:,end-9:end));
  100. % end
  101. % T = [-0.7949 -0.6067;0.6067 -0.7949];
  102. T = [1 0; 0 1];
  103. temp = T * [pos_res(1);pos_res(2)];
  104. pos_res(1) = temp(1);
  105. pos_res(2) = temp(2);
  106. % adc(1)=double(bitshift(int16(buf(20)),8)+int16(buf(21)));
  107. % adc(2)=double(bitshift(int16(buf(22)),8)+int16(buf(23)));
  108. % signal_out(1)=double(bitshift(int32(buf(24)),24)+bitshift(int32(buf(25)),16)+bitshift(int32(buf(26)),8)+int32(buf(27)));
  109. % signal_out(2)=double(bitshift(int32(buf(28)),24)+bitshift(int32(buf(29)),16)+bitshift(int32(buf(30)),8)+int32(buf(31)));
  110. % signal_out(3)=double(bitshift(int32(buf(32)),24)+bitshift(int32(buf(33)),16)+bitshift(int32(buf(34)),8)+int32(buf(35)));
  111. % norm([acc(1);acc(2);acc(3);]./4096/1.1992*9.88)
  112. % datestr(now,'mmmm dd,yyyy HH:MM:SS.FFF AM')
  113. % pos = footPDR(dt, [gry(1);gry(2);gry(3)]./16.384*pi/180, [acc(1);acc(2);acc(3);]./4096/1.1992*9.7833, press(1));
  114. % disp(press(1));
  115. dt = dt + 1;
  116. t=[t dt];
  117. if dt>250
  118. x=x+1;
  119. end
  120. imu_data = [imu_data [gry(1)/16.384*pi/180;gry(2)/16.384*pi/180;gry(3)/16.384*pi/180;press(1);]];
  121. m=[m [gry(1)/16.384*pi/180;gry(2)/16.384*pi/180;gry(3)/16.384*pi/180;(press(1)-9000000)*0.0000005;pos_res(1)/100;pos_res(2)/100;pos_res(3)/100]];
  122. set(p(1),'XData',t,'YData',m(1,:))
  123. set(p(2),'XData',t,'YData',m(2,:))
  124. set(p(3),'XData',t,'YData',m(3,:))
  125. % set(p(4),'XData',t,'YData',m(4,:))
  126. % set(p(1),'XData',t,'YData',m(5,:))
  127. % set(p(2),'XData',t,'YData',m(6,:))
  128. % set(p(3),'XData',t,'YData',m(7,:))
  129. % set(p(8),'XData',t,'YData',m(8,:))
  130. % set(p(9),'XData',t,'YData',m(9,:))
  131. % set(p(10),'XData',t,'YData',m(10,:))
  132. end
  133. buf = buf(Len:end);
  134. else
  135. buf(1) = [];
  136. end
  137. state = 0;
  138. end
  139. otherwise
  140. state = 0;
  141. end
  142. end
  143. drawnow
  144. axis([x x+300 -2 2]);
  145. end
  146. fclose(object);
  147. delete(object);
  148. clear object;