0% found this document useful (0 votes)
3 views2 pages

microcontroller_pid

The document presents a simulation of a linear system defined by the equation x(k+1)=3x(k) and explores the state evolution with and without controllers. It implements Proportional (P), Proportional-Integral (PI), and Proportional-Integral-Derivative (PID) controllers to control the system towards a desired goal of xd=4. The results are visualized through plots showing state evolution and control inputs for each controller type.

Uploaded by

ripfoc92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

microcontroller_pid

The document presents a simulation of a linear system defined by the equation x(k+1)=3x(k) and explores the state evolution with and without controllers. It implements Proportional (P), Proportional-Integral (PI), and Proportional-Integral-Derivative (PID) controllers to control the system towards a desired goal of xd=4. The results are visualized through plots showing state evolution and control inputs for each controller type.

Uploaded by

ripfoc92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

clc

clear
close all

%%%% Linear system: x(k+1)=3x(k).


% We are using the system expression as we do not have sensor data while
simulation.
% Let us see the state evolution without any controller.
xs(1)=0.05;
for k=1:10
xs(k+1)=3*xs(k);
end

%%% Now we introduce controller for the system. In this program we have Linear
system: x(k+1)=3x(k)+u(k). We are designing P, PI, and PID controllers for this
system.

%%Initial states for P, PI, and PID controllers, respectively.


x(1,1)=0.05; %% for P controller
x(1,2)=0.05; %% for PI controller
x(1,3)=0.05; %% for PID controller

%%Desired goal/poin
xd=4;

%% Gain parameters
kp=3;ki=1;kd=0.01;

%% "P" controller impementation


e(1,1)=xd-x(1,1); %% error at first instant
for k=1:10 %% iteration for time instants here
x(k,1)=xd-e(k,1); %% state at current time instant
u(k,1)=kp*e(k,1); %% "P" control input
e(k+1,1)=xd-3*x(k,1)-u(k,1); %% error at next instant 'k+1'
end

%% "PI" controller impementation


e(1,2)=xd-x(1,2); %% error at first instant
ei1(1)=e(1,2); %% introducing error variable for "I"
component
for k=1:10 %% iteration for time instants here
x(k,2)=xd-e(k,2); %% state at current time instant
if k>1
ei1(k)=ei1(k-1)+e(k,2); %% summation of error occurs here for
"I" component
end
u(k,2)=kp*e(k,2)+ki*ei1(k); %% "PI" control input
e(k+1,2)=xd-3*x(k,2)-u(k,2); %% error at next instant 'k+1'
end

%% "PID" controller impementation


e(1,3)=xd-x(1,3); %% error at first instant
ei2(1)=e(1,3); %% introducing error variable for "I"
component
ed(1)=e(1,3); %% introducing error variable for "D"
component
for k=1:10 %% iteration for time instants here

x(k,3)=xd-e(k,3); %% state at current time instant


if k>1
ei2(k)=ei2(k-1)+e(k,3); %% summation of error occurs here for
"I" component
ed(k)=e(k,3)-e(k-1,3); %% difference between errors of
consecutive instants
end
u(k,3)=kp*e(k,3)+ki*ei2(k)+kd*ed(k); %% "PID" control input
e(k+1,3)=xd-3*x(k,3)-u(k,3); %% error at next instant 'k+1'
end

%% Plot of state evolution in open-loop


figure
plot(xs(:),'red','linestyle','--','linewidth',2)
xlabel('$k$','interpreter','latex','FontName','Times New Roman','fontsize',20);
ylabel('$xs$','interpreter','latex','FontName','Times New Roman','fontsize',20);

%% Plot of "states-x" for each controller


figure
plot(x(:,1),'red','linestyle','--','linewidth',2)
hold on
plot(x(:,2),'blue','linestyle','-.','linewidth',2.5)
hold on
plot(x(:,3),'green','linestyle','-','linewidth',1)
xlabel('$k$','interpreter','latex','FontName','Times New Roman','fontsize',20);
ylabel('$x$','interpreter','latex','FontName','Times New Roman','fontsize',20);
legend('P','PI','PID','Location','northeast');

%% Plot of "control input-u" for each controller


figure
plot(u(:,1),'red','linestyle','--','linewidth',2)
hold on
plot(u(:,2),'blue','linestyle','-.','linewidth',2.5)
hold on
plot(u(:,3),'green','linestyle','-','linewidth',1)
xlabel('$k$','interpreter','latex','FontName','Times New Roman','fontsize',20);
ylabel('$u$','interpreter','latex','FontName','Times New Roman','fontsize',20);
legend('P','PI','PID','Location','northeast');

You might also like