You are on page 1of 12

Modeling of DC motor in state space and

position controller design


Marco Baltazar, Branco Palma, Yerack Paredes
Faculty of Engineering, School of Electronic Engineering
Universidad Peruana de Ciencias Aplicadas (UPC)
U201213835@upc.edu.pe
U201417983@upc.edu.pe
U201512654@upc.edu.pe

 systems, to focus not just on the input and output


Abstract: this paper describes the procedure to signals but rather to describe the interaction and
design a position controller of a DC motor, taking time-evolution of several key variables or signals
data from an encoder sensor. From angular position that are associated with the various component
data we get transfer function and converter in state- processes internal to the system. A state-space model
space model to analyze the properties of the system. is built around a set of state variables; the number of
state variables in a model or system is referred to as
its order.
I. INTRODUCTION
The principle objective of this paper is design a Finite-dimensional linear systems can always be
controller position, taking data from encoder sensor. modeled using a set of differentials (or difference)
An encoder sensor it´s an electromechanical device equations as follows:
that creates a digital signal from a motion. This
device provides users with information on position, 𝑥(𝑡̇ ) = 𝐴(𝑡)𝑥(𝑡) + 𝐵(𝑡)𝑢(𝑡)
velocity and direction. Our data contains information 𝑦(𝑡) = 𝐶 (𝑡)𝑥(𝑡) + 𝐷(𝑡)𝑢(𝑡)
about angular position and encoder values were The matrices appearing in the above formulas are in
taken for 25 seconds. general functions of time and have the correct
dimensions to make the equations meaningful.
First, we need to get overshoot, peak time, settling
time values from a data angular position. With those Where:
values we can get natural frequency and damping
ratio. Then, obtain function transfer, this function 𝑥 (𝑡): 𝑆𝑡𝑎𝑡𝑒 𝑣𝑒𝑐𝑡𝑜𝑟
Will be transformed into a model in state space
𝑦(𝑡): 𝑂𝑢𝑡𝑝𝑢𝑡 𝑣𝑒𝑐𝑡𝑜𝑟
𝑢(𝑡): 𝐼𝑛𝑝𝑢𝑡 𝑣𝑒𝑐𝑡𝑜𝑟
To design position controller is necessary know
𝐴(𝑡): 𝑆𝑡𝑎𝑡𝑒 𝑚𝑎𝑡𝑟𝑖𝑥
basic concept about transfer function, state-space,
𝐵 (𝑡): 𝐼𝑛𝑝𝑢𝑡 𝑚𝑎𝑡𝑟𝑖𝑥
and controller and observer design. Transfer function
𝐶 (𝑡): 𝑂𝑢𝑡𝑝𝑢𝑡 𝑚𝑎𝑡𝑟𝑖𝑥
expresses the system dynamics as a set simultaneous
𝐷 (𝑡): 𝐷𝑖𝑟𝑒𝑐𝑡 𝑡𝑟𝑎𝑛𝑠𝑚𝑖𝑠𝑠𝑖𝑜𝑛 𝑚𝑎𝑡𝑟𝑖𝑥
algebraic equation.
The properties analyzed in this paper are the
Th estate-space model it is often natural and
following:
convenient, when studying or modeling physical
action concepts. To design a system with integral
Stability: A system is stable if the natural response action the original system must be an integrator-less
decays exponentially to zero as 𝑡 → ∞ or when all tracking system and the integral action is generated
polos are on left half s-plane. by software.

Remembering the general system, we have the


following expression:

Fig 1: S-Plane 𝑥(𝑡̇ ) = 𝐴(𝑡)𝑥(𝑡) + 𝐵(𝑡)𝑢(𝑡)


𝑦(𝑡) = 𝐶 (𝑡)𝑥(𝑡) + 𝐷(𝑡)𝑢(𝑡)

Controllability: In order to be able to do whatever we We assume a reference 𝑟(𝑡) , then we get the
want with the given Dynamic system under control characteristics the control law:
input, the system must be controllable. If the system
is controllable, we can find a control input that Will 𝑢 = −𝐾𝑥 + 𝐾𝐼 𝜉
transfer our system from any initial state to any final
state. To know controllability be having to apply the Where 𝜉 is the output of the integrator, K is the
following expressions: proportional gain vector and 𝐾𝐼 is the integral gain.

