#! /bin/tcsh -f # # operate the scintilator plunger # # goto Setup Help: cat << EOF usage: $0 [in|out] EOF exit 9 Return_from_Setup: getpos: ################################################################################## # retrieve the state of the Scintilator plunger (by any means neccesary) set state = "" if("$state" == "") then # read the position from XOS set state = `echo -n "" | xos_exchange.tcl $control 1 "Fluor" 1 |& awk '/configure_bistable/ && /luorescen/{print}'` if($#state >= 5) then set Scintilator = $state[2] set in = $state[3] set out = $state[4] set state = $state[5] set USE_XOS if("$goal" == "in") set goal = "$in" if("$goal" == "out") set goal = "$out" else set state = "" endif endif ################################################################################## if("$state" == "") then # find the wago foreach host ( $wago ) echo -n "\000\000\000\000\000\006\000\001\000\000\000\025" |\ sock_exchange.tcl $host 502 7 "\000" 1 >&! $tempfile if($status) continue set statusword = `od -j 9 -d $tempfile |& awk '! /a-z/{print $2}'` if ("$statusword" == "") continue set bit = `echo "$statusword" | awk '{for(r=2^15;r>=1;r/=2){printf "%d ", $1/r; $1=$1%r}; print ""; exit}'` # make sure both limit switches match expectations if($#bit == 16) then if( $bit[9] && ! $bit[10]) set state = "out" if(! $bit[9] && $bit[10]) set state = "in" endif if("$state" != "") break end if("$state" != "") set wago = $host endif rm -f ${tempfile} >& /dev/null if("$state" == "") then # try to read comanded position # find the wago foreach host ( $wago ) echo -n "\000\000\000\000\000\006\000\003\002\004\000\005" |\ sock_exchange.tcl $host 502 9 "\000" 1 >&! $tempfile if($status) continue set cmdbyte = `od -j 9 -t d1 ${tempfile} |& awk '! /[a-z]/{print $3}'` rm -f ${tempfile} >& /dev/null if("$cmdbyte" == "") continue set wagobit = `echo $cmdbyte | awk '{for(r=2^7;r>=1;r/=2){printf "%d ", $1/r; $1=$1%r}; print ""; exit}'` if($#wagobit == 8) then if($wagobit[5] == 1) set state = 'in?' if($wagobit[5] == 0) set state = 'out?' endif if("$state" != "") break end if("$state" != "") set wago = $host endif rm -f ${tempfile} >& /dev/null ################################################################################## if("$state" == "") then echo "unable to read scintilator position." echo "Sorry! " set BAD goto report endif ################################################################################## if(! $?goal) set goal = "" if(("$goal" == "")||("$goal" == "$state")) goto report move: ################################################################################## # put the Scintilator plunger where the user wants it (by any means neccesary) set state = "" if($?USE_XOS) then echo "gtos_become_master force\ngtos_set_shutter_state $Scintilator $goal\ngtos_set_bistable_state $Scintilator $goal" |\ xos_exchange.tcl $control 2 "$Scintilator" 1 >&! ${tempfile} set state = `awk '/'$Scintilator'/{print $NF}' ${tempfile} | tail -1` rm -f ${tempfile} if("$state" == "$goal") goto report endif if("$state" != "") goto wait #echo "no reply from dcss $state $goal ..." ################################################################################## # DCSS didn't work, try talking directly to WAGO # retrieve current (commanded) state of WAGO echo -n "\000\000\000\000\000\006\000\003\002\004\000\005" |\ sock_exchange.tcl $wago 502 9 "\000" 1 |\ od -j 9 -t d1 | awk '{print $3}' |\ awk '{for(r=2^7;r>=1;r/=2){printf "%d ", $1/r; $1=$1%r}; print ""; exit}' |\ cat >! ${tempfile} set wagobit = `cat $tempfile` rm -f ${tempfile} if($#wagobit != 8) set wagobit = ( 0 0 0 0 0 0 0 0 ) # wago bits: x x light Diode Fluor Se Cu Al if(("$goal" == "in")||("$goal" == "extended")) then set wagobit[5] = 1 endif if(("$goal" == "out")||("$goal" == "retracted")) then set wagobit[5] = 0 endif # command the WAGO to our new state echo "$wagobit" |\ awk '{printf "\000\000\000\000\000\011\000\020\002\004\000\003\000\000%c", $1*128+$2*64+$3*32+$4*16+$5*8+$6*4+$7*2+$8*1}' |\ sock_exchange.tcl $wago 502 6 "\000" 1 |\ od -b > /dev/null wait: # wait for plunger to reach desired state set timeout = 50 set state = "" while (("$state" != "$goal")&&($timeout > 0)) # get limit switch statuses (as bits) from WAGO set statusword = `echo -n "\000\000\000\000\000\006\000\001\000\000\000\025" | sock_exchange.tcl $wago 502 7 "\000" 1 | od -j 9 -d | awk '{print $2}'` set bit = `echo "$statusword" | awk '{for(r=2^15;r>=1;r/=2){printf "%d ", $1/r; $1=$1%r}; print ""; exit}'` # make sure both limit switches match if($#bit == 16) then if( $bit[9] && ! $bit[10]) set state = "out" if(! $bit[9] && $bit[10]) set state = "in" endif if("$state" != "$goal") then usleep 100000 @ timeout = ( $timeout - 1 ) endif end if($timeout == 0) then # we ran out of time set BAD = timeout set goal = "" goto getpos endif ################################################################################## report: # report on status of plunger if("$state" == "") set state = "in unknown state" echo "scintilator is $state" if($?BAD) exit 9 exit ################################################################################## Setup: if(! $?beamline) then # default to beamline this computer belongs to setenv beamline `beamline.com` if($status) then echo "ERROR: $beamline" exit 9 endif endif #if(! $?beamline) setenv beamline 831 # default names set in = in set out = out set Scintilator = Scintilator set wago = wago-0 if("$beamline" != 831) set wago = bl${beamline}c set control = $wago # find a writable temporary file name set tempfile = /tmp/Scintilator_tmp$$ # interpret the command line set goal = "$1" if(("$goal" != "in") && ("$goal" != "out") && ("$goal" != "extended") && ("$goal" != "retracted")) then if("$1" != "") goto Help endif goto Return_from_Setup