I have a problem with plotting the data read from the serial port from an Arduino uno board.

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

 Accepted Answer

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 Comments

Hi Geoff, Thanks for taking the time to go through this. Actually, it might be some data structure problem. I changed the serial input to another variable instead of the motor current, i.e. the encoder position. It seems that it starts to get lagged very faster (in about 1/3 of the previous time) Since the current numbers were in the order of 200-600 and the encoder position is increasing constantly getting to numbers such as 3000, this seems to me that there is a problem with the large numbers and their accumulation. Please see the new code below.
delete(instrfind({'Port'},{'COM4'}));
clear all;
s1=serial('COM4','Baudrate',38400, 'OutputBufferSize',500000, 'InputBufferSize',2000);
set(s1,'DataBits',8);
set(s1,'StopBits',1);
set(s1,'Parity','none');
fopen(s1);
x=ones(10000,1);
SS=ones(10000,1);
SSfilt=ones(10000,1);
str='';
j=1;
tic;
time=0;
ssfilt=0;
alpha=0.9;
while(j<10000);
str=fgetl(s1);
ss=str2num(str);
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
pause(0.001);
end
ss;
% Exponential Moving Average (EMA) Filter
% 1st order discrete feedback low-pass filter
ssfilt=(1-alpha).*ssfilt+alpha.*ss;
% x(j)=j/1000;
if j<=1;
SS(j)=ss;
SSfilt(j)=ssfilt;
x(1)=0;
elseif j>1;
SS(j)=ss;
SSfilt(j)=ssfilt;
x(j)=toc;
end
if isreal(ss);
if j>1;
subplot(1,2,1)
% cla;
% line([ss x(j)], [0 0],'Color', 'r', 'Linewidth',2, 'Marker','o');
plot(x(j-1:j), SS(j-1:j), '.-');
hold on
grid on
drawnow;
axis([toc-10 toc+10 -2500 2500]);
subplot(1,2,2)
% cla;
% line([ssfilt x(j)], [0 0],'Color', 'r', 'Linewidth',2, 'Marker','o');
plot(x(j-1:j), SSfilt(j-1:j), '.-');
grid on
hold on
drawnow;
axis([toc-10 toc+10 -2500 2500]);
end
% plot(time, SS(j)/1000, '.-')
hold on
% siz=size(x);
% plot(x(j), j, '.-r')
% axis([toc-10 toc+10 0 2500]);
else
b='2222222222222222222222222222222'
end
drawnow;
hold on
grid on
j=j+1;
% end
get(s1,{'OutputBufferSize','BytesToOutput'});
str=0;
pause(0.01);
end
ElapsedTime=toc;
delete(instrfind({'Port'},{'COM4'}));
clear s1
Hi Sina - where do you notice the lag? Is it that the serial object (in MATLAB) is waiting longer until it gets some valid data (and so it is the Arduino that is slow to provide data), or is it that the code is not responding fast enough to the data being sent? What hints in the behaviour of the code are telling you that there is a lag?
I noticed that in your code, you have a number of hold on, grid on, and drawnow statements. You don't need to be calling these commands at every iteration of the loop. For sure remove those three lines outside of the if isreal(ss) condition. And minimize their usage to perhaps on the first update of the subplots (j==2).
Also, remove any unnecessary code like
ss;
get(s1,{'OutputBufferSize','BytesToOutput'});
etc.
Geoff, I removed those lines. However the problem still occurs. The plotting gets slow. Also it responses to the physical input (more load on the motor) with delay while it is not the case at the beginning of each run. Thanks.
What happens if you don't do any plotting of the data?
Sorry Geoff, I was away for some time. If I don't plot it, I won't know what happens, the motor turns indefinitely.
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 7 Aug 2014

Edited:

on 29 Jul 2016

Community Treasure Hunt

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

Start Hunting!