#! /bin/tcsh -f # # operate the detector cover plunger # # goto Setup Help: cat << EOF usage: $0 [on|off] EOF exit 9 Return_from_Setup: getpos: ################################################################################## # retrieve the state of the cover plunger (by any means neccesary) set state = "" if("$state" == "") then # read the position from XOS set state = `echo -n "" | xos_exchange.tcl $control 1 "over " 1 |& awk '/configure_bistable/ && /over/{print}'` if($#state >= 5) then set cover = "$state[2] $state[3]" set in = $state[4] set out = $state[5] set state = $state[6] set USE_XOS if("$goal" == "on") set goal = "$in" if("$goal" == "off") set goal = "$out" else unset USE_XOS 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[1] && ! $bit[2]) set state = "on" if(! $bit[1] && $bit[2]) set state = "off" 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[2] == 1) set state = 'in?' if($wagobit[2] == 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 cover position." echo "Sorry! " set BAD goto report endif ################################################################################## if(! $?goal) set goal = "" if(("$goal" == "")||("$goal" == "$state")) goto report move: ################################################################################## # put the cover 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 $cover $goal\ngtos_set_bistable_state $cover $goal" |\ xos_exchange.tcl $control 2 "$cover" 1 >&! ${tempfile} set state = `awk -v cover="$cover" 'cover{print $NF}' ${tempfile} | tail -1` rm -f ${tempfile} if("$state" == "$goal") goto report endif if("$state" != "") goto wait ################################################################################## # 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 backlight Fluor Se Cu Al if(("$goal" == "in")||("$goal" == "on")||("$goal" == "extended")) then set wagobit[2] = 0 endif if(("$goal" == "out")||("$goal" == "off")||("$goal" == "retracted")) then set wagobit[2] = 1 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 = 15 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[1] && ! $bit[2]) set state = "on" if(! $bit[1] && $bit[2]) set state = "off" 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 "cover 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 = on set out = off set cover = cover set wago = wago-0 if("$beamline" != 831) set wago = bl${beamline}c set control = $wago # find a writable temporary file name set tempfile = /tmp/cover_tmp$$ # interpret the command line set goal = "$1" if(("$goal" != "in") && ("$goal" != "out") && ("$goal" != "on") && ("$goal" != "off") && ("$goal" != "extended") && ("$goal" != "retracted")) then if("$1" != "") echo "usage $0 [on|off]" endif goto Return_from_Setup