Anda di halaman 1dari 15

Simulation of PID Controller

Dr. Chi-Hsu Wang, IEEE Fellow

Department of Electrical Engineering

National Chiao Tung University

Hsinchu, Taiwan

R(t)

+

Y(t)

R(t) + Y(t) e K P +K D s+K I /s u Plant  1. R(t)
R(t) + Y(t) e K P +K D s+K I /s u Plant  1. R(t)

e

R(t) + Y(t) e K P +K D s+K I /s u Plant  1. R(t)

K P +K D s+K I /s

u Plant
u
Plant

1.

R(t) : reference input

2.

Plant: can be any systems to be controlled by the PID controller.

3.

e(t): Input of PID controller.

4.

u(t): Output of PID controller.

5.

Y(t): The system output.

1. If the plant is linear with transfer function G(s), then we can multiply the transfer function of the PID controller with G(s) to get the overall closed-

loop transfer function.

2. If the plant is nonlinear, then we can not obtain the closed-loop transfer function.

3. So, the way to simulate the nonlinear system with PID controller is to treat the two systems independently.

In Matlab, we can use ode23or ode45to simulate the dynamical systems represented by state space equation, either linear or nonlinear.

4.

represented by state space equation , either linear or nonlinear. 4. Dr. C. Wang Simulation of
represented by state space equation , either linear or nonlinear. 4. Dr. C. Wang Simulation of

5. The key issue is how to represent the PID controller using the state space equation.

6. If we can, then we can combine the two systems in one ode23or ode45.

the two systems in one “ ode23 ” or “ ode45 ” . 7. If there
the two systems in one “ ode23 ” or “ ode45 ” . 7. If there

7. If there is only one state variable in PID controller, and the nonlinear system has two

state variables, then we can say that the overall

system has three state variables.

8. This is to say that we can have x 1 from PID controller and x 2 and x 3 from the nonlinear system.

9. For instance, if the nonlinear system is the inverted pendulum, then we can have the following arrangement:

R(t)

+

u Inverted Pendulum
u Inverted
Pendulum

Y(t)

the following arrangement: R(t) + u Inverted Pendulum Y(t) e K P +K D s+K I
the following arrangement: R(t) + u Inverted Pendulum Y(t) e K P +K D s+K I

e

following arrangement: R(t) + u Inverted Pendulum Y(t) e K P +K D s+K I /s

K P +K D s+K I /s

x

1

x 2

, x

3

The PID controller
The PID controller
K x  P 1 K P e= x  1 K I x 1
K
x
P
1
K
P
e= x
1
K
I x 1
u
K
I /s
K
D s
K
x

D
1
u  K
x 
K x
K  x
p
1
I
1
D
1

The Inverted Pendulum

 l
l

= x 1

mg

mc

The Inverted Pendulum  l  = x 1 mg mc u  x  x

u

The Inverted Pendulum  l  = x 1 mg mc u  x  x
The Inverted Pendulum  l  = x 1 mg mc u  x  x
The Inverted Pendulum  l  = x 1 mg mc u  x  x
 x  x 1 2 g sin x 1 m  m m 
x
 x
1
2
g
sin
x
1
m
 m
m
 m
c
c
x 
u
2 
2
2
4
m
cos
x
4
m
cos
x
1
l (
) l (
1 )
3
m
 m
3
m
 m
c
c

mlx

2

2 cos

x

1

sin

x

1

cos x

1

m c =1kg, m=0.1kg, and l=0.5m

The inverted pendulum is asked to stay as perpendicular as possible, i.e., Y==0.0 degree.

Therefore the reference signal R(t) = 0.0.

Therefore e = R - = - = - x 2.

Therefore

u K

K

(

p

x

1

x

2

)

p

K x

I

1

K x

I

1

K  x

D

1

K

D

(

x

3

)

Thus we have represented u in terms of x 1 , x 2 and x 3 .

• Thus the function called by ode23 or ode45 must have three state variables. i.e.,

Thus the function called by ode23 or ode45 must have three state variables. i.e., x 1 , x 2 and x 3 .

After the initial declarations inside the function, we must have:

KP

 

x (2)

KI  x (1) 
KI
x
(1)
we must have: KP   x (2)  KI  x (1)  KD x

KD

x (3);

u

xdot

(1)

xdot

(2)

xdot

(3)

 x (2);

x (3);

PID controller

Inverted Pendulum

 

u

;

% PID controller to control the Inverted Pendulum close all; clear all;

t0=0;

tf=10;

tspan=[t0 tf]; ang=[-20 -10 -7 4 8 12 16 30];

ang=ang*pi/180;

axis([0 tf -45 45]);

for h=ang, y0=[0 h 0]; [t y]=ode23('pend3', tspan, y0);

hold on;

yout=(y(:,2)*180/pi);

% limit -90<=yout<90; for index=1:length(yout), if yout(index)>90

yout(index:length(yout))=90;

break; end if yout(index)<-90

yout(index:length(yout))=-90;

break;

end

end

plot(t,yout);

end

title('PID controlled Inverted Pendulum');

ylabel('Angle (Degrees)'); xlabel('Time (Second)');

function xdot=pend3(t,x) g=9.8; mc=1; m=0.1; l=0.5; M=mc+m; kp=100; ki=0; kd=100;

% to generate PID controller

u=-kp*x(2)+ki*x(1)-kd*x(3);

n1=g*sin(x(2));

n2=l*(m*x(3)^2)*cos(x(2))*sin(x(2))/M;

n3=cos(x(2))/M;

den=l*(4/3-(m*cos(x(2))^2))/M;

xdot(1)=-x(2);

xdot(2)=x(3);

xdot(3)=(n1-n2)/den+n3*u/den;

xdot=xdot';

return

Angle (Degrees)

PID controlled Inverted Pendulum

40

30

20

10

0

-10

-20

-30

-40

controlled Inverted Pendulum 40 30 20 10 0 -10 -20 -30 -40 0 2 4 6

0

2

4

6

Time (Second)

8

10

The above results are too good!

In fact, there is a physical limit of u, i.e., u min u u max .

In the above nonlinear example, m=1.0Kg. If we let K P =100, then we are asking the controller to generate 100 NT (force, Kg/m 2 ) or so in the beginning. This is not a right scale.

If we limit K p to be within the scale of m, say, 1 K P 5, then the result will be more realistic.

Try to find an optimal set of {K P , K I , K D } using the correct scales.

On page 257 of the following book:

A Course in Fuzzy Systems and Control”, Li-Xin

Wang, Prentice Hall, 1997.

there is the topic of how to tune the PID controller using fuzzy if-then-else rules.

Also in the following journal article:

Zhao, Z. Y., M. Tomizuka, and S. Isaka, “Fuzzy gain scheduling of PID controllers,” IEEE Trans. On Systems, Man, and Cybernetics, Vol. 23, No. 5, pp. 1392~1398, 1993.