program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/flight/src/null_err_v02.src"
descr "null_err - null error signals & lock ISS"
# program null_err.src, created 95.12.14 Igor Zayer 
#
# Part of alignment procedure; adjust ISS parameters to be able to
# close its loop. Usually called by 'setup_iss'. --> IS NOT STAND-
# ALONE - ASSUMES DOOR OPEN, OBSMODE, ISS ON AND OPEN LOOP AND A 
# VALID !GR8 VALUE !!!
#
# Calls: check_errsig
# ------
#
# Variable usage:
#----------------
# !GI1: X-offset (multxofs)
# !GI2: Y-offset (multyofs)
# !GI8: 'error signal nulled' indicator (0 = not nulled, 1 = OK to lock ISS)
# !GR0: X-error signal, average (multxave)
# !GR1: Y-error signal, average (multyave)
# !GR8: Voltage value of nulled error signal (MUST BE SUPPLIED EXTERNALLY!!)
# !GS9: name of user logfile
#
# !LI1: Newly calculated X-offset (integer)
# !LI2: Newly calculated Y-offset (integer)
# !LI7: "X nulled criterion": set to 1 if satisfied
# !LI8: "Y nulled criterion": set to 1 if satisfied
# !LI9: "all done criterion": = !LI7 * !LI8
# !LR2: Newly calculated X-offset (fractional)
# !LR3: Newly calculated Y-offset (fractional)
# !LR4: Rounding variable = +0.5 or -0.5 depending on sign of !LR2 resp. !LR3
# !LS0: answer string
#----------------

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

display "Executing procedure null_err"
write  !GS9 "Executing procedure null_err :"
write  !GS9 " %s" !DATE
writen !GS9 " %s" !TIME
writen !GS9 " Using voltage value of null = %4.2f" !GR8

again:
  perform  check_errsig_v02
  if !GI8 = 1 then
    display "DONE...error signals nulled"
    writen !GS9 " DONE...error signals nulled"
    goto done
  end if

  ## WORK IN TWO'S COMPLEMENT HEX
  if !GI1 > 127 then
    set !GI1 = !GI1 - 256
  end if
  if !GI2 > 127 then
    set !GI2 = !GI2 - 256
  end if
  ## CALCULATE THE CORRECT OFFSET
  writen !GS9 " ...calculating new offsets..."
  set !LR2 = !GR0 - !GR8
  set !LR3 = !GR1 - !GR8
  set !LR2 = !LR2 / .14
  set !LR3 = !LR3 / .14
  set !LR2 = !LR2 + !GI1
  set !LR3 = !LR3 + !GI2
  ## ROUND OFF PROPERLY (X)
  set !LR4 = 0.5
  if !LR2 < 0 then
    set !LR4 = -0.5
  end if
  set !LI1 = !LR2 + !LR4
  ## ROUND OFF PROPERLY (Y)
  set !LR4 = 0.5
  if !LR3 < 0 then
    set !LR4 = -0.5
  end if
  set !LI2 = !LR3 + !LR4
  ## ASK FOR PERMISSION TO ACTUALLY UPLINK
  display "Calculated Offsets: %4d" !LI1
  display ", %4d" !LI2 to 25
  ## UPLINK & LOG THE CALCULATED OFFSET VALUES
  tcblock mbltxofs !LI1
  tcblock mbltyofs !LI2
  write  !GS9 " commanded X offset: %3d " !LI1
  writen !GS9 " commanded Y offset: %3d " !LI2
  display "Uplinked Offsets: %4d" !LI1 to 35
  display ", %4d" !LI2 to 57
  ###sync_hk###
  ## CONTINUE WITH ITERATION?
  menu !LS0 "Continue?" "YES, looks good" "NO, something's wrong, exit!"
  if !LS0 = "YES, looks good" then
    goto again
  else
    setstr !GS6 = "  "   ## disable continuing with ISS...
  end if

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