###CHECKPOINT ARGS:  -queue=0 -nostartlog -nolog-flow -nolog-blocks
program "%Z% MODULE: %M% RELEASE: %R%.%L% CREATED: %G% %U%"
source "/mdisw/cfl/sim/src/iss_cal.src"
descr "iss_cal - Take exposures while offsetting PZT's"
# program iss_cal.src, created 96.01.26 Igor Zayer 
#
#	Offsets one PZT at a time & takes measurements in ISS mode
#	NOT Stand-alone - though nothing will break. Makes only 
#	sense if MDI is in ISS mode & ISS is properly nulled
#
# Calls: log_iss_par
# ------
#
# Variable usage:
#----------------
# !GS8: name of 160k datafile
# !GS9: name of user logfile 
# 
# !LI1: original electronic x-offset
# !LI2: original electronic y-offset
# !LI3: uplinked electronic x-/y-offset
#----------------

display "Executing procedure iss_cal"

## MAKE SURE LOOP IS OPEN
tcb mbltopnl
wait time 8   ## queueing has been turned off just prior to this CF
writen !GS9 " ISS OPEN LOOP"

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

## OFFSET PZT A 
tcblock mbltaofs 34
writen !GS9 " PZT offset A = 34"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltaofs 64
writen !GS9 " PZT offset A = 64 = HOME"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltaofs 94
writen !GS9 " PZT offset A = 94"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltaofs 64	## go back home
writen !GS9 " PZT offset HOME"
writen !GS9 " "
perf log_iss_par_v03

## OFFSET PZT B
tcblock mbltbofs 34
writen !GS9 " PZT offset B = 34"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltbofs 64
writen !GS9 " PZT offset B = 64 = HOME"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltbofs 94
writen !GS9 " PZT offset B = 94"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltbofs 64	## go back home
writen !GS9 " PZT offset HOME"
writen !GS9 " "
perf log_iss_par_v03

## OFFSET PZT C 
tcblock mbltcofs 34
writen !GS9 " PZT offset C = 34"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltcofs 64
writen !GS9 " PZT offset C = 64 = HOME"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltcofs 94
writen !GS9 " PZT offset C = 94"
writen !GS9 " "
perf log_iss_par_v03

tcblock mbltcofs 64	## go back home
writen !GS9 " PZT offset HOME"
writen !GS9 " "
perf log_iss_par_v03

writen !GS9 " DONE with PZT offsetting... "
writen !GS9 " "

## NOW CLOSE THE LOOP
tcb mbltclsl
writen !GS9 " ISS CLOSED LOOP:"

## NOW PLAY WITH X- & Y-OFFSETS
writen !GS9 " Changing x- & y- electronic offsets.."
perf log_iss_par_v03
set !LI1 = getint(mkltxofs)	## STORE X0 & WORK IN DECIMAL
if !LI1 > 127 then
  set !LI1 = !LI1 - 256
end if
set !LI2 = getint(mkltyofs)	## STORE Y0 & WORK IN DECIMAL
if !LI2 > 127 then
  set !LI2 = !LI2 - 256
end if

## STEP X-OFFSETS
set !LI3 = !LI1 - 5	## X0-5
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltxofs !LI3
perf log_iss_par_v03

set !LI3 = !LI1 - 10	## X0-10
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltxofs !LI3
perf log_iss_par_v03

set !LI3 = !LI1 - 15	## X0-15
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltxofs !LI3
perf log_iss_par_v03

set !LI3 = !LI1 + 5	## X0+5
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltxofs !LI3
perf log_iss_par_v03

set !LI3 = !LI1 + 10	## X0+10
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltxofs !LI3
perf log_iss_par_v03

set !LI3 = !LI1 + 15	## X0+15
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltxofs !LI3
perf log_iss_par_v03

if !LI1 < 0 then	## BACK TO X0 IN 2'S COMPLEMENT HEX
  set !LI1 = !LI1 + 256
end if
tcblock mbltxofs !LI1 
perf log_iss_par_v03

## STEP Y-OFFSETS
set !LI3 = !LI2 - 5	## Y0-5
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltyofs !LI3
perf log_iss_par_v03

set !LI3 = !LI2 - 10	## Y0-10
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltyofs !LI3
perf log_iss_par_v03

set !LI3 = !LI2 - 15	## Y0-15
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltyofs !LI3
perf log_iss_par_v03

set !LI3 = !LI2 + 5	## Y0+5
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltyofs !LI3
perf log_iss_par_v03

set !LI3 = !LI2 + 10	## Y0+10
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltyofs !LI3
perf log_iss_par_v03

set !LI3 = !LI2 + 15	## Y0+15
if !LI3 < 0 then	## UPLINK THE VALUES IN 2'S COMPLEMENT HEX
  set !LI3 = !LI3 + 256
end if
tcblock mbltyofs !LI3
perf log_iss_par_v03

if !LI2 < 0 then	## BACK TO Y0 IN 2'S COMPLEMENT HEX
  set !LI2 = !LI2 + 256
end if
tcblock mbltyofs !LI2
perf log_iss_par_v03

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