Add a driver for the Omron ZS-HLDC Laser Displacement device

This commit is contained in:
Douglas Clowes
2014-06-16 17:37:20 +10:00
parent fa3154bdcd
commit fce610a142
3 changed files with 366 additions and 0 deletions

View File

@ -0,0 +1,48 @@
# Simple driver generator for the Omron ZS-HLDC Laser Displacement
# vim: ts=8 sts=2 sw=2 expandtab autoindent smartindent
#
driver omron_hldc = {
debug_threshold = 5;
vendor = Omron; device = 'ZS-HDLC'; protocol = std;
class = environment;
simulation_group = environment_simulation;
group = {
readable = 1;
var id = {
type = text
read_function = read_id; read_command = '0501';
}
var distance = {
type = float
read_function = read_mm; read_command = '0201C02030008001'; }
}
code getValue = {%%
set cmd "00000${cmd}"
%%}
code read_id = {%%
if {[string length ${data}] < 14} {
sct geterror "read_id short response ${data}"
} else {
set data [string range ${data} 14 end]
}
%%}
code read_mm = {%%
if {[string length "${data}"] < 34} {
sct geterror "read_mm short response ${data}"
} else {
set hex [string range ${data} 26 end]
debug_log ${tc_root} 1 "hex = ${hex}"
set n [scan "${hex}" "%x" val]
if {${n} == 1} {
set data [expr {0.000001 * ${val}}]
} else {
sct geterror "read_mm n = ${n} for hex = ${hex}"
}
}
%%}
}

View File

