%% Connection with Cheetah
% Cheetah_Matlab_Connection

%% outer diameter of the track
Diam_inner=78-4; % 100cm

%% beep at the beginning of the tracking
fs_sin=8000;
fs0=800;
t=0 : 1/fs_sin : 0.1;
beep0 = sin(fs0*2*pi*t);

%%
% put the headstage at the outer edge of the circular track. When hear the
% beep, move the headstage around the outer edge of the track, and then
% stop also at outer edge. When hear another beep, remove the headstage.
pause(10);
% drop the old data
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
sound(beep0,fs_sin);
pause(20);
[~, ~, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
sound(beep0,fs_sin);

x_circle_pixel=double(locationArray(1:2:end-1));
y_circle_pixel=double(locationArray(2:2:end));
ind=find(x_circle_pixel>0);
x_circle_pixel=x_circle_pixel(ind);
y_circle_pixel=y_circle_pixel(ind);

x_circle_pixel_sort=sort(x_circle_pixel);
y_circle_pixel_sort=sort(y_circle_pixel);


x_pixel_min=mean(x_circle_pixel_sort(1:10));
x_pixel_max=mean(x_circle_pixel_sort(end-9:end));
y_pixel_min=mean(y_circle_pixel_sort(1:10));
y_pixel_max=mean(y_circle_pixel_sort(end-9:end));


scale_x=Diam_inner/abs(x_pixel_max-x_pixel_min); % convert to cm
scale_y=Diam_inner/abs(y_pixel_max-y_pixel_min);

plot(x_circle_pixel,y_circle_pixel)
%% Disconnection with Cheetah
% Cheetah_Matlab_Disconnection