###CHECKPOINT ARGS:  -###queue=0 -nostartlog -nolog-flow -nolog-blocks
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/flight/src/iss_opt_v01.src"
descr "iss_opt - step-wise optimize ISS gain (in ISSmode)"
# program iss_opt.src, created 96.01.24 Igor Zayer 
#
#	Puts MDI into ISS mode, then steps up the x & y gains in 7 steps.
#	At each gain setting the "jitter" datastream is sampled in both opel
#	and closed loop with PAW turning (done in do_the_iss)
#
# Calls: checkiss, null_err, do_the_iss, close_log, go_issmode, end_issmode
# ------
#
# Variable usage:
#----------------
# !GI8: 'error signal nulled' indicator (0 = not nulled, 1 = OK to lock ISS)
# !GR8: Voltage value of nulled error signal (=2.27), MUST be supplied HERE !!!
# !GS0: 'bypass limit switch check': (= "MDISIM" for testing on MDISIM)
# !GS6: 'OK to proceed with ISS' indicator (= "enable ISS" if OK)
# !GS8: name of 160k datafile
# !GS9: name of user logfile 
# 
# !LS0: answer string
#----------------

display "Executing procedure iss_opt"

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

## SEE WHETHER ISS IS USEABLE
if !GS0 = "MDISIM" then	      ## skip this checking if on SIM system
  goto mode
end if
set !GR8 = 2.265
perform checkiss_v04	       ## this also logs initial ISS params.
if !GI8 <> 1 then
  if !GS6 <> "enable ISS" then
    goto done
  end if
  beep
  beep
  display " You need to null error signals first !"
  menu !LS0 "Want to null error signals?" "YES" "NO, giveup"
  if !LS0 = "NO, giveup" then 
    writen !GS9 " Error signals not nulled - giving up!"
    goto done
  end if
  perform null_err_v04
  if !GS6 <> "enable ISS" then
    writen !GS9 " Giving up!"
    goto done
  end if
end if

mode:
## GO TO NOMINAL OBSMODE
queue on ## Queue 
perform obsmode_1_v01

## PUT MDI INTO ISS MODE
writen !GS9 " MDI to ISS mode"
perf start_iss_v01
queue off ## Synchronize w/ POCC 

## DO ISS-MODE CALIBRATIONS (OPENS ITS OWN DATAFILE)
perform iss_cal_v01

## OPEN DATAFILE FOR ISS MODE
open d !GS8
writen !GS9 " 160k (ISS mode-) data stored in: %s" !GS8

## USE GAINS X:113, Y:107 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 113
tcblock mbltygn 107
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## USE GAINS X:98, Y:87 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 98
tcblock mbltygn 87
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## USE GAINS X:81, Y:63 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 81
tcblock mbltygn 63
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## USE GAINS X:64, Y:38 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 64
tcblock mbltygn 38
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## USE GAINS X:48, Y:16 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 48
tcblock mbltygn 16
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## USE GAINS X:31, Y:248 = -8 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 31
tcblock mbltygn 248
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## USE GAINS X:0, Y:205 = -51 & GO MEASURE
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 0
tcblock mbltygn 205
perform do_the_iss_v01	## EXPECTS QUEUING ON & TURNS IT OFF...

## GO BACK TO GAINS X:99, Y:87 
queue on ## Queue next 3 commands.                                    ##-#
tcblock mbltxgn 99
tcblock mbltygn 87

## PUT MDI BACK INTO SCIENCE OBSERVING MODE
writen !GS9 " MDI back to science mode"
perf stop_iss_v01
queue off ## Synchronize w/ POCC 

## OPEN DATAFILE FOR FURTHER DATA (KEEP SEPARATE FROM ISS MODE)
open d !GS8
writen !GS9 " opening new 160k data file for next activity: %s" !GS8

done:
writen !GS9 "Finished procedure iss_opt : %s" !TIME
writen !GS9 " "
display "Finished Procedure iss_opt                                   "
perform close_log_v03
return
