%
% This example shows how a Work Envelope image can be used as a
% background for robot arm's Animation
%
%

close all; clear; clc;
global debugLevel_01; debugLevel_01 = 0;

% % % % N O T E :
% % % % debugLevel_01 = 1 % PRINTS DEBUGGING INFORMATION


global L1;            L1 = 110.0; % Link_1 Length in mm
global L2;            L2 = 140.0; % Link_2 Length in mm
global SUM_L1_L2;     SUM_L1_L2 = L1 + L2;


initalizeArmCoordinates;
printArmCoordinates;

hf = figure('color','white');
% maximize(hf);
axis equal

maximum_x_limit =   ceil(SUM_L1_L2/100)*100;
maximum_y_limit =   ceil(SUM_L1_L2/100)*100;
minimum_x_limit = - ceil(SUM_L1_L2/100)*100;
minimum_y_limit = - ceil(SUM_L1_L2/100)*100;

bk_image = imread('background_c.bmp');
bk_image = flipdim(bk_image,1);
imagesc([minimum_x_limit, maximum_x_limit], [minimum_y_limit, maximum_y_limit], bk_image);

% % % %  Experiment: 0     Please note that:
clear bk_image % Clear the large sized variable which is not to be used further in the code.
               % Try the command size(bk_image) before you clear it and see
               % its a variable of size 600 x 600 x 3  Datatype: uint8
               % 1080000 bytes = 1055 KByte = 1.03 MB
               % Have you ever declared a variable in C-Language of 1MB ?
               % and left it unused.

set(gca,'ydir','normal');  % set the y-axis back to normal.

xlim([minimum_x_limit maximum_x_limit]); % Set X-axis range
ylim([minimum_y_limit maximum_y_limit]); % Set Y-axis range

grid on
set(gca, 'GridLineStyle', '-');

% % % %  Experiment: 1 : Run the code with and without
% % % %  the below commented line:
%  grid(gca,'minor') % Turns ON minor grid

title('Experiment: on setting a background image in an animation');

hold on % causes subsequent plotting commands to add to what's already in the figure, instead of replacing it.

 % Draw the arm once and handle for further use
 ht(1) = plot(lx(1:2), ly(1:2), 'color', [.4 .4 .8],'LineWidth',3);
 ht(2) = plot(lx(2:3), ly(2:3), 'color', [.8 .4 .8],'LineWidth',3);
 ht(3) = plot(lx(1),ly(1),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',6);
 ht(4) = plot(lx(2),ly(2),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[.99 1 .63], 'MarkerSize',6);
 ht(5) = plot(lx(3),ly(3),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 0 .63], 'MarkerSize',6);


% % % %  Experiment: 2 :
% % % %  SOURCE: HELP : web([docroot '/matlab/ref/axes.html'])
% % % %
% % % %  Setting the PlotBoxAspectRatio disables
% % % %  stretch-to-fill behavior. By default, MATLAB stretches the axes to fill the axes
% % % %  position rectangle (the rectangle defined by the last two elements
% % % %  in the Position property). This results in graphs
% % % %  that use the available space in the rectangle. However, some 3-D graphs
% % % %  (such as a sphere) appear distorted because of this stretching, and
% % % %  are better viewed with a specific three-dimensional aspect ratio.
% % % %  Run the code with and without
% % % %  the below line:
 set(gca,'DataAspectRatio',[1 1 1]);


 drawnow;

 % % % %  Experiment: 3 : Read Help on : daspect([1 1 1])

 initalizeArmCoordinates_rest_horiz
% pause on;
for alpha = 1:19 % 18 + 1

    for beta = 1:19 % 18 + 1

    set(ht(1),'XData',lx(1:2));    set(ht(1),'YData',ly(1:2));
    set(ht(2),'XData',lx(2:3));    set(ht(2),'YData',ly(2:3));
    set(ht(3),'XData',lx(1));      set(ht(3),'YData',ly(1));
    set(ht(4),'XData',lx(2));      set(ht(4),'YData',ly(2));
    set(ht(5),'XData',lx(3));      set(ht(5),'YData',ly(3));
       [ successFlag, lx, ly ] = rotate_motor_logically( 2,  -10, lx, ly, debugLevel_01);
        drawnow;
        pause (0.01); % % % %  Experiment: 4 : change the value to 0.1

    end

     initalizeArmCoordinates_rest_horiz;

    [ successFlag, lx, ly ] = rotate_motor_logically( 1,  -alpha*10, lx, ly, debugLevel_01);

end

saveas(gcf,'output.jpg')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 lx[1] = 0.00      lx[2] = 110.00    lx[3] = 110.00 
 ly[1] = 0.00      ly[2] = 0.00      ly[3] = 140.00