123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- clc
- close all;
- clear all;
- dt = 1;
- t=[0];
- % m=[0;0;0;0]
- m=[0;0;0;0;0;0;0;0;0;]
- p = plot(t,m)
- data = [];
- buf = [];
- state = 0;
- x=0;
- %legend('璘신폭箕','塘신폭箕','r2','r3','b0','b1','b2','b3')
- legend('품신','빈신')
- grid on;
- set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.7]);
- delete(instrfindall('Type','serial'));%헌잿눔왯
- object = serial('com3','BaudRate',115200);%토零눔왯
- fopen(object);
- while true
- data=fread(object,230);%뗍혤鑒앴
- while ~isempty(data)
- buf = [buf data(1)];
- data(1) = [];
- switch state
- case 0
- if length(buf)>=5
- if buf(1)==170
- Len = buf(2);
- Lenf = 255 - buf(2);
- if buf(3) == Lenf
- state = 1;
- else
- buf(1) = [];
- end
- else
- buf(1) = [];
- end
- end
- case 1
- if length(buf)>=Len
- ver = 0;
- for i=1:(Len-1)
- ver = ver + buf(i);
- end
- ver = mod(ver,256);
- % disp(ver);
- % disp(buf(Len));
- if ver==buf(Len)
- if buf(4) == 0
- disp(buf);
- ax_n = double(bitshift(int16(buf(5)),8)+bitshift(int16(buf(6)),0))/100;
- ay_n = double(bitshift(int16(buf(7)),8)+bitshift(int16(buf(8)),0))/100;
- az_n = double(bitshift(int16(buf(9)),8)+bitshift(int16(buf(10)),0))/100;
-
- gx_n = double(bitshift(int16(buf(11)),8)+bitshift(int16(buf(12)),0))/100;
- gy_n = double(bitshift(int16(buf(13)),8)+bitshift(int16(buf(14)),0))/100;
- gz_n = double(bitshift(int16(buf(15)),8)+bitshift(int16(buf(16)),0))/100;
-
- mx_n = double(bitshift(int16(buf(17)),8)+bitshift(int16(buf(18)),0))/100;
- my_n = double(bitshift(int16(buf(19)),8)+bitshift(int16(buf(20)),0))/100;
- mz_n = double(bitshift(int16(buf(21)),8)+bitshift(int16(buf(22)),0))/100;
-
- m = [m [ax_n;ay_n;az_n;gx_n;gy_n;gz_n;mx_n;my_n;mz_n;]];
-
- dt = dt + 1;
- t=[t dt];
- if dt>250
- x=x+1;
- end
- end
- % set(p(1),'XData',t,'YData',m(1,:))
- set(p(2),'XData',t,'YData',m(2,:))
- % set(p(3),'XData',t,'YData',m(3,:))
- % set(p(4),'XData',t,'YData',m(4,:))
- % set(p(5),'XData',t,'YData',m(5,:))
- % set(p(6),'XData',t,'YData',m(6,:))
- % set(p(7),'XData',t,'YData',m(7,:))
- % set(p(8),'XData',t,'YData',m(8,:))
- % set(p(9),'XData',t,'YData',m(9,:))
-
- buf = buf(Len:end);
- else
- buf(1) = [];
- end
- state = 0;
- end
- otherwise
- state = 0;
- end
- end
- drawnow limitrate nocallbacks
- axis([x x+300 -5 5]);
- end
- fclose(object);
- delete(object);
- clear object;
|