PID block algorithm in Simatic ?

Then it is back to the basics

What ever happened to using the basics? This is how I solved these kinds of problems back in college ( 30+ years ago ) before I learned about state space and the z domain. Simple differential equations still work well. Notice the response is the same as my PID response in Mathcad.

Lurkers can cut an paste the program below into their Scilab and run it. This should work with Matlab too with little if any modification because I tried not to use any fancy functions.

Code:
// TANK LEVEL CONTROL
Kp=-0.2368;				 // pump gain.  (m^3/m)/CO% 
Tp=1/12;					// plump time constant. minutes
Kt=0.0279;				  // 1/(tank surface area)   1/m^2
Tc=0.25;					// closed loop time constant. minutes
Kc=-605.444;				// controller Gain. percent output per meter of error
Ti=3*Tc;					// integrator time constant   minutes
T=1/120;					// simulation update period  minutes
PV(1)=0;					// fluid level  meters
CO(1)=0;					// control output. percent of full output
ui(1) = 0;				  // integrator contribution to CO
flow_in=20;				 // flow in.  m^3/min
flow_out(1)=0;			  // flow pumped out.  m^3/min

N=round(10/T);			  // CONVERT TO SAMPLE TIMES

for n=1:N;				  // simulate 10 minutes	   
  SP(n)=2;				  // set point  meters
  
  // calculate the rate of change in flow_out.  This uses the simple differential
  // equation Tp*flow_out_dot+flow_outy=Kp*CO(n).  Note flow_out is negative.
  flow_out_dot(n) = -flow_out(n)/Tp + (Kp/Tp)*CO(n);
  // calculate the new flow out by adding the rate of change times T
  flow_out(n+1) = flow_out(n) + flow_out_dot(n)*T;
  // integrate the difference between the flow in and flow out.
  PV(n+1) = PV(n) + Kt*(flow_in+flow_out(n))*T;
  err(n) =  SP(n) - PV(n);  // calculate error
  up = Kc*err(n);		   // calculate the proportional control output percent
  ui = ui + (Kc*T/Ti)*err(n); // calculate the integrator control output percent
  if ui > (100-up) then	 // limit the integrator contribution to the control output
	ui = 100-up;
  elseif ui < 0-up then
	ui = 0-up;
  end
  CO(n+1) = ui + up;		// the control output is the sum of the integrator and proportional terms
 end
 PV($)=[];				  // LIMIT TO 1200 ITEMS
 CO($)=[];
 
// PLOT THE SIMULATION
// CREATE TO PLOTS USING THE SUBPLOTS FUNCTION
// THE TOP PLOT SHOWS THE SP AND PV. THE BOTTOM PLOT SHOWS THE CO 

t=T:T:N*T;		// COMPUTE TIME INDEXES. START A TIME T IN INCREMENTS OF T TO N*T
clf();			// CLEAR OR RESET THE CURRENT GRAPHICS FIGURE

subplot(2,1,1);   // PLOT TEMPERATURES ON THE TOP PLOT
plot(t,[SP PV]);
xtitle('Tank Level Control Simulation','Time In Minutes','Tank Level');
legend("SP","PV");
subplot(2,1,2);   // PLOT CONTROL OUTPUT ON THE BOTTOM PLOT

// PLOT THE CONTROL OUTPUT TERMS

plot(t,[CO]);
xtitle('Tank Level Control Simulation','Time In Minutes','Control Output%');
legend("CO")

TankLevel.PNG
 
Last edited:
Of course, it worked in Matlab-
Here's Matlab code (almost without modifications):
Code:
%TANK LEVEL CONTROL
clear all
clc

Kp=-0.2368;				 % pump gain.  (m^3/m)/CO% 
Tp=1/12;					% plump time constant. minutes
Kt=0.0279;				  % 1/(tank surface area)   1/m^2
Tc=0.25;					% closed loop time constant. minutes
Kc=-605.444;				% controller Gain. percent output per meter of error
Ti=3*Tc;					% integrator time constant   minutes
T=1/120;					% simulation update period  minutes
PV(1)=0;					% fluid level  meters
CO(1)=0;					% control output. percent of full output
ui(1) = 0;				  % integrator contribution to CO
flow_in=20;				 % flow in.  m^3/min
flow_out(1)=0;			  % flow pumped out.  m^3/min

