esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

#%TITLE% NHQ204M.mac
#%NAME%
#  Support for the NHQ204M high voltage powersupply
#
#%CATEGORY% Other hardware
#
#%DESCRIPTION%
#  The following set of macros allow the user to interrogate the state,
# read and change the voltage setting on an NHQ204M high voltage powersupply.
#%SETUP%
#The serial line cable is connected to the serial line interface card
#COMU 2000 (COMU 30), known as IBAM. A "device server" called SerialLine
#should be running in the VME crate, where the IBAM is. The configuration
#of the "server" can be found in a the resource file xxxSerilaLine.res,
#where xxx is the name of the crate (i.e. d291, id011 ...)
#The %B%RS232%B%  serial line parameters are:%BR%
#		- 8 bits%BR%
#		- no parity%BR%
# 		- 1 stop bit%BR%
#		- 9600 bauds%BR%
#%END%


#%UU%  [serial line] [channel]
#%MDESC%
# setup NHQ204M channel (1/2) and serial_line. The %B%serial line%B%
#parameter is the ordinal number of the serial line device (starting from 0),
#as assigned in the %B%config%B% file
#
def nhq204m_setup '{
    global NHQ204M_POS NHQ204M_CH NHQ204M_SL
    global NHQ204M_CMNE NHQ204M_MMNE NHQ204M_POS
    local junk
    if ($# > 2) {
      	print "Usage:  nhq204m_setup [serial line] [channel]" 
      	exit
    }

    NHQ204M_SL= ($#<1)?getval("Serial line number (starting form 0):",NHQ204M_SL):$1
    NHQ204M_CH= ($#<2)?getval("What channel do you want to use (1/2) ",NHQ204M_CH):$2

# empty serial line buffer of any junk characters
    junk= ser_get(NHQ204M_SL,0)
}'

#%IU% (str)
#%MDESC%  
#    Write string charcater by character to NHQ204M serial line
def _nhq204m_write(str) '{
    local nch ch i

    nch = length(str)

# code to write character by character to NHQ204M serial line
#
    for (i=1; i<nch+1; i++)
    {
       ch = substr(str,i,1)
#      printf("_nhq204m_write(%d): put %s\n",i,ch)
       ser_put(NHQ204M_SL,ch)
       ch = ser_get(NHQ204M_SL,1)
#      printf("_nhq204m_write(%d): get %s\n",i,ch)
    }
    ser_put(NHQ204M_SL,sprintf("%c%c",13,10))
    ch = ser_get(NHQ204M_SL,2)

# sleep nch+2 * 0.05 seconds - to give powersupply time to think !

    sleep((nch+2)*0.05)
}' 

#%UU%
#%MDESC% 
#    Print Status, Voltage, Ramp of NHQ204M
def nhq204m_status '{
	print "- NHQ204M -"
	print "SerialLine :", NHQ204M_SL
	print "Channel    :", NHQ204M_CH
	print "Status     :", _nhq204m_state()
	print "Voltage    :", _nhq204m_get()
	print "Ramp       :", _nhq204m_get_ramp()
}'
    
#%IU% ()
#%MDESC%
#    Return state of NHQ204M
def _nhq204m_state() '{
   local str
   
   str = sprintf("S%1d",NHQ204M_CH)
   _nhq204m_write(str)
   str = ser_get(NHQ204M_SL,0)
#  printf("nhq204m_state(): get %s\n",str)
   return (substr(str,3))
}'

#%UU% [voltage]
#%MDESC%
#    Set voltage of NHQ204M
def nhq204m_set '{
    local value
    if ($# > 2) {
	print "Usage:  nhq204m_set [voltage]"
	exit
    }
    value= ($#<1)?getval("Set the NHQ204M to what voltage ",0):$1
    _nhq204m_set(value)
}'

#%IU% (voltage)
#%MDESC%
#    Set voltage of NHQ204M
def _nhq204m_set(voltage) '{
   local str
   
   str = sprintf("D%1d=%d",NHQ204M_CH,voltage)
   _nhq204m_write(str)
   str = ser_get(NHQ204M_SL,0)
