level.m
来自「Matlab学习课件」· M 代码 · 共 127 行
M
127 行
function level
% Level control task
procglbl % Global definitions
global slevel_next
% These aren't really global, but do have to be 'static'
% Matlab doesn't have a 'static' declaration so this will have to do!
if Slevel == 0
% Initialization section - occurs once only
Slevel = 1; % Make sure this section is not executed again!
slevel_next = 1; % First state.
%The default is -1 which indicates stay in same state
return;
end
% This task could be run either as timed or continuous. As a timed task it
% saves some computing time if it needn't run all the time
if tstep < Tlevel_next
return;
end
Tlevel_next = Tlevel_next + Tlevel; % Time for next run
if slevel_next ~= -1
% There has been a state change (including self-transition)
[i_audit,trans_trail]=audit(3,Slevel,slevel_next,tstep,trans_trail,i_audit); % Record this transition
Slevel = slevel_next;
run_entry = 1;
slevel_next = -1; % Default - stay in same state
else
run_entry = 0;
end
if Slevel == 1 % Monitor
% Figure out which tank needs service
% Needing service is determined by the tank with the lowest level
% Service is needed if the lowest level is less than (high + low)/2
% No entry section
% No action section
% Test/exit section
imin = 1; hmin = h1;
if h2 < hmin, imin = 2; hmin = h2; end
if h3 < hmin, imin = 3; hmin = h3; end
if hmin < (hhigh + hlow) / 2
% A tank needs filling
if imin == 1, slevel_next = 2; % to Move1
elseif imin == 2, slevel_next = 4; % to Move2
elseif imin == 3, slevel_next = 6; % to Move3
end
end
elseif Slevel == 2 % Move1
if run_entry
Xnext_car = xt1; % Set new carriage position for tank1
end
% No action section
% Test/exit section
if abs(Xset_car - xt1) < 0.1
% OK to start puming
slevel_next = 3;
end
elseif Slevel == 3 % Pump1
if run_entry
flow_a = pa1set * flowmax;
flow_b = (1 - pa1set) * flowmax;
end
% No action section
% Test/exit section
% In this implementation it is easier to eliminate the Wait_Pump
% state and include that test here
if h1 >= hhigh
flow_a = 0; % Turn pumps off
flow_b = 0;
slevel_next = 1; % to Monitor
end
elseif Slevel == 4 % Move2
if run_entry
Xnext_car = xt2; % Set new carriage position for tank2
end
% No action section
% Test/exit section
if abs(Xset_car - xt2) < 0.1
% OK to start puming
slevel_next = 5;
end
elseif Slevel == 5 % Pump2
if run_entry
flow_a = pa2set * flowmax;
flow_b = (1 - pa2set) * flowmax;
end
% No action section
% Test/exit section
% In this implementation it is easier to eliminate the Wait_Pump
% state and include that test here
if h2 >= hhigh
flow_a = 0; % Turn pumps off
flow_b = 0;
slevel_next = 1; % to Monitor
end
elseif Slevel == 6 % Move3
if run_entry
Xnext_car = xt3; % Set new carriage position for tank3
end
% No action section
% Test/exit section
if abs(Xset_car - xt3) < 0.1
% OK to start puming
slevel_next = 7;
end
elseif Slevel == 7 % Pump3
if run_entry
flow_a = pa3set * flowmax;
flow_b = (1 - pa3set) * flowmax;
end
% No action section
% Test/exit section
% In this implementation it is easier to eliminate the Wait_Pump
% state and include that test here
if h3 >= hhigh
flow_a = 0; % Turn pumps off
flow_b = 0;
slevel_next = 1; % to Monitor
end
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?