Peter Nachtwey
Member
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.
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")
Last edited: