program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/sim/src/alignit.src"
descr "alignit - Verify / Correct MDI's pointing"
# program alignit.src, created 95.12.14 Igor Zayer 
#
# Universal alignment procedure; run to verify and/or adjust MDI's 
# pointing. Will OPTIONALLY perform all necessary steps: take 
# verification images, adjust on-board Alignment Mechanisms (legs),
# null the ISS' error signals (find the correct offset), and 
# ultimately close the ISS loop.
#
# Calls: check_errsig, take_pic, move_legs, setup_iss, close_log
# ------
#
# Variable usage:
#----------------
# !GI8: 'error signal nulled' indicator (0 = not nulled, 1 = OK to lock ISS)
# !GR8: Voltage value of nulled error signal (=2.28), MUST be supplied HERE !!!
# !GS0: 'bypass limit switch check': (= "MDISIM" for testing on MDISIM)
# !GS7: 'enable calling the leg routine' string; needed by 'move_legs'
# !GS9: name of user logfile 
#
# !LI0: loop count variable
# !LI1: position of CALwheel 1
# !LI2: position of CALwheel 2
# !LI3: CAL lens indicator (= 1 if NOT obsmode, 0 otherwise)
# !LS0: answer string
# !LS1: front door open status string
#----------------

display "Executing procedure alignit"

## OPEN A SPECIFIC LOGFILE (NO CHOICE OF NAMING)
if !GS9 <> "" then
  perf   close_log_v01
end if
setstr !LS0 = "alignit"
open user !LS0 dt !GS9 w
display "Logging to: %s" !GS9
write  !GS9 "Log File: %s" !GS9
writen !GS9 " "

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

## THIS IS THE VALUE OF ERROR SIGNAL NULL
set !GR8 = 2.28

## DONT' ASSUME ANYTHING: SET !GI8 and !LI8 = 0 and !GS7 = "  "
set !GI8 = 0
setstr !GS7 = "  "

if !GS0 = "MDISIM" then
  goto mode
end if

##CHECK IF DOOR IS OPEN
if getdig(mktmsel) = "           1" then
  setstr !LS1 = getdig(msfdopn)
else
  setstr !LS1 = getdig(msfdopr)
end if
if !LS1 <> "        OPEN" then
  beep
  beep
  display "Sorry, MDI's window MUST be open - exiting !!"
  writen !GS9 "MDI's window is NOT open !!"
  goto done
end if

mode:
## CHECK THAT WE AREN'T IN CALMODE (is either lens in the beam?)
set !LI3 = 0
set !LI1 = getint(mkc1pos)
set !LI2 = getint(mkc2pos)
if !LI1 >= 17 then
  if !LI1 <= 20 then
    set !LI3 = 1
  end if
end if
if !LI2 >= 123 then
  if !LI2 <= 126 then
    set !LI3 = 1
  end if
end if
if !LI3 = 1 then
  beep
  display "You are NOT in OBSmode!!"
  writen !GS9 "!..NOT in OBSmode..!"
  menu !LS0 "Go to OBSmode?" "YES, command OBSmode" "NO, just exit!"
  if !LS0 = "YES, command OBSmode" then
    display "going to OBSmode..."
    tcblock mbdpconf 10 47 125 91 89
    writen !GS9 "going to OBSmode..."
    writen !GS9 "sent command: tcblock mbdpconf 10 47 125 91 89"
    ###sync hk###
    goto mode
  else
    goto done
  end if
end if

## CHECK ERROR SIGNAL STATUS
perform   check_errsig_v01
if !GI8 = 1 then
  beep
  display "Alignment seems VERY CLOSE, suggest proceeding directly with ISS.."
end if

## THE MAIN LOOP:
open d !GS8	## open datafile 
writen !GS9 " 160k data stored in: %s" !GS8
for !LI0 = 0 to 32000
  menu !LS0 "PLEASE CHOOSE:" "TAKE PICTURE" "ADJUST LEGS" "SETUP ISS" "QUIT"
  if !LS0 = "TAKE PICTURE" then
    perform   take_pic_v01
  end if
  if !LS0 = "ADJUST LEGS" then 
    if !GS7 = "enable legs" then	## 'take_pic' sets this one
      perform   move_legs_v04
    else
      beep
      beep
      display "You MUST take a picture first !!"
    end if
  end if
  if !LS0 = "SETUP ISS" then
    perform   setup_iss_v01
  end if
  exit if !LS0 = "QUIT"
end for

## FINAL PICTURE?
menu !LS0 "TAKE LAST VERIFICATION PICTURE?" "YES, take one" "NO, just quit"
if !LS0 = "YES, take one" then 
  perform   take_pic_v01
end if
close d		## close datafile 

done:
## CHECK WHETHER AM POWER IS STILL ON
if getdig(mkpwam) =  "          ON" then
  beep
  display " AM power is still ON"
  writen !GS9 " AM power is still ON.."
  menu !LS0 "Turn AM power OFF again??" "turn legs OFF" "leave legs ON"
  if !LS0 = "turn legs OFF" then
    tcblock mbpwamof
    display " ..turning AM power OFF"
    writen !GS9 " ..turning AM power OFF"
  end if
end if

writen !GS9 "Finished procedure alignit : %s" !TIME
writen !GS9 " "
perf   close_log_v01

display "Finished procedure alignit        "
return
