【发布时间】:2016-06-28 14:37:58
【问题描述】:
我正在尝试从加速度计传感器收集数据。我有一个 Arduino 进行信号的模数转换,并通过串行端口将其发送到 Windows 上的 MATLAB。
我每 5 毫秒通过串口从 Arduino 发送一个读数。我正在使用 MATLAB 在向量中的串行读取以及使用时钟方法读取数据的时间来保存该数据。
如果我要绘制在我读取的那一秒时保存的向量列,我会得到一条曲线(非线性),当我查看 1 读取和另一个读取之间的差异时,我看到它略有不同。
有什么方法可以让数据实时保存,采样时间固定?
注意:我使用的是 250000 波特率。
Matlab 代码:
%%%%% Initialisation %%%%%
clear all
clc
format shortg
cnt = 1;%File name changer
sw = 1;%switch: 0 we add to current vector and 1 to start new vector
%%%%% Initialisation %%%%%
%%%%% Communication %%%%%
arduino=serial('COM7','BaudRate',250000);
fopen(arduino);
%%%%% Communication %%%%%
%%%%% Reading from Serial and Writing to .mat file%%%%%
while true,
if sw == 0,
if (length(Vib(:,1))==1000),% XXXX Samples in XX minutes
filename = sprintf('C:/Directory/%d_VibrationReading.mat',cnt);
save (filename,'Vib');
clear Vib
cnt= cnt+1;
sw = 1;
end
end
scan = fscanf(arduino,'%f');
if isfloat(scan) && length(scan(:,1))==6,% Change length for validation
vib = scan';
if sw == 1,
Vib = [vib clock];
sw = 0;
else
Vib = [Vib;vib clock];
end
end
end
%%%%% Reading from Serial and Writing to .mat file%%%%%
% Close Arduino Serial Port
fclose(arduino);
图 1 显示了通过串行接收的数据(每行对应 1 次串行读取) 图 2 显示了与时钟一起保存的数据
【问题讨论】:
-
请将您的代码和测量值放入您的问题中。您不会达到完美的精度,因为 MATLAB 无法进行实时处理,但曲线应该是(大致)线性的。
-
我已经添加了代码,我对实时主题进行了一些研究,发现 matlab 中有一个工具箱,专门用于从一些“特定”设备(如 National)读取数据Instruments 数据采集工具,我不太了解它们是如何做到的,但显然必须有办法解决这个问题,希望更简单。顺便说一句,如果你的采样频率变低,我看到 Matlab 有点能够处理这个问题,但是我需要一个高采样率,这样就不会为我削减它
-
该行
length(scan(:,1))==6可能会丢弃消息,当您的系统运行缓慢时,它可能会同时收到两条消息。 -
是的,但这是(我发现)确保不会由于串行通信而读取错误字符的唯一方法
-
您能否将收到的示例消息放入问题中?我的猜测是你已经超过了可用的波特率。每个数字使用多少个字符?
标签: windows matlab real-time frequency sampling