program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/flight/src/setup_iss_v02.src"
descr "setup_iss - null error signals & lock ISS"
# program setup_iss.src, created 95.12.14 Igor Zayer 
#
# Part of alignment procedure; null error signals and optionally
# close the loop. Usually called by 'alignit.src'.
# IS NOT STAND-ALONE: ASSUMES DOOR OPEN AND MDI IN OBSMODE.
#
# Calls: checkiss, check_errsig, null_err, take_pic
# ------
#
# Variable usage:
#----------------
# !GI8: 'error signal nulled' indicator (0 = not nulled, 1 = OK to lock ISS)
# !GS6: 'OK to proceed with ISS' indicator (= "enable ISS" is OK)
# !GS9: name of user logfile
#
# !LI0: loop count variable
# !LI1: suggested x-gain
# !LI2: suggested y-gain
# !LI3: chosen/commanded x-gain
# !LI4: chosen/commanded y-gain
# !LI5: existing x-gain
# !LI6: existing y-gain
# !LR1: Photodiode 1 signal / average X intensity
# !LR2: Photodiode 2 signal
# !LR3: Photodiode 3 signal / average Y intensity
# !LR4: Photodiode 4 signal
# !LS0: answer string
#----------------

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

## CHECK IF SET-UP PROPERLY
perform  checkiss_v01
if !GS6 <> "enable ISS" then
  goto done
end if

## READ ILLUMINATION INTENSITY
set !LR1 = getreal(multdio1)
set !LR2 = getreal(multdio2)
set !LR3 = getreal(multdio3)
set !LR4 = getreal(multdio4)
set !LR1 = !LR1 + !LR2		## average x-intensity
set !LR1 = !LR1 / 2.0
if !LR1 < 0.1 then		## check for sufficient illumination
  beep
  display "You don't have enough signal on diodes 1 + 2 !!"
  writen !GS9 " Not enough signal on ISS diodes 1 + 2 !!"
  goto done
end if
set !LR3 = !LR3 + !LR4		## average y-intensity
set !LR3 = !LR3 / 2.0
if !LR3 < 0.1 then		## check for sufficient illumination
  beep
  display "You don't have enough signal on diodes 3 + 4 !!"
  writen !GS9 " Not enough signal on ISS diodes 3 + 4 !!"
  goto done
end if

## CHECK IF X-ERROR IS CLOSE ENOUGH TO NULL
perform  check_errsig_v01
if !GI8 = 1 then
  display "Error signals are close enough!"
  menu !LS0 "Seems CLOSE, satisfied?" "OK, good enough!" "NO, continue!"
  if !LS0 = "OK, good enough!" then
    writen !GS9 "Error signals are already nulled"
    goto lock
  end if
end if

doit:
## TAKE PICTURES AND NULL ERROR SIGNALS IF NEEDED
menu !LS0 "Need pictures?" "YES, insist on pictures" "NO, don't need one.."
if !LS0 = "YES, insist on pictures" then
  perf  take_pic_v02
end if
menu !LS0 "Null ISS error signals?" "YES, go iterate" "NO, giveup on ISS..."
if !LS0 = "NO, giveup on ISS..." then
  goto done
end if
perf  null_err_v01
if !GS6 <> "enable ISS" then
  goto done
end if

lock:
## LOCK THE ISS ONLY AFTER IT'S SAFE AND CONFIRMED
menu !LS0 "CLOSE the ISS loop?" "YES, lock the ISS" "NO, leave loop OPEN"
if !LS0 = "NO, leave loop OPEN" then
  goto done
end if

## CALCULATE SAFE DEFAULT GAINS
set !LR2 = 18.6 / !LR1
set !LI1 = 127 - !LR2		## safe x-gain
set !LR4 = 26.04 / !LR3
set !LI2 = 127 - !LR4		## safe y-gain; is 1.4x higher than x-gain
display "Recommended ISS gain (X,Y): %4d" !LI1
display ", %4d" !LI2 to 33
beep
menu !LS0 "Compare ISS page!!" "Use EXISTING gain" "enter NEW gain" "GIVEUP"
if !LS0 = "GIVEUP" then
  goto done
end if
if !LS0 = "enter NEW gain" then
  ## ASK FOR X- & Y-GAIN SETTINGS
  for !LI0 = 0 to 32000
    set !LI3 = 0
    ask !LI3 "Enter X-gain (enter zero for above recommended value):"
    if !LI3 = 0 then
      set !LI3 = !LI1
    end if
    set !LI4 = 0
    ask !LI4 "Enter Y-gain (enter zero for above recommended value):"
    if !LI4 = 0 then
      set !LI4 = !LI2
      end if
    display "Chosen gain (X,Y): %4d" !LI3 to 45
    display ", %4d" !LI4 to 68
    menu !LS0 "Is this correct?" "YES" "NO, reenter" "use EXISTING.." "GIVEUP"
    if !LS0 = "GIVEUP" then
      goto done
    end if
    if !LS0 = "use EXISTING.." then
      goto closeit
    end if
    exit if !LS0 = "YES"
  end for
  ## NOW GO UPLINK THE GAINS
  tcblock mbltxgn !LI3
  tcblock mbltygn !LI4
  ###sync_hk###
end if

closeit:
menu !LS0 "REALLY close it (check ESR flag!)??" "CLOSE the LOOP!" "No, GIVEUP"
if !LS0 = "CLOSE the LOOP!" then
  if !GI8 = 1 then
    tcblock mbltclsl
    display "...CLOSING ISS loop"
    writen !GS9 "...CLOSING ISS loop"
    ###sync_hk###
    menu !LS0 "Is ISS locked properly? " "YES, looks good" "NO, OPEN the LOOP"
    if !LS0 = "NO, OPEN the LOOP" then
      tcblock mbltopnl
      beep
      display " ...OPENING ISS loop again!"
      writen !GS9 " something's wrong - opening the loop!"
      ###sync_hk###
    end if
  else
    beep
    menu !LS0 "SORRY, You're NOT aligned! Adjust? " "OK, try again.." "GIVEUP"
    if !LS0 = "OK, try again.." then
      goto doit
    end if
  end if
end if

done:
perform  log_iss_par_v01 	## log the final state of ISS
writen !GS9 "Finished procedure setup_iss : %s" !TIME
writen !GS9 " "
display "Finished Procedure setup_iss                                    "
return
