#! /bin/tcsh -f # # operate the foil attenuator plungers # # goto Setup Help: cat << EOF usage: $0 [in|out] EOF exit 9 Return_from_Setup: get_position: ################################################################################## # retrieve the state of the foil plunger (by any means neccesary) set state = "" # read the position from DCSS set state = `echo -n "" | xos3_exchange.tcl 1 "motor_dependency" 1 |& awk '/configure_/ && / '$foil' /{print}'` if($#state >= 5) then set foil = $state[2] set state = $state[$#state] if (("$state" == "open")||("$state" == "out")||("$state" == "retracted")) set state = "out" if (("$state" =~ close*)||("$state" == "in")||("$state" == "extended")) set state = "in" set USE_XOS if("$state" != "") goto got_position endif # that didn't work, read the position from BLAK-ICE set state = `echo -n "" | xos_exchange.tcl $control 1 "shutter $foil" 1 |& awk '/configure_/ && / '$foil' /{print}'` if($#state >= 5) then set foil = $state[2] set state = $state[$#state] if (("$state" == "open")||("$state" == "out")||("$state" == "retracted")) set state = "out" if (("$state" =~ close*)||("$state" == "in")||("$state" == "extended")) set state = "in" set USE_BLAKICE if("$state" != "") goto got_position endif # that didn't work, try labview directly? set state = `blmotor.com $foil Filter |& awk '$NF ~ /^1/{print "in"} $NF~/^0/{print "out"}'` if("$state" != "") goto got_position ################################################################################## # that didn't work, try finding the hardware if("$state" == "" && "$wago" != "") 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[$limaddr[1]] && ! $bit[$limaddr[2]]) set state = "out" if(! $bit[$limaddr[1]] && $bit[$limaddr[2]]) set state = "in" endif if("$state" != "") break end if("$state" != "") set wago = $host endif rm -f ${tempfile} >& /dev/null if("$state" == "" && "$wago" != "") 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[$cmdaddr] == 1) set state = 'in?' if($wagobit[$cmdaddr] == 0) set state = 'out?' endif if("$state" != "") break end if("$state" != "") set wago = $host endif rm -f ${tempfile} >& /dev/null ################################################################################## if("$state" == "") then set BAD = "unable to read $foil foil." goto report endif ################################################################################## got_position: if(! $?goal) set goal = "" if(("$goal" == "")||("$goal" == "$state")) goto report if($?waiting) then # we told the thing to move, but we are not at the goal yet usleep 100000 set waiting = `echo $waiting | awk '{print $1-1}'` if($waiting == 0) then set BAD = "timed out waiting for move to complete" unset waiting endif goto get_position endif move: ################################################################################## # put the foil plunger where the user wants it (by any means neccesary) set state = "" if($?USE_XOS) then if(("$goal" =~ close*) || "$goal" == "in") set goal = in if(("$goal" == open) || "$goal" == "out") set goal = out if("$goal" == in) set xosgoal = "closed" if("$goal" == out) set xosgoal = "open" echo "gtos_become_master force\ngtos_set_shutter_state $foil $xosgoal\n" |\ xos3_exchange.tcl 2 "shutter $foil" 1 >&! ${tempfile} set state = `awk '/'$foil'/{print $NF}' ${tempfile} | tail -1` rm -f ${tempfile} if(("$state" =~ close*) || "$state" == "in") set state = in if(("$state" == open) || "$state" == "out") set state = out endif if("$state" == "$goal") goto report if("$state" != "") goto wait if($?USE_BLAKICE) then if(("$goal" =~ close*) || "$goal" == "in") set goal = in if(("$goal" == open) || "$goal" == "out") set goal = out if("$goal" == in) set xosgoal = "in" if("$goal" == out) set xosgoal = "out" echo "gtos_become_master force\ngtos_set_shutter_state $foil $xosgoal\ngtos_set_bistable_state $foil $xosgoal" |\ xos_exchange.tcl $control 2 "$foil" 1 >&! ${tempfile} set state = `awk '/'$foil'/{print $NF}' ${tempfile} | tail -1` rm -f ${tempfile} if(("$state" =~ close*) || "$state" == "in") set state = in if(("$state" == open) || "$state" == "out") set state = out endif if("$state" == "$goal") goto report if("$state" != "") goto wait # that didn't work, try labview directly set bit = `echo $goal | awk '{print (/close/ || /in/)}'` set state = `blmotor.com $foil Filter $bit |& awk '$NF ~ /^1/{print "in"} $NF~/^0/{print "out"}'` if("$state" == "$goal") goto report if("$state" != "") goto wait ################################################################################## # that 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 '! /[a-z]/{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) then # wago bits: x x light Diode Fluor Se Cu Al if(("$goal" == "in")||("$goal" =~ close*)) then set wagobit[$cmdaddr] = 1 endif if(("$goal" == "out")||("$goal" == "open")) then set wagobit[$cmdaddr] = 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 else # connect to wago failed? # set BAD = "unable to move $foil foil" endif wait: # wait for plunger to reach desired state set waiting = 15 set state = "" goto get_position ################################################################################## report: # report on status of plunger if("$state" == "open") set state = "out" if("$state" =~ close*) set state = "in" if("$state" == "") set state = "in unknown state" echo "$foil foil is $state" if("$goal" != "" && "$state" != "$goal") set BAD = "unable to move $foil foil" if($?BAD) then echo "ERROR: $BAD" exit 9 endif 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 goal = "" set foil = Al set wago = wago-0 if("$beamline" != 831) set wago = bl${beamline}c set control = $wago # find a writable temporary file name set tempfile = /tmp/attenuator_tmp$$ ############################################################# # platform-specific issues set test = `echo "123asdf" | awk '{print $1+0}'` if("$test" != "123") then alias awk nawk set test = `echo "123asdf" | awk '{print $1+0}'` if("$test" != "123") alias awk gawk set test = `echo "123asdf" | awk '{print $1+0}'` if("$test" != "123") then echo "ERROR: awk no good" exit 9 endif endif set test = `echo -n "\r" |& od -c | wc -w` if("$test" != 3) then alias echo /bin/echo set test = `echo -n "\r" |& od -c | wc -w` if("$test" != 3) alias echo '/bin/echo -e' set test = `echo -n "\r" |& od -c | wc -w` if("$test" != 3) alias echo /usr/bin/echo set test = `echo -n "\r" |& od -c | wc -w` if("$test" != 3) alias echo '/bin/echo \!* | awk '\''/^-n/{printf "%s", substr($0,4) ; exit} {print}'\' set test = `echo -n "\r" |& od -c | wc -w` if("$test" != 3) then echo "ERROR: echo command is broken" exit 9 endif endif set test = `echo "string is digit 1" | /usr/bin/tclsh |& wc -l` if($test != 0) then set test = `echo "string is digit 1" | tclsh |& wc -l` if($test != 0) alias tclsh tclsh8.2 set test = `echo "string is digit 1" | tclsh |& wc -l` if($test != 0) alias tclsh tclsh8.3 set test = `echo "string is digit 1" | tclsh |& wc -l` if($test != 0) then setenv LD_LIBRARY_PATH ~jamesh alias tclsh ~jamesh/tclsh8.2 echo "" | tclsh >& /dev/null if("$status") then echo "ERROR: tclsh not available" exit 9 endif endif alias sock_exchange.tcl "tclsh `which sock_exchange.tcl` \!*" endif ###################################################################### # interpret the command line foreach arg ( $* ) if(("$arg" == "in") || ("$arg" == "extended") || ("$arg" == "close")) set goal = in if(("$arg" == "out") || ("$arg" == "retracted")) set goal = out if(("$arg" == "Al") || ("$arg" == "al") || "arg" == "aluminium") set foil = Al if(("$arg" == "Cu") || ("$arg" == "cu") || "arg" == "copper") set foil = Cu if(("$arg" == "Se") || ("$arg" == "se") || "arg" == "selenium") set foil = Se if(("$arg" == "Au") || ("$arg" == "au") || "arg" == "gold") set foil = Au if(("$arg" == "Zn") || ("$arg" == "zn") || "arg" == "zinc") set foil = Zn end if(("$goal" != "out") && ("$goal" != "in") && ("$goal" != "")) then goto Help endif set cmdaddr = "" if($foil == Al) set cmdaddr = 8 if($foil == Cu) set cmdaddr = 7 if($foil == Se) set cmdaddr = 6 set limaddr = `echo $cmdaddr | awk '{print $1*2-1, $1*2}'` if("$cmdaddr" == "") set wago = "" goto Return_from_Setup