% Balayage SPESO

%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_GENTEC=-98000;
%pas_en_Z=8000;
%pas_en_Y=-10000;
course_en_Y=795000;

% 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

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

% Definition device translation Y et attributs
devRobotTransY=['LT1/DG/','ROBOT-SPESO.MT_Ts.1'];
attr_list_TransY={'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:1000
    
    yloop=2*str2num(datestr(now,'HH'))+mod(str2num(datestr(now,'MM'))/30,2)
    disp([ 'yloop= ' num2str(yloop) ] );
    
    pos_next_Y=-1*floor(course_en_Y/48)*(yloop+0.5);
        
    moveSPESO_TS(pos_next_Y);
    pause(0.2)
    pos_next_Z=pos_hublot_Z_GENTEC;
    zloop=pos_next_Z;
    tango_write_attribute2(devRobotTransZ,'position',pos_hublot_Z_GENTEC);
    pause(0.2)
    
    previous_hour=str2num(datestr(now,'HH'));
    previous_half_hour=(mod(str2num(datestr(now,'MM')),30)>=20); %To enter the while loop
    while ((str2num(datestr(now,'HH'))==previous_hour)&&((mod(str2num(datestr(now,'MM')),30)>=20)==previous_half_hour))
        
        if (mod(str2num(datestr(now,'MM')),30)>=20)
            pos_res=8000;
        else
            pos_res=1350000;
        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;
        
        disp([ 'pos_next_Y= ' num2str(pos_next_Y) ] );
        
        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))
            fprintf('     %3.2f (previous= %3.2f )',getRemainingTime,previousRemainingTime)
            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) ]);
            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= ' tloop ]);
end %for tloop
%system([zip date_today '.zip' SPESO_data_directory date_today])