###CHECKPOINT ARGS:  -nostartlog -nolog-flow -nolog-blocks 
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/flight/src/legff_v03.src"
descr "legff - execute a flat-field using MDI's legs"
# program legff.src, created 96.01.26 Igor Zayer 
#	96.02.02  IZ 	- fixed two's complement hex handling
#			  for DEP legposiotions..
#
# 	Moves the MDI alignment legs in a spiral-like pattern to
#	perform a flat-field measurement. The observation is taked
#	care of by whatever sequence is running on-board. Nomianlly
#	'cam_leff_fd' and 'cam_legff_hr' are foreseen. Spends ca 15min
#	at each position. Does NOT return to center!! <-------
#
# 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 legff"
perform open_log_v04
write  !GS9 "Executing procedure legff :"
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
set !LI3 = 0
set !LI4 = 0
write !GS9 "Starting at (steps, according to DEP): AM1 %4d" !LI1
writen !GS9 ",  AM2 %4d" !LI2
writen !GS9 " "

set !LR3 = 208.0	## maximum AM1 INC excursion in pattern
set !LR4 = -257.0	## maximum AM1 DEC excursion in pattern
set !LR5 = 188.0	## maximum AM2 INC excursion in pattern
set !LR6 = -214.0	## maximum AM2 DEC excursion in pattern

if !GS0 = "MDISIM" then	      ## skip this checking if on SIM system
  goto mode
end if
## FIRST CHECK LEG STEP RANGE ACCORDING TO DEP HOUSEKEEPING
set !LR7 = !LI1 + !LR3
if !LR7 > 350. then
  beep
  display "Sorry, You don't have enough AM1 INC range!"
  writen !GS9 "  Not enough AM1 INC range - quitting!"
  goto done
end if
set !LR7 = !LI1 + !LR4
if !LR7 < -350. then
  beep
  display "Sorry, You don't have enough AM1 DEC range!"
  writen !GS9 "  Not enough AM1 DEC range - quitting!"
  goto done
end if
set !LR7 = !LI2 + !LR5
if !LR7 > 350. then
  beep
  display "Sorry, You don't have enough AM2 INC range!"
  writen !GS9 "  Not enough AM2 INC range - quitting!"
  goto done
end if
set !LR7 = !LI2 + !LR6
if !LR7 < -350. then
  beep
  display "Sorry, You don't have enough AM2 DEC range!"
  writen !GS9 "  Not enough AM2 DEC range - quitting!"
  goto done
end if

## NOW CHECK AM POTS RANGE
set !LR1 = getreal(mxam1pos)
set !LR2 = getreal(mxam2pos)
write !GS9 "  Present pot positions: AM1 %4.2f" !LR1
writen !GS9 ",  AM2 %4.2f" !LR2
set !LR3 = -0.59 * !LR3		## pot1 ~ -0.59 * AM1
set !LR3 = !LR1 + !LR3
set !LR4 = -0.59 * !LR4		## pot1 ~ -0.59 * AM1
set !LR4 = !LR1 + !LR4

set !LR5 = 0.661 * !LR5		## pot2 ~ 0.661 * AM2
set !LR5 = !LR2 + !LR5
set !LR6 = 0.661 * !LR6		## pot2 ~ 0.661 * AM2
set !LR6 = !LR2 + !LR6

if !LR4 > 217.0 then
  beep
  display "Sorry, You'd be running into the AM1 decrement limit!"
  writen !GS9 "  Would reach the AM1 increment limit - quitting!"
  goto done
end if
if !LR3 < -246.0 then
  beep
  display "Sorry, You'd be running into the AM1 increment limit!"
  writen !GS9 "  Would reach the AM1 decrement limit - quitting!"
  goto done
end if
if !LR5 > 117.0 then
  beep
  display "Sorry, You'd be running into the AM2 increment limit!"
  writen !GS9 "  Would reach the AM2 increment limit - quitting!"
  goto done
end if
if !LR6 < -306.0 then
  beep
  display "Sorry, You'd be running into the AM2 decrement limit!"
  writen !GS9 "  Would reach the AM2 decrement limit - quitting!"
  goto done
end if

## CHECK ENDSWITCHES & POWER
perform  checkleglimit_v02
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 900

