###CHECKPOINT ARGS: -nolog-flow -nolog-blocks -nostartlog -queue=0
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/sim/src/checkleglimit.src"
descr "checkleglimit - check status of AM limit switches"
# program checkleglimit.src, created 95.12.12 Igor Zayer
#	96.01.31  IZ	- added checkpoint args, removed extra question,
#			- added queueing control manually
#
# Part of alignment procedure; checks that the AM are ON and the status
# of the AM limit switches and moves the legs out of the limits if desired. 
# Called by 'move_legs.src'.
#
# --> HAS A BYPASS OPTION FOR LIMIT CHECKS FOR SIM-TEST PURPOSES VIA !GS0 <--
#
# Calls: --
# ------
#
# Variable usage:
#----------------
# !GS0: 'bypass limit switch check': (= "MDISIM" for testing on MDISIM)
# !GS7: 'enable calling the leg routine' string (ensures prerequisites are met)
# !GS9: name of user logfile (actually, the extension only)
#
# !LI0: loop count variable
# !LI8: number of steps to move legs out of limit
# !LI9: status of AM limitswitches (0: all OK, +-1 for AM1, +-4 for AM2)
# !LS0: answer string
# !LS9: status of AM limitswitches - HK tlm query 
#----------------

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

## SET NUMBER OF STEPS TO MOVE LEG OUT OF LIMIT
set !LI8 = 80

## CHECK THAT AM POWER IS ON
if getdig(mkpwam) <>  "          ON" then
  beep
  beep
  display " AM power is OFF!"
  writen !GS9 " AM power is OFF,"
  menu !LS0 "Turn AM power ON??" "turn legs ON" "just QUIT altogether!!"
  if !LS0 = "just QUIT altogether!!" then
    goto giveup
  end if
  display " ..turning AM power ON.."
  writen !GS9 " ..turning AM power ON.."
  queue on
  tcblock mbam1rs
  tcblock mbam2rs
  tcblock mbpwamon
  tcblock mbam1rs
  queue off
  tcblock mbam2rs
  wait time 8
  ## SEE IF WE SUCCEEDED TO TURN POWER ON
  if getdig(mkpwam) <>  "          ON" then
    beep
    beep
    display "Failed to power up AM!!"
    writen !GS9 " Failed to power up AM!!"
    goto giveup
  end if
end if

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

again:
## !LI9 IS WHERE WE ENCODE THE ACTUAL SITUATION
set !LI9 = 0
## CHECK AM1 CW (INCREMENT) LIMIT 
setstr !LS9 = getdig(mkam1cws)
if !LS9 =  "      CLOSED" then
  set !LI9 = !LI9 + 1
end if
## CHECK AM1 CCW (DECREMENT) LIMIT 
setstr !LS9 = getdig(mkam1ccs)
if !LS9 =  "      CLOSED" then
  set !LI9 = !LI9 - 1
end if
## CHECK AM2 CW (INCREMENT) LIMIT 
setstr !LS9 = getdig(mkam2cws)
if !LS9 =  "      CLOSED" then
  set !LI9 = !LI9 + 4
end if
## CHECK AM2 CCW (DECREMENT) LIMIT 
setstr !LS9 = getdig(mkam2ccs)
if !LS9 =  "      CLOSED" then
  set !LI9 = !LI9 - 4
end if

## NOW ACT UPON IT..
if !LI9 <> 0 then
  beep
  beep
  beep
  display "The LEGS are at their LIMIT !!"
  writen !GS9 "The LEGS are at their LIMIT !!"
  menu !LS0 "Recover from limit(s)??" "YES, move out!" "just QUIT altogether!!"
  if !LS0 = "just QUIT altogether!!" then
    goto giveup
  end if
  ## FINALLY MOVE LEGS OUT OF THE APPROPRIATE LIMIT(S) AS ENCODED IN !LI9
  if !LI9 > 1 then
    tcblock mbam2dec !LI8
    set !LI9 = !LI9 - 4
  end if
  if !LI9 < -1 then
    tcblock mbam2inc !LI8
    set !LI9 = !LI9 + 4
  end if
  if !LI9 > 0 then
    tcblock mbam1dec !LI8
  end if
  if !LI9 < 0 then
    tcblock mbam1inc !LI8
  end if
  ###sync hk###
  ## SEE IF WE SUCCEEDED TO GET LEGS OUT OF LIMIT(S)
  goto again
end if

simtest:
goto done  ## skip this if all went well
giveup:
writen !GS9 " giving up!!"
setstr !GS7 = " "	## disable continuation of 'move_legs' procedure

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

