I have a problem with plotting the data read from the serial port from an Arduino uno board.
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi Everyone, I am trying to read the data printed from an Arduino board with MATLAB serial read and I have problem when I plot it real time. After some time, the data becomes delayed and it has lagged response to my input. Actually, there is a motor and its current is being read. Please take a look at my code and see if you can advise on why this happens. Also, I would like to know how to plot the data versus time. Thank you. Sina.
delete(instrfind({'Port'},{'COM4'}));
clear all;
s1=serial('COM4','Baudrate',115200);
set(s1,'DataBits',8);
set(s1,'StopBits',1);
set(s1,'Parity','none');
fopen(s1);
x=[0];
SS=[1];
str='';
j=1;
tic;
time=0;
while(j<10000);
str=fgetl(s1);
ss=str2num(str);
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
end
if j<=1;
SS(j)=ss;
x(1)=1;
elseif j>1;
SS(j)=mean([SS(j-1);ss]);
x(j)=toc;
end
if isreal(ss);
% ts = timeseries(SS(1:j));
if (exist('last'))
plot([x(j),get(last,'xdata')],[SS(j),get(last,'ydata')]);
end
last=plot(x(j), SS(j), '.-');
% plot(time, SS(j)/1000, '.-')
hold on
% siz=size(x);
% plot(x(j), siz(2), '.-r')
axis([toc-10 toc+10 0 4000]);
end
drawnow;
hold on
grid on
j=j+1;
pause(0.00001);
end
ElapsedTime=toc;
delete(instrfind({'Port'},{'COM4'}));
% fclose(s)
% delete(s)
clear s1
댓글 수: 0
채택된 답변
Geoff Hayes
2014년 8월 8일
Sina - you say that After some time, the data becomes delayed and it has lagged response to my input. What exactly of you mean by this? What is your input? Does the MATLAB code take longer to read from the serial device and so the code is spending more time in the
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
end
loop? Or is it that the code is doing more work and is not responding fast enough to your inputs?
The following seems to be doing more than it should or need
if (exist('last'))
plot([x(j),get(last,'xdata')],[SS(j),get(last,'ydata')]);
end
last=plot(x(j), SS(j), '.-');
The above is putting the most recent element at the beginning of the array, and then re-plotting all of the data. Only to re-plot the latest element again. Are you plotting time from right to left? And isn't x(1:j-1) equal to get(last,'data')? Are you doing this because the new plot command erases all previous plots, and you are trying to show past history on the figure? If so, then consider using the hold on command. I see that you are using this command already, but I don't think you need both it and the code to re-plot all previous data. You may be able to replace the above with just
plot(x(j),SS(j),'.-');
if j==1
hold on;
end
As well, consider pre-allocating memory to your x and SS arrays so that they do not grow at each iteration. Since j is at most 10000, then
numIterations = 10000;
x = zeros(numIterations,1);
SS = ones(numIterations,1);
% etc.
while j<numIterations
% do stuff
end
Also, you may want a pause in the first inner while loop
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
pause(0.01);
end
just so that the CPU is able to process other threads.
댓글 수: 6
jawad mansoor
2016년 7월 29일
편집: jawad mansoor
2016년 7월 29일
Hey, I solved this problem like this.
plot(object handle,data...);
hold on;
while(1)
gca;
%%acquire new data
%%acquire new data
plot(object handle,data...);
drawnow;
end
gca clears data on your axis, and with hold on you plot on the same axis. it is much faster. gives almost real time graphs for me.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!