function varargout = kontrollab(varargin)

if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'reuse');

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
	guidata(fig, handles);
	if nargout > 0
		varargout{1} = fig;
	end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		if (nargout)
			[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
		else
			feval(varargin{:}); % FEVAL switchyard
		end
	catch
		disp(lasterr);
	end

end

function model_open(handles)
% Make sure the diagram is still open
if  isempty(find_system('Name','kontroll')),
	open_system('kontroll'); open_system('kontroll/Pressurescope');open_system('kontroll/Levelscope');
	openfig('kontrollab.fig','reuse')
	

end

% --------------------------------------------------------------------
function varargout = manuallevelslider_Callback(h, eventdata, handles, varargin)

model_open(handles)

% Get the new value for the levelinput from the slider
NewVal1 = get(h,'Value');

% Set the value of the levelinputCurrentValue to the new value set by slider
set(handles.manuallevelvalue,'String',NewVal1)

% Set the value parameter of the manuallevel_input to the new value
set_param('kontroll/manuallevel_input','Value',num2str(NewVal1))


% --------------------------------------------------------------------
function varargout = manuallevelvalue_Callback(h, eventdata, handles, varargin)

model_open(handles)
% Get the new value for the manuallevelvalue
NewStrVal1 = get(h,'String');
NewVal1 = str2double(NewStrVal1);

% Check that the entered value falls within the allowable range
if  isempty(NewVal1) | (NewVal1< 0) | (NewVal1>10),
	% Revert to last value, as indicated by manuallevelslider
	OldVal1 = get(handles.manuallevelslider,'Value');
	set(h,'String',OldVal1)
	
else
	% Set the value of the manuallevelslider to the new value
	set(handles.manuallevelslider,'Value',NewVal1)
	
	% Set the value parameter of the manuallevel_input to the new value
	set_param('kontroll/manuallevel_input','Value',NewStrVal1)
end


% --------------------------------------------------------------------
function varargout = manualpressureslider_Callback(h, eventdata, handles, varargin)

model_open(handles)

NewVal = get(h,'Value');
set(handles.manualpressurevalue,'String',NewVal)
set_param('kontroll/manualpressure_input','Value',num2str(NewVal))

% --------------------------------------------------------------------
function varargout = manualpressurevalue_Callback(h, eventdata, handles, varargin)

model_open(handles)

NewStrVal = get(h,'String');
NewVal = str2double(NewStrVal);

if  isempty(NewVal) | (NewVal< 0) | (NewVal>10),
	OldVal = get(handles.manualpressureslider,'Value');
	set(h,'String',OldVal)
	
else
	set(handles.manualpressureslider,'Value',NewVal)
	set_param('kontroll/manualpressure_input','Value',NewStrVal)
end

% --------------------------------------------------------------------
function varargout = levelsetpointslider_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewVal = get(h,'Value');

set(handles.levelsetpointvalue,'String',NewVal)
set_param('kontroll/levelsetpoint','Value',num2str(NewVal))

% --------------------------------------------------------------------
function varargout = levelsetpointvalue_Callback(h, eventdata, handles, varargin)

model_open(handles)

NewStrVal = get(h,'String');
NewVal = str2double(NewStrVal);

if  isempty(NewVal) | (NewVal< 0) | (NewVal>50),
	OldVal = get(handles.levelsetpointslider,'Value');
	set(h,'String',OldVal)
	
else
	set(handles.levelsetpointslider,'Value',NewVal)
	set_param('kontroll/levelsetpoint','Value',NewStrVal)
end

% --------------------------------------------------------------------
function varargout = pressuresetpointslider_Callback(h, eventdata, handles, varargin)

model_open(handles)

NewVal = get(h,'Value');

set(handles.pressuresetpointvalue,'String',NewVal)
set_param('kontroll/pressuresetpoint','Value',num2str(NewVal))

% --------------------------------------------------------------------
function varargout = pressuresetpointvalue_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal = get(h,'String');
NewVal = str2double(NewStrVal);

if  isempty(NewVal) | (NewVal< 1) | (NewVal>3.5),
	
	OldVal = get(handles.pressuresetpointslider,'Value');
	set(h,'String',OldVal)
	
else
	set(handles.pressuresetpointslider,'Value',NewVal)
	set_param('kontroll/pressuresetpoint','Value',NewStrVal)
end

% --------------------------------------------------------------------
function varargout = KcValuePIDlevel_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal=get(h,'String');
NewVal = str2double(NewStrVal);

set(h,'String',NewVal);
set_param('kontroll/PIDlevelcontroller','Kc',NewStrVal)

% --------------------------------------------------------------------
function varargout = TiValuePIDlevel_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal=get(h,'String');
NewVal = str2double(NewStrVal);

set(h,'String',NewVal);
set_param('kontroll/PIDlevelcontroller','Ti',NewStrVal)

% --------------------------------------------------------------------
function varargout = TdValuePIDlevel_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal=get(h,'String');
NewVal = str2double(NewStrVal);

set(h,'String',NewVal);
set_param('kontroll/PIDlevelcontroller','Td',NewStrVal)

% --------------------------------------------------------------------
function varargout = KcValuePIDpressure_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal=get(h,'String');
NewVal = str2double(NewStrVal);

set(h,'String',NewVal);
set_param('kontroll/PIDpressurecontroller','Kc',NewStrVal)

% --------------------------------------------------------------------
function varargout = TiValuePIDpressure_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal=get(h,'String');
NewVal = str2double(NewStrVal);

set(h,'String',NewVal);
set_param('kontroll/PIDpressurecontroller','Ti',NewStrVal)

% --------------------------------------------------------------------
function varargout = TdValuePIDpressure_Callback(h, eventdata, handles, varargin)
model_open(handles)

NewStrVal=get(h,'String');
NewVal = str2double(NewStrVal);

set(h,'String',NewVal);
set_param('kontroll/PIDpressurecontroller','Td',NewStrVal)

% --------------------------------------------------------------------
function varargout = start_Callback(h, eventdata, handles, varargin)
model_open(handles)

%sets the last used values as default when the program starts
oldval=get_param('kontroll/manuallevel_input','value');
set(handles.manuallevelvalue,'string',str2double(oldval));
set(handles.manuallevelslider,'value',str2double(oldval));
oldval=get_param('kontroll/manualpressure_input','value');
set(handles.manualpressurevalue,'string',str2double(oldval));
set(handles.manualpressureslider,'value',str2double(oldval));

oldval=get_param('kontroll/pressuresetpoint','value');
set(handles.pressuresetpointvalue,'string',str2double(oldval));
set(handles.pressuresetpointslider,'value',str2double(oldval));

oldval=get_param('kontroll/levelsetpoint','value');
set(handles.levelsetpointvalue,'string',str2double(oldval));
set(handles.levelsetpointslider,'value',str2double(oldval));

%sets the last used PID parametres as default when the program starts.
oldval=get_param('kontroll/PIDpressurecontroller','Kc');
set(handles.KcValuePIDpressure,'string',str2double(oldval));
oldval=get_param('kontroll/PIDpressurecontroller','Ti');
set(handles.TiValuePIDpressure,'string',str2double(oldval));
oldval=get_param('kontroll/PIDpressurecontroller','Td');
set(handles.TdValuePIDpressure,'string',str2double(oldval));


oldval=get_param('kontroll/PIDlevelcontroller','Kc');
set(handles.KcValuePIDlevel,'string',str2double(oldval));
oldval=get_param('kontroll/PIDlevelcontroller','Ti');
set(handles.TiValuePIDlevel,'string',str2double(oldval));
oldval=get_param('kontroll/PIDlevelcontroller','Td');
set(handles.TdValuePIDlevel,'string',str2double(oldval));

%sets the controllers to manual and the simulink ready to start
set_param('kontroll/Constant1','Value',num2str(0))
set_param('kontroll/Constant2','Value',num2str(1))
set_param('kontroll/Constant3','Value',num2str(2))

%sets the log to not start logging
off = [handles.startlog];
mutual_exclude(off);
set(handles.stoplog,'Value',1);

set_param('kontroll/Constant4','Value',num2str(1));
set_param('kontroll/To File','Filename','tankvar.mat');

%sets the controller to manual at start
set(handles.Manuallevelbutton,'Value',1)
set(handles.Manualpressurebutton,'Value',1)

%starts the simulation
%sim('kontroll')

% --------------------------------------------------------------------
function varargout = startlog_Callback(h, eventdata, handles, varargin)
model_open(handles)
off = [handles.stoplog];
mutual_exclude(off);

set_param('kontroll/Constant4','Value',num2str(2));

% --------------------------------------------------------------------
function varargout = stoplog_Callback(h, eventdata, handles, varargin)
model_open(handles)
off = [handles.startlog];
mutual_exclude(off);

set_param('kontroll/Constant4','Value',num2str(1));

% --------------------------------------------------------------------
function varargout = quit_Callback(h, eventdata, handles, varargin)
model_open(handles);

pos_size = get(handles.figure1,'Position');
user_response = modaldlg([pos_size(1)+pos_size(3)/5 pos_size(2)+pos_size(4)/5]);
switch user_response
case {'no','cancel'}
	% take no action
case 'yes'
	% Prepare to close GUI application window
	%                  .
	%sets the controllers to manual and the valves to open
    set_param('kontroll/Constant3','Value',num2str(2))
    set_param('kontroll/Constant2','Value',num2str(1))
    set_param('kontroll/manualpressure_input','Value',num2str(10))
    set_param('kontroll/manuallevel_input','Value',num2str(10))
    pause(0.05);
%stops the simulation
    set_param('kontroll/Constant1','Value',num2str(1))
    pause(0.05);
    save_system('kontroll')
    pause(0.05);
    
    delete(handles.figure1)
    close_system('kontroll')
end
%-------------------------------------------------------------------
function varargout = figure1_CloseRequestFcn(h, eventdata, handles, varargin)
quit_Callback(h,eventdata,handles)

% --------------------------------------------------------------------
function varargout = Manuallevelbutton_Callback(h, eventdata, handles, varargin)

model_open(handles)
off = [handles.PIDlevelbutton];
mutual_exclude(off)

set_param('kontroll/Constant2','Value',num2str(1))
set_param('kontroll/PIDlevelcontroller/Constant1','Value',num2str(0));

% --------------------------------------------------------------------
function varargout = PIDlevelbutton_Callback(h, eventdata, handles, varargin)
model_open(handles)

off = [handles.Manuallevelbutton];
mutual_exclude(off)
set_param('kontroll/Constant2','Value',num2str(2))
%resets the integrator for the PIDcontroller
set_param('kontroll/PIDlevelcontroller/Constant1','Value',num2str(1));

%-----------------------------------------------------------------------
function mutual_exclude(off)
set(off,'Value',0)

% --------------------------------------------------------------------
function varargout = Manualpressurebutton_Callback(h, eventdata, handles, varargin)
model_open(handles)

off = [handles.PIDpressurebutton];
mutual_exclude(off)

%switches to manual control
set_param('kontroll/Constant3','Value',num2str(2))
set_param('kontroll/PIDpressurecontroller/Constant1','Value',num2str(0));

% --------------------------------------------------------------------
function varargout = PIDpressurebutton_Callback(h, eventdata, handles, varargin)

model_open(handles)
off = [handles.Manualpressurebutton];
mutual_exclude(off)

%Swithces to automatic mode
set_param('kontroll/Constant3','Value',num2str(1))
set_param('kontroll/PIDpressurecontroller/Constant1','Value',num2str(1));


% --------------------------------------------------------------------
function varargout = stopsimulation_Callback(h, eventdata, handles, varargin)
set_param('kontroll/Constant1','Value',num2str(1))

% --------------------------------------------------------------------
function varargout = figure1_ResizeFcn(h, eventdata, handles, varargin)
% % 