function varargout = EKFParam(varargin)
% EKFParam
%   EKFParam opens up a window to enter the EKF parameters

%   Project: IMPACT
%   Title of Code: EKFParam
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EKFParam_OpeningFcn, ...
                   'gui_OutputFcn',  @EKFParam_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before EKFParam is made visible.
function EKFParam_OpeningFcn(hObject, eventdata, handles, varargin)
addIMPACTIcon(hObject);

if nargin>3
    handles.data = varargin{1};
    handles.hParent = varargin{2};
    handles.stType = varargin{3};
    zFields = fieldnames(handles.data);
    for iCont = 1:numel(zFields)
        eval(sprintf('set(handles.edit_%s,''String'',%g)',zFields{iCont},handles.data.(zFields{iCont})));
    end
end



% Choose default command line output for EKFParam
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes EKFParam wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = EKFParam_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;



function edit_sampleTime_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;0 0])
    errordlgMLS('The value for the sample time is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_sampleTime_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_procCovariance1_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;0 0])
    errordlgMLS('The value for the process covariance Fexc is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_procCovariance1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_procCovariance2_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;0 0])
    errordlgMLS('The value for the process covariance Fexc'' is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_procCovariance2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_procCovariance3_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;0 0])
    errordlgMLS('The value for the covariance for frequency is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_procCovariance3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_measCovariance_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;0 0])
    errordlgMLS('The value for the measured covariance is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_measCovariance_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_upperFreq_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;1 0])
    errordlgMLS('The value for the upper frequency is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_upperFreq_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_ok.
function pushbutton_ok_Callback(hObject, eventdata, handles)
edits = {'sampleTime','procCovariance1','procCovariance2','procCovariance3','measCovariance','upperFreq','procCovariance1_0','procCovariance2_0','procCovariance3_0','freq_0','Fexc_0'};

for iCont =1:numel(edits)
    eval(sprintf('EKF.%s = str2double(get(handles.edit_%s,''String''));',edits{iCont},edits{iCont}));
end
hParentHndls= guidata(handles.hParent);
hParentHndls.Design.(sprintf('EKF%s',handles.stType)) = EKF;
guidata(handles.hParent,hParentHndls);
close(ancestor(hObject,'figure'));


% --- Executes on button press in pushbutton_cancel.
function pushbutton_cancel_Callback(hObject, eventdata, handles)
close(ancestor(hObject,'figure'));



function edit_procCovariance1_0_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;1 0])
    errordlgMLS('The value for the initial process covariance Fexc is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_procCovariance1_0_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_procCovariance2_0_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;1 0])
    errordlgMLS('The value for the initial process covariance Fexc'' is not correct');
end

% --- Executes during object creation, after setting all properties.
function edit_procCovariance2_0_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_procCovariance3_0_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;1 0])
    errordlgMLS('The value for the initial frequency covariance is not correct');
end

% --- Executes during object creation, after setting all properties.
function edit_procCovariance3_0_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_Fexc_0_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;1 0])
    errordlgMLS('The value for initial force is not correct');
end

% --- Executes during object creation, after setting all properties.
function edit_Fexc_0_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_freq_0_Callback(hObject, eventdata, handles)
if ~isFiniteNum(getValue(hObject),[0 inf;1 0])
    errordlgMLS('The value for the initial frequency is not correct');
end


% --- Executes during object creation, after setting all properties.
function edit_freq_0_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function value = getValue(hObject)
zVal = get(hObject,'String');
value = str2double(zVal);
