% edited on 20140907
% using Track_each_session_v2

%% settings
vt_acq_entvt_acq_ent='VT1';
session_time0=60*10; % 10min in a session
rot='Clockwise';
% rot='Couterclockwise';
zone_tolerance=0.11; % rad  =10cm
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.3; % 1s
vfs=29.97;  % ======= the sample frequency of video ========
scale_x=0.28; % convert to cm
scale_y=0.31;

subdir='C:\DATA\Chenguang\Rat47';

% sound
load train
sound_error=y;
sound_error_fs=Fs;

load chirp
sound_correct=y;
sound_correct_fs=Fs;

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
TrackCenter

TrackZones_v2

X_CT=[x_N,x_E,x_S,x_W,x_center];
Y_CT=[y_N,y_E,y_S,y_W,y_center];
plot(X_CT,Y_CT,'.')
hold on
plot(x1_center0,y1_center0,'r.')
plot(x2_center0,y2_center0,'g.')
hold off

% rotate the track angle because of different running directions
if strcmp(rot,'Clockwise');
    Ang_center=2*pi-Ang_center;
end

%% begin sessions

% begin1
learning_lap_session1=100; % the first 10 laps are learing laps (10*num_zones trials)
learning_lap=learning_lap_session1;
tic
Track_each_session_v2
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;
save(strcat(subdir,'\',date,'_CT_tracking.mat'));


learning_lap_session2=4; % 
learning_lap=learning_lap_session2;
tic
Track_each_session_v2
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;
save(strcat(subdir,'\',date,'_CT_tracking.mat'));

learning_lap_session3=2; % 
learning_lap=learning_lap_session3;
tic
Track_each_session_v2
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;

save(strcat(subdir,'\',date,'_CT_tracking.mat'));

%% Disconnection with Cheetah
Cheetah_Matlab_Disconnection