当前位置: 首页 > 工具软件 > bwt > 使用案例 >

MATLAB读取BWT901BLECL5.0

茅秦斩
2023-12-01

MATLAB读取BWT901BLECL5.0

这个太简单了,使用BWT901CL的MATLAB程序改了一下参数。

代码

clear all;
close all;
clc;
instrreset;
disp('Press Ctrl+C to stop collecting data!')
s=serial('com4','baudrate',115200);
fopen(s) ;%Open Com Port   请将COM19换成电脑识别到的COM口,波特率115000换成传感器对应的波特率
f = 10;%DataFrequce
t=0;
cnt = 1;
aa=[0 0 0];
ww=[0 0 0];
AA = [0 0 0];
tt = 0;
a=[0 0 0]';
w=[0 0 0]';
A=[0 0 0]';
while(1)
    Head = fread(s,2,'uint8');
    if (Head(1)~=uint8(85))
        continue;
    end   
    Head(2)
    switch(Head(2))
        case 97 
            a = fread(s,3,'int16')/32768*16 ; 
            w = fread(s,3,'int16')/32768*2000 ;    
            A = fread(s,3,'int16')/32768*180;
            aa=[aa;a'];
            ww = [ww;w'];
            AA = [AA;A'];
            tt = [tt;t];
            if (cnt>(f/5)) %Plot in low frequce, 
                subplot(3,1,1);plot(tt,aa);title(['Acceleration = ' num2str(a') 'm2/s']);ylabel('m2/s');
                subplot(3,1,2);plot(tt,ww);title(['Gyro = ' num2str(w') '°/s']);ylabel('°/s');
                subplot(3,1,3);plot(tt,AA);title(['Angle = ' num2str(A') '°']);ylabel('°');              
                cnt=0;
                drawnow;
                if (size(aa,1)>5*f)%clear history data
                    aa = aa(f:5*f,:);
                    ww = ww(f:5*f,:);
                    AA = AA(f:5*f,:);
                    tt = tt(f:5*f,:);
                end
            end
            cnt=cnt+1;
            t=t+0.01;
    end 
end
fclose(s);

想读磁场(0x71磁场包),但是这个传感器磁场只能发送读寄存器代码读取,发一次读一次。加速度、角速度、速度组成一个0x61包,自动发送。我的代码,运行时做不到数据同步,有时0x61包连续有3个,0x71包才一个,导致不能绘图。分析维特上位机原始输出,发现他们是每读12个0x61包,才读一个0x71包。以下是我的错误代码:

clear all;
close all;
clc;
instrreset;
disp('Press Ctrl+C to stop collecting data!')
s=serial('com4','baudrate',115200);
fopen(s) ;%Open Com Port   请将COM19换成电脑识别到的COM口,波特率115000换成传感器对应的波特率
f = 10;%DataFrequce
t=0;
cnt = 1;
aa=[0 0 0];
ww=[0 0 0];
AA = [0 0 0];
MAG = [0 0 0];
tt = 0;
a=[0 0 0]';
w=[0 0 0]';
A=[0 0 0]';
mag =[0 0 0]';
bin=[255 170 39 58 0];
while(1)
    fwrite(s,bin,'uint8');
    Head = fread(s,2,'uint8');
    if (Head(1)~=uint8(85))
        continue;
    end   
    Head(2)
    switch(Head(2))
        case 97
            a = fread(s,3,'int16')/32768*16 ; 
            w = fread(s,3,'int16')/32768*2000 ;    
            A = fread(s,3,'int16')/32768*180;    
            
        case 113
            dir = fread(s,1,'int16');
            mag = fread(s,3,'int16');
            aa=[aa;a'];
            ww = [ww;w'];
            AA = [AA;A'];
            MAG = [MAG;mag'];
            tt = [tt;t];
            if (cnt>(f/5)) %Plot in low frequce, 
                subplot(4,1,1);plot(tt,aa);title(['Acceleration = ' num2str(a') 'm2/s']);ylabel('m2/s');
                subplot(4,1,2);plot(tt,ww);title(['Gyro = ' num2str(w') '°/s']);ylabel('°/s');
                subplot(4,1,3);plot(tt,AA);title(['Angle = ' num2str(A') '°']);ylabel('°');
                %subplot(4,1,4);plot(tt,MAG);title(['Magnetometer = ' num2str(mag') 'mg']);ylabel('mg');
                cnt=0;
                drawnow;
                if (size(aa,1)>5*f)%clear history data
                    aa = aa(f:5*f,:);
                    ww = ww(f:5*f,:);
                    AA = AA(f:5*f,:);
                   MAG = MAG(f:5*f,:);
                    tt = tt(f:5*f,:);
                end
            end
            cnt=cnt+1;
            t=t+0.01;
    end
end
fclose(s);
 类似资料: