Adc Pid

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

clc

close all
clear all
ar = arduino('com5');
Fs = 50; %Frecuencia de Muestreo
N = 1000; %Tama�o del Vector
u = zeros(N,1);
y = zeros(N,1);%Vector de entrada
t = linspace(0,(N-1)/Fs,N);
l1 = line(nan,nan,'Color','r','LineWidth',2)
l2 = line(nan,nan,'Color','b','LineWidth',2)
ylim([-0.1 5.1]);
xlim([0 (N-1)/Fs]);
grid
Stop = 1;
uicontrol('Style','Pushbutton','String','Parar','Callback','Stop=0;')

H = tf(8,[1 2 8]);
Hd = c2d(H,1/Fs);
a = Hd.den{1};
a1 = a(2);
a2 = a(end);
b = Hd.num{1};
b1 = b(2);
b2 = b(end);

tic
while Stop
if toc > 1/Fs
tic
u(1:end-1) = u(2:end);
u(end) = ar.analogRead(0)*5/1023;
y(1:end-1) = y(2:end);
y(end) = -a1*y(end-1)-a2*y(end-2)+b1*u(end-1)+b2*u(end-2);
set(l1,'XData',t,'YData',u)
set(l2,'XData',t,'YData',y)
drawnow
end
end

You might also like