𝑆 = [𝐵 𝐴𝐵 𝐴2 𝐵 … 𝐴𝑛−1 𝐵]
𝜉̇ = 𝑟 − 𝑦
The system Will be controllable if and only if 𝜉̇ = 𝑟 − (𝐶𝑥 + 𝐷𝑢)

𝑟𝑎𝑛𝑘(𝑆) = 𝑛 The system is resized and has the following


expression:
Observability: In order to see what is going on inside
the system under observation, the system must be 𝑥̇ 𝐴 0 𝑥 𝐵
observable. If the systems are observability, we can [ ̇] = [ ] [𝜉 ] + [ ] 𝑢
𝜉 −𝐶 0 0
design an observer to estimate all the states of the
system. The observability has the following If we evaluate at infinity the state variables and we
expression: define a new vector:

𝐶 𝑥𝑒(𝑡)
𝐶𝐴 𝑒(𝑡) = [ 𝜉 ]
𝑒(𝑡)
𝑉 = 𝐶𝐴2

[𝐶𝐴𝑛−1 ] We obtain a new control law:

The system Will be observable if and only if ̂𝑒


𝑢 = −𝐾

𝑟𝑎𝑛𝑘(𝑉 ) = 𝑛 Where:

Fort he controller system, we have known integral ̂ = [𝐾 − 𝐾𝐼 ]


𝐾
Finally, we have the following expression: for the number corresponding to the order of the
maximum value of the output.

𝑥̇ 𝐴 0 𝑥 𝐵 Third, the steady state error will be determined


[ ̇] = [ ][ ] + [ ]𝑢
𝜉 −𝐶 0 𝜉 0 using the following formula.

𝑥
𝑦 = [𝐶 0] [𝜉 ] + [𝐷]𝑢 𝑒𝑠𝑠 = |𝑒∞ − 𝑟𝑒𝑓|

Fourth, the delay time is calculated, which is 50% of


𝑢 = −𝐾𝑥 + 𝐾𝐼 𝜉
the steady state output value.

Finally, the value of the establishment time will be


Sometimes all state space variables are not available
determined with a tolerance of 2% and 5% will
for measurements, or it is not practical to measure all
respect to time at infinity.
of them, or it is too expensive to measure all state
space variables. To solve this problem, we must
Once these curve parameters have been obtained, we
design an observer. The observer have the following
can reconstruct the transfer function of a second-
structure:
order or approximate system as shown in the image
below.
𝑦 = 𝐶𝑥 + 𝐷𝑢

𝑥̃̇ = 𝐴𝑥̃ + 𝐵𝑢 + 𝐿(𝑦 − 𝑦̃)

Where:

𝐿: 𝐺𝑎𝑖𝑛 𝑣𝑒𝑐𝑡𝑜𝑟
𝑥̃: 𝐸𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 𝑠𝑡𝑎𝑡𝑒𝑠 𝑣𝑒𝑐𝑡𝑜𝑟
𝑦̃: 𝐸𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑 𝑜𝑢𝑡𝑝𝑢𝑡

II. WORK DEVELOPMENT AND ANALYSIS

For the development of this work, the transfer


function was obtained from the position data sensed
by encoder of a DC motor. Fig 2: Signal Modeling

First, we start by calculating the maximum Because the encoder has noise, the signal is altered
overshoot, because with it we can determine the in amplitude, for this reason, it was decided to
damping factor. develop a digital low-pass filter, which removes
high-frequency signals and smooths it out.
𝑧𝑒𝑡𝑎
−( )𝜋
𝑀𝑝 = 𝑒 √1−𝑧𝑒𝑡𝑎 2

Second, the peak time Will be determined by looking


real values of the filtered encoder of the DC motor,
then we proceed to determine the system in the state
space, which are:

𝑋̇ 0 1 0
[ 1] = [ ][ ]
𝑋̇2 −0.7224 −0.4930 1.8144

0
𝑦 = [1 0] [ ] + [0]𝑢
1.8144

Then, the stability, controllability and


observability of the system will be analyzed. These
Fig 3: Comparison original and filtered signals parameters are important to continue the design of
the state controller and the state observer with
For this work, a filter of order 100 was used, as integral action. In addition, the poles for the
shown in figure 4, since it was found that the higher controller and the observer are as follows.
the order of the filter the encoder signal presented
less high-frequency noise. Also, as the filter order 𝑝𝑑𝑐 = [−4 − 4 − 4]
increases, the 5% and 2% tolerance curve approach 𝑝𝑑𝑜 = [−4 − 4]
the encoder output curve without noise.

