% Balayage SPESO vertical

% Updated 22/9/2015
% Vertical: ND 19/3/2015
%Updared JB,ND 24 March 2014
%Updated 16/2/2015 ND

%global SPESO_scope_url;
%SPESO_scope_url = 'http://172.17.14.16/';
SPESO_data_directory = '/home/operateur/GrpDiagnostics/matlab/SPESO/data/';
date_today = datestr(now(),'yyyy-mm-dd');
pos_hublot_Z=-98000;
pos_hublot_Y=-300000;
%pas_en_Z=8000;
%pas_en_Y=-10000;
course_en_Z=80000;
course_en_Y=750000;
course_en_X=400000;
pos_ref_rs1=44;
pos_ref_rs2=70;
pos_ref_ty=-300000;

backposTx=0;
%backposTx=-300000;

% state=7 means standby
AXIS_ON_STANDBY=7;

% state=11 means limit switch
AXIS_ON_LIMIT=11;

%Number of loop before exiting the acquisition wait loop
ACQ_WAIT_LOOPOUT=1000

N_DAYS=12

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Definition device translation Y et attributs
devRobotTransY=['LT1/DG/','ROBOT-SPESO.MT_Ts.1'];
attr_list_TransY={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

% Definition device translation Y et attributs
devRobotTransX=['LT1/DG/','ROBOT-SPESO.MT_Tx.1'];
attr_list_TransX={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

% Definition device rotation RS1 et attributs
devRobotRS1=['LT1/DG/','ROBOT-SPESO.MT_Rs.1'];
attr_list_RS1={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

% Definition device rotation RS2 (gentec) et attributs
devRobotRS2=['LT1/DG/','ROBOT-SPESO.MT_Rs.2'];
attr_list_RS2={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

% Definition device grating translation et attributs
devRobotGrating=['LT1/DG/','SPESO.RESEAU-MT_Tx.1'];
attr_list_Grating={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

% Definition device translation Z et attributs
devRobotTransZ=['LT1/DG/','ROBOT-SPESO.MT_Tz.1'];
attr_list_TransZ={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

wait_for_standby_SZR(1,1,1);

YneedsInit=1;
ZneedsInit=1;

initialize_axis_SZ(YneedsInit,ZneedsInit)

%%Positionnement GENTEC au centre (face au réseau en Y et Z)
%tango_write_attribute2(devRobotTransZ,'position',pos_hublot_Z_GENTEC);
%tango_write_attribute2(devRobotTransY,'position',pos_reseau_Y_GENTEC);
%tango_command_inout2(devRobotTransY,'DefinePosition',pos_init);
%tango_command_inout2(devRobotTransZ,'DefinePosition',pos_init);
fprintf('Detecteurs prets...\n')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for tloop=1:10000
    
    zloop=2*str2num(datestr(now,'HH'))+floor((str2num(datestr(now,'MM')))/30)
    disp([ 'zloop= ' num2str(zloop) ] );
    
    %day 0: y scan 
    %day 1: x scan
    %day 2: x scan near
    %day 3: long y scan part 1
    %day 4: long y scan part 2
    %day 5: long y scan part 3
    %day 6: long y scan part 4
    %day 7: rotation scan @200000
    %day 8: rotation scan @300000
    %day 9: rotation scan @400000
    %day 10: Grating pos
    %day 11: Grating pos near
    
    pos_res_out=8000;
    pos_res_in=1350000;
    theDate=str2num(datestr(now,'DD'))-4; % offset of 4 to start tomorrow at 1
        if(mod(theDate,N_DAYS)==0)
    %day 0: y scan 
        pos_next_Y=-1*(floor(course_en_Y/48)*(zloop+0.5));
        tango_write_attribute2(devRobotTransY,'position',pos_next_Y);
        pause(0.2)
        tango_write_attribute2(devRobotRS1,'position',pos_ref_rs1);
        pause(0.2)
        tango_write_attribute2(devRobotRS2,'position',pos_ref_rs2);
        pause(0.2)
        tango_write_attribute2(devRobotTransX,'position',backposTx);
        pause(0.2)
        elseif((mod(theDate,N_DAYS)==1)||(mod(theDate,N_DAYS)==2))
    %day 1: x scan
    %day 2: x scan near
        dayval=mod(theDate,N_DAYS)-0
        pos_next_X=-1*(floor(course_en_X/(dayval*48))*(zloop+0.5));
        tango_write_attribute2(devRobotTransY,'position',pos_ref_ty);
        pause(0.2)
        tango_write_attribute2(devRobotRS1,'position',pos_ref_rs1);
        pause(0.2)
        tango_write_attribute2(devRobotRS2,'position',pos_ref_rs2);
        pause(0.2)
        tango_write_attribute2(devRobotTransX,'position',pos_next_X);
        pause(0.2)
        elseif((mod(theDate,N_DAYS)>=3)&&(mod(theDate,N_DAYS)<=6))
    %day 3: long y scan part 1
    %day 4: long y scan part 2
    %day 5: long y scan part 3
    %day 6: long y scan part 4
        dayval=mod(theDate,N_DAYS)-2
        pos_next_Y=-1*(floor(course_en_Y/(4*48))*((zloop+(48*dayval))+0.5));
        tango_write_attribute2(devRobotTransY,'position',pos_next_Y);
        pause(0.2)
        %tango_write_attribute2(devRobotTransX,'position',backposTx);
        %pause(0.2)
        tango_write_attribute2(devRobotTransX,'position',-500);
        pause(0.2)
        tango_write_attribute2(devRobotTransY,'position',pos_ref_ty);
        pause(0.2)
        tango_write_attribute2(devRobotRS1,'position',pos_ref_rs1);
        pause(0.2)
        tango_write_attribute2(devRobotRS2,'position',pos_ref_rs2);
        pause(0.2)
        elseif((mod(theDate,N_DAYS)>=7)&&(mod(theDate,N_DAYS)<=9))
    %day 7: rotation scan @200000
    %day 8: rotation scan @300000
    %day 9: rotation scan @400000
        dayval=mod(theDate,N_DAYS)-6
        tango_write_attribute2(devRobotTransX,'position',-500);
        pause(0.2)
        ypos=dayval*(-100000)
        tango_write_attribute2(devRobotTransY,'position',ypos);
        pause(0.2)
        posRS1=floor((60/48)*(zloop+0.5))+(pos_ref_rs1-30);
        posRS2=floor((60/48)*(zloop+0.5))+(pos_ref_rs2-30);
        tango_write_attribute2(devRobotRS1,'position',posRS1);
        pause(0.2)
        tango_write_attribute2(devRobotRS2,'position',posRS2);
        pause(0.2)
    else
    %day 10: Grating pos
    %day 11: Grating pos near
        dayval=mod(theDate,N_DAYS)-9
        tango_write_attribute2(devRobotTransY,'position',pos_ref_ty);
        pause(0.2)
        tango_write_attribute2(devRobotRS1,'position',pos_ref_rs1);
        pause(0.2)
        tango_write_attribute2(devRobotRS2,'position',pos_ref_rs2);
        pause(0.2)
        tango_write_attribute2(devRobotTransX,'position',-500);
        pause(0.2)
        pos_res_in=1350000-floor((250000/(dayval*48))*(zloop+0.5));
        pos_res_out=8000;
    end % depending on the day


    
    pos_next_Z=pos_hublot_Z;
    tango_write_attribute2(devRobotTransZ,'position',pos_next_Z);
    pause(0.2)
    
    
    previous_hour=2*str2num(datestr(now,'HH'))+floor((str2num(datestr(now,'MM')))/30);
    previous_half_hour=(floor((str2num(datestr(now,'MM')))/2)>=20); %To enter the while loop
    while (((2*str2num(datestr(now,'HH'))+floor((str2num(datestr(now,'MM')))/30))==previous_hour)&&((floor((str2num(datestr(now,'MM')))/2)>=20)==previous_half_hour))
        
            if (floor(mod(str2num(datestr(now,'MM')),30))>=25)
                pos_res=pos_res_out;
            else
                pos_res=pos_res_in;
            end
        tango_write_attribute2(devRobotGrating,'position',pos_res);
        pause(2)
        
%        pos_next_Y_array(tloop) = pos_next_Y;
%        pos_next_Z_array(tloop) = pos_next_Z;
%        pos_next_ResTx_array(tloop) = pos_res;
        
        wait_for_standby_SZR(1,1,1)
        
%        disp(['Positions actuelles yloop= ' num2str(yloop) '  zloop= ' num2str(zloop) ' pos_next_Y= ' num2str(pos_next_Y)  ' pos_next_Z= ' num2str(pos_next_Z)  ' pos_det=' num2str(pos_res) ]);
        
        wloop=1;
        %        fprintf('Nombre d''acquisitions à cette position %d \n',nloop)
        date_pos=datestr(now(),'yyyy-mm-dd-HH-MM-SS');
        fprintf('Heure pour cette position %s \n',date_pos)
        getRemainingTime
        %    getNumAcq
        
        previousRemainingTime=getRemainingTime;
        fprintf('     %3.2f (previous= %3.2f )',getRemainingTime,previousRemainingTime)
        %%% Wait for the scope to have acquired data        
        while ((getRemainingTime<=previousRemainingTime)&&(getRemainingTime~=9999)&&(wloop<100))
            fprintf('     %3.2f (previous= %3.2f )',getRemainingTime,previousRemainingTime)
            %disp(['Positions actuelles  zloop= ' num2str(zloop) ' pos_next_Y= ' num2str(pos_next_Y)  ' pos_next_Z= ' num2str(pos_next_Z)  ' pos_det=' num2str(pos_res) ]);
            previousRemainingTime=getRemainingTime;
            pause(5)
            wloop=wloop+1;
        end % while
        if ((getRemainingTime==9999)||(getRemainingTime<-1000))
            pause(60)
        end
        date_point=datestr(now(),'yyyy-mm-dd-HH-MM-SS')
        
%        todayDir=[ '/home/operateur/GrpDiagnostics/matlab/SPESO/data/' datestr(now(),'yyyy-mm-dd') ]
%        mkdir(todayDir);
%        save([ todayDir '/point_info_' date_point ],'date_point','pos_next_Y','pos_next_Z','previousRemainingTime','tloop','pos_next_Y_array','pos_next_Z_array','pos_next_ResTx_array');
        
        %         while (str2num(datestr(now,'HHMM'))>=2340)
        %             disp('ampouleFwdBwdMotion');
        %             ampouleFwdBwdMotion;
        %             if (str2num(datestr(now,'HHMM'))>=2340)
        %             ampouleFlip;
        %             end;
        %         end
        %
        %            save_scope;
        datestr(now(),'yyyy-mm-dd-HH-MM-SS')
    end %for while same time
    %end %for zloop
    %end %for yloop
    
    date_title=datestr(now(),'yyyy-mm-dd-HH-MM-SS');
    
    disp(['tloop= ' num2str(tloop) ]);
end %for tloop
%system([zip date_today '.zip' SPESO_data_directory date_today])