July 24, 2008

PID Controller and Built in Filter Design


We have designed a PID control loop simulator using Matlab and Simulink. Below is the PID control loop graphically designed using Simulink (Matlab and Simulink are excellent software design tools developed by The MathWorks Inc). The PID controller we have implemented is of type C. For more info about different types of PID controller please visit http://bestune.50megs.com/typeABC.htm.


The PID controller contains a built in digital filter. It receives 8 inputs:

x(1): Filtered PV, which will be further filtered in the built in digital filter.
x(2): Setpoint.
x(3): The proportional gain Kp.
x(4): The integral gain Ki.
x(5): The derivative gain Kd.
x(6): Time.
x(7): If it is 1, then use PID control action, otherwise use manual CO value.
x(8): Manual CO value.

The PID controller plus a built in digital filter is designed as a Matlab function "pid1.m", as shown below.

function u=pid(x)

global Tpid Tf t0 t1 tf k

global sp ek uk uk_1 yk yk_1 vk vk_1


t=x(6); % Read time

if t==0

k=0;

Tpid=1; % PID loop update time

t0=0;

t1=0;

uk=x(8); % Initial CO value

yk=x(1); % Initial PV value

sp=x(2); % Initial setpoint

ek=sp-yk;

Tf=0.1; % Filter time constant

tf=200; % Simulation stop time

else % Filtering yk

T0=t-t0;

yk=yk*Tf/(Tf+T0)+x(1)*T0/(Tf+T0);

t0=t;

end

if t-t1>=Tpid | x(2)~=sp %Every Tpid sec calculate the new CO value

sp=x(2);

T=t-t1;

if T>0.0001 %Avoid division by T=0

if x(7)==1 & k>=3 % Then use PID control

Kp=x(3);

Ki=x(4);

Kd=x(5);

ek=sp-yk;

vk=(yk-yk_1)/T;

ak=(vk-vk_1)/T;

Du=(Ki*ek-Kp*vk-Kd*ak)*T;

uk=uk_1+Du;

else

if k>=1 vk=(yk-yk_1)/T; end

if k>=2 ak=(vk-vk_1)/T; end

uk=x(8); % Use manual uk value x(8)

k=k+1;

end

end

if uk>5 uk=5;end % uk must be <= upper limit

if uk<-5 uk=-5;end % uk must be >= lower limit



% Show the control results and update t1, yk_1, vk_1, uk_1

fprintf('time=%5.2f setpoint=%5.2f pv=%5.2f co=%5.2f\n',t,x(2),yk,uk)

t1=t;yk_1=yk;vk_1=vk;uk_1=uk;

end

% End of the PID controller

u=uk;


The filter part is scaned at a very high frequency (i.e., every T0 seconds the filter will be executed once and T0 is very small). The PID controller is executed every Tpid=1 seconds.

Please note that the filter design and PID controller design are separated. When you tune this PID controller, the filter should be considered as a part of the process. So strictly speaking each time when you make changes to the filter, you should re-tune your PID parameters.

1 comment:

Cyberlife said...

i donate u 3 kliks.. do the same for me..
makasih yah