The purpose of this work is to control the angular


position of the DC motor, for this reason a reference
of π rad was chosen for the output in a stable state as
shown in figure 5.

Fig 4: Filtered encoder signal: original, 5%


tolerance and 2% tolerance

Therefore, the transfer function was chosen with a


tolerance of 5% of the data obtained from the filtered Fig 5: Output modeling with reference of π rad.
encoder.
Also, it is necessary be careful with the current in
0.6246 the motor DC. For a correct functioning of the
𝐻 (𝑠 ) = control of the motor, the current has to be between
𝑠 2 + 0.2419𝑠 + 0.252
This function represents the closest curve to the no excessive values. The figure 6 shows the values
of current along the time. in the figure 8.

Fig 8: Current comparison according state controller


poles

Fig 6: Motor current comparison When the poles of the feedback state controller
change and become more negative, that is, moving to
III. CONCLUSIONS
the left of the coordinate axes, the system becomes
Finally, a comparison is made by assigning more faster to reach the reference which is π as shown in
negative poles to the controller and the observer as figure 7.
shown below.
When the observer's poles shift and become more 𝑝𝑑𝑐 = [−4 − 4 − 4]
negative, that is, they move to the left, the system 𝑝𝑑𝑜 = [−10 − 10]
tracks the state variables faster as shown in figure 7.

𝑝𝑑𝑐 = [−10 − 10 − 10]


𝑝𝑑𝑜 = [−4 − 4]

Fig 9: State Observer Poles Comparison

The current undergoes changes in a way equivalent


to the output of the system. In other words, if the
Fig 7: State Controller Poles Comparison
response of the system is faster, the current tends to
increase its amplitude and a peak occurs, however, if
Other effect in the system is the change in the
the response of the system is slow, this peak does not
amplitude of de current. Although the system
exist and the current does not reach high values. This
becomes more faster with more negative poles, this
is evident in figure 10.
effect causes current to increase considerably as seen
Fig 10: Current comparison according state
observer poles

IV. BIBLIOGRAPHY
[1] Enrique Arnáez Braschi, Enfoque práctico del control
moderno, Primera edición. Lima: Metrocolor S.A., 2014.

[2] Katsuhiko Ogata, Editorial Pearson 5ta ed., 2014,


Ingeniería de control moderna

[3] Saldaña José; Castillo Ober (2018), Modelamiento y


Diseño de Controlador de posición con Observador de
estados de un motor DC. Lima, Perú

[4] Alan Oppenheim and George Verghese. 6.011 Introduction


