% Copyright 1997 Thomas Mc Avoy
% MATLAB routine for entering level controller parameters, KCL and TRL
% The vector kkk gives the MV's used for level control in order, reactor,
% separator, and stripper
function [kkk,KCL,TRL]=levcon(kkk,KCL,TRL)
disp(' ')
disp('		LEVEL CONTROL TUNING')
disp(' ')
disp('MANIPULATED VARIABLES')
	disp(' ')
	disp('	1: THE D-FEED FLOW SET POINT')
	disp('	2: THE E-FEED FLOW SET POINT')
	disp('	3: THE A-FEED FLOW SET POINT') 
	disp('	4: THE C-FEED FLOW SET POINT')
	disp('	5: COMPRESSOR RECYCLE VALVE')
	disp('	6: THE PURGE FLOW SET POINT')
	disp('	7: THE SEPARATOR EXIT FLOW SET POINT')
	disp('	8: THE PRODUCT FLOW SET POINT')
	disp('	9: THE STEAM FLOW SET POINT')
	disp('	10: THE REACTOR CW TEMP SET POINT')
	disp('	11: THE CONDENSER CW TEMP SET POINT')
	disp('	12: AGITATOR SPEED')
disp(' ')
while 1
disp(' CURRENT LEVEL LOOP')
disp('CONTROLLER PARAMETERS 	        KC            TR')
for i=1:3
		if i==1
			s=sprintf('	REACTOR MV   %5.2f',kkk(i));
		elseif i==2
			s=sprintf('	SEPARATOR MV %5.2f',kkk(i));
		elseif i==3
			s=sprintf('	STRIPPER MV  %5.2f',kkk(i));
		end
		s1=sprintf('        %5.2f',KCL(i));
		s2=sprintf('  		%5.2f',TRL(i));
		disp([s,s1,s2])
end
disp(' ')
disp('HIT RETURN TO CONTINUE')
pause
disp('ENTER WHICH LEVEL LOOP IS TO BE TUNED')
disp('	1 REACTOR')
disp('	2 SEPARATOR')
disp('	3 STRIPPER')
disp('	4 FINISHED')
	temp=input(' ');
		while (isempty(temp)==1)|temp<1|temp>4
				disp('PLEASE ENTER CHOICE AGAIN ')
				disp('CHOICE NEEDS TO BE BETWEEN 1 and 4')
				temp=input(' ');
		end

	if temp==1
		tom='REACTOR LEVEL';
		iii=1;
	elseif temp==2
		tom='SEPARATOR LEVEL';
		iii=2;
	elseif temp==3
		tom='STRIPPER LEVEL';
		iii=3;
	else
		break
	end
	disp(' ')
	s='SELECT FROM LIST BELOW FOR LEVEL CONTROL MV OF ';
	disp([s,tom])
	disp(' ')
	disp('	1: THE D-FEED FLOW SET POINT')
	disp('	2: THE E-FEED FLOW SET POINT')
	disp('	3: THE A-FEED FLOW SET POINT') 
	disp('	4: THE C-FEED FLOW SET POINT')
	disp('	5: COMPRESSOR RECYCLE VALVE')
	disp('	6: THE PURGE FLOW SET POINT')
	disp('	7: THE SEPARATOR EXIT FLOW SET POINT')
	disp('	8: THE PRODUCT FLOW SET POINT')
	disp('	9: THE STEAM FLOW SET POINT')
	disp('	10: THE REACTOR CW TEMP SET POINT')
	disp('	11: THE CONDENSER CW TEMP SET POINT')
	disp('	12: AGITATOR SPEED')
	disp(' ')
		while 1
			disp(['INPUT NUMBER OF MANIP VARIABLE FOR','  ',tom])
			temp=input(' ');
			while (isempty(temp)==1)|temp<1|temp>12
				disp('PLEASE ENTER MANIP VARIABLE AGAIN')
				disp('CHOICE NEEDS TO BE BETWEEN 1 and 12')
				temp=input(' ');
			end
				kkk(iii)=temp;
			break
		end
		disp(' ')
		if kkk(iii)==1
			s=('	D-SET POINT');
		elseif kkk(iii)==2
			s=('	E-SET POINT');
		elseif kkk(iii)==3
			s=('	A-SET POINT');
		elseif kkk(iii)==4
			s=('	C-SET POINT');
		elseif kkk(iii)==5
			s=('	RECYCLE VALVE');
		elseif kkk(iii)==6
			s=('	PURGE-SET POINT');
		elseif kkk(iii)==7
			s=('	SEP EXIT-SET POINT');
		elseif kkk(iii)==8
			s=('	PRODUCT-SET POINT');
		elseif kkk(iii)==9
			s=('	STEAM-SET POINT');
		elseif kkk(iii)==10
			s=('	REACTOR CW TEMP-SET POINT');
		elseif kkk(iii)==11
			s=('	COND CW TEMP-SET POINT');
		else
		s=('	AGITATOR SPEED');
		end
		ss=['MANIPULATED VARIABLE FOR ',tom,'  - ',s];
		disp(ss)
		disp(' ')
		s=sprintf('INPUT THE CONTROLLER GAIN FOR LOOP %3.0f',kkk(iii));
		disp(s)
		temp=input(' ');
		while (isempty(temp)==1)
			disp('PLEASE ENTER KC AGAIN')
			disp(s)
			temp=input(' ');
		end
		KCL(iii)=temp;
		disp(' ')
		s=sprintf('INPUT THE RESET TIME (min) FOR LOOP %3.0f',kkk(iii));
		disp(s)
		temp=input(' ');
		while (isempty(temp)==1)
			disp('PLEASE ENTER TR AGAIN')
			disp(s)
			temp=input(' ');
		end
		TRL(iii)=temp;
		if TRL(iii)==0
			TRL(iii)=1.e6;
		end
end
if kkk(1)==kkk(2)|kkk(2)==kkk(3)|kkk(1)==kkk(3)
	disp('	YOU HAVE CHOSEN THE SAME MANIPULATED VARIABLE FOR AT LEAST')
	disp('	2 LEVEL LOOPS.  THIS ERROR NEEDS TO BE CORRECTED.  ')
	disp('	SELECT  3.  INPUT LEVEL CONTROLLER PARAMETERS AGAIN')
end


