JYReceive.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. clear all;
  2. close all;
  3. instrreset;
  4. disp('Press Ctrl+C to stop collecting data!')
  5. s=serial('/dev/ttyUSB0','baudrate',115200) ;fopen(s) ;
  6. f = 20;%DataFrequce
  7. t=0;
  8. cnt = 1;
  9. aa=[0 0 0];
  10. ww=[0 0 0];
  11. AA = [0 0 0];
  12. tt = 0;
  13. a=[0 0 0]';
  14. w=[0 0 0]';
  15. A=[0 0 0]';
  16. while(1)
  17. Head = fread(s,2,'uint8');
  18. if (Head(1)~=uint8(85))
  19. continue;
  20. end
  21. Head(2)
  22. switch(Head(2))
  23. case 81
  24. a = fread(s,3,'int16')/32768*16 ;
  25. case 82
  26. w = fread(s,3,'int16')/32768*2000 ;
  27. case 83
  28. A = fread(s,3,'int16')/32768*180;
  29. aa=[aa;a'];
  30. ww = [ww;w'];
  31. AA = [AA;A'];
  32. tt = [tt;t];
  33. if (cnt>(2)) %Plot in low frequce,
  34. subplot(3,1,1);plot(tt,aa);title(['Acceleration = ' num2str(a') 'm2/s']);ylabel('m2/s');
  35. subplot(3,1,2);plot(tt,ww);title(['Gyro = ' num2str(w') '°/s']);ylabel('°/s');
  36. subplot(3,1,3);plot(tt,AA);title(['Angle = ' num2str(A') '°']);ylabel('°');
  37. cnt=0;
  38. drawnow;
  39. if (size(aa,1)>5*f)%clear history data
  40. aa = aa(f:5*f,:);
  41. ww = ww(f:5*f,:);
  42. AA = AA(f:5*f,:);
  43. tt = tt(f:5*f,:);
  44. end
  45. end
  46. cnt=cnt+1;
  47. t=t+0.01;
  48. end
  49. End = fread(s,3,'uint8');
  50. end
  51. fclose(s);