###CHECKPOINT ARGS:  -nostartlog -nolog-flow -nolog-blocks -queue=0
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/sim/src/move_mirror.src"
descr "move_mirror - adjust on-board Alignment Mechanisms (legs)"
# program move_mirror.src, created 96.01.31 Igor Zayer 
#
#
# Part of alignment procedure; adjust image very finely using PZT offsets;
# usually called by 'alignit.src'. 
# The philosophy is that real-time (VC2) telemetry MUST be available to do this.
#
# Calls: checkiss, take_pic1x1
# ------
#
# Variable usage:
#----------------
# !GI8: 'error signal nulled' indicator (0 = not nulled, 1 = OK to lock ISS)
# !GS7: 'enable calling the leg routine' string (ensures prerequisites are met)
# !GS9: name of user logfile (actually, the extension only)
#
# !LI0: loop count variable
# !LI1: X-coordinate of image center
# !LI2: Y-coordinate of image center
# !LI3: rounded calculated total motion of AM1
# !LI4: rounded calculated total motion of AM2
# !LI5: commanded motion of AM1
# !LI6: commanded motion of AM2
# !LI7: remaining motion of AM1
# !LI8: remaining motion of AM2
# !LI9: status of AM limitswitches (0: all OK, +-1 for AM1, +-4 for AM2)
# !LR1: starting/projected AM1 pot position
# !LR2: starting/projected AM2 pot position
# !LR3: calculated total motion of AM1
# !LR4: calculated total motion of AM2
# !LR5: auxiliary variable needed in calculation
# !LS0: answer string
# !LS9: status of AM limitswitches - HK tlm query 
#----------------

display "Executing procedure move_mirror"
write  !GS9 "Executing procedure move_mirror :"
write  !GS9 " %s" !DATE
writen !GS9 " %s" !TIME

## UNDER CONSTRUCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  beep
  beep
  display "PROCEDURE UNDER CONSTRUCTION...."
  writen !GS9 "PROCEDURE UNDER CONSTRUCTION...."
  goto done
## UNDER CONSTRUCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

## CHECK FOR VC2(= DEP Reg.#3 must have a value of 2)
if getint(mksqrg3) <> 2 then
  beep
  beep
  display "Not in VC2 - not allowed to adjust legs!!"
  writen !GS9 "Not in VC2 - not allowed to adjust legs!!"
  goto done
end if

## ISS LOOP MUST BE OPEN!!
if getdig(mkltloop) = "       CLOSE" then
  beep
  beep
  display "ISS loop MUST be OPEN - opening loop.."
  writen !GS9 " opening ISS loop first.."
  tcb mbltopnl
end if

## CHECK IF WE'RE ALREADY CLOSE ENOUGH
if !GI8 = 1 then
  beep
  writen !GS9 "Error signal seems VERY CLOSE..."
  menu !LS0 "Seems CLOSE, give up on legs?" "OK, skip legs" "INSIST on legs!"
  if !LS0 = "OK, skip legs" then 
    goto done
  end if
end if

## ASK FOR PRESENT POINTING
center:
for !LI0 = 0 to 32000
  ask !LI1 "Enter center of image (X-coordinate in 1024 frame):"
  ask !LI2 "Enter center of image (Y-coordinate in 1024 frame):"
  display "Image center (X,Y): %4d" !LI1
  display ", %4d" !LI2 to 25
  menu !LS0 "Is this correct?" "CORRECT" "NO, re-enter" "just QUIT"
  if !LS0 = "just QUIT" then
    goto done
  end if
  exit if !LS0 = "CORRECT"
end for

## SEE HOW WE'RE DOING...
perf  take_pic1x1_v01
menu !LS0 "Are we getting closer?" "YES, continue" "Something's wrong, QUIT!"
if !LS0 = "Something's wrong, QUIT!" then 
  display " legs don't move in the right direction!!"
  writen !GS9 " legs don't move in the right direction - quitting!!"
  setstr !GS7 = "  "  ## disable calling until meeting prerequisites again
  goto done
end if

display "Done..."

done:
writen !GS9 "Finished procedure move_mirror : %s" !TIME
writen !GS9 " "
display "Finished Procedure move_mirror                                   "
return
