% Process Simulator

% This file simulates a control room display for a single feedback loop in chemical process
% control.  A controlled variable is influenced by disturbance and manipulated input variables
% in a process.  The process maay be operated open-loop, or under automatic control.

global timenow timeinc sp dv mv cv
global h_fig_dist                   % from f_dist
global h_fig_moni  h_ax_cv  h_ax_mv  h_ax_dv  is_run is_exit   % from f_moni


% set initial values of global variables before considering the actual process
sp = 50; % set point
dv = 50; % disturbance variable
mv = 50; % manipulated variable
cv = 50; % controlled variable

% set up time scale based on process
% (may want to make these user-adjustable)
numinc = 200; % number of increments for smoothness
timescale = 100; % time units in display
timeinc = timescale/numinc; % the time step for changes in value and controller computations
% delayindexd = round(thetad/timeinc); %index to set back for time delay
% delayindexp = round(thetap/timeinc); 

% build the graphical user interface boxes
f_dist('initialize');
f_moni('initialize');
disp 'boxes initialized; waiting for input'


% set up vectors to hold signals
time = timeinc * linspace (-numinc+1, 0, numinc);
disturbance = linspace (dv, dv, numinc);
setpt = linspace (sp, sp, numinc);
output = linspace (mv, mv, numinc);
contvar = linspace (cv, cv, numinc);


% outer loop for process in stopped state
junk = 1;

while is_exit == 'no'
    % wait around for input
    pause (.3)
    junk = junk * 1;
    
    % inner loop for running the process
    while is_run == 'ys'  % do the simulation
		time = time + timeinc; % increment every element of time array
        timenow = time(numinc); % current time

        % advance the set point and manipulated variable arrays
        setpt(1:numinc-1) = setpt(2:numinc);
        output(1:numinc-1) = output(2:numinc);
        % get set point and valve output, whether manual or automatic
        % ********************************
        % here is where you can write in changes to the set point (sp) and 
        % manipulated variable (mv).  They can be simple constants, representing 
        % a step when the simulation starts at time zero, or they can be a function
        % of time (timenow) or any other way you wish to manipulate them.
        % when you write the controller, the manipulated variable will be computed
        % from the set point, controlled variable, and controller parameters.
        % sp = set_point_new  , for example
        % mv = mv_new , for example
        % *********************************8
        setpt(numinc) = sp;   % update with most recent value
        output(numinc) = mv;   % update with most recent value
    
        % advance the disturbance variable array
        disturbance(1:numinc-1)=disturbance(2:numinc); % shift all elements back by one
		% get disturbance
        f_dist('update') ; % query the disturbance box
        disturbance(numinc) = dv; % update with most recent value
        
    
        % advance the controlled variable array
        contvar(1:numinc-1) = contvar(2:numinc);
        % get controlled variable from the disturbance and manip variables
        % ********************************
        % here is where you can write in the dependence of controlled variable (cv)
        % on the disturbance (dv) and manipulated (mv) variables.  THis is your process,
        % and the dependence of the process on dv and mv is the same whether mv was a manual
        % setting, or came from the controller.
        % cv = gain * mv  , for example, a pure gain process depending only on mv
        % ************************************
        % update controlled variable
        contvar(numinc) = cv;
        
        % plot the results
        figure(h_fig_moni) % make monitor current

		set(h_fig_moni,'currentaxes',h_ax_cv)
		axis ([time(1) time(numinc) -5 105])
		v = axis;
		plot (time, contvar, time, setpt, ':')
		title('controlled variable')
		axis (v)
		
		set(h_fig_moni,'currentaxes',h_ax_mv)
		axis ([time(1) time(numinc) -5 105])
		plot (time, output)
		title('manipulated variable')
		axis (v)
		
		set(h_fig_moni,'currentaxes',h_ax_dv)
		axis ([time(1) time(numinc) -5 105])
		plot (time, disturbance)
		title('disturbance variable')
		axis (v)
		
		pause (.3)
    
        
    end % while loop for running
    
end % while loop for selecting process
disp 'simulator all done'
close all
% be sure user wants to exit
% write data to initialization file
