function turnctrl
% adopted from code by Bharanidharan Asokan
% code by jamie and jph

% connect to mcc
mccif('open','172.19.32.67')  

axi='ADCS_SS1_Angle';
axo='ADCS_SS2_Angle';

% initialise port to turn table
fid=serial('COM3');

% settings for com port
set(fid,...
  'BaudRate',115200,...
  'Parity','none',...
  'DataBit',8,...
  'StopBit',1,...
  'terminator','CR');
  
% open com port
fopen(fid);

% legacy behavior for whatever reason
fprintf(fscanf(fid))

rad=32;
stp=2;
inner=-rad:stp:rad; % alpha angle increment
outer=-rad+stp:stp:rad; % beta angle increment
delay=0.4;

% move from center (0,0) to corner (-rad,-rad)
for c0=0:-stp:-rad
  fprintf(fid,'AXI')
  fprintf(fid,['MOV' num2str(i)]);
  fprintf(fid,'AXO')
  fprintf(fid,['MOV',num2str(i)]);
  mccif('set',axi,c0,'float')
  mccif('set',axo,c0,'float')
  pause(delay)
end

% increment inner angle
for c0=inner
  fprintf(fid,'AXI')
  fprintf(fid,['MOV',num2str(i)]);
  disp(['AXI_' ['MOV',num2str(i)]])
  mccif('set',axi,c0,'float')  
  pause(delay)
  
  % increment outer angle
  for c1=outer
    fprintf(fid,'AXO')
    fprintf(fid,['MOV',num2str(j)]);
    disp(['AXO_' ['MOV',num2str(j)]])
    mccif('set',axo,c1,'float')    
    pause(delay)
  end
  
  % alternate direction of outer angle
  outer=-outer;
end

% move from corner (rad,rad) to center (0,0)
for c0=rad:-stp:0
  fprintf(fid,'AXI')
  fprintf(fid,['MOV' num2str(c0)]);
  fprintf(fid,'AXO')
  fprintf(fid,['MOV',num2str(c0)]);
  mccif('set',axi,c0,'float')
  mccif('set',axo,c0,'float')
  pause(delay)
end

% close com port
fclose(fid)

% close connection to mcc
mccif('close')
