% edited on 20150125
% using Track_each_session_v7

% no need to track reward locations, just get the reward location from his
% behavior. That is, to record the location where he stops to get food
% 
% in v6, we use averaged diam_inner thoughout the whole recording.
% However,the camera is not above the track center, so the radius is longer
% or shorter than diam_inner/2 at somewhere. So we divide the track into 12
% segments, and caculate the mean radius for each segment. 
% use ceil(ang_ontrack/( 2*pi/12))to show which segment is the current loc in

%% settings
vt_acq_entvt_acq_ent='VT1';
rot='Clockwise';
% rot='Couterclockwise';

sample_time=0.1;
time_stay_correct_alarm=0.1; % 1s
time_stay_error_alarm=0.2; % 1s
vfs=29.97;  % ======= the sample frequency of video ========
scale_x=0.28; % convert to cm
scale_y=0.31;



subdir='C:\DATA\Chenguang\Rat68';

% sound
load('beep.mat')
sound_beep_start=beep;
sound_beep_end=beep_long;
sound_beep_fs=Fs;

%% Connection with Cheetah
Cheetah_Matlab_Connection

%% Track the circular track center
% put the headstage at the inner edge of the circular track. When hear the
% beep, move the headstage around the inner edge of the track, and then
% stop also at inner edge. When hear another beep, remove the headstage.
TrackCenter_v3
diam_outer=diam_inner+20;
R_inner=diam_inner/2;

figure;
plot(x_circle-x_center,y_circle-y_center,'k.')
xlim([-50,50]);
ylim([-50,50]);
axis square
%% track the starting zone
% When hear the beep, put the headstage at the first border of starting
% zone. When hear the second beep, put the headstge at the second border of
% starting zone. When hear the 3th beep, done.
TrackStartingZone_v1
Ang_StartZone_depart0=max(Ang_StartZone);
Ang_StartZone_arrive0=min(Ang_StartZone);
% consider the Ang_StartZone_depart to be 0, and calculated all angles
% compared to it
Ang_StartZone_depart=0;
Ang_StartZone_arrive=mod(Ang_StartZone_arrive0-Ang_StartZone_depart0,2*pi);

%% plot the circular track
figure;
plot(x_circle-x_center,y_circle-y_center,'k.')
hold on
plot(x1_StartZone,y1_StartZone,'r.')
plot(x2_StartZone,y2_StartZone,'g.')
xlim([-50,50]);
ylim([-50,50]);
axis square
hold off

%% Disconnection with Cheetah
Cheetah_Matlab_Disconnection

%% Connection with Cheetah
Cheetah_Matlab_Connection

%% begin sessions

% setting in the trials
n_sample=6; % 6 learning laps in sample trial
n_test=1; % only one testing lap in test trial
time_stay_sample=0.4; % in sample trials, if the rat stay at a location for 'time_stay_sample'
                    % we will mark here as a reward location
time_fix=30; % the rat needs to sit in starting zone for time_fix before running
distance_nomving=2; % 2cm
                    % if the rat's current location is less than 2cm close
                    % to last position, then we consider he's not moving
ang_nomving=distance_nomving/R_inner;

distance_startzone_border=5; % 5cm
% reward location starts from Ang_StartZone_depart + ang_startzone_border,
% and ends at Ang_StartZone_arrive - ang_startzone_border
ang_startzone_border=distance_startzone_border/R_inner;

distance_correct_tolerance=5; % 5 cm at each side of the reward location
ang_correct_tolerance=distance_correct_tolerance/R_inner;
ang_stop_tolerance=ang_correct_tolerance;% within this tolerance, the rat is supposed to find the same location

distance_incorrect_tolerance=50; % 50 cm at each side of the reward location
ang_incorrect_tolerance=distance_incorrect_tolerance/R_inner;

% find averaged R_inner for 12 segments of the track
x0 = x_circle-x_center;
y0 = y_circle-y_center;
ind=find(x0==0);
x0(ind)=0.01;
ind=find(x0>0);
ang0(ind)=mod(atan(y0(ind)./x0(ind)),2*pi);
ind=find(x0<0);
ang0(ind)=mod(atan(y0(ind)./x0(ind))+pi,2*pi);
if strcmp(rot,'Clockwise');
    ang0=2*pi-ang0;
end
ang_ontrack=mod(ang0-Ang_StartZone_depart0,2*pi);
distance_center=sqrt(x0.*x0+y0.*y0);
ind_segment=ceil(ang_ontrack/(2*pi/12));
R_inner_segment=zeros(12,1);
for ns=1:12
    ind=find(ind_segment==ns);
    R_inner_segment(ns)=mean(distance_center(ind));
end
R_inner_segment=R_inner_segment+1.5;
R_inner=R_inner+1.5;

% setting of the tones
fs_tone1 = 3000;
fs_sample_tone1=fs_tone1*10;
T = 0.25; % 1 seconds duration
t = 0:(1/fs_sample_tone1):T;
tone1 = sin(2*pi*fs_tone1*t);
sound(tone1, fs_sample_tone1);
pause(5)
fs_tone2 = 6000;
fs_sample_tone2=fs_tone2*10;
T = 0.25; % 1 seconds duration
t = 0:(1/fs_sample_tone2):T;
tone2 = sin(2*pi*fs_tone2*t);
sound(tone2, fs_sample_tone2);

% For rat 68, use tone1 as correct tone, and tone2 as incorrect tone
tone_correct=tone1;
fs_tone_correct=fs_sample_tone1;
tone_incorrect=tone2;
fs_tone_incorrect=fs_sample_tone2;

%% session start

for nsession=1:10
    pause(60*5);
    sound(beep0,fs_sin);
    Track_each_session_v7
    Ang_sample_reward_actual(:,nsession)=ang_sample_reward_actual;
    Ang_sample_reward_ontrack(:,nsession)=ang_sample_reward_ontrack;
    Time_sample_reward(:,nsession)=time_sample_reward;
    Ang_sample_reward_ontrack_mean(1,nsession)=ang_sample_reward_ontrack_mean;
    Ang_test_reward_actual(1,nsession)=ang_test_reward_actual;
    Ang_test_reward_ontrack(1,nsession)=ang_test_reward_ontrack;
    Time_test_reward(1,nsession)=time_test_reward;
    Sign_correct(1,nsession)=sign_correct;
end
save(strcat(subdir,'\',date,'_CT_tracking.mat'));

%% Disconnection with Cheetah
Cheetah_Matlab_Disconnection