绘制来自arduino的实时信号的移动轴

时间:2014-03-21 16:58:10

标签: matlab arduino matlab-figure arduino-uno

我是matlab的新手,我发现很难定义一个参数来绘制一个移动轴,特别是x轴和xlim。我从arduino获得了一个实时信号,并想要与时间进行对比。看看下面的代码:

  clear all; clc; clf; 

  %a=arduino('/dev/tty.usbmodem1a12211');

  dt = 0.02; %sec. 
  %adjusted_dt = dt; 
  plot_window = 50;
  N_timesteps = 1000; 

  a = arduino('COM3');         
  a.pinMode(15,'input'); % z 
  a.pinMode(16,'input'); % y 
  a.pinMode(17,'input'); % x 

  a.pinMode(18,'output'); 
  a.pinMode(19,'output'); 

  a.digitalWrite(18,0);
  a.digitalWrite(19,1);

 x=zeros();
 y=zeros();
 z=zeros();
 t=zeros();

 %For Calibration.====================
 x_p_1g = 614.7500;  x_n_1g = 392.1351;
 y_p_1g = 612.6383;  y_n_1g = 387.7179;
 z_p_1g = 615.0000;  z_n_1g = 421.5684;
 %%%%%%%%==============================

 x_0g = (x_p_1g + x_n_1g) / 2.0 ;
 y_0g = (y_p_1g + y_n_1g) / 2.0 ;
 z_0g = (z_p_1g + z_n_1g) / 2.0 ;

 x_sntv = (x_p_1g - x_n_1g) / 2.0;
 y_sntv = (y_p_1g - y_n_1g) / 2.0;
 z_sntv = (z_p_1g - z_n_1g) / 2.0;


 elapsed_time_from_t_0 = 0.0; 

 %toc =0; 
 %tic; 

 for i=1:N_timesteps

 tic; 
 t(i,1) = elapsed_time_from_t_0;

 pause(dt); %toc;

 %  fprintf('dt = %10.7f;  (realtime dt)/(simul. time dt) = %5.2f  \n',dt, toc/dt);    

 %Simulation time is different from real time. 
 % t(i,1) = i*dt; % < = this is no longer valid. 


 x(i,1)=a.analogRead(3);
 y(i,1)=a.analogRead(2);
 z(i,1)=a.analogRead(1);

 %Calbiration  --------------------------

 x=(x(i,1) - x_0g)/x_sntv;
 y=(y(i,1) - y_0g)/y_sntv;
 z=(z(i,1) - z_0g)/z_sntv; 

 %Additional calibration code  --------------------------

 x(i,1)=a.analogRead(3);
 y(i,1)=a.analogRead(2);
 z(i,1)=a.analogRead(1);

 x=(x(i,1) - x_0g)/x_sntv;
 y=(y(i,1) - y_0g)/y_sntv;
 z=(z(i,1) - z_0g)/z_sntv;

 NEED EXTRA CODE/ARGUMENT TO CAUSE X AXIS TO MOVE/SHIFT TO PLOT THE LATEST 100 DATA

 clf;

 subplot(3,1,1); grid on; hold on;
 ylim([-2 2]);    
 xlim([ ]); need help
 plot(t,z,'b-o');
 xlabel('t[sec]');
 ylabel('z [g]');
 title('z');
 end


 subplot(3,1,2); grid on; hold on;
 ylim([-2 2]);
 xlim([ ]); need help
 plot(t,x,'b-o');
 xlabel('t[sec]');
 ylabel('x [g]');
 title('x');
 end   

 subplot(3,1,3); grid on; hold on;
 ylim([-2 2]);
 xlim([ ]);     need help
 plot(t,y,'b-o');
 xlabel('t[sec]');
 ylabel('y [g]');
 title('y');
 end
 toc; 
 elapsed_time_from_t_0 = elapsed_time_from_t_0 + toc; 

 end

1 个答案:

答案 0 :(得分:0)

当你在一个循环中绘图时,情节是“不好的”。它会重现整个事物,而您只是尝试重绘其中的几个点。最好的做法如下:

h = plot(NaN);
for k = 1:N
  set(h,'YData',data,'XData',time);
  axis([time(k-100) time(k)+1 min(data)*1.1 max(data)*1.1]);
  redraw;
end

限制的另一种选择:

% symmetrical y limits
xlim([time(k-100) time(k)+1]);
if (k > 102)
  border = max(abs(data(k-100:end))) *1.1;
  ylim([-border border]);
else
  ylim([ min(data) max(data)]);
end

此方法仅替换图中的数据。 axis语句先获取x,然后是y限制。下限(左,机器人)是从100个样本之前的时间,右边的+1秒。我将min和max设置为比max / min高10%/更低。随意在这里玩。 有关使用matlab / arduino和二进制数字传输(arduino更容易)的完整示例,请参阅:

https://stackoverflow.com/questions/24368670/matlab-plot-serial-data-continously