###CHECKPOINT ARGS:  -nostartlog -nolog-flow -nolog-blocks -queue=0
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
#
#	96.01.31  IZ - included more options (1x1 pics & very fine
#			alignment with mirror)
#		     - streamline (remove checking if door open, simply
#			SET the obsmode, remove "last picture?")
#		     - updated error signal NULL to 2.265 (from 2.8)
#		     - leave datafile open at end of procedure
#	96.03.19  IZ - modified to not disturb on-board sequence if only
#			doing ISS setup alone
#
# 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.265), 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_v04
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.265

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

## BYPASS THE WHOLE LOT IF ONLY CLOSING ISS LOOP
for !LI0 = 0 to 32000
  menu !LS0 "Menu:" "DO ISS ONLY" "DO FULL ALIGNMENT" "QUIT"
  if !LS0 = "DO ISS ONLY" then
    perform setup_iss_v07
  end if
  if !LS0 = "QUIT" then
    goto done
  end if
  exit if !LS0 = "DO FULL ALIGNMENT"
end for

## GO TO OBSMODE FOCUS = 1 (NOMINAL)
writen !GS9 "going to OBSmode, focus = 1"
perf obsmode_1_v01

## ENABLE IP QUEUE
tcb mbipldin 0x0136

## CHECK ERROR SIGNAL STATUS
perform  check_errsig_v03
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 "Menu:" "4x4 PIC" "1x1 PIC" "ADJ.MIRROR" "ADJ.LEGS" "ISS" "QUIT"
  if !LS0 = "4x4 PIC" then
    perform  take_pic_v05
  end if
  if !LS0 = "1x1 PIC" then
    perform  take_pic1x1_v01
  end if
  if !LS0 = "ADJ.MIRROR" then
    perform  move_mirror_v01
  end if
  if !LS0 = "ADJ.LEGS" then 
    if !GS7 = "enable legs" then	## take_pic's set this one
      perform  move_legs_v09
    else
      beep
      beep
      display "You MUST take a picture first !!"
    end if
  end if
  if !LS0 = "ISS" then
    perform  setup_iss_v07
  end if
  exit if !LS0 = "QUIT"
end for

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_v04

display "Finished procedure alignit        "
return
