'***************************************************************************
'Galil Motion Control Sample DMC Code
'***************************************************************************
'Description:
'This code is useful for positioning a rotary stage within one revolution. 
'Once the encoder counts per revolution parameter has been set, the rotary 
'target position can be defined in degrees. The example allows for the 
'position target to be set continuously. The shortest path determines the 
'rotation direction.
'***************************************************************************
#main;'                         main subroutine
~a= 0;'                         set axis designator
target= 30;'                    target position in degrees
rev2cnts= 4000;'                assume 4000 count per rev encoder
deg2cnts= rev2cnts/360;'        calculate counts per degree

#loop;'                         loop for position tracking
tarcur= target % 360;'          target deg within current rev
poscur= _RP~a / deg2cnts;'      current deg within current rev

dist= tarcur - poscur;'         degrees to traverse
IF (tarcur - poscur) > 180;'    traverse never needs to be > 180
  dist= tarcur - poscur - 360;' if it is, correct this
ENDIF

PR~a= dist*deg2cnts;'           command required move in counts
BG~a;'                          start A axis
AM~a;'                          profiling complete
WT200
JP#loop;'                       infinite loop

EN;'                            good practice but not necessary