MATLAB读取BWT901BLECL5.0

博主在尝试使用MATLAB读取BWT901BLECL5.0传感器的磁场数据时遇到挑战,由于传感器的特性,磁场数据需通过读取寄存器来获取,而加速度、角速度和速度数据则自动发送。问题在于数据不同步,0x61包可能会连续出现多次,而0x71包间隔不规律,影响绘图。博主参考维特上位机原始输出,发现其读取模式为每12个0x61包后读取一个0x71包,但博主的代码未能实现这一同步机制。

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X340823

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值