to Communication, control and signal processing. Spring
2010.Massachusetts Institute of Technology: MIT
OpenCourseWare, https://ocw.mit.edu. License: Creative
Commons BY-NC-SA.
V. ANEXO 1 break
end
clc;clear all;close all end
load('PL1.mat') tr = trf - tri
sensor = X1; disp(' ')
t = T;
[l,a] = size(sensor); %obtener datos %% Calculo tiempo de subida 10% - 90%.
%% Mp máximo sobreimpulso disp('Tiempo de subida de 10% - 90%: ')
disp('Maximo sobreimpulso (%): ') for i=1:l
Mp = (max(sensor)- if sensor(i) > (sensor(l)*0.10)
sensor(l))/sensor(l)*100 tri = t(i-1);
disp(' ') break
%% Calculo tiempo pico end
disp('Tiempo pico: ') end
ftp = find(sensor == max(sensor));
tp = t(ftp) disp(' ')
disp(' ')
for i=1:l
%% Error estado estacionario if sensor(i) > (sensor(l)*0.90)
disp('Error estado estacionario: ') trf = t(i-1);
Ess = abs(sensor(l)-1) break
disp(' ') end
end
%% tiempo de retardo (cuando alcanza el tr = trf - tri
50% disp(' ')
disp('tiempo retardo: ')
for i=1:l %% Tiempo de establecimiento 2%
if sensor(i)>(sensor(l)*0.50) Tolerancia.
td=t(i-1) disp('Tiempo de establecimiento 2%
break Tolerancia: ')
end for i = l:-1:1
end if
disp(' ') (sensor(i)<sensor(l)*0.98)|(sensor(i)>sen
sor(l)*1.02)
%% SCalculo de tiempo de subida ( de 0 a ts2 = t(i+1)
100%) break
disp('tiempo de subida de 0 - 100%: ') end
for i=1:l end
if sensor(i)>sensor(l) disp(' ')
trf = t(i-1);
tr = trf - 0 %% Tiempo de establecimiento 5%
break Tolerancia
end disp('Tiempo de establecimiento 5%
end Tolerancia: ')
disp(' ') for i = l:-1:1
%% Tiempo de subida 5% - 95%. (tr) if
disp('Tiempo de subida 5% - 95%: ') (sensor(i)<sensor(l)*0.95)|(sensor(i)>sen
for i=1:l sor(l)*1.05)
if sensor(i) > (sensor(l)*0.05) ts5 = t(i+1)
tri = t(i-1); break
break end
end end
end disp(' ')

disp(' ') %% Función de transferencia parámetros


disp('Funcion de transferecnia con
for i=1:l ruido')
if sensor(i) > (sensor(l)*0.95) disp(' ')
trf = t(i-1); % Z (factor de amortiguamiento)
disp('Zeta:')
ln2 = (log(Mp/100))^2; %Mp esta en %, [X_fsys2 X_fsys5] =filtra(X_filtrado,T);
debemos colocarlo en valor real.
z = sqrt(ln2/(ln2+pi^2)) figure(3)

disp(' ') plot(T,X_filtrado);hold on


% Wn (frecuencia natural no amortiguada) step(X_fsys2,T,'k');hold on
% Para una tolerancia del 2% step(X_fsys5,T,'r');
disp('Wn for 2% Tolerancia:') grid on
wn2 = 4/(z*ts2) legend('Señal filtrada','2%
disp(' ') Tolerancia','5%
% Para una tolerancia del 2% Tolerance','Location','Southeast')
disp('Wn for 5% Tolerancia:') title('Filtro orden 100 aplicado a Señal
wn5 = 3/(z*ts5) original')
disp(' ')
%% Ganancia del sistema
disp('Ganancia de sistema:')
k = sensor(l)/1 %% Transformación a espacio estados
disp(' ')
%% Funcion de transferencia FCC =
% 2% Tolerancia transpose(canon(X_fsys5,'companion'));
disp('Función de transferencia con 2% [A,B,C,D] = ssdata(FCC)
Tolerancia con ruido')
num2 = k*wn2^2;
den2 = [1 2*z*wn2 wn2^2]; %estabilidad
sys2 = tf(num2,den2) polos = eig(A)
disp(' ') estado = true;

% 5% Tolerancia for i = 1:length(polos)


disp('Función de transferencia con 5% if (real(polos(i)) > 0) |
Tolerancia con ruido') (real(polos(i)) == 0)
num5 = k*wn5^2; estado = false;
den5 = [1 2*z*wn5 wn5^2]; end
sys5 = tf(num5,den5) end
%% GRAFICA
figure(1)
plot(T,sensor);hold on if estado == true
step(sys2,T,'k');hold on disp('Sistema Estable')
step(sys5,T,'r') else
grid on disp('Sistema Inestable')
legend('Señal original','2% end
Tolerancia','5%
Tolerancia','Location','Southeast') % Controlabilidad
title('Función de transferencia señal CC = [B A*B A*A*B];
original')
if rank(CC) == rank(A)
figure(2) disp('Sistema Controlable')
plot(T,X1);hold on else
disp('Sistema no Controlable')
X_filtrado=filter((1/100)*ones(1,100),1,X end
1);
% Observabilidad
plot(T,X_filtrado,'g'); OO= [ C; C*A; C*A*A];
grid on rank(OO);
legend('Señal original', 'Filtro orden
100','Location','Southeast') if rank(OO) == rank(A)
ylabel('Amplitud') disp('Sistema Observable')
xlabel('Tiempo(s)') else
title('Filtrado de ruido del sensor ') disp('Sistema NO Observable')
end
u = -K*xo + kI*x(3,1);
%% Diseño de controlador
x = x + xp*dt;
% matrices xo = xo + xop*dt;
Am = [ A zeros(2,1)
-C zeros(1,1) ]; k = k + 1;
end
Bm = [ B ; zeros(1,1) ];
%% Graficas
Wm = [ zeros(2,1);eye(1,1) ];
figure(4)
Cm = [ C zeros(1,1) ];
subplot(211)
Dm = D; plot(T,X1-Xo1);grid on;ylabel('Error X1')

% controlador subplot(212)
pdc = -4*ones(3,1) plot(T,X2-Xo2);grid on;ylabel('Error X2')
grid on
Km = acker(Am,Bm,pdc);
K = Km(1,1:2); figure(5)
kI = -Km(1,3); subplot(1,2,1),plot(T,Y,'y','LineWidth',4
);hold on
%Observador subplot(1,2,1),plot(T,X1,'LineWidth',1);h
pdo = -4*ones(2,1) old on
L = acker(A',C',pdo)'; subplot(1,2,1),plot(T,Xo1,'LineWidth',2);
hold on
subplot(1,2,1),plot(T,X1-
% Tiempo Xo1,'LineWidth',1);grid on
ti = 0; grid on;
dt = 0.001; ylabel('Radianes');xlabel('Tiempo(s)');ti
tf = 5; tle('Posición angular con referencia de
pi radianes')
% condiciones iniciales legend('Posicion','estado X1','estado
x = [0 0 0]'; Xo1','X1-Xo1','Location','best')
xo = [0.2 2]'; %%%%%%%%%%%%%%%%%%%%%%%%% subplot(1,2,2),plot(T,X2);hold on
u = 2.5; title('ESTADO
r = pi; X2'),xlabel('Tiempo(s)'),ylabel('Amperios
');
% Simulaciones subplot(1,2,2), plot(T,Xo2); hold on
k = 1; subplot(1,2,2),plot(T,X2-Xo2);grid on;
for t = ti:dt:tf legend('X2','Xo2', 'X2 -Xo2');
X1(k,1) = x(1,1);
X2(k,1) = x(2,1);
X3(k,1) = x(3,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xo1(k,1) = xo(1,1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xo2(k,1) = xo(2,1); %% Comparación de pdc =4 y pdc=10 del
controlador
T(k,1) = t;
% controlador
xp = Am*x + Bm*u + Wm*r; pdc = -4*ones(3,1);
y = Cm*x + Dm*u;
Km = acker(Am,Bm,pdc);
Y(k,1) = y; K = Km(1,1:2);
kI = -Km(1,3);
yo = C*xo + D*u;
xop = A*xo + B*u + L*(y-yo); %Observador
pdo = -4*ones(2,1);
L = acker(A',C',pdo)'; % controlador
pdc = -10*ones(3,1);
% Tiempo
ti = 0; Km = acker(Am,Bm,pdc);
dt = 0.001; K = Km(1,1:2);
tf = 5; kI = -Km(1,3);

% condiciones iniciales %Observador


x = [0 0 0]'; pdo = -4*ones(2,1);
xo = [0.2 2]'; %%%%%%%%%%%%%%%%%%%%%%%%% L = acker(A',C',pdo)';
u = 2.5;
r = pi; % Tiempo
ti = 0;
% Simulaciones dt = 0.001;
k = 1; tf = 5;
for t = ti:dt:tf
X1(k,1) = x(1,1); % condiciones iniciales
X2(k,1) = x(2,1); x = [0 0 0]';
X3(k,1) = x(3,1); xo = [0.2 2]'; %%%%%%%%%%%%%%%%%%%%%%%%%
u = 2.5;
Xo1(k,1) = xo(1,1); r = pi;
Xo2(k,1) = xo(2,1);
% Simulaciones
T(k,1) = t; k = 1;
for t = ti:dt:tf
xp = Am*x + Bm*u + Wm*r; X1(k,1) = x(1,1);
y = Cm*x + Dm*u; X2(k,1) = x(2,1);
X3(k,1) = x(3,1);
Y(k,1) = y;
Xo1(k,1) = xo(1,1);
yo = C*xo + D*u; Xo2(k,1) = xo(2,1);
xop = A*xo + B*u + L*(y-yo);
T(k,1) = t;
u = -K*xo + kI*x(3,1);
xp = Am*x + Bm*u + Wm*r;
x = x + xp*dt; y = Cm*x + Dm*u;
xo = xo + xop*dt;
Y(k,1) = y;
k = k + 1;
end yo = C*xo + D*u;
xop = A*xo + B*u + L*(y-yo);
figure(6)
subplot(2,2,1),plot(T,X1,'g');hold on u = -K*xo + kI*x(3,1);
title('Estado
X1'),ylabel('Posición'),xlabel('Tiempo'); x = x + xp*dt;
grid on; xo = xo + xop*dt;

subplot(2,2,[3,4]),plot(T,Y,'g');hold on k = k + 1;
ylabel('Radianes');xlabel('Tiempo(s)'); end
title('Posición angular con referencia de subplot(2,2,1), plot(T,X1,'-r');hold on
pi radianes'); legend('Posición pdc = 4','Posición pdc =
grid on; 10','Location','Southeast')
subplot(2,2,[3,4]),plot(T,Y,'-r');hold on
subplot(2,2,2),plot(T,X2,'g');hold on legend('Posición pdc = 4','Posición pdc =
ylabel('Amperios');xlabel('Tiempo(s)'); 10','Location','Southeast')
title('ESTADO X2'); grid on
grid on; subplot(2,2,2),plot(T,X2,'-r');hold on
%ylabel('Radianes');xlabel('Tiempo(s)');
%title('Posición angular con referencia
de pi radianes') k = k + 1;
legend('Corriente pdc = 4','Corriente pdc end
= 10','Location','Southeast')
figure(7)
subplot(2,2,1),plot(T,X1,'g');hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% title('Estado
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% X1');xlabel('Tiempo(s)');ylabel('Posición
%% Comparación de pdo =4 y pdo=4 del ');
observador grid on;
subplot(2,2,[3,4]),plot(T,Y,'g');hold on
% controlador title('Posición angular con referencia de
pdc = -4*ones(3,1); pi
radianes');ylabel('Radianes');xlabel('Tie
Km = acker(Am,Bm,pdc); mpo(s)');
K = Km(1,1:2); %legend('Posición pdo = 4','Posición pdo
kI = -Km(1,3); = 10','Location','Southeast');
grid on;
subplot(2,2,2),plot(T,X2,'g');hold on
%Observador title('Estado X2'),
pdo = -4*ones(2,1); ylabel('Amperios');xlabel('Tiempo(s)');
L = acker(A',C',pdo)'; grid on;

% Tiempo % controlador
ti = 0; pdc = -4*ones(3,1);
dt = 0.001;
tf = 5;
Km = acker(Am,Bm,pdc);
K = Km(1,1:2);
% condiciones iniciales kI = -Km(1,3);
x = [0 0 0]';
xo = [0.2 2]'; %%%%%%%%%%%%%%%%%%%%%%%%%
u = 2.5; %Observador
r = pi; pdo = -10*ones(2,1);
L = acker(A',C',pdo)';
% Simulaciones
k = 1; % Tiempo
for t = ti:dt:tf ti = 0;
X1(k,1) = x(1,1); dt = 0.001;
X2(k,1) = x(2,1); tf = 5;
X3(k,1) = x(3,1);
% condiciones iniciales
Xo1(k,1) = xo(1,1); x = [0 0 0]';
Xo2(k,1) = xo(2,1); xo = [0.2 2]'; %%%%%%%%%%%%%%%%%%%%%%%%%
u = 2.5;
r = pi;
T(k,1) = t;
% Simulaciones
xp = Am*x + Bm*u + Wm*r; k = 1;
y = Cm*x + Dm*u; for t = ti:dt:tf
X1(k,1) = x(1,1);
Y(k,1) = y; X2(k,1) = x(2,1);
X3(k,1) = x(3,1);
yo = C*xo + D*u;
xop = A*xo + B*u + L*(y-yo); Xo1(k,1) = xo(1,1);
Xo2(k,1) = xo(2,1);
u = -K*xo + kI*x(3,1);
T(k,1) = t;
x = x + xp*dt;
xo = xo + xop*dt; xp = Am*x + Bm*u + Wm*r;
y = Cm*x + Dm*u;

Y(k,1) = y;

yo = C*xo + D*u;
xop = A*xo + B*u + L*(y-yo);

u = -K*xo + kI*x(3,1);

x = x + xp*dt;
xo = xo + xop*dt;

k = k + 1;
end
subplot(2,2,1),plot(T,X1,'-r');hold on
grid on
legend('Posición pdo = 4','Posición pdo =
10','Location','Southeast')
subplot(2,2,[3,4]),plot(T,Y,'-r');hold on
grid on
legend('C pdo = 4','Posición pdo =
10','Location','Southeast')
subplot(2,2,2),plot(T,X2,'-r');hold on
grid on
%ylabel('Radianes');xlabel('Tiempo(s)');
%title('Posición angular con referencia
de pi radianes')
legend('Corriente pdo = 4','Posición pdo
= 10','Location','Southeast')

You might also like