###CHECKPOINT ARGS:  -nostartlog -nolog-flow -nolog-blocks 
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/flight/src/jogleg.src"
descr "jogleg - Verify alignment mechanism functionality"
# program jogleg.src, created 97.03.25 Scott Williams
#	97.03.25  SDW   - derived from I. Zayer's legff_v05
#
# 	Moves the MDI alignment legs in a small step cross pattern to
#	verify functionality.  Dwells 60 seconds at each of 8 positions
#	and should be run non checkpointed.
#
# Calls: open_log, checkleglimit, close_log
# ------
#
# Variable usage:
#----------------
# !GI0: exposure time in ticks
# !GS0: 'bypass limit switch check': (= "MDISIM" for testing on MDISIM)
# !GS6: 'OK to proceed with ISS' indicator (= "enable ISS" if OK)
# !GS7: 'enable calling the leg routine' string (ensures prerequisites are met)
# !GS8: name of 160k datafile
# !GS9: name of user logfile 
# 
# !LI0: loop count variable
# !LI1: commanded AM1 position (assuming DEP is reliable)
# !LI2: commanded AM2 position (assuming DEP is reliable)
# !LI3: total accumulated AM1 motion (do -!L3 to get "home")
# !LI4: total accumulated AM2 motion (do -!L4 to get "home")
# !LR1: starting/projected AM1 pot position
# !LR2: starting/projected AM2 pot position
# !LR3: projected maximum AM1 INC step/potentiometer value 
# !LR4: projected maximum AM1 DEC step/potentiometer value 
# !LR5: projected maximum AM2 INC step/potentiometer value 
# !LR6: projected maximum AM2 DEC step/potentiometer value 
# !LR7: auxiliary variable
# !LS0: answer string
#----------------

display "Executing procedure jogleg"
perform open_log_v04
write  !GS9 "Executing procedure jogleg :"
write  !GS9 " %s" !DATE
writen !GS9 " %s" !TIME

## DEP THINKS WE'RE HERE
set !LI1 = getint(mkam1cmd)
set !LI2 = getint(mkam2cmd)
if !LI1 > 32767 then
  set !LI1 = !LI1 - 65536
end if
if !LI2 > 32767 then
  set !LI2 = !LI2 - 65536
end if
write !GS9 "Starting at (steps, according to DEP): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
writen !GS9 " "

## CHECK ENDSWITCHES & POWER
setstr !GS7 = "enable legs"
perform  checkleglimit_v03
if !GS7 <> "enable legs" then
  goto done
end if

mode:
## MAKE SURE YOU HAVE THE PROPER ON-BOARD SEQUENCE RUNNING
beep
menu !LS0 "DO YOU HAVE PROPER DEP SEQUENCE RUNNING???" "YES" "NO - ABORT!!"
if !LS0 = "NO - ABORT!!" then 
  goto done
end if

## OPEN ISS LOOP
tcb mbltopnl

## OPEN DATAFILE 
open d !GS8
writen !GS9 " 160k LEGFF data stored in: %s" !GS8

## AT CENTER POSITION
wait time 60

## GOTO POSITION 1 (X/Y) =       1.86  -1.50
set !LI3 = 3
set !LI1 = !LI1 + !LI3
display "Sending: am1inc %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam1inc !LI3
display ", final position is %3d" getint(mkam1cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 2 (X/Y) =       0.00   0.00
set !LI3 = 3
set !LI1 = !LI1 - !LI3
display "Sending: am1dec %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam1dec !LI3
display ", final position is %3d" getint(mkam1cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 3 (X/Y) =      -1.86   1.50
set !LI3 = 3
set !LI1 = !LI1 - !LI3
display "Sending: am1dec %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam1dec !LI3
display ", final position is %3d" getint(mkam1cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 4 (X/Y) =       0.00   0.00
set !LI3 = 3
set !LI1 = !LI1 + !LI3
display "Sending: am1inc %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam1inc !LI3
display ", final position is %3d" getint(mkam1cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 5 (X/Y) =       1.89   1.32
set !LI4 = 3
set !LI2 = !LI2 + !LI4
display "Sending: am2inc %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam2inc !LI3
display ", final position is %3d" getint(mkam2cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 6 (X/Y) =       0.00   0.00
set !LI4 = 3
set !LI2 = !LI2 - !LI4
display "Sending: am2dec %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam2dec !LI3
display ", final position is %3d" getint(mkam2cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 7 (X/Y) =      -1.89  -1.32
set !LI4 = 3
set !LI2 = !LI2 - !LI4
display "Sending: am2dec %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam2dec !LI3
display ", final position is %3d" getint(mkam2cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

## GOTO POSITION 8 (X/Y) =       0.00   0.00
set !LI4 = 3
set !LI2 = !LI2 + !LI4
display "Sending: am2inc %3d" !LI3
display "to move to %3d" !LI1 to 22
tcblock mbam2inc !LI3
display ", final position is %3d" getint(mkam2cmd) to 36
write !GS9 "  We now should be at (steps): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
write !GS9 "  DEP says we're at (steps): AM1 %4d" getint(mkam1cmd)
writen !GS9 ",  AM2 %4d" getint(mkam2cmd)
write !GS9 "  The pots read: AM1 %4.2f" getreal(mxam1pos)
writen !GS9 ",  AM2 %4.2f" getreal(mxam2pos)
writen !GS9 " "
wait time 60

set !LI1 = getint(mkam1cmd)
set !LI2 = getint(mkam2cmd)
if !LI1 > 32767 then
  set !LI1 = !LI1 - 65536
end if
if !LI2 > 32767 then
  set !LI2 = !LI2 - 65536
end if
write !GS9 " Back at (steps, according to DEP): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
writen !GS9 " "

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 legff : %s" !TIME
writen !GS9 " "
display "Finished Procedure legff                      "
perform close_log_v03

return
