% the main program to demonstrate visual serving with vertically
% aligning the tips of 2 dark regions Lots of assumptions made
% to simplify the program for ease of presentation

% initialize RTX and move to a standard position with the tip in view
[NX,NY,NZ,NY,NP,NR] = calibrate_rtx(0);
[NX,NY,NZ,NY,NP,NR] = abs_position_rtx(4,4,400,0,400,0,90,0);
pause

  % get initial image and separation
  binimage=getbinimage(2,0,1,5);
  del2=finddist(binimage,fig6);
  ['Current error distance: ',int2str(del2)]

  % loop until close enough
  while del2 > 5

     % estimate Jacobean (here simplify to 1D - vertical move)
     J = estJacob(binimage,7)
     deltaz = del2/J
     
     % move half of the distance
     [NX,NY,NZ,NY,NP,NR] = rel_position_rtx(4,4,0,0,-deltaz/2,0,0,0);  

     % get new image and separation
     binimage=getbinimage(0,0,0,5);
     del2=finddist(binimage,0);
     ['Current error distance: ',int2str(del2)]

     pause
  end
  rtx_exit
