#! /bin/csh -f # # operate the vertical stage motor using XOS or the direct delta-tau connection # # goto Setup Help: cat << EOF usage: stage.com [newpos] where: newpos - the desired new stage position example: stage.com up stage.com down stage.com -10 EOF exit 9 Return_from_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 set pmac = pmac1 if("$beamline" != 831) set pmac = bl${beamline} set control = bl${beamline}c set tempfile = /tmp/stage_temp$$ # default to absolute move unset relative # interpret command line first if("$1" != "") set move set goal = "" set up = "" set low = "" set down = "" foreach arg ( $* ) if("$arg" == "by") then set relative continue endif if("$arg" == "to") then unset relative continue endif if("$arg" == "up") then set goal = up continue endif if("$arg" == "low") then set goal = low continue endif if("$arg" == "down") then set goal = down continue endif if("$arg" == "save") then set goal = "save" continue endif # interpret numbers as the desired new stage position (in mm) set goal = `echo $arg | awk '$1 ~ /^[0-9-]/ || /^.[0-9]/{print $1+0}' | awk '$1<210 && $1>-200'` end onintr report get_position: # query the motor controller for the stage's current position in mm set pos = `echo -n "#4p\r" | sock_exchange.tcl $pmac 14001 1 "\006" |& awk '{gsub("[\006\r]"," ",$0); print}' | awk 'NF==1{print $1/2000}'` # see if deltatau responded amicably if($#pos == 1) then if($?move) goto move goto finish endif # didn't work, try DCSS set pos = "" # pretend to be a gui, get motor state, and then disconnect echo -n "" |\ xos3_exchange.tcl 1 "set_motor_dependency" 10 >&! ${tempfile} # retreive stage's absolute position in mm set pos = `awk '/^stog_configure_real_motor vertical_collimator/{print $5}' ${tempfile}` set up = `awk '/^stog_configure_pseudo_motor stage_up/{print $5}' ${tempfile}` set low = `awk '/^stog_configure_pseudo_motor stage_low/{print $5}' ${tempfile}` set down = `awk '/^stog_configure_pseudo_motor stage_down/{print $5}' ${tempfile}` #cp ${tempfile} /tmp/${USER}/what_the_fuck rm -f ${tempfile} >& /dev/null if($#pos == 1) goto got_position # try BLAK-ICE echo -n "" |\ xos_exchange.tcl $control 1 "over out" 1 >&! ${tempfile} # retreive stage's absolute position in mm set dcspos = `awk '/^stog_configure_real_motor vertical_collimator/{print $5}' ${tempfile}` rm -f ${tempfile} >& /dev/null set pos = `echo "$dcspos" | awk 'NF>0{print $1/1000}'` # see if BLAK-ICE responded amicably if($#pos == 1) goto got_position # retreive stage's position from labview? set pos = `pxmotor.com Collimator Y |& awk '$NF!~/[^0-9\.-]/{print $NF}' | tail -1` if("$pos" != "") goto got_position # see if BLAK-ICE responded amicably if($#pos == 1) goto got_position # didn't work, try something else? set BAD = "unable to get stage position! " set pos = "" got_position: # jump ahead if all we need to do is report the position if($?move) goto move goto finish move: #set pos = "" if(! $?goal) set goal = "" if("$goal" == "") goto finish # deduce values of "English" goal positions if("$up" == "") then set up = 0 # get the "up" position of the stage, and define all named moves relative to it if(-e /data/calibrations/stage_up.txt) then set up = `cat /data/calibrations/stage_up.txt` endif endif if ("$low" == "") set low = `echo $up | awk '{print $1-25}'` if ("$down" == "") set down = `echo $up | awk '{print $1-193}'` # interpret "English" goal positions if ("$goal" == "up") set goal = "$up" if ("$goal" == "down") set goal = "$down" if ("$goal" == "low") set goal = "$low" # compute new desired position in mm if($?relative) then set goal = `echo "$pos $goal" | awk '{print ($1+$2)}'` endif # see if "goal" is not really a move if("$goal" == "save") then # just store the new up position goto finish endif # command the motor to move! try_again: echo "$goal" | awk '/save/{exit} {printf "#4j=%f\r", 2000*$1}' |\ sock_exchange.tcl $pmac 14001 1 "\006" >& /dev/null if($status) goto XOS_move usleep 100000 set speed = 1 while ("$speed" != "0") # now check and see where they are set info = `echo -n "#4p\r#4v\r" | sock_exchange.tcl $pmac 14001 2 "\006" | awk '{gsub("[\006\r]"," ",$0); print}'` if($#info != 2) then echo "problem with network : $info" #echo "mcf\n8\n" | sock_exchange.tcl $pmac 9999 > /dev/null sleep 2 continue endif set reached = `echo "$info" | awk '{printf "%.3f", $1/2000}'` set speed = "$info[2]" if($?debug) echo "GOTHERE $info $speed" echo "$reached" if("$speed" == "0") continue usleep 500000 end # see if we made it set delta = `echo "$goal $reached" | awk '{printf "%d", 1000*sqrt(($1-$2)^2)}'` if(($delta > 1)&&($?RETRY)) then # finite number of retries if("$RETRY" =~ *[0-9]) set RETRY = `echo $RETRY | awk '{printf "%d", $1-1}'` if("$RETRY" == "0") set RETRY = "" if("$RETRY" == "") unset RETRY set delta = `echo "$goal $info" | awk '{print $1-$2}'` echo "stage is $delta mm off the mark" echo "trying again..." echo -n "#4j/\r" | sock_exchange.tcl $pmac 14001 1 "\006" 3 > /dev/null goto try_again endif unset move goto get_position XOS_move: # convert goal position to some other units? set xosgoal = `echo $goal | awk '{print 1*$1}'` unset move # pretend to be a gui, set motor states, and then disconnect echo "gtos_become_master force\ngtos_start_motor_move vertical_collimator $xosgoal" |\ xos3_exchange.tcl 1 "stog_motor_move_completed vertical_collimator" 30 |&\ tee ${tempfile} |\ awk '/update_motor_position vertical_collimator/{printf "%.3f\n", $3}' set pos = `awk '/completed vertical_collimator/{print $3}' ${tempfile} | tail -1` rm -f ${tempfile} if ("$pos" != "") goto finish # try BLAK-ICE set dcsgoal = `echo $goal | awk '{print 1000*$1}'` if($beamline == 831) set dcsgoal = `echo $goal | awk '{print 10000*$1}'` echo "gtos_become_master force\ngtos_start_motor_move vertical_collimator $dcsgoal" |\ xos_exchange.tcl $control 1 "stog_motor_move_completed vertical_collimator" 30 |&\ tee ${tempfile} |\ awk '/update_motor_position vertical_collimator/{printf "%.3f\n", $3}' set pos = `tail -1 ${tempfile}` rm -f ${tempfile} if("$pos" != "") goto get_position # try Labview directly set pos = `pxmotor.com Collimator Y $goal |& awk '$NF!~/[^0-9\.-]/{print $NF}' | tail -1` if("$pos" != "") goto get_position set BAD = "unable to move stage" unset move goto get_position report: finish: if("$goal" == "save") then change.com stage up $pos echo "gtos_become_master force\ngtos_start_motor_move stage_up $pos" |\ xos3_exchange.tcl 1 "completed stage_up" 10 > /dev/null if(! $status) echo "stage_up calibration stored in DCSS" # there is no way to do this in BLACK-ICE if(-w /data/calibrations/) then rm -f /data/calibrations/stage_up.txt >& /dev/null echo "$pos" >! /data/calibrations/stage_up.txt if(! $status) echo "current stage position now saved in /data/calibrations/stage_up.txt" endif if ( "$beamline" =~ 82? ) then # prepare to login to the beamline database set mysql = "mysql --host=131.243.74.103 --user=ewcornel --password=2cats3fish mcf" set ID = 1000 foreach name ( bl$beamline Pmac1 "Vertical Collimator" ) # trace down the silly tree of numbers by searching for names $mysql << EOF >! ${tempfile} select rl.Object_ID as ID from Object_Relationship rl,Beam_Line_Object bl where rl.is_current=1 and bl.is_current=1 and rl.Object_ID=bl.Object_ID and Parent_ID=$ID and name="$name" ; EOF if($status) break echo -n "${ID}'s $name is " set ID = `tail -1 ${tempfile}` echo "$ID" end rm -f ${tempfile} if("$ID" == "") then set BAD = "stupid database..." goto exit endif echo "we need to put $beam into object $ID" $mysql << EOF >& /dev/null update Beam_Line_Param set value="$dcspos" where Object_ID=$ID and name="In position" and is_current=1; EOF echo "probably updated stage-up position in database." echo "you must restart DCS for changes to take effect." endif set goal = "" endif if("$pos" == "") then set pos = "unknown" else if(! $?goal) set goal if("$goal" != "") then set miss = `echo "$goal $pos" | awk '{miss=sqrt(($1-$2)^2)} miss>0.01{print $1-$2}'` if("$miss" != "") set BAD = "off by $miss mm" endif set pos = `echo "$pos" | awk '{printf "%.3f mm", $1}'` endif echo "stage position is now $pos" 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 set pmac = pmac1 if("$beamline" != 831) set pmac = bl${beamline} set control = bl${beamline}c set tempfile = /tmp/stage_temp$$ # default to absolute move unset relative ############################################################# # 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 command line first if("$1" != "") set move set goal = "" set up = "" set low = "" set down = "" foreach arg ( $* ) if("$arg" == "by") then set relative continue endif if("$arg" == "to") then unset relative continue endif if("$arg" == "up") then set goal = up continue endif if("$arg" == "low") then set goal = low continue endif if("$arg" == "down") then set goal = down continue endif if("$arg" == "save") then set goal = "save" continue endif # interpret numbers as the desired new stage position (in mm) set goal = `echo $arg | awk '$1 ~ /^[0-9-]/ || /^.[0-9]/{print $1+0}' | awk '$1<210 && $1>-200'` end goto Return_from_Setup