Simultaneous Data Acquisition using Parallel Computing Toolbox

조회 수: 9 (최근 30일)
Kevin KIm
Kevin KIm 2021년 6월 7일
답변: Pratik 2024년 4월 10일
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