% Balayage SPESO vertical

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

%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=-66000;
pos_hublot_Y=-575000;
course_en_Z=140000;
course_en_Y=750000;

pos_ref_rs1=-18;
pos_ref_rs2=30;
pos_ref_rs3=0;
pos_ref_rs4=0;
backposTx=-200000;
%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=5

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

 % Definition device grating translation et attributs
 devRobotGrating=['LT1/DG/','SPESO.RESEAU-MT_Tx.1'];
 attr_list_Grating={'position','State','backwardLimitSwitch','forwardLimitSwitch'};
%
% % Definition device translation Y et attributs
% devRobotTransY=['LT1/DG/','ROBOT-SPESO.MT_Ts.1'];
% attr_list_TransY={'position','State','backwardLimitSwitch','forwardLimitSwitch'};
%
% % Definition device translation X et attributs
% devRobotTransX=['LT1/DG/','ROBOT-SPESO.MT_Tx.1'];
% attr_list_TransX={'position','State','backwardLimitSwitch','forwardLimitSwitch'};
%
% % Definition device translation X et attributs
% devRobotTransZ=['LT1/DG/','ROBOT-SPESO.MT_Tz.1'];
% attr_list_TransZ={'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 et attributs
% devRobotRS2=['LT1/DG/','ROBOT-SPESO.MT_Rs.2'];
% attr_list_RS2={'position','State','backwardLimitSwitch','forwardLimitSwitch'};
%
% % Definition device rotation RS3 et attributs
% devRobotRS3=['LT1/DG/','ROBOT-SPESO.MT_Rs.3'];
% attr_list_RS3={'position','State','backwardLimitSwitch','forwardLimitSwitch'};
%
% % Definition device rotation RS4 et attributs
% devRobotRS4=['LT1/DG/','ROBOT-SPESO.MT_Rs.4'];
% attr_list_RS4={'position','State','backwardLimitSwitch','forwardLimitSwitch'};

devVanne=['LT1/VI/','VS.3'];
attr_list_Vanne={ 'State'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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 @X=-200000
    %day 1: y scan @X=-400000
    %day 2: z scan @X=-200000
    %day 3: z scan @X=-400000
    %day 4: grating scan
    
    pos_res_out=8000;
    pos_res_in=1450000;
    theDate=floor(now)-8;
    if(mod(theDate,N_DAYS)==0)
        %day 0: y scan @X=-200000
        pos_next_Y=-200000+(-1)*(floor(600000/48)*(zloop+0.5));
        moveSPESO_Tx_Ts_Tz(-200000,pos_next_Y,pos_hublot_Z)
    elseif(mod(theDate,N_DAYS)==1)
        %day 1: y scan @X=-400000
        pos_next_Y=-200000+(-1)*(floor(600000/48)*(zloop+0.5));
        moveSPESO_Tx_Ts_Tz(-400000,pos_next_Y,pos_hublot_Z)
    elseif(mod(theDate,N_DAYS)==2)
        %day 2: z scan @X=-200000
        pos_next_Z=(-1)*(floor(140000/48)*(zloop+0.5));
        moveSPESO_Tx_Ts_Tz(-200000,pos_hublot_Y,pos_next_Z)
    elseif(mod(theDate,N_DAYS)==3)
        %day 3: z scan @X=-400000
        pos_next_Z=(-1)*(floor(140000/48)*(zloop+0.5));
        moveSPESO_Tx_Ts_Tz(-400000,pos_hublot_Y,pos_next_Z)
    else
        moveSPESO_Tx_Ts_Tz(-200000,pos_hublot_Y,pos_hublot_Z)
    end % depending on the day
    
    
    
    
    
    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))
        
        vanneValue=tango_read_attribute2(devVanne,'State');
        ivanne=0;
        while ((vanneValue.value~=3)&&(ivanne<10))
            disp(' Vanne fermee!!!')
            pause(60)
            vanneValue=tango_read_attribute2(devVanne,'State');
            ivanne=ivanne+1;
        end % while

        if(mod(theDate,N_DAYS)==4)
            pos_res_in=1450000-(10000*zloop);
        end
        if (floor(mod(str2num(datestr(now,'MM')),30))>=20)
            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])