Simultaneous Data Acquisition using Parallel Computing Toolbox

7 次查看(过去 30 天)
I need to acquire data from an Arduino Due (using serial port) and a USB-1608fs DAQ device simultaneously. Some lines I have for each of the DAQ tasks are included in the following code below. Using the parallel computing toolbox, what is the exact code/syntax to be able to run the specified code blocks simultaneously? The goal here is to create one table with matching times and plot data from the Arduino and DAQ device corresponding to each timestamp.
%% Initialize USB-1608fs DAQ Device
d = daq("mcc");
addinput(d,"Board0","Ai0","Voltage");
d.Rate = 100;
%% Initialize Arduino Object
a = serialport('COM3', 115200);
configureTerminator(a,"CR/LF");
flush(a);
a.UserData = struct("Data",[],"Count",1);
%% Data Storage from Arduino Due
digitalData = [];
times = [];
%% Reading data from DAQ device for 5 seconds -- MUST BE RUN SIMULTANEOUSLY
analogData = read(d,seconds(5));
%% Reading data from Arduino Due must take 5 seconds -- MUST BE RUN SIMULTANEOUSLY
i = 1; j = 0;
for i <= 500
digitalData(i) = str2double(readline(a));
times(i) = j;
i = i + 1;
j = j + 0.01;
end

回答(1 个)

Pratik
Pratik 2024-4-10
Hi Kevin,
As per my understanding, you want to modify the provided code to enable concurrent data acquisition using parallel computing technique.
To run the specified code blocks simultaneously, "parfeval" can be used. This way, data acquisition can be started from the Arduino Due and the USB-1608fs DAQ device at the same time. Data acquisition parts have to be wrapped into separate functions that can be called by "parfeval".
Please refer to the modified code snippet below:
% Function definitions
function digitalData = acquireFromArduino(a)
digitalData = zeros(1, 500); % Preallocate for speed
for i = 1:500
digitalData(i) = str2double(readline(a));
end
end
function [analogData, timeStamps] = acquireFromDAQ(d)
% Assuming you want to capture the timestamps as well
[analogData, timeStamps] = read(d,seconds(5));
end
%Use parfeval to Run Functions in Parallel
% Initialize Parallel Pool (if not already initialized)
if isempty(gcp('nocreate'))
parpool;
end
% Initialize USB-1608fs DAQ Device
d = daq("mcc");
addinput(d,"Board0","Ai0","Voltage");
d.Rate = 100;
% Initialize Arduino Object
a = serialport('COM3', 115200);
configureTerminator(a,"CR/LF");
flush(a);
% Start asynchronous data acquisition
f1 = parfeval(@acquireFromArduino, 1, a); % Request 1 output from acquireFromArduino
f2 = parfeval(@acquireFromDAQ, 2, d); % Request 2 outputs from acquireFromDAQ
Please refer to the documentation of "parfeval" for more information:

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by