uart.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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;]
  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('com4','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. is_down = 0;
  35. % for i=1:shuliang
  36. while true
  37. data=fread(object);%读取数据
  38. while ~isempty(data)
  39. buf = [buf data(1)];
  40. data(1) = [];
  41. switch state
  42. case 0
  43. if length(buf)>=5
  44. if buf(1)==170 && buf(2)==187 && buf(3)==204
  45. Len = buf(4);
  46. Lenf = 255 - buf(5);
  47. if Len==Lenf
  48. state = 1;
  49. else
  50. buf(1) = [];
  51. end
  52. else
  53. buf(1) = [];
  54. end
  55. end
  56. case 1
  57. if length(buf)>=Len
  58. ver = 0;
  59. for i=1:(Len-1)
  60. ver = ver + buf(i);
  61. end
  62. ver = mod(ver,256);
  63. % disp(ver);
  64. % disp(buf(Len));
  65. if ver==buf(Len)
  66. if buf(6)==0 && buf(7)==1
  67. end
  68. if buf(6)==1 && buf(7)==1
  69. % clc
  70. % disp(buf);
  71. if buf(8)==0
  72. disp('MOTION_STOP');
  73. elseif buf(8)==1
  74. disp('MOTION_RUN');
  75. elseif buf(8)==2
  76. disp('MOTION_JUMP');
  77. elseif buf(8)==3
  78. disp('MOTION_DOWN');
  79. is_down = 1;
  80. elseif buf(8)==4
  81. disp('MOTION_LEFT');
  82. elseif buf(8)==5
  83. disp('MOTION_RIGHT');
  84. elseif buf(8)==6
  85. disp('MOTION_FRONT');
  86. elseif buf(8)==7
  87. disp('MOTION_BACK');
  88. end
  89. disp(double(bitshift(int16(buf(9)),8)+int16(buf(10))));
  90. end
  91. if buf(6)==1 && buf(7)==0
  92. press(1)=double(bitshift(int32(buf(8)),24)+bitshift(int32(buf(9)),16)+bitshift(int32(buf(10)),8)+int32(buf(11)));
  93. press(2)=double(bitshift(int32(buf(12)),24)+bitshift(int32(buf(13)),16)+bitshift(int32(buf(14)),8)+int32(buf(15)));
  94. time = double(bitshift(int16(buf(16)),8)+int16(buf(17)));
  95. disp([press time])
  96. dt = dt + 1;
  97. t=[t dt];
  98. if dt>250
  99. x=x+1;
  100. end
  101. imu_data = [imu_data [press(1);press(2);time]];
  102. m=[m [(press(1)- 9550000)/10e4;(press(2)-9550000)/10e4;time]];
  103. set(p(1),'XData',t,'YData',m(1,:))
  104. set(p(2),'XData',t,'YData',m(2,:))
  105. % set(p(3),'XData',t,'YData',m(3,:))
  106. % set(p(4),'XData',t,'YData',m(4,:))
  107. % set(p(5),'XData',t,'YData', m(5,:))
  108. % is_down = 0;
  109. % set(p(1),'XData',t,'YData',m(5,:))
  110. % set(p(2),'XData',t,'YData',m(6,:))
  111. % set(p(3),'XData',t,'YData',m(7,:))
  112. % set(p(8),'XData',t,'YData',m(8,:))
  113. % set(p(9),'XData',t,'YData',m(9,:))
  114. % set(p(10),'XData',t,'YData',m(10,:))
  115. end
  116. buf = buf(Len:end);
  117. else
  118. buf(1) = [];
  119. end
  120. state = 0;
  121. end
  122. otherwise
  123. state = 0;
  124. end
  125. end
  126. drawnow limitrate nocallbacks
  127. axis([x x+300 0 10]);
  128. end
  129. fclose(object);
  130. delete(object);
  131. clear object;