% edited on 20150122
% using Track_each_session_v6

% 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

%% settings
vt_acq_entvt_acq_ent='VT1';
rot='Clockwise';
% rot='Couterclockwise';

zone_tolerance=[0.111,0.111]; % rad  =12cm for loc1, 10cm for loc2
zone_error=0.44; % rad  =20cm
zone_seq=[1,2]; % suppose two areas in the recording
num_zones=length(zone_seq);
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;

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=10; % 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=(2*pi)*distance_nomving/(pi*diam_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=(2*pi)*distance_startzone_border/(pi*diam_inner);

distance_correct_tolerance=5; % 5 cm at each side of the reward location
ang_correct_tolerance=(2*pi)*distance_correct_tolerance/(pi*diam_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=(2*pi)*distance_incorrect_tolerance/(pi*diam_inner);

% 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
Track_each_session_v6


%% Disconnection with Cheetah
Cheetah_Matlab_Disconnection

%% sessions start
% begin1
learning_lap_session1=8; % the first 10 laps are learing laps (10*num_zones trials)
learning_lap=learning_lap_session1;
tic
Track_each_session_v5
t_session1=toc
learn_correct_begin1=learn_correct;
learn_areas_begin1=learn_areas;
learn_ts_begin1=learn_ts;
learn_num_begin1=learn_num;
learn_ang_begin1=learn_ang;
learn_correct_rate_begin1=length(find(learn_correct_begin1==1))/learn_num_begin1;
test_correct_begin1=test_correct;
test_areas_begin1=test_areas;
test_ts_begin1=test_ts;
test_num_begin1=test_num;
test_ang_begin1=test_ang;
test_correct_rate_begin1=length(find(test_correct_begin1==1))/test_num_begin1;
ind=find(learn_areas_begin1==1);learn_correct_rate_begin1(1)=length(find(learn_correct_begin1(ind)==1))/length(ind);
ind=find(learn_areas_begin1==2);learn_correct_rate_begin1(2)=length(find(learn_correct_begin1(ind)==1))/length(ind);
ind=find(test_areas_begin1==1);test_correct_rate_begin1(1)=length(find(test_correct_begin1(ind)==1))/length(ind);
ind=find(test_areas_begin1==2);test_correct_rate_begin1(2)=length(find(test_correct_begin1(ind)==1))/length(ind);
save(strcat(subdir,'\',date,'_CT_tracking.mat'));
run_num
learn_correct_rate_begin1
test_correct_rate_begin1

learning_lap_session2=4; % 
learning_lap=learning_lap_session2;
tic
Track_each_session_v5
t_session2=toc
learn_correct_begin2=learn_correct;
learn_areas_begin2=learn_areas;
learn_ts_begin2=learn_ts;
learn_num_begin2=learn_num;
learn_ang_begin2=learn_ang;
learn_correct_rate_begin2=length(find(learn_correct_begin2==1))/learn_num_begin2;
test_correct_begin2=test_correct;
test_areas_begin2=test_areas;
test_ts_begin2=test_ts;
test_num_begin2=test_num;
test_ang_begin2=test_ang;
test_correct_rate_begin2=length(find(test_correct_begin2==1))/test_num_begin2;
ind=find(learn_areas_begin2==1);learn_correct_rate_begin2(1)=length(find(learn_correct_begin2(ind)==1))/length(ind);
ind=find(learn_areas_begin2==2);learn_correct_rate_begin2(2)=length(find(learn_correct_begin2(ind)==1))/length(ind);
ind=find(test_areas_begin2==1);test_correct_rate_begin2(1)=length(find(test_correct_begin2(ind)==1))/length(ind);
ind=find(test_areas_begin2==2);test_correct_rate_begin2(2)=length(find(test_correct_begin2(ind)==1))/length(ind);
save(strcat(subdir,'\',date,'_CT_tracking.mat'));
run_num
learn_correct_rate_begin2
test_correct_rate_begin2

learning_lap_session3=0; % 
learning_lap=learning_lap_session3;
tic
Track_each_session_v5
t_session3=toc
learn_correct_begin3=learn_correct;
learn_areas_begin3=learn_areas;
learn_ts_begin3=learn_ts;
learn_num_begin3=learn_num;
learn_ang_begin3=learn_ang;
learn_correct_rate_begin3=length(find(learn_correct_begin3==1))/learn_num_begin3;
test_correct_begin3=test_correct;
test_areas_begin3=test_areas;
test_ts_begin3=test_ts;
test_num_begin3=test_num;
test_ang_begin3=test_ang;
test_correct_rate_begin3=length(find(test_correct_begin3==1))/test_num_begin3;
ind=find(learn_areas_begin3==1);learn_correct_rate_begin3(1)=length(find(learn_correct_begin3(ind)==1))/length(ind);
ind=find(learn_areas_begin3==2);learn_correct_rate_begin3(2)=length(find(learn_correct_begin3(ind)==1))/length(ind);
ind=find(test_areas_begin3==1);test_correct_rate_begin3(1)=length(find(test_correct_begin3(ind)==1))/length(ind);
ind=find(test_areas_begin3==2);test_correct_rate_begin3(2)=length(find(test_correct_begin3(ind)==1))/length(ind);
save(strcat(subdir,'\',date,'_CT_tracking.mat'));
run_num
learn_correct_rate_begin3
test_correct_rate_begin3

%% Disconnection with Cheetah
Cheetah_Matlab_Disconnection