uart.asv 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. pos_data = [pos_data [pos_res(1)/10000;pos_res(2)/10000]];
  88. signal_out = int16(buf(30));
  89. if(signal_out == 2)
  90. disp("MOTION_JUMP")
  91. elseif(signal_out == 3)
  92. disp("MOTION_DOWN")
  93. max(press_data(end-6:end)) - min(press_data(end-6:end))
  94. elseif(signal_out == 4)
  95. disp("MOTION_LEFT")
  96. elseif(signal_out == 5)
  97. disp("MOTION_RIGHT")
  98. end
  99. if(norm(pos_data(1:2,end)) > 0.3)
  100. % pca_filter(pos_data(:,end-9:end));
  101. end
  102. % T = [-0.7949 -0.6067;0.6067 -0.7949];
  103. T = [1 0; 0 1];
  104. pos_data(1:2,end) = T * [pos_res(1)/10000;pos_res(2)/10000];
  105. % adc(1)=double(bitshift(int16(buf(20)),8)+int16(buf(21)));
  106. % adc(2)=double(bitshift(int16(buf(22)),8)+int16(buf(23)));
  107. % signal_out(1)=double(bitshift(int32(buf(24)),24)+bitshift(int32(buf(25)),16)+bitshift(int32(buf(26)),8)+int32(buf(27)));
  108. % signal_out(2)=double(bitshift(int32(buf(28)),24)+bitshift(int32(buf(29)),16)+bitshift(int32(buf(30)),8)+int32(buf(31)));
  109. % signal_out(3)=double(bitshift(int32(buf(32)),24)+bitshift(int32(buf(33)),16)+bitshift(int32(buf(34)),8)+int32(buf(35)));
  110. % norm([acc(1);acc(2);acc(3);]./4096/1.1992*9.88)
  111. % datestr(now,'mmmm dd,yyyy HH:MM:SS.FFF AM')
  112. % 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));
  113. % disp(press(1));
  114. dt = dt + 1;
  115. t=[t dt];
  116. if dt>250
  117. x=x+1;
  118. end
  119. imu_data = [imu_data [gry(1)/16.384*pi/180;gry(2)/16.384*pi/180;gry(3)/16.384*pi/180;press(1);]];
  120. 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_data(1,end)+2;pos_data(2,end)-2;pos_res(3)/10000]];
  121. % set(p(1),'XData',t,'YData',m(1,:))
  122. % set(p(2),'XData',t,'YData',m(2,:))
  123. % set(p(3),'XData',t,'YData',m(3,:))
  124. % set(p(4),'XData',t,'YData',m(4,:))
  125. set(p(1),'XData',t,'YData',m(5,:))
  126. set(p(2),'XData',t,'YData',m(6,:))
  127. % set(p(3),'XData',t,'YData',m(7,:))
  128. % set(p(8),'XData',t,'YData',m(8,:))
  129. % set(p(9),'XData',t,'YData',m(9,:))
  130. % set(p(10),'XData',t,'YData',m(10,:))
  131. end
  132. buf = buf(Len:end);
  133. else
  134. buf(1) = [];
  135. end
  136. state = 0;
  137. end
  138. otherwise
  139. state = 0;
  140. end
  141. end
  142. drawnow
  143. axis([x x+300 -10 10]);
  144. end
  145. fclose(object);
  146. delete(object);
  147. clear object;