%% v16: 
% edited on 02/19/2017
% modified from Track_each_session_v16
% only run post-tests (after sample-test trials)

% The experiment paradihm is setting as:
% 
% 1. All reward locations are marked by white door bumpers (0.7inch) at the inner edge of
%    the track.
% 2. In sample trials, a striking marker in a different color (orange) was
%    at reward location besides the corresponding white bumper; but the orange
%    sticker will be removed in the following test trials.
% 3. Sample1(marker)-test1(no marker)-Sample2(marker)-test2(no marker)-
%    Sample3(marker)-test3(no marker)
%    Sample1-20s-test1-40s-sample2-20s-test2........
% 4. 3 mins rest in the animal box in between sessions, and before the
%    first session and after the last session
% 5. A correct/incorrect tone goes off when he stops at correct/incorrect spot.
% 6. In sample trial, rats can stop at other locations besides reward
%    locaiton, and food will be delivered as long as they stop at reward
%    location. However, in test trials, rats are not allowed to stop and
%    turn to the center at the other locations. That means, if rats stop at
%    a wrong location and turn to the center, a incorrect tone will go off
%    and then no food reward will be delivered.
% 7. However, if the rat turned back, criteria are different. If the rat
%    turned back without already passing by the reward location, he still
%    has chance to get the reward; otherwise, he can't get the reward
%    anymore even if he ran back and force around the reward location.
% 8. At the end of each lap, the rat needs to be back to the animal box.
%    This lap (sample or test) will end if he's angle is around 0 degrees,
%    which is the center of the animal box.

% set the missing data [0,0] as the same as the last visited data
locationArray_end=[0,0];
fig = figure('Units','normalized','Position',[0 0 0.4 0.5]);

%% initialize variables for this session
ts_prerunning_start=nan(n_prerunning,1);
ts_prerunning_end=nan(n_prerunning,1);

%% Post-tests
for n_trial = 1:n_prerunning
    %% 50s between prerunning
    figure(fig);
    cla
    hold on
    text(0.5,0.8,strcat('Location .', num2str(ind_rewardloc)),'color','b','fontsize',70,'HorizontalAlignment','center');
    text(0.5,0.4,strcat('PreRunning . ', num2str(n_trial)),'color','b','fontsize',70,'HorizontalAlignment','center');
    xlim([0,1]);ylim([0,1])
    hold off
    axis off
    
    h=waitbar(0, strcat('PreRunning:',num2str(n_trial)));
    for min0=1:time_prerunning
        pause(1);
        waitbar(min0/time_prerunning);
    end
    close(h)
    
    %% Pre-running trials
%     sound(sound_beep_start,sound_beep_fs);  % make a sound when a test trial starts
    sound(beep0,fs_sin);
    
    pause(sample_time);
    get_current_loc_v1  % get the current location and angle from on-line video  
    ts_prerunning_start(n_trial,1)=time0;
    ind_loc=0;
    ind_loc_max=0;
    ang_ontrack=0;
    ang_ontrack_end=0;
    locationArray_end=[0,0];
    while 1
        pause(sample_time);
        get_current_loc_v1  % get the current location and angle from on-line video
        ang_ontrack=mod(ang0-Ang_StartZone_center,2*pi);
        vel_ontrack=(ang_ontrack-ang_ontrack_end)/sample_time;
        if ind_loc_max > N_loc/2 && ...
                (ang_ontrack>Ang_StartZone_arrive_ontrack || ang_ontrack<Ang_StartZone_depart_ontrack)
            % if the rat is back entering the animal box, sample tests end
            % sound(sound_beep_start,sound_beep_fs);
            break
        end
        
        ind_loc_temp=find(ang_ontrack>Ang_RewardLoc_ontrack_zone_narrow(:,1) & ang_ontrack<Ang_RewardLoc_ontrack_zone_narrow(:,2));
        if ~isempty(ind_loc_temp)
            if ind_loc_temp > ind_loc_max
                ind_loc_max=ind_loc_temp;
            end
        end
    end
    
    ts_prerunning_end(n_trial,1)=time0;
    
end
close(fig)