Add a driver for the 3He Polariser/Analyser (rudimentary)

This commit is contained in:
Douglas Clowes
2014-10-16 15:52:30 +11:00
parent 11d008620a
commit 0cd3e85607
2 changed files with 433 additions and 0 deletions

View File

@ -0,0 +1,78 @@
# vim: ts=8 sts=2 sw=2 expandtab autoindent smartindent nocindent
driver he3_polanal = {
protocol = std;
class = instrument;
simulation_group = rfgen_simulation;
group = {
var spin = {
type = text;
readable = 10;
read_command = 'spin 0 0';
writeable = 1;
write_command = 'spin ';
check_function = chkWrite;
}
}
code chkWrite = {%%
rdValue ${tc_root}
%%}
code rdValue = {%%
set dlist [split ${data}]
if {[lindex ${dlist} 0] != "Spin"} {
sct geterror "Unexpected 'Spin' response '${data}'"
error "[sct geterror]"
}
set idx [lsearch ${dlist} "and"]
if { ${idx} < 0 } {
sct geterror "Unexpected 'and' response '${data}'"
error "[sct geterror]"
}
set plist [lrange ${dlist} 1 ${idx}-1]
set alist [lrange ${dlist} ${idx}+1 end]
if {[lindex ${plist} 0] != "Polariser"} {
sct geterror "Unexpected 'Polariser' response '${data}'"
error "[sct geterror]"
} elseif {[lindex ${plist} 1] == "In" && [lindex ${plist} 3] == "State"} {
set pstate [string index [lindex ${plist} 2] 1]
} elseif {[lindex ${plist} 1] == "Not" && [lindex ${plist} 2] == "Active"} {
set pstate 0
} else {
sct geterror "Unexpected 'Polariser State' response '${data}'"
error "[sct geterror]"
}
if {[lindex ${alist} 0] != "Analyser"} {
sct geterror "Unexpected 'Analyser' response '${data}'"
error "[sct geterror]"
} elseif {[lindex ${alist} 1] == "In" && [lindex ${alist} 3] == "State"} {
set astate [string index [lindex ${alist} 2] 1]
} elseif {[lindex ${alist} 1] == "Not" && [lindex ${alist} 2] == "Active."} {
set astate 0
} else {
sct geterror "Unexpected 'Analyser State' response '${data}'"
error "[sct geterror]"
}
set data "${pstate} ${astate}"
%%}
code setValue = {%%
set geterror "Spin target must be 'x/y', where x,y are 0,+,- and not '[sct target]'"
set dlist [split [sct target]]
if {[llength ${dlist}] != 2} {
set dlist [split [sct target] /]
if {[llength ${dlist}] != 2} {
sct geterror ${geterror}
error "[sct geterror]"
}
}
if { [lindex ${dlist} 0] != "+" && [lindex ${dlist} 0] != "-" && [lindex ${dlist} 0] != "0" } {
sct geterror ${geterror}
error "[sct geterror]"
}
if { [lindex ${dlist} 1] != "+" && [lindex ${dlist} 1] != "-" && [lindex ${dlist} 1] != "0" } {
sct geterror ${geterror}
error "[sct geterror]"
}
set cmd "spin [lindex ${dlist} 0] [lindex ${dlist} 1]"
%%}
}

View File

@ -0,0 +1,355 @@
# Generated driver for he3_polanal
# vim: ft=tcl tabstop=8 softtabstop=2 shiftwidth=2 nocindent smartindent
#
namespace eval ::scobj::he3_polanal {
set debug_threshold 5
}
proc ::scobj::he3_polanal::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 fd [open "../log/he3_polanal_[basename ${tc_root}].log" "a"]
set line "[clock format [clock seconds] -format "%T"] ${debug_string}"
puts ${fd} "${line}"
close ${fd}
}
} catch_message ]
}
proc ::scobj::he3_polanal::sics_log {debug_level debug_string} {
set catch_status [ catch {
set debug_threshold ${::scobj::he3_polanal::debug_threshold}
if {${debug_level} >= ${debug_threshold}} {
sicslog "::scobj::he3_polanal::${debug_string}"
}
} catch_message ]
}
# check function for hset change
proc ::scobj::he3_polanal::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 check the write parameter on a device
proc ::scobj::he3_polanal::chkWrite {tc_root} {
set catch_status [ catch {
debug_log ${tc_root} 1 "chkWrite tc_root=${tc_root} sct=[sct] resp=[sct result]"
# chkWrite hook code starts
rdValue ${tc_root}
# chkWrite hook code ends
return "idle"
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
# function to request the read of a parameter on a device
proc ::scobj::he3_polanal::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 parse the read of a parameter on a device
proc ::scobj::he3_polanal::rdValue {tc_root} {
set catch_status [ catch {
debug_log ${tc_root} 1 "rdValue 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]"
}
# rdValue hook code starts
set dlist [split ${data}]
if {[lindex ${dlist} 0] != "Spin"} {
sct geterror "Unexpected 'Spin' response '${data}'"
error "[sct geterror]"
}
set idx [lsearch ${dlist} "and"]
if { ${idx} < 0 } {
sct geterror "Unexpected 'and' response '${data}'"
error "[sct geterror]"
}
set plist [lrange ${dlist} 1 ${idx}-1]
set alist [lrange ${dlist} ${idx}+1 end]
if {[lindex ${plist} 0] != "Polariser"} {
sct geterror "Unexpected 'Polariser' response '${data}'"
error "[sct geterror]"
} elseif {[lindex ${plist} 1] == "In" && [lindex ${plist} 3] == "State"} {
set pstate [string index [lindex ${plist} 2] 1]
} elseif {[lindex ${plist} 1] == "Not" && [lindex ${plist} 2] == "Active"} {
set pstate 0
} else {
sct geterror "Unexpected 'Polariser State' response '${data}'"
error "[sct geterror]"
}
if {[lindex ${alist} 0] != "Analyser"} {
sct geterror "Unexpected 'Analyser' response '${data}'"
error "[sct geterror]"
} elseif {[lindex ${alist} 1] == "In" && [lindex ${alist} 3] == "State"} {
set astate [string index [lindex ${alist} 2] 1]
} elseif {[lindex ${alist} 1] == "Not" && [lindex ${alist} 2] == "Active."} {
set astate 0
} else {
sct geterror "Unexpected 'Analyser State' response '${data}'"
error "[sct geterror]"
}
set data "${pstate} ${astate}"
# rdValue 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::he3_polanal::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 starts
set geterror "Spin target must be 'x/y', where x,y are 0,+,- and not '[sct target]'"
set dlist [split [sct target]]
if {[llength ${dlist}] != 2} {
set dlist [split [sct target] /]
if {[llength ${dlist}] != 2} {
sct geterror ${geterror}
error "[sct geterror]"
}
}
if { [lindex ${dlist} 0] != "+" && [lindex ${dlist} 0] != "-" && [lindex ${dlist} 0] != "0" } {
sct geterror ${geterror}
error "[sct geterror]"
}
if { [lindex ${dlist} 1] != "+" && [lindex ${dlist} 1] != "-" && [lindex ${dlist} 1] != "0" } {
sct geterror ${geterror}
error "[sct geterror]"
}
set cmd "spin [lindex ${dlist} 0] [lindex ${dlist} 1]"
# setValue 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 "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}
}
proc ::scobj::he3_polanal::mkDriver { sct_controller name device_class simulation_flag ip_address tcp_port } {
::scobj::he3_polanal::sics_log 9 "::scobj::he3_polanal::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} klass ${device_class}
sicslist setatt ${name} long_name ${name}
set scobj_hpath /sics/${name}
hfactory ${scobj_hpath}/spin plain user text
hsetprop ${scobj_hpath}/spin read ${ns}::getValue ${scobj_hpath} rdValue {spin 0 0}
hsetprop ${scobj_hpath}/spin rdValue ${ns}::rdValue ${scobj_hpath}
hsetprop ${scobj_hpath}/spin write ${ns}::setValue ${scobj_hpath} chkWrite {spin }
hsetprop ${scobj_hpath}/spin chkWrite ${ns}::chkWrite ${scobj_hpath}
hsetprop ${scobj_hpath}/spin check ${ns}::checkrange ${scobj_hpath}
hsetprop ${scobj_hpath}/spin control true
hsetprop ${scobj_hpath}/spin data true
hsetprop ${scobj_hpath}/spin mutable true
hsetprop ${scobj_hpath}/spin nxsave true
hsetprop ${scobj_hpath}/spin oldval UNKNOWN
hsetprop ${scobj_hpath}/spin klass "parameter"
hsetprop ${scobj_hpath}/spin sdsinfo "::nexus::scobj::sdsinfo"
hsetprop ${scobj_hpath}/spin type "part"
hsetprop ${scobj_hpath}/spin nxalias "${name}_spin"
hsetprop ${scobj_hpath} data "true"
hsetprop ${scobj_hpath} klass "@none"
hsetprop ${scobj_hpath} type "part"
if {[string equal -nocase "${simulation_flag}" "false"]} {
${sct_controller} poll ${scobj_hpath}/spin 10
${sct_controller} write ${scobj_hpath}/spin
} else {
::scobj::he3_polanal::sics_log 9 "simulation_flag=${simulation_flag} => No poll/write for he3_polanal"
}
hsetprop ${scobj_hpath} klass ${device_class}
hsetprop ${scobj_hpath} data true
hsetprop ${scobj_hpath} debug_threshold 5
# mkDriver hook code goes here
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
proc ::scobj::he3_polanal::add_driver {name device_class simulation_flag ip_address tcp_port} {
set catch_status [ catch {
::scobj::he3_polanal::sics_log 9 "::scobj::he3_polanal::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::he3_polanal::sics_log 9 "makesctcontroller sct_${name} aqadapter ${tcp_port}"
makesctcontroller sct_${name} aqadapter ${tcp_port}
} else {
::scobj::he3_polanal::sics_log 9 "makesctcontroller sct_${name} std ${ip_address}:${tcp_port}"
makesctcontroller sct_${name} std ${ip_address}:${tcp_port}
}
} else {
::scobj::he3_polanal::sics_log 9 "simulation_flag={simulation_flag} => No sctcontroller for he3_polanal"
}
::scobj::he3_polanal::sics_log 1 "::scobj::he3_polanal::mkDriver sct_${name} ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port}"
::scobj::he3_polanal::mkDriver sct_${name} ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
namespace eval ::scobj::he3_polanal {
namespace export debug_threshold
namespace export debug_log
namespace export sics_log
namespace export mkDriver
namespace export add_driver
}
proc add_he3_polanal {name ip_address tcp_port} {
set simulation_flag "[string tolower [SplitReply [rfgen_simulation]]]"
::scobj::he3_polanal::add_driver ${name} "instrument" "${simulation_flag}" ${ip_address} ${tcp_port}
}
clientput "file evaluation of sct_he3_polanal.tcl"
::scobj::he3_polanal::sics_log 9 "file evaluation of sct_he3_polanal.tcl"
proc ::scobj::he3_polanal::read_config {} {
set catch_status [ catch {
set ns "::scobj::he3_polanal"
dict for {k u} $::config_dict {
if { [dict exists $u "implementation"] } {
set simulation_flag "[string tolower [SplitReply [rfgen_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"] "he3_polanal"] } {
if { ![string equal -nocase "${simulation_flag}" "false"] } {
set asyncqueue "null"
${ns}::sics_log 9 "simulation_flag=${simulation_flag} => using null asyncqueue"
} 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]
}
} else {
if { [dict exists $v "asyncprotocol"] } {
set asyncprotocol [dict get $v "asyncprotocol"]
} else {
set asyncprotocol ${name}_protocol
MakeAsyncProtocol ${asyncprotocol}
if { [dict exists $v "terminator"] } {
${asyncprotocol} sendterminator "[dict get $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"]"
}
}
if { [string equal -nocase ${asyncqueue} "sct"] } {
${ns}::add_driver ${name} ${device_class} ${simulation_flag} ${ip_address} ${tcp_port}
} else {
${ns}::add_driver ${name} ${device_class} ${simulation_flag} "aqadapter" ${asyncqueue}
}
}
}
}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
if { [info exists ::config_dict] } {
::scobj::he3_polanal::read_config
} else {
::scobj::he3_polanal::sics_log 5 "No config dict"
}