esrf

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

#%TITLE% WAGODAC.MAC
#%NAME% Macros to implement WAGO DAC as macro motors
#
#%CATEGORY% Generic I/O
#
#%DESCRIPTION%
# Macros to implement WAGO DAC as macro motors
# using the WAGO Device Server and parameters from SPEC config only.
# This set of macros is based of ID23 macros (written by VR). The main
# change is the use of SPEC config instead of properties file, to get
# parameters. Therefore to configure the macro motors, nothing else
# than SPEC is needed.
#
#%UL%
#
#%LI% For each macro motor to configure:
#%UL%
#%LI% "Generic Parameter 1": WAGO box name (ex: wcid23b)
#%LI% "Generic Parameter 2": DS logical name of the DAC(s) (ex: dac_piezo)
#%LI% "Generic Parameter 3": DS logical channel of the DAC (ex: 0)
#%LI% "Generic Parameter 4": convertion factor (ex: 1.5)
#%XUL%
#
#%LI% The "Generic Parameter 3" is optional and would be 0 by default which 
# means that there is only one DAC channel with the logical name specified as
# seconde parameter.
#
#%LI% The "Generic Parameter 4" is optional and would be 1 by default which 
# means that the user of the macro motor gives directly  the voltage that
# would be generated by the WAGO. If a factor is given...
#
#%LI% The WAGO Device Server name will be automatically generated from 
# the beamline name and the WAGO box name (ex: id13/wcid13b/wc or
# tango:id13/wcid13b/tg)
#
#%XUL%
#
#%END%




#%IU%
#%MDESC%
# Called by spec after reading the config file
#
def wagodac_config( motnum, type, unit, module, chan ) '{
  local mne wago_addr dev_name

  global WAGODAC_DEV[] WAGODAC_KEY[] 
  global WAGODAC_NAM[] WAGODAC_CHA[]
  global WAGODAC_FAC[] WAGODAC_VAL[]

  if (motnum != "..") {
     #print "mne: " motor_mne(motnum) " " "num: " motnum

     mne = motor_mne(motnum)

     if (motnum >= 0) {
       wago_addr = motor_par(motnum, "misc_par_1")
       if (wago_addr) {
         dev_name = sprintf("%s/%s/wc", SPECBL, wago_addr)
	 ESRF_ERR = -1
	 if ((esrf_io(dev_name, "DevState") < 0) && \
	     (index(ESRF_ERR_MSG, "Device not defined in the database") > 0))
	     dev_name = sprintf("tango:%s/%s/tg", SPECBL, wago_addr)
         WAGODAC_DEV[mne] = dev_name
       } else {
         printf("WAGODAC: %s: error, invalid generic parameter 1 (should be wago name)", mne)
         return ".error."
       }

       if (motor_par(motnum, "misc_par_2")) {
         WAGODAC_NAM[mne] = motor_par(motnum, "misc_par_2") 
       } else {
         printf("WAGODAC: %s: error, invalid generic paramter 2 (should be wago key)", mne)
         return ".error."
       }

       WAGODAC_KEY[mne] = esrf_io(WAGODAC_DEV[mne], "DevName2Key", WAGODAC_NAM[mne])
       WAGODAC_CHA[mne] = motor_par(motnum, "misc_par_3")
       if (motor_par(motnum, "misc_par_4") == 0) {
         WAGODAC_FAC[mne] = 1
       } else {
         WAGODAC_FAC[mne] = motor_par(motnum, "misc_par_4") 
       }
     } else {
       printf("WAGODAC: invalid motor %s\n", mne)
       return ".error."
     }
  }
}'


#%IU%
#%MDESC%
# Called by spec
#
def wagodac_cmd(num, cmd, p1, p2) '{
   local mne
 
   if(num == "..") { return }
 
   mne=motor_mne(num)

   if ( cmd == "position" ) {
        return wagodac_read(mne)
   }

   if ( cmd == "start_one") {
        return wagodac_set(mne, p1)
   }

   if ( cmd == "set_position") {
        return wagodac_set(mne, p1)
   }
}'


#%IU%
#%MDESC%
#
def wagodac_read(mne) '{
    local wdac_out[]
    local ret

    ret = esrf_io(WAGODAC_DEV[mne],"DevReadNoCachePhys",WAGODAC_KEY[mne],wdac_out)

    if ( ret == 1 ) {
      WAGODAC_VAL[mne] = wdac_out[WAGODAC_CHA[mne]]*WAGODAC_FAC[mne]
      return  WAGODAC_VAL[mne]
    }
}'



#%IU%
#%MDESC%
#
def wagodac_set(mne,newpos) '{
    local pin[]

    pin[0] = WAGODAC_KEY[mne]
    pin[1] = WAGODAC_CHA[mne]
    pin[2] = newpos / WAGODAC_FAC[mne]
    ret = esrf_io(WAGODAC_DEV[mne],"DevWritePhys", pin )

    t0=time()
    while (time()-t0 < 2) {
      ret = wago_readch(WAGODAC_NAM[mne])
      if (fabs(ret - pin[2]) < 0.01) {
        break
      }
      sleep(0.1)
    }
}'



#%MACROS%
#%IMACROS%
#%AUTHOR% VR+MP / BLISS / ESRF Feb 2005
#%TOC%