% graph_com Plots control loop
%   graph_com(handles) updates the graphics of the control loop

%   Project: IMPACT
%   Title of Code: graph_com
%   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.


function graph_com( handles )
% used to update the diagram graph for controller_design

if handles.wtConfig == 0
    errordlgMLS('Please load a wind turbine model');
    return;
end


% erase all blocks in the diagram
for i=1:5, set([handles.graphic{i}{:}],'Visible','Off'); end
for i=[1:3 5]
    for j=1:numel(handles.graphic{i})
        set([handles.graphic{i}{j}],'Color','k');
    end
end

% get input names
inputStr = get(handles.listbox1, 'String');
inputVal = get(handles.listbox1, 'Value');
var1 = inputStr{inputVal};

h = findobj('tag', 'controller_design');
set(0,'CurrentFigure',h)

switch var1
    case 'u1'
        input_path=[handles.graphic{1}{[1:9,19]} ...
                    handles.graphic{2}{[1 2 ]} ...
                    handles.graphic{3}{[1 2 ]} ...
                    handles.graphic{4}{[1:3,8]} ...
                    handles.graphic{5}{[1:4,6,11]}];
     case 'u2'
        input_path=[handles.graphic{1}{[1:9,19]} ...
                    handles.graphic{2}{[1 2 ]} ...
                    handles.graphic{3}{[1 2 ]} ...
                    handles.graphic{4}{[1:3,8]} ...
                    handles.graphic{5}{[3:4,6,11,15,16]}];
        
    case 'PTO Force Demand'
        input_path=[handles.graphic{1}{[3:9,19]} ...
                    handles.graphic{2}{2} ...
                    handles.graphic{3}{2} ...
                    handles.graphic{4}{[2:3,8]} ...
                    handles.graphic{5}{[3:4,6,11]}];
end

% get output names
outputStr = get(handles.listbox2, 'String');
outputVal = get(handles.listbox2, 'Value');
var2 = outputStr{outputVal};
switch var2
    case 'Relative velocity'
        output_path = [handles.graphic{1}{[15]},...
            handles.graphic{4}{[5]},...
            handles.graphic{5}{7}];
    case 'Floater velocity'
        output_path = [handles.graphic{1}{[16]},...
            handles.graphic{4}{[6]},...
            handles.graphic{5}{8}];
    case 'Spar velocity'
        output_path = [handles.graphic{1}{[17]},...
            handles.graphic{4}{[7]},...
            handles.graphic{5}{10}];
        
    case 'Relative velocity X'
        output_path = [handles.graphic{1}{[15]},...
            handles.graphic{4}{[5]},...
            handles.graphic{5}{13}];
    case 'Floater velocity X'
        output_path = [handles.graphic{1}{[16]},...
            handles.graphic{4}{[6]},...
            handles.graphic{5}{12}];
    case 'Spar velocity X'
        output_path = [handles.graphic{1}{[17]},...
            handles.graphic{4}{[7]},...
            handles.graphic{5}{14}];
end


% get mode from radiobuttons
if ( get( handles.radiobutton_CL, 'Value' ) == 1 )
    mode = 'cl'; % mode = close loop
elseif ( get( handles.radiobutton_OL, 'Value' ) == 1 )
    mode = 'ol'; % mode = open loop
end

switch mode
    case 'cl'
        mode_path = [handles.graphic{1}{[11:14]}, ...
                     handles.graphic{4}{[4]},...
                     handles.graphic{5}{[5]}];
    case 'ol'
        mode_path = [];
end


diagram = [input_path output_path mode_path];
set(diagram, 'Visible', 'On' );
guidata(handles.listbox1, handles);
