% drawLoop Creates graphis
%   drawLoop creates the graphics for the displar of  the control loop

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


% handles.graphic is the cell array for all graphic objects, where
% handles.graphic{1:5} represent the cell arrays for lines, rectangular, circle,
% arrow and text respectively, as defined as follows:
%-----------
rx=1; ry=23.07; th=linspace(0,pi/2,50);
yL = 400; yM = 500; yH = 600;                % "railing" levels for inputs
oL = 400; oM = 500; oH = 600;               % "railing" levels for outputs
px1 = 60; px2 = 83;                  % left and right x-locations of plant
py1 = 300; py2 = 700;                % bottom and top y-locations of plant
sx1 = 20; sx2 = 47; sx3 = 30;              % x-locations of summing blocks
bx1 = 35; bx2 = 45;          % x-locations of C1-4 blocks, and blade block
w = 3.5; w1 = 5; w2 = 5;                      % widths of certain blocks
clx1 = 40; clx2 = 40;                               % x-locations of CL1-2
glx1 = 54; glx2 = 54;                       % x-locations of Gtar and Gdtr
xf = 1;
HAL = 'HorizontalAlignment';
%% 1. Lines
% u1 row
handles.graphic{1}{1}=line([15 sx1-1],[yH yH]);
handles.graphic{1}{2}=line([sx1+1 bx1-w1],[yH yH]);
handles.graphic{1}{3}=line([bx1+w1 sx2-1],[yH yH]);
handles.graphic{1}{4}=line([sx2+1 px1],[yH yH]);
handles.graphic{1}{5}=[];

% plant box
handles.graphic{1}{6}=[line([px1 px1],[py1+ry py2-ry]) line([px1+rx-rx*cos(th) px1+rx+0.1],[py1+ry-ry*sin(th) py1])];
handles.graphic{1}{7}=[line([px1+rx px2-rx],[py2 py2]) line(px1+rx-rx*cos(th),py2-ry+ry*sin(th))];
handles.graphic{1}{8}=[line([px2-rx px1+rx],[py1 py1]) line(px2-rx+rx*cos(th),py1+ry-ry*sin(th))];
handles.graphic{1}{9}=[line([px2 px2],[py2-ry py1+ry]) line([px2-rx+rx*cos(th) px2-rx-0.1],[py2-ry+ry*sin(th) py2])];

% Closed loop
handles.graphic{1}{10}=line([px2 px2+2*xf],[oM oM]);
handles.graphic{1}{11}=line([px2+2*xf clx1+w],[py2+2*xf*ry py2+2*xf*ry]);
handles.graphic{1}{12}=line([px2+2*xf px2+2*xf],[oM py2+2*xf*ry]);
handles.graphic{1}{13}=line([px2+2*xf sx1],[py2+2*xf*ry py2+2*xf*ry]);
handles.graphic{1}{14}=line([sx1 sx1],[py2+2*xf*ry yH+ry]);

handles.graphic{1}{15}=line([px2 100],[oM oM]);
handles.graphic{1}{16}=line([px2 100],[oL oL]);
handles.graphic{1}{17}=line([px2 100],[oH oH]);


% CL1
handles.graphic{1}{18}=line([sx1 clx1-w],[py2+2*xf*ry py2+2*xf*ry]);
handles.graphic{1}{19}=line([sx2 sx2],[yH-ry yH-ry-100]);

%% 2. Blocks
% the length is 7 for the rectangular in x-axes,width 80 in y-axes
% C1, PTO
handles.graphic{2}{1}=rect(bx1-w1,yH,2*w1);
handles.graphic{2}{2}=rect(px1,yH,2*w);


%% 3. Circles
% the length is 1 for the circle in x-axes
% u1
th=linspace(0,2*pi,200);
handles.graphic{3}{1}=line(sx1+rx*sin(th),yH+ry*cos(th));
handles.graphic{3}{2}=line(sx2+rx*sin(th),yH+ry*cos(th));

%% 4. Arrows
% u1
handles.graphic{4}{1}=arrow(sx1-1,yH,1);
handles.graphic{4}{2}=arrow(sx2-1,yH,1);
handles.graphic{4}{3}=arrow(px1,yH,1);
handles.graphic{4}{4}=arrow(sx1,yH+ry,4);
handles.graphic{4}{5}=arrow(100,oM,1);
handles.graphic{4}{6}=arrow(100,oL,1);
handles.graphic{4}{7}=arrow(100,oH,1);
handles.graphic{4}{8}=arrow(sx2,yH-ry,2);


%% 5. Texts
handles.graphic{5}{1}=text(12.5,yH,'u1');
handles.graphic{5}{2}=text(bx1,yH,'C1',HAL,'center');
handles.graphic{5}{3}=text(px1+w,yH,'PTO',HAL,'center');

handles.graphic{5}{4}=text((px1+px2)/2,675,'WEC',...
    HAL,'center','FontWeight','bold','FontSize',11);
handles.graphic{5}{5}=text(sx1+0.5,yH+70,'\_');
handles.graphic{5}{6}=text(bx1+w1,yH+60,sprintf('Force\ndemand'));
handles.graphic{5}{7}=text(px2-1,oM,'PTO vel.',HAL,'Right');
handles.graphic{5}{8}=text(px2-1,oL,'Floater vel.',HAL,'Right');
handles.graphic{5}{9}=text(px2-1,oM,'Relative vel.',HAL,'Right');
handles.graphic{5}{10}=text(px2-1,oH,'Spar vel.',HAL,'Right');
handles.graphic{5}{11}=text(sx2,yH-ry-150,sprintf('Hydrod.\nForces'),HAL,'Center');
handles.graphic{5}{12}=text(px2-1,oL,'Floater vel. X',HAL,'Right');
handles.graphic{5}{13}=text(px2-1,oM,'Relative vel. X',HAL,'Right');
handles.graphic{5}{14}=text(px2-1,oH,'Spar vel. X',HAL,'Right');
handles.graphic{5}{15}=text(bx1,yH,'C2',HAL,'center');
handles.graphic{5}{16}=text(12.5,yH,'u2');

guidata( hObject, handles );
%----------
for i=1:5,     set([handles.graphic{i}{:}],'Visible','Off'); end
for i=[1:3 5], set([handles.graphic{i}{:}],'Color','k');     end

hInitialPath=[handles.graphic{1}{[1:10,15,19]} ...
    handles.graphic{2}{[1 2]} ...
    handles.graphic{3}{[1 2]} ...
    handles.graphic{4}{[1:3,5,8]}  ...
    handles.graphic{5}{[1:4,6,7,11]}];

set(hInitialPath,'Visible','On');

axis off;
set(gcf, 'Visible', 'on');


