How to read data from two Arduino to Matlab ?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have two Arduino which gives me raw value of the MPU-6050. I am using the MATLAB to perform some operation in realtime. I am able to get data from the two arduino and plot it real time. But the there is delay between the two readings and the delay is kept increasing with time.
clear all;
clc;
s1=serial('COM3','baudrate',9600);
try
fopen(s1);
catch err
fclose(instrfind);
error('Make sure you select the correct COM3 Port where the Arduino is connected.');
end
%///////////////MPU6050-2//////////////////////////
s2=serial('COM10','baudrate',9600);
try
fopen(s2);
catch err
fclose(instrfind);
error('Make sure you select the correct COM10 Port where the Arduino is connected.');
end
figure ;
lin=zeros(1,2);
grid on;
hold on;
for k=1:2
lin(k)=plot(0,0);
end
hold off;
tic;
e=0;
raw1=zeros(1,6);
raw2=zeros(1,6);
a1=zeros(1000,4);
while(1)
while(strcmp(s1.TransferStatus,'read'))
pause(0.01);
end
while(strcmp(s2.TransferStatus,'read'))
pause(0.01);
end
b1=fscanf(s1);
b2=fscanf(s2);
e=toc;
%//////////////////MPU6050-1//////////////////////
if strcmp(b1(1:3),'AcX')
c=b1(4:end);
raw1=sscanf(c,'%f %f %f %f %f %f');
end
%////////////////// MPU6050-1/////////////////
if strcmp(b2(1:3),'AcX')
c=b2(4:end);
raw2=sscanf(c,'%f %f %f %f %f %f');
end
i=1;
xdata=[get(lin(i),'XData'),e];
ydata=[get(lin(i),'YData'),raw1(1)];
set(lin(i),'XData',xdata,'YData',ydata);
i=2;
xdata=[get(lin(i),'XData'),e];
ydata=[get(lin(i),'YData'),raw2(2)];
set(lin(i),'XData',xdata,'YData',ydata);
drawnow;
end
fclose(s1);
fclose(s2);
delete(s1);
delete(s2);
clear s;
This is the graph what I am getting.
However when I am runnining two separate instances of matlab and each drawing data from separate Arduino and plotting it in different graph I am getting no error (You may ask that how do I know that? well!! after 2 or 3 minutes you can feel the delay) . I need the two data in one session so that I can process it.
Where am I making mistake? Can you suggest any other way? Can it be done with parallel computing?
![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201235/untitled.jpeg)
0 Kommentare
Antworten (2)
neuromechanist
am 23 Jan. 2020
Hi,
I guess this part of the code might cause the problem:
while(strcmp(s1.TransferStatus,'read'))
pause(0.01);
end
while(strcmp(s2.TransferStatus,'read'))
pause(0.01);
end
Matlab runs these commnads in series, so it waits for the first stream and then reads the seond one. Naturally it will create a delay that will increase will time quite fast.
Can you read the sensors with Simulink?
0 Kommentare
Darío Urbina Meléndez
am 5 Feb. 2021
I am having the same issue. Were you able to solve your problem?
It would be great if you explain what was your solution!
2 Kommentare
neuromechanist
am 5 Feb. 2021
What you need is multi-thread data i/o and processing. As far as I know, it is not easily achievable with MATLAB alone, but there are two work arounds:
1- Using Simulink. You can have your Arduinos set up to send data via UDP on different ports. Since Simulink is actually a multi-threadded application, it will read form both ports at the same time.
2- You might want to create your own MEX file from your code and call the MEX file in your script. MEX files run in parallel to MATALB, so you can have multiple MEX file running in parallel and read data from them very fast. A successful example of this implementation is Labstreaminglayer (LSL). BTW, creating MEX files might be harder than running the Simulink implementation.
Walter Roberson
am 5 Feb. 2021
I am not aware of Simulink running in threads. That would seem to be contradictory to the 'parallel' option.
It is outright false that mex files run parallel to MATLAB. Mex files can, however deliberately invoke operating system or C++ threads, provided that they are very careful about how they talk to matlab.
Siehe auch
Kategorien
Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!