## GOTO POSITION 1 (X/Y) =      19.69     -2.685
tcblock mbam1inc 18
set !LI3 = 18
tcblock mbam2inc 14
set !LI4 = 14
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 2 (X/Y) =        1.79     -2.685
tcblock mbam1dec 14
set !LI3 = !LI3 - 14
tcblock mbam2dec 15
set !LI4 = !LI4 - 15
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 3 (X/Y) =       1.79     15.215
tcblock mbam1dec 19
set !LI3 = !LI3 - 19
tcblock mbam2inc 19
set !LI4 = !LI4 + 19
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 4 (X/Y) =       19.69     33.115
tcblock mbam1dec 6
set !LI3 = !LI3 - 6
tcblock mbam2inc 34
set !LI4 = !LI4 + 34
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 5 (X/Y) =      55.49     15.215
tcblock mbam1inc 46
set !LI3 = !LI3 + 46
tcblock mbam2inc 11
set !LI4 = !LI4 + 11
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 6 (X/Y) =      73.39    -20.585
tcblock mbam1inc 52
set !LI3 = !LI3 + 52
tcblock mbam2dec 22
set !LI4 = !LI4 - 22
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 7 (X/Y) =      37.59    -56.385
tcblock mbam1inc 12
set !LI3 = !LI3 + 12
tcblock mbam2dec 69
set !LI4 = !LI4 - 69
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 8 (X/Y) =       1.79    -56.385
tcblock mbam1dec 27
set !LI3 = !LI3 - 27
tcblock mbam2dec 30
set !LI4 = !LI4 - 30
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 9 (X/Y) =      -51.91    -38.485
tcblock mbam1dec 60
set !LI3 = !LI3 - 60
tcblock mbam2dec 27
set !LI4 = !LI4 - 27
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 10 (X/Y) =     -69.81     33.115
tcblock mbam1dec 90
set !LI3 = !LI3 - 90
tcblock mbam2inc 61
set !LI4 = !LI4 + 61
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 11 (X/Y) =     -34.01     86.815
tcblock mbam1dec 31
set !LI3 = !LI3 - 31
tcblock mbam2inc 87
set !LI4 = !LI4 + 87
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 12 (X/Y) =       55.49    104.715
tcblock mbam1inc 48
set !LI3 = !LI3 + 48
tcblock mbam2inc 95
set !LI4 = !LI4 + 95
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 13 (X/Y) =      162.89     15.215
tcblock mbam1inc 177
set !LI3 = !LI3 + 177
tcblock mbam2dec 3
set !LI4 = !LI4 - 3
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 14 (X/Y) =     144.99    -92.185
tcblock mbam1inc 102
set !LI3 = !LI3 + 102
tcblock mbam2dec 129
set !LI4 = !LI4 - 129
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 15 (X/Y) =      55.49   -145.885
tcblock mbam1dec 10
set !LI3 = !LI3 - 10
tcblock mbam2dec 133
set !LI4 = !LI4 - 133
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 16 (X/Y) =     -34.01   -145.885
tcblock mbam1dec 67
set !LI3 = !LI3 - 67
tcblock mbam2dec 76
set !LI4 = !LI4 - 76
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 17 (X/Y) =    -159.31    -74.285
tcblock mbam1dec 171
set !LI3 = !LI3 - 171
tcblock mbam2dec 31
set !LI4 = !LI4 - 31
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 18 (X/Y) =     -141.41     15.215
tcblock mbam1dec 82
set !LI3 = !LI3 - 82
tcblock mbam2inc 110
set !LI4 = !LI4 + 110
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 19 (X/Y) =    -141.41    140.515
tcblock mbam1dec 135
set !LI3 = !LI3 - 135
tcblock mbam2inc 132
set !LI4 = !LI4 + 132
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

## GOTO POSITION 20 (X/Y) =       1.79    176.315
tcblock mbam1inc 69
set !LI3 = !LI3 + 69
tcblock mbam2inc 160
set !LI4 = !LI4 + 160
set !LI1 = !LI1 + !LI3
set !LI2 = !LI2 + !LI4
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 900

set !LI3 = -1 * !LI3
set !LI4 = -1 * !LI4
write !GS9 "To center legs again, do (AM1, AM2): %4d" !LI3
writen !GS9 ",  %4d" !LI4

done:
writen !GS9 "Finished procedure legff : %s" !TIME
writen !GS9 " "
display "Finished Procedure legff                      "
perform close_log_v03
beep
beep
display "To center, send (AM1, AM2):%4d" !LI3
display ",%4d" !LI4 to 32

return
