Simple driver for the "Green Magnet"

r3710 | dcl | 2012-08-28 15:06:20 +1000 (Tue, 28 Aug 2012) | 1 line
This commit is contained in:
Douglas Clowes
2012-08-28 15:06:20 +10:00
parent 0338f9973b
commit 200d539adc

View File

@@ -0,0 +1,329 @@
# Define procs in ::scobj::xxx namespace
# MakeSICSObj $obj SCT_<class>
# The MakeSICSObj cmd adds a /sics/$obj node. NOTE the /sics node is not browsable.
namespace eval ::scobj::green_magnet {
# Environment controllers should have at least the following nodes
# /envcont/setpoint
# /envcont/sensor/value
proc debug_log {args} {
set fd [open "/tmp/green_magnet.log" a]
puts $fd "[clock format [clock seconds] -format "%T"] $args"
close $fd
}
# issue a command with a value in the target property of the variable
proc setPoint {tc_root nextState cmd} {
debug_log "setPoint: cmd=$cmd sct=[sct] $tc_root"
set par "[sct target]"
sct send "s $par"
sct driving 1
sct time_check [clock seconds]
debug_log "setPoint: write $cmd$par"
if {$par != [sct oldval]} {
sct oldval $par
sct update [sct target]
sct utime readtime
debug_log "setPoint: new data for $tc_root [sct] result=$par"
}
return $nextState
}
# issue a command to read and expect a value response
proc getValue {tc_root nextState cmd} {
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
debug_log "getValue $cmd sct=[sct] root=$tc_root nextState=$nextState"
sct send "?"
return $nextState
}
proc rdValue {tc_root} {
debug_log "rdValue tc_root=$tc_root sct=[sct]"
set data [sct result]
if {[ catch {
debug_log "rdValue $tc_root [sct] result=$data"
} catch_message ]} {
debug_log "rdValue $tc_root failure"
}
if {[string equal -nocase -length 7 $data "ASCERR:"]} {
sct geterror "$data"
set nextState idle
} elseif {[string equal -nocase -length 1 $data "?"]} {
sct geterror "Error: $data"
set nextState idle
} else {
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
set rslt [scan $data %f data]
debug_log "rdValue $tc_root Green_Magnet result=$data"
if {$data != [sct oldval]} {
sct oldval $data
sct update $data
sct utime readtime
debug_log "rdValue new data for $tc_root [sct] result=$data"
}
}
return idle
}
proc noResponse {} {
return idle
}
# check that a target is within allowable limits
proc check {tc_root} {
set setpoint [sct target]
set lolimit [hval $tc_root/lowerlimit]
set hilimit [hval $tc_root/upperlimit]
if {$setpoint < $lolimit || $setpoint > $hilimit} {
sct driving 0
error "setpoint violates limits"
}
return OK
}
# Check that the sensor is reading within tolerance of the setpoint.
# Return 1 or 0 if it is or is not, respectively.
proc checktol {tc_root currtime timecheck} {
debug_log "checktol $tc_root $currtime $timecheck"
set field [hval $tc_root/sensor/value]
set lofield [expr {[hval $tc_root/setpoint] - [hval $tc_root/tolerance]}]
set hifield [expr {[hval $tc_root/setpoint] + [hval $tc_root/tolerance]}]
debug_log "checktol: field = $field, lofield = $lofield, hifield = $hifield"
if { $field < $lofield || $field > $hifield} {
hset $tc_root/emon/isintol 0
set rslt 0
} else {
set timeout [hval $tc_root/tolerance/settletime]
if { ($currtime - $timecheck) > $timeout } {
hset $tc_root/emon/isintol 1
}
set rslt 1
}
debug_log "checktol: result = $rslt"
return $rslt
}
##
# @brief Implement the checkstatus command for the drivable interface
#
# NOTE: The drive adapter initially sets the writestatus to "start" and will
# only call this when writestatus!="start"
proc drivestatus {tc_root} {
if {[sct driving]} {
set intol [checktol $tc_root [clock seconds] [sct time_check]]
if {$intol == 0} {
sct time_check [clock seconds]
} else {
sct driving 0
}
return busy
} else {
sct print "drivestatus: idle"
return idle
}
}
proc halt {tc_root} {
debug_log "halt $tc_root"
sct print "halt $tc_root"
set my_driving [SplitReply [hgetprop $tc_root/setpoint driving]]
hset $tc_root/drive_state "HALT"
if { $my_driving } {
return read
}
return idle
}
##
# @brief createNode() creates a node for the given nodename with the properties and virtual
# function names provided
# @param scobj_hpath string variable holding the path to the object's base node in sics (/sample/tc1)
# @param sct_controller name of the scriptcontext object (typically sct_xxx_yyy)
# @param cmdGroup subdirectory (below /sample/tc*/) in which the node is to be created
# @param varName name of the actual node typically representing one device command
# @param readable set to 1 if the node represents a query command, 0 if it is not
# @param writable set to 1 if the node represents a request for a change in settings sent to the device
# @param drivable if set to 1 it prepares the node to provide a drivable interface
# @param dataType data type of the node, must be one of none, int, float, text
# @param permission defines what user group may read/write to this node (is one of spy, user, manager)
# @param rdCmd actual device query command to be sent to the device
# @param rdFunc nextState Function to be called after the getValue function, typically rdValue()
# @param wrCmd actual device write command to be sent to the device
# @param wrFunc Function to be called to send the wrCmd to the device, typically setValue()
# @param allowedValues allowed values for the node data - does not permit other
# @param klass Nexus class name (?)
# @return OK
proc createNode {scobj_hpath sct_controller cmdGroup varName readable writable\
drivable dataType permission rdCmd rdFunc wrCmd\
wrFunc allowedValues klass} {
set catch_status [ catch {
set ns "[namespace current]"
set nodeName "$scobj_hpath/$cmdGroup/$varName"
if {1 > [string length $cmdGroup]} {
set nodeName "$scobj_hpath/$varName"
}
debug_log "Creating node $nodeName"
hfactory $nodeName plain $permission $dataType
if {$readable > 0} {
hsetprop $nodeName read ${ns}::getValue $scobj_hpath $rdFunc $rdCmd
hsetprop $nodeName $rdFunc ${ns}::$rdFunc $scobj_hpath
set poll_period 30
if { $readable >= 0 && $readable <= 9 } {
set poll_period [lindex [list 0 1 2 3 4 5 10 15 20 30] $readable]
}
debug_log "Registering node $nodeName for poll at $poll_period seconds"
$sct_controller poll $nodeName $poll_period
}
if {$writable == 1} {
set parts [split "$wrFunc" "."]
if { [llength $parts] == 2 } {
set func_name [lindex $parts 0]
set next_state [lindex $parts 1]
hsetprop $nodeName write ${ns}::$func_name $scobj_hpath $next_state $wrCmd
hsetprop $nodeName $next_state ${ns}::$next_state $scobj_hpath
} else {
hsetprop $nodeName write ${ns}::$wrFunc $scobj_hpath noResponse $wrCmd
hsetprop $nodeName noResponse ${ns}::noResponse
}
hsetprop $nodeName writestatus UNKNOWN
debug_log "Registering node $nodeName for write callback"
$sct_controller write $nodeName
}
switch -exact $dataType {
"none" { }
"int" { hsetprop $nodeName oldval -1 }
"float" { hsetprop $nodeName oldval -1.0 }
default { hsetprop $nodeName oldval UNKNOWN }
}
if {1 < [string length $allowedValues]} {
hsetprop $nodeName values $allowedValues
}
# Drive adapter interface
if {$drivable == 1} {
hsetprop $nodeName check ${ns}::check $scobj_hpath
hsetprop $nodeName driving 0
hsetprop $nodeName checklimits ${ns}::check $scobj_hpath
hsetprop $nodeName checkstatus ${ns}::drivestatus $scobj_hpath
hsetprop $nodeName halt ${ns}::halt $scobj_hpath
}
} message ]
if {$catch_status != 0} {
return -code error "in createNode $message"
}
return OK
}
proc mk_sct_green_magnet {sct_controller klass tempobj tol} {
set catch_status [ catch {
set ns "[namespace current]"
MakeSICSObj $tempobj SCT_OBJECT
sicslist setatt $tempobj klass $klass
sicslist setatt $tempobj long_name $tempobj
set scobj_hpath /sics/$tempobj
set deviceCommand {\
{} setpoint 0 1 1 float user {} {} {} {setPoint} {}\
sensor value 1 0 0 float internal {} {rdValue} {} {} {}\
}
hfactory $scobj_hpath/sensor plain spy none
foreach {cmdGroup varName readable writable drivable dataType permission rdCmd rdFunc wrCmd wrFunc allowedValues} $deviceCommand {
createNode $scobj_hpath $sct_controller $cmdGroup $varName $readable $writable $drivable $dataType $permission $rdCmd $rdFunc $wrCmd $wrFunc $allowedValues $klass
}
hfactory $scobj_hpath/tolerance plain user float
hsetprop $scobj_hpath/tolerance units "T"
hfactory $scobj_hpath/tolerance/settletime plain user float
hset $scobj_hpath/tolerance/settletime 5.0
hsetprop $scobj_hpath/tolerance/settletime units "s"
hset $scobj_hpath/tolerance $tol
hfactory $scobj_hpath/lowerlimit plain mugger float
hsetprop $scobj_hpath/lowerlimit units "T"
hset $scobj_hpath/lowerlimit 0
hfactory $scobj_hpath/upperlimit plain mugger float
hsetprop $scobj_hpath/upperlimit units "T"
hset $scobj_hpath/upperlimit 6
hfactory $scobj_hpath/emon plain spy none
hfactory $scobj_hpath/emon/monmode plain user text
hsetprop $scobj_hpath/emon/monmode values idle,drive,monitor,error
hset $scobj_hpath/emon/monmode "idle"
hfactory $scobj_hpath/emon/isintol plain user int
hset $scobj_hpath/emon/isintol 1
hfactory $scobj_hpath/emon/errhandler plain user text
hset $scobj_hpath/emon/errhandler "pause"
hfactory $scobj_hpath/drive_state plain mugger text
hset $scobj_hpath/drive_state "UNKNOWN"
hsetprop $scobj_hpath type part
foreach snsr {sensor} {
foreach {rootpath hpath klass priv} "\
$scobj_hpath $snsr NXsensor spy\
$scobj_hpath $snsr/value sensor user\
" {
hsetprop $rootpath/$hpath klass $klass
hsetprop $rootpath/$hpath privilege $priv
hsetprop $rootpath/$hpath control true
hsetprop $rootpath/$hpath data true
hsetprop $rootpath/$hpath nxsave true
}
hsetprop $scobj_hpath/$snsr type part
hsetprop $scobj_hpath/$snsr/value nxalias tc1_${snsr}_value
hsetprop $scobj_hpath/$snsr/value mutable true
hsetprop $scobj_hpath/$snsr/value sdsinfo ::nexus::scobj::sdsinfo
}
hsetprop $scobj_hpath privilege spy
::scobj::hinitprops $tempobj setpoint
if {[SplitReply [magnetic_simulation]]=="false"} {
hsetprop $scobj_hpath/setpoint type drivable
ansto_makesctdrive ${tempobj}_driveable $scobj_hpath/setpoint $scobj_hpath/sensor/value $sct_controller
}
} catch_message ]
if {$catch_status != 0} {
return -code error $catch_message
}
}
namespace export mk_sct_green_magnet
}
##
# @brief Create a Green Magnet power supply controller
#
# @param name, the name of the power supply controller (eg tc1)
# @param IP, the IP address of the device, this can be a hostname, (eg ca1-kowari)
# @param port, the IP protocol port number of the device
# @param _tol (optional), this is the initial tolerance setting
proc add_green_magnet {name IP port {_tol 5.0}} {
set fd [open "/tmp/green_magnet.log" a]
if {[SplitReply [magnetic_simulation]]=="false"} {
puts $fd "makesctcontroller green_magnet std ${IP}:$port"
makesctcontroller green_magnet std ${IP}:$port
}
puts $fd "mk_sct_green_magnet green_magnet environment $name $_tol"
mk_sct_green_magnet green_magnet environment $name $_tol
puts $fd "makesctemon $name /sics/$name/emon/monmode /sics/$name/emon/isintol /sics/$name/emon/errhandler"
makesctemon $name /sics/$name/emon/monmode /sics/$name/emon/isintol /sics/$name/emon/errhandler
close $fd
}
puts stdout "file evaluation of sct_green_magnet.tcl"
set fd [open "/tmp/green_magnet.log" w]
puts $fd "file evaluation of sct_green_magnet.tcl"
close $fd
namespace import ::scobj::green_magnet::*
#add_green_magnet magnet 137.157.201.213 502 5
#add_green_magnet magnet localhost 30509 0.001