N=round(10/T);			  % CONVERT TO SAMPLE TIMES

for n=1:N;				  % simulate 10 minutes	   
  SP(n)=2;				  % set point  meters
  
  % calculate the rate of change in flow_out.  This uses the simple differential
  % equation Tp*flow_out_dot+flow_outy=Kp*CO(n).  Note flow_out is negative.
  flow_out_dot(n) = -flow_out(n)/Tp + (Kp/Tp)*CO(n);
  % calculate the new flow out by adding the rate of change times T
  flow_out(n+1) = flow_out(n) + flow_out_dot(n)*T;
  % integrate the difference between the flow in and flow out.
  PV(n+1) = PV(n) + Kt*(flow_in+flow_out(n))*T;
  err(n) =  SP(n) - PV(n);  % calculate error
  up = Kc*err(n);		   % calculate the proportional control output percent
  ui = ui + (Kc*T/Ti)*err(n); % calculate the integrator control output percent
  if ui > (100-up) 	 % limit the integrator contribution to the control output
	ui = 100-up;
  elseif ui < 0-up
	ui = 0-up;
  end
  CO(n+1) = ui + up;		% the control output is the sum of the integrator and proportional terms
 end
 PV=PV(1:length(PV)-1);				  % LIMIT TO 1200 ITEMS
 CO=CO(1:length(CO)-1);
 
% PLOT THE SIMULATION
% CREATE TO PLOTS USING THE SUBPLOTS FUNCTION
% THE TOP PLOT SHOWS THE SP AND PV. THE BOTTOM PLOT SHOWS THE CO 

t=T:T:N*T;		% COMPUTE TIME INDEXES. START A TIME T IN INCREMENTS OF T TO N*T
clf();			% CLEAR OR RESET THE CURRENT GRAPHICS FIGURE

subplot(2,1,1);   % PLOT TEMPERATURES ON THE TOP PLOT
plot(t, SP,'Linewidth', 2);
hold on
plot(t, PV, 'r','LineWidth', 2);
title('Tank Level Control Simulation'),xlabel('Time In Minutes'),ylabel('Tank Level');
legend('SP','PV');
subplot(2,1,2);   % PLOT CONTROL OUTPUT ON THE BOTTOM PLOT

% PLOT THE CONTROL OUTPUT TERMS

plot(t,[CO],'g','LineWidth', 2);
title('Tank Level Control Simulation'),xlabel('Time In Minutes'),ylabel('Control Output%');
legend('CO')

Response is naturally same as yours.
Yes, after I make windup correction I will rest.
I must spend some time trying to figure out exactly why I had problems with transfer functions blocks in Simulin and clasical PID block. I must figure that out also.

Thank you very much "teacher" :)
 
Last edited:
Hi all! I am new to PID and I need help. Can somebody post here an example of usage Simatic PID with two discrete outputs? Is it difficult to program own PID algorithm (with discrete outputs)? Are there any examples?
Thanks.
 

Similar Topics

HI.I REVIEWED A PROJECT DONE. THIS FB41 PID BLOCK WAS USED FOR THE PASTEURIZATION PROCESS. THE PROCESS I CAN'T UNDERSTAND IS THE ACTION. WHILE THE...
Replies
2
Views
1,117
Hello, I have a PID block in my program controlling pump speed in order to get a correct Differential pressure. The way I have it set up is the...
Replies
3
Views
1,271
Is there a way to create proportional logic for a control valve without using a PID function? I want to create a simple P controller without...
Replies
8
Views
2,628
So I have a PID block on CCW set up to control a motor. I have my variables input and the absolute error is changing but the CV is always 0. There...
Replies
1
Views
1,621
I want to change the setpoint of PID block in Codesys for every 1 hour. like that i have to give 48 setpoints for 48 hours. So how can I give 48...
Replies
2
Views
1,529
Back
Top Bottom