#  printf("nhq204m_set(): get %s\n",str)
   str = sprintf("G%1d",NHQ204M_CH)
   _nhq204m_write(str)
   str = ser_get(NHQ204M_SL,0)
#  printf("nhq204m_set(): get %s\n",str)
}'

#%IU% ()
#%MDESC% 
#    Return voltage of NHQ204M
def _nhq204m_get() '{
   local str ii value

   for (ii = 0; ii<3; ii++) {
     str = sprintf("U%1d",NHQ204M_CH)
     _nhq204m_write(str)
     str = ser_get(NHQ204M_SL,0)
     if (sscanf(str, "%f", value) == 0) { 
        printf ("Error on _nhq204m_get, got %s\n", str)
     }
     else {
       return (value)
     }
   }
   return (-1)
}'

#%UU% [ramp]
#%MDESC%
#    Set ramp speed of NHQ204M
def nhq204m_ramp '{
   local value
   if ($#>2) {
	print "Usage:  nhq204m_ramp [speed]"
	exit
   }
   value= ($#<1)?getval("Set NHQ204M ramp to what speed", 2):$1
   _nhq204m_set_ramp(value)
}'

#%IU% (ramp)
#%MDESC%
#    Set ramp speed of NHQ204M
def _nhq204m_set_ramp(ramp) '{
   local str
   
   str = sprintf("V%1d=%d",NHQ204M_CH,ramp)
   _nhq204m_write(str)
#  str = ser_get(NHQ204M_SL,0)
#  printf("nhq204m_set(): get %s\n",str)
}'

#%IU% ()
#%MDESC%
#    Get ramp speed of NHQ204M
def _nhq204m_get_ramp() '{
   local str
   
   str = sprintf("V%1d",NHQ204M_CH)
   _nhq204m_write(str)
   str = ser_get(NHQ204M_SL,0)
   if (sscanf(str, "%d", value)==0) {
	printf("Error on _nhq204m_get_ramp, got %s\n", str)
        return (-1)
   }
   return (value)
}'


#%UU% [c/m/b]
#%MDESC%
#    Defines the nhq204m as pseudo c(ounter), pseudo m(otor) or b(oth)
#%BR%Pseudo names are: nhqps_c for counter, nhqps_m for motor
def nhq204m_on '{
    local ps_type ps_mne

    if (($#<1)||(("$1"!="c")&&("$1"!="m")&&("$1"!="b"))) {
	print "Usage: nhq204m_on <c(ounter)|m(otor)|b(oth)>"
    }
    ps_type= "$1"

    if (ps_type=="c") {
   	cdef ("","","nhqps_m","delete")
	if (motor_num(nhqps_m)!=-1) { A[nhqps_m]= 0 }
   	cdef ("user_getcounts", "S[nhqps_c]=_nhq204m_get();", "nhqps_c", 0x02)
    }
    if (ps_type=="m") {
   	cdef ("","","nhqps_c","delete")
	if (cnt_num(nhqps_c)!=-1) { S[nhqps_c]= 0 }
   	cdef ("user_getpangles","A[nhqps_m]=_nhq204m_get(); NHQ204M_POS=A[nhqps_m];","nhqps_m", 0x01)
   	cdef ("user_checkall","NHQ204M_POS=A[nhqps_m]; _nhq204m_set(A[nhqps_m]);","nhqps_m", 0x01)
    }
}'

#%UU%
#%MDESC%
#    Undefines pseudo counter/motor for nhq204m
def nhq204m_off '{
	cdef ("","","nhqps_c","delete")
	if (cnt_num(nhqps_c)!=-1) { S[nhqps_c]= 0 }
	cdef ("","","nhqps_m","delete")
	if (motor_num(nhqps_m)!=-1) { A[nhqps_m]= 0 }
}'

#%MACROS%
#%IMACROS%
#%AUTHOR%
#  NHQ204M.MAC  AG 2.98 EP 3.03
#%TOC%