@ -0,0 +1,317 @@
# Generated driver for omron_hldc
# vim: ft=tcl tabstop=8 softtabstop=2 shiftwidth=2 nocindent smartindent
#
namespace eval ::scobj::omron_hldc {
set debug_threshold 5
}
proc ::scobj::omron_hldc::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/omron_hldc_[basename ${tc_root}].log" "a"]
set line "[clock format [clock seconds] -format "%T"] ${debug_string}"
puts ${fd} "${line}"
close ${fd}
}
} catch_message ]
}
proc ::scobj::omron_hldc::sics_log {debug_level debug_string} {
set catch_status [ catch {
set debug_threshold ${::scobj::omron_hldc::debug_threshold}
if {${debug_level} >= ${debug_threshold}} {
sicslog "::scobj::omron_hldc::${debug_string}"
}
} catch_message ]
}
# check function for hset change
proc ::scobj::omron_hldc::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::omron_hldc::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 starts
set cmd "00000${cmd}"
# getValue hook code ends
if { [hpropexists [sct] geterror] } {
debug_log ${tc_root} 9 "[sct] error: [sct geterror]"
error "[sct geterror]"
}
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::omron_hldc::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 parse the read of a parameter on a device
proc ::scobj::omron_hldc::read_id {tc_root} {
set catch_status [ catch {
debug_log ${tc_root} 1 "read_id 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_id hook code starts
if {[string length ${data}] < 14} {
sct geterror "read_id short response ${data}"
} else {
set data [string range ${data} 14 end]
}
# read_id 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 parse the read of a parameter on a device
proc ::scobj::omron_hldc::read_mm {tc_root} {
set catch_status [ catch {
debug_log ${tc_root} 1 "read_mm 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_mm hook code starts
if {[string length "${data}"] < 34} {
sct geterror "read_mm short response ${data}"
} else {
set hex [string range ${data} 26 end]
debug_log ${tc_root} 1 "hex = ${hex}"
set n [scan "${hex}" "%x" val]
if {${n} == 1} {
set data [expr {0.000001 * ${val}}]
} else {
sct geterror "read_mm n = ${n} for hex = ${hex}"
}
}
# read_mm 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::omron_hldc::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}
}
proc ::scobj::omron_hldc::mkDriver { sct_controller name } {
::scobj::omron_hldc::sics_log 9 "::scobj::omron_hldc::mkDriver for ${name}"
set ns "[namespace current]"
set catch_status [ catch {
MakeSICSObj ${name} SCT_OBJECT
sicslist setatt ${name} klass environment
sicslist setatt ${name} long_name ${name}
set scobj_hpath /sics/${name}
hfactory ${scobj_hpath}/distance plain user float
hsetprop ${scobj_hpath}/distance read ${ns}::getValue ${scobj_hpath} read_mm {0201C02030008001}
hsetprop ${scobj_hpath}/distance read_mm ${ns}::read_mm ${scobj_hpath}
hsetprop ${scobj_hpath}/distance control true
hsetprop ${scobj_hpath}/distance data true
hsetprop ${scobj_hpath}/distance mutable false
hsetprop ${scobj_hpath}/distance nxsave true
hsetprop ${scobj_hpath}/distance oldval 0.0
hsetprop ${scobj_hpath}/distance sdsinfo "::nexus::scobj::sdsinfo"
hsetprop ${scobj_hpath}/distance type "part"
hsetprop ${scobj_hpath}/distance nxalias "${name}_distance"
hfactory ${scobj_hpath}/id plain user text
hsetprop ${scobj_hpath}/id read ${ns}::getValue ${scobj_hpath} read_id {0501}
hsetprop ${scobj_hpath}/id read_id ${ns}::read_id ${scobj_hpath}
hsetprop ${scobj_hpath}/id control true
hsetprop ${scobj_hpath}/id data true
hsetprop ${scobj_hpath}/id mutable false
hsetprop ${scobj_hpath}/id nxsave true
hsetprop ${scobj_hpath}/id oldval UNKNOWN
hsetprop ${scobj_hpath}/id sdsinfo "::nexus::scobj::sdsinfo"
hsetprop ${scobj_hpath}/id type "part"
hsetprop ${scobj_hpath}/id nxalias "${name}_id"
if {[string equal -nocase [SplitReply [environment_simulation]] "false"]} {
${sct_controller} poll ${scobj_hpath}/distance 1
${sct_controller} poll ${scobj_hpath}/id 1
} else {
::scobj::omron_hldc::sics_log 9 "[environment_simulation] => No poll/write for omron_hldc"
}
hsetprop ${scobj_hpath} klass environment
hsetprop ${scobj_hpath} debug_threshold 5
# mkDriver hook code goes here
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
namespace eval ::scobj::omron_hldc {
namespace export debug_threshold
namespace export debug_log
namespace export sics_log
namespace export mkDriver
}
proc add_omron_hldc {name IP port} {
set catch_status [ catch {
::scobj::omron_hldc::sics_log 9 "add_omron_hldc ${name} ${IP} ${port}"
if {[string equal -nocase [SplitReply [environment_simulation]] "false"]} {
if {[string equal -nocase "aqadapter" "${IP}"]} {
::scobj::omron_hldc::sics_log 9 "makesctcontroller sct_${name} aqadapter ${port}"
makesctcontroller sct_${name} aqadapter ${port}
} else {
::scobj::omron_hldc::sics_log 9 "makesctcontroller sct_${name} std ${IP}:${port}"
makesctcontroller sct_${name} std ${IP}:${port}
}
} else {
::scobj::omron_hldc::sics_log 9 "[environment_simulation] => No sctcontroller for omron_hldc"
}
::scobj::omron_hldc::sics_log 1 "::scobj::omron_hldc::mkDriver sct_${name} ${name}"
::scobj::omron_hldc::mkDriver sct_${name} ${name}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
puts stdout "file evaluation of sct_omron_hldc.tcl"
::scobj::omron_hldc::sics_log 9 "file evaluation of sct_omron_hldc.tcl"
proc ::scobj::omron_hldc::read_config {} {
set catch_status [ catch {
set ns "::scobj::omron_hldc"
dict for {k v} $::config_dict {
if { [dict exists $v "driver"] } {
if { [dict get $v "driver"] == "omron_hldc" } {
if { [dict get $v enabled] } {
set name [dict get $v name]
if { ![string equal -nocase [SplitReply [environment_simulation]] "false"] } {
set asyncqueue "null"
${ns}::sics_log 9 "[environment_simulation] => using null asyncqueue"
} elseif { [dict exists $v "asyncqueue"] } {
set asyncqueue [dict get $v "asyncqueue"]
} 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 [dict get $v ip]
set PORT [dict get $v port]
MakeAsyncQueue ${asyncqueue} ${asyncprotocol} ${IP} ${PORT}
if { [dict exists $v "timeout"] } {
${asyncqueue} timeout "[dict get $v "timeout"]"
}
}
add_omron_hldc ${name} "aqadapter" ${asyncqueue}
}
}
}
}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
if { [info exists ::config_dict] } {
::scobj::omron_hldc::read_config
} else {
::scobj::omron_hldc::sics_log 5 "No config dict"
}

View File

@ -28,6 +28,7 @@ fileeval $cfPath(counter)/counter.tcl
fileeval $cfPath(environment)/sct_syr.tcl
fileeval $cfPath(environment)/sct_mvp.tcl
fileeval $cfPath(environment)/sct_protek_common.tcl
fileeval $cfPath(environment)/sct_omron_hldc.tcl
fileeval $cfPath(environment)/magneticField/sct_bruker_BEC1.tcl
fileeval $cfPath(environment)/temperature/sct_lakeshore_336.tcl
fileeval $cfPath(environment)/temperature/sct_lakeshore_340.tcl