# Generated driver for shutters # vim: ft=tcl tabstop=8 softtabstop=2 shiftwidth=2 nocindent smartindent # namespace eval ::scobj::shutters { set debug_threshold 5 } proc ::scobj::shutters::debug_log {tc_root debug_level debug_string} { set catch_status [ catch { set debug_threshold [hgetpropval ${tc_root} debug_threshold] if {${debug_level} >= ${debug_threshold}} { set now [clock seconds] set ts [clock format ${now} -format "%Y%m%d"] set log_file_name "../log/shutters_[basename ${tc_root}]_${ts}.log" set fd [open "${log_file_name}" "a"] set ts [clock format ${now} -format "%T"] puts ${fd} "${ts} ${debug_string}" close ${fd} } } catch_message ] } proc ::scobj::shutters::sics_log {debug_level debug_string} { set catch_status [ catch { set debug_threshold ${::scobj::shutters::debug_threshold} if {${debug_level} >= ${debug_threshold}} { sicslog "::scobj::shutters::${debug_string}" } } catch_message ] } # check function for hset change proc ::scobj::shutters::checkrange {tc_root} { set catch_status [ catch { debug_log ${tc_root} 1 "checkrange tc_root=${tc_root} sct=[sct] target=[sct target]" set setpoint [sct target] if { [hpropexists [sct] lowerlimit] } { set lolimit [sct lowerlimit] } else { # lowerlimit not set, use target set lolimit [sct target] } if { [hpropexists [sct] upperlimit] } { set hilimit [sct upperlimit] } else { # upperlimit not set, use target set hilimit [sct target] } # checkrange hook code goes here if { ${setpoint} < ${lolimit} || ${setpoint} > ${hilimit} } { error "setpoint ${setpoint} violates limits (${lolimit}..${hilimit}) on [sct]" } return OK } catch_message ] handle_exception ${catch_status} ${catch_message} } # function to request the read of a parameter on a device proc ::scobj::shutters::getValue {tc_root nextState cmd_str} { set catch_status [ catch { debug_log ${tc_root} 1 "getValue tc_root=${tc_root} sct=[sct] cmd=${cmd_str}" if { [hpropexists [sct] geterror] } { hdelprop [sct] geterror } set cmd "${cmd_str}" # getValue hook code goes here debug_log ${tc_root} 1 "getValue sct send ${cmd}" if {![string equal -nocase -length 10 ${cmd} "@@NOSEND@@"]} { sct send "${cmd}" } return ${nextState} } catch_message ] handle_exception ${catch_status} ${catch_message} } # function to check the write parameter on a device proc ::scobj::shutters::noResponse {tc_root} { set catch_status [ catch { debug_log ${tc_root} 1 "noResponse tc_root=${tc_root} sct=[sct] resp=[sct result]" # noResponse hook code goes here return "idle" } catch_message ] handle_exception ${catch_status} ${catch_message} } # function to write a parameter value on a device proc ::scobj::shutters::no_write {tc_root nextState cmd_str} { set catch_status [ catch { debug_log ${tc_root} 1 "no_write tc_root=${tc_root} sct=[sct] cmd=${cmd_str}" if { [hpropexists [sct] geterror] } { hdelprop [sct] geterror } set par [sct target] set cmd "${cmd_str}${par}" # no_write hook code starts error "Setting this value is disallowed" # no_write hook code ends if { [hpropexists [sct] geterror] } { debug_log ${tc_root} 9 "[sct] error: [sct geterror]" error "[sct geterror]" } if { [hpropexists [sct] driving] } { if { [hpropexists [sct] writestatus] && [sct writestatus] == "start" } { sct driving 1 } } debug_log ${tc_root} 1 "no_write sct send ${cmd}" if {![string equal -nocase -length 10 ${cmd} "@@NOSEND@@"]} { sct send "${cmd}" } return ${nextState} } catch_message ] handle_exception ${catch_status} ${catch_message} } # function to parse the read of a parameter on a device proc ::scobj::shutters::read_switch_pair {tc_root} { set catch_status [ catch { debug_log ${tc_root} 1 "read_switch_pair tc_root=${tc_root} sct=[sct] result=[sct result]" if { [hpropexists [sct] geterror] } { hdelprop [sct] geterror } set data [sct result] set nextState "idle" if {[string equal -nocase -length 7 ${data} "ASCERR:"]} { # the protocol driver has reported an error sct geterror "${data}" error "[sct geterror]" } # read_switch_pair hook code starts if { [string equal -nocase -length 1 "${data}" "?"] } { sct geterror "Galil error in: '${data}'" set cb_event "fatal_error" } else { set data_list [split [string trim "${data}"]] if { [llength ${data_list}] > 2 && [lindex ${data_list} end] == ":" } { set data_list [lrange ${data_list} 0 1] } if { [llength ${data_list}] == 2 } { set left [expr [lindex ${data_list} 0]] set right [expr [lindex ${data_list} 1]] if { ${left} == 1 && ${right} == 0 } { # open set data "out" set cb_event $data } elseif { ${left} == 0 && ${right} == 1 } { # closed set data "in" set cb_event $data } else { # indeterminate set data "moving" set cb_event $data } } else { sct geterror "Syntax error in: '${data}'=>'${data_list}'" set cb_event "fatal_error" } } call_oneshot [sct] $cb_event # read_switch_pair hook code ends if { [hpropexists [sct] geterror] } { debug_log ${tc_root} 9 "[sct] error: [sct geterror]" error "[sct geterror]" } if { ${data} != [sct oldval] } { debug_log ${tc_root} 1 "[sct] changed to new:${data}, from old:[sct oldval]" sct oldval ${data} sct update ${data} sct utime readtime } return ${nextState} } catch_message ] handle_exception ${catch_status} ${catch_message} } # function to write a parameter value on a device proc ::scobj::shutters::setValue {tc_root nextState cmd_str} { set catch_status [ catch { debug_log ${tc_root} 1 "setValue tc_root=${tc_root} sct=[sct] cmd=${cmd_str}" if { [hpropexists [sct] geterror] } { hdelprop [sct] geterror } set par [sct target] set cmd "${cmd_str}${par}" # setValue hook code goes here if { [hpropexists [sct] driving] } { if { [hpropexists [sct] writestatus] && [sct writestatus] == "start" } { sct driving 1 } } debug_log ${tc_root} 1 "setValue sct send ${cmd}" if {![string equal -nocase -length 10 ${cmd} "@@NOSEND@@"]} { sct send "${cmd}" } return ${nextState} } catch_message ] handle_exception ${catch_status} ${catch_message} } # function to write a parameter value on a device proc ::scobj::shutters::write_switch {tc_root nextState cmd_str} { set catch_status [ catch { debug_log ${tc_root} 1 "write_switch tc_root=${tc_root} sct=[sct] cmd=${cmd_str}" if { [hpropexists [sct] geterror] } { hdelprop [sct] geterror } set par [sct target] set cmd "${cmd_str}${par}" # write_switch hook code starts if { [string equal -nocase -length 2 "${par}" "in"] } { set cmd "SB${cmd_str}" } elseif { [string equal -nocase -length 2 "${par}" "out"] } { set cmd "CB${cmd_str}" } else { sct geterror "Value error: '${par}' not in ('in', 'out')" } # write_switch hook code ends if { [hpropexists [sct] geterror] } { debug_log ${tc_root} 9 "[sct] error: [sct geterror]" error "[sct geterror]" } if { [hpropexists [sct] driving] } { if { [hpropexists [sct] writestatus] && [sct writestatus] == "start" } { sct driving 1 } } debug_log ${tc_root} 1 "write_switch sct send ${cmd}" if {![string equal -nocase -length 10 ${cmd} "@@NOSEND@@"]} { sct send "${cmd}" } return ${nextState} } catch_message ] handle_exception ${catch_status} ${catch_message} } proc ::scobj::shutters::mkDriver { sct_controller name device_class simulation_flag ip_address tcp_port } { ::scobj::shutters::sics_log 9 "::scobj::shutters::mkDriver ${sct_controller} ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port}" set ns "[namespace current]" set catch_status [ catch { MakeSICSObj ${name} SCT_OBJECT sicslist setatt ${name} driver shutters sicslist setatt ${name} klass ${device_class} sicslist setatt ${name} long_name ${name} set scobj_hpath /sics/${name} hfactory ${scobj_hpath}/fast_shutter plain user text hsetprop ${scobj_hpath}/fast_shutter read ${ns}::getValue ${scobj_hpath} read_switch_pair {MG @IN[5], @IN[6]} hsetprop ${scobj_hpath}/fast_shutter read_switch_pair ${ns}::read_switch_pair ${scobj_hpath} hsetprop ${scobj_hpath}/fast_shutter control true hsetprop ${scobj_hpath}/fast_shutter data true hsetprop ${scobj_hpath}/fast_shutter mutable true hsetprop ${scobj_hpath}/fast_shutter nxsave true hsetprop ${scobj_hpath}/fast_shutter oldval UNKNOWN hsetprop ${scobj_hpath}/fast_shutter klass "collimator" hsetprop ${scobj_hpath}/fast_shutter nxalias "fast_shutter" hsetprop ${scobj_hpath}/fast_shutter sdsinfo "::nexus::scobj::sdsinfo" hsetprop ${scobj_hpath}/fast_shutter type "part" if {[string equal -nocase "${simulation_flag}" "false"]} { ${sct_controller} poll ${scobj_hpath}/fast_shutter 1 hsetprop ${scobj_hpath}/fast_shutter simulated false } else { ::scobj::shutters::sics_log 9 "simulation_flag=${simulation_flag} => No poll/write for shutters" hsetprop ${scobj_hpath}/fast_shutter simulated true } hfactory ${scobj_hpath}/rough_100 plain user text hsetprop ${scobj_hpath}/rough_100 read ${ns}::getValue ${scobj_hpath} read_switch_pair {MG @IN[15], @IN[16]} hsetprop ${scobj_hpath}/rough_100 read_switch_pair ${ns}::read_switch_pair ${scobj_hpath} hsetprop ${scobj_hpath}/rough_100 write ${ns}::no_write ${scobj_hpath} noResponse {11} hsetprop ${scobj_hpath}/rough_100 noResponse ${ns}::noResponse ${scobj_hpath} hsetprop ${scobj_hpath}/rough_100 check ${ns}::checkrange ${scobj_hpath} hsetprop ${scobj_hpath}/rough_100 control true hsetprop ${scobj_hpath}/rough_100 data true hsetprop ${scobj_hpath}/rough_100 mutable true hsetprop ${scobj_hpath}/rough_100 nxsave true hsetprop ${scobj_hpath}/rough_100 values in,out hsetprop ${scobj_hpath}/rough_100 oldval UNKNOWN hsetprop ${scobj_hpath}/rough_100 klass "collimator" hsetprop ${scobj_hpath}/rough_100 nxalias "rough_100" hsetprop ${scobj_hpath}/rough_100 sdsinfo "::nexus::scobj::sdsinfo" hsetprop ${scobj_hpath}/rough_100 type "part" if {[string equal -nocase "${simulation_flag}" "false"]} { ${sct_controller} poll ${scobj_hpath}/rough_100 1 ${sct_controller} write ${scobj_hpath}/rough_100 hsetprop ${scobj_hpath}/rough_100 simulated false } else { ::scobj::shutters::sics_log 9 "simulation_flag=${simulation_flag} => No poll/write for shutters" hsetprop ${scobj_hpath}/rough_100 simulated true } hfactory ${scobj_hpath}/rough_40 plain user text hsetprop ${scobj_hpath}/rough_40 read ${ns}::getValue ${scobj_hpath} read_switch_pair {MG @IN[13], @IN[14]} hsetprop ${scobj_hpath}/rough_40 read_switch_pair ${ns}::read_switch_pair ${scobj_hpath} hsetprop ${scobj_hpath}/rough_40 write ${ns}::no_write ${scobj_hpath} noResponse {10} hsetprop ${scobj_hpath}/rough_40 noResponse ${ns}::noResponse ${scobj_hpath} hsetprop ${scobj_hpath}/rough_40 check ${ns}::checkrange ${scobj_hpath} hsetprop ${scobj_hpath}/rough_40 control true hsetprop ${scobj_hpath}/rough_40 data true hsetprop ${scobj_hpath}/rough_40 mutable true hsetprop ${scobj_hpath}/rough_40 nxsave true hsetprop ${scobj_hpath}/rough_40 values in,out hsetprop ${scobj_hpath}/rough_40 oldval UNKNOWN hsetprop ${scobj_hpath}/rough_40 klass "collimator" hsetprop ${scobj_hpath}/rough_40 nxalias "rough_40" hsetprop ${scobj_hpath}/rough_40 sdsinfo "::nexus::scobj::sdsinfo" hsetprop ${scobj_hpath}/rough_40 type "part" if {[string equal -nocase "${simulation_flag}" "false"]} { ${sct_controller} poll ${scobj_hpath}/rough_40 1 ${sct_controller} write ${scobj_hpath}/rough_40 hsetprop ${scobj_hpath}/rough_40 simulated false } else { ::scobj::shutters::sics_log 9 "simulation_flag=${simulation_flag} => No poll/write for shutters" hsetprop ${scobj_hpath}/rough_40 simulated true } hsetprop ${scobj_hpath} data "true" hsetprop ${scobj_hpath} klass "@none" hsetprop ${scobj_hpath} nxsave "true" hsetprop ${scobj_hpath} type "part" hsetprop ${scobj_hpath} driver shutters hsetprop ${scobj_hpath} klass ${device_class} hsetprop ${scobj_hpath} data true hsetprop ${scobj_hpath} debug_threshold 5 # mkDriver hook code starts # mkDriver hook code ends } catch_message ] handle_exception ${catch_status} ${catch_message} } proc ::scobj::shutters::add_driver {name device_class simulation_flag ip_address tcp_port} { set catch_status [ catch { ::scobj::shutters::sics_log 9 "::scobj::shutters::add_driver ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port}" if {[string equal -nocase "${simulation_flag}" "false"]} { if {[string equal -nocase "aqadapter" "${ip_address}"]} { ::scobj::shutters::sics_log 9 "makesctcontroller sct_${name} aqadapter ${tcp_port}" makesctcontroller sct_${name} aqadapter ${tcp_port} } else { ::scobj::shutters::sics_log 9 "makesctcontroller sct_${name} dmc2280 ${ip_address}:${tcp_port}" makesctcontroller sct_${name} dmc2280 ${ip_address}:${tcp_port} } } else { ::scobj::shutters::sics_log 9 "simulation_flag=${simulation_flag} => Null sctcontroller for shutters" ::scobj::shutters::sics_log 9 "makesctcontroller sct_${name} aqadapter NULL" makesctcontroller sct_${name} aqadapter NULL } ::scobj::shutters::sics_log 1 "::scobj::shutters::mkDriver sct_${name} ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port}" ::scobj::shutters::mkDriver sct_${name} ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port} } catch_message ] handle_exception ${catch_status} ${catch_message} } namespace eval ::scobj::shutters { namespace export debug_threshold namespace export debug_log namespace export sics_log namespace export mkDriver namespace export add_driver # postamble hook code starts variable rough_slits_enabled 0 variable rough_slits_cb_pending 0 proc selrs {slit {cb_timeout 60}} { variable rough_slits_enabled variable rough_slits_cb_pending set usage_msg "Valid arguments are 40 or 100 or use 'reset' to terminate a previous selrs call" set catch_status [ catch { if {$slit == "help"} { clientput "Usage: $usage_msg" return } if { $rough_slits_enabled != 1 } { error "Roughing slit selection disabled" } if {$slit == "reset"} { set_oneshot /sics/shutters/rough_40 xxx "in" "clear" set_oneshot /sics/shutters/rough_100 xxx "in" "clear" return } else { if {$slit != 40 && $slit != 100} { error "[info level 0]: Invalid roughing slit $slit. $usage_msg" } if [hpropexists /sics/shutters/rough_40 oneshot_state] { set R40_state [hgetpropval /sics/shutters/rough_40 oneshot_state] } else { set R40_state 0 } if [hpropexists /sics/shutters/rough_100 oneshot_state] { set R100_state [hgetpropval /sics/shutters/rough_100 oneshot_state] } else { set R100_state 0 } if {$R40_state == 1 || $R100_state == 1} { error "Waiting for a previous selrs call to complete" } } if {![string is integer $cb_timeout]} { error "The timeout must be an integer" } switch $slit { 40 { mc4 send SB10 # Raise RS100 on receiving the "in" event on RS40 within the given timeout set_oneshot /sics/shutters/rough_40 { apply {{hp args} {mc4 send CB11}} } "in" $cb_timeout } 100 { mc4 send SB11 # Raise RS40 on receiving the "in" event on RS100 within the given timeout set_oneshot /sics/shutters/rough_100 { apply {{hp args} {mc4 send CB10}} } "in" $cb_timeout } } } catch_message ] handle_exception ${catch_status} ${catch_message} } namespace export selrs publish selrs mugger # postamble hook code ends } proc add_shutters {name ip_address tcp_port} { set simulation_flag "[string tolower [SplitReply [motor_simulation]]]" ::scobj::shutters::add_driver ${name} "instrument" ${simulation_flag} ${ip_address} ${tcp_port} } clientput "file evaluation of shutters_sct.tcl" ::scobj::shutters::sics_log 9 "file evaluation of shutters_sct.tcl" proc ::scobj::shutters::read_config {} { set catch_status [ catch { set ns "::scobj::shutters" dict for {k u} $::config_dict { if { [dict exists $u "implementation"] } { set simulation_flag "[string tolower [SplitReply [motor_simulation]]]" set device_class "instrument" if { !([dict exists $u "name"] && [dict exists $u "enabled"]) } { continue } set enabled [string tolower [dict get $u "enabled"]] if { ! ([string equal -nocase $enabled "true" ] || [string equal -nocase $enabled "always"]) } { continue } if { [dict exists $u "simulation_group"] } { set simulation_flag [SplitReply [[string tolower [dict get $u "simulation_group"]]]] } if { [dict exists $u "device_class"] } { set device_class "[dict get $u "device_class"]" } set name [dict get $u name] set implementation [dict get $u "implementation"] if { !([dict exists $::config_dict $implementation]) } { continue } set v [dict get $::config_dict $implementation] if { !([dict exists $v "driver"]) } { continue } if { [string equal -nocase [dict get $v "driver"] "shutters"] } { if { ![string equal -nocase "${simulation_flag}" "false"] } { set asyncqueue "null" ${ns}::sics_log 9 "simulation_flag=${simulation_flag} => using null asyncqueue" ${ns}::sics_log 9 "makesctcontroller sct_${name} aqadapter NULL" makesctcontroller sct_${name} aqadapter NULL } elseif { [dict exists $v "asyncqueue"] } { set asyncqueue [dict get $v "asyncqueue"] if { [string equal -nocase ${asyncqueue} "sct"] } { set ip_address [dict get $v ip] set tcp_port [dict get $v port] makesctcontroller sct_${name} dmc2280 ${ip_address}:${tcp_port} } else { makesctcontroller sct_${name} aqadapter ${asyncqueue} } } else { if { [dict exists $v "asyncprotocol"] } { set asyncprotocol [dict get $v "asyncprotocol"] } else { set asyncprotocol ${name}_protocol MakeAsyncProtocol ${asyncprotocol} if { [dict exists $v "sendterminator"] } { ${asyncprotocol} sendterminator "[dict get $v "sendterminator"]" } elseif { [dict exists $v "terminator"] } { ${asyncprotocol} sendterminator "[dict get $v "terminator"]" } if { [dict exists $v "replyterminator"] } { ${asyncprotocol} replyterminator "[dict get $v "replyterminator"]" } elseif { [dict exists $v "terminator"] } { ${asyncprotocol} replyterminator "[dict get $v "terminator"]" } } set asyncqueue ${name}_queue set ip_address [dict get $v ip] set tcp_port [dict get $v port] MakeAsyncQueue ${asyncqueue} ${asyncprotocol} ${ip_address} ${tcp_port} if { [dict exists $v "timeout"] } { ${asyncqueue} timeout "[dict get $v "timeout"]" } makesctcontroller sct_${name} aqadapter ${asyncqueue} } ${ns}::mkDriver sct_${name} ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port} } } } } catch_message ] handle_exception ${catch_status} ${catch_message} } if { [info exists ::config_dict] } { ::scobj::shutters::read_config } else { ::scobj::shutters::sics_log 5 "No config dict" }