esrf

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

#%TITLE% dac.mac
#%NAME% 
#  Macros to control a dac device server (VME card ICV712)
#
#%CATEGORY% Generic I/O
#
#%DESCRIPTION%
#  This macro file allows to define dac channels as motors in SPEC.
#  As this is implemented on the macro level not all commands will work.
#
#%EXAMPLE%
#%DL%
#%DT%  mv dac1 50  %DD% Sets an output of 50 in the dac1 device server.     
#        this is converted to dac output depending on the ICV712 settings.
#%DT%  ascan dac2 0 10 10 1 %DD%
#        (scan dac2 from 0 to 10 in 10 steps and count for 1 second)
#%DT%  a2scan dac1 0 20 opti -5 5 10 1 %DD%
#        (scan the optical table stepper motor from
#        -5 to 5 degrees and scan at the same time the dac1 from 0 to 20 micr.)
#%XDL%
#%END%

global DACON DAC_LIST
list_test DAC_LIST

#%UU% dac-mnemonic dac-device [scale-factor]
#%MDESC%
#   Add definitions for a dac pseudomotor
def dacadd  'dac_add("$1", "$2", "$3")'

#%IU% (dac-mnemonic, dac-device)
#%MDESC%
#   Add definitions for a dac pseudomotor
def dac_add(mne, dev, factor) '{
    list_add(DAC_LIST,mne)
    list_setpar(DAC_LIST,mne,"dev",dev)
    if(factor==0) factor=1.0;
    list_setpar(DAC_LIST,mne,"factor",factor)

    setup_tail("dac", sprintf("dac_%s %s", mne, mne))
}'

def dacunsetup '{
   dacdel("$2")
}'

#%UU% (dac-mnemonic)
#%MDESC%
#   Delete definitions for one DAC mnemonic
#
def dacdel(mne) '{
    list_remove(DAC_LIST,mne)
    cdef("", "", mne, "delete")

}'

#%UU% [dac-mne1 dac-dev1 [...]]
#%MDESC%
#  Sets parameters for a group of DAC pseudomotors. It can be called
#  interactively or non-interactively by giving all parameters in the
#  command line.
def dacsetup '
{
   local daclist i
   local params
   local dacmne dacdev dacno factor

   if (!$#) {
      dacshow
    
      if ((dacno = list_n(DAC_LIST)) > 0 )
          _yn = yesno("Do you want to change any of this values",0)

      if (_yn || !dacno) {
          dacoff
          print "\nEnter as mnemonic \`end\' to finish or \`delete\' to remove a dac."
          list_init daclist
          for(i = 1; ; i++) {
             printf("\nDAC #%d:", i)
             if ((dacmne = list_item(DAC_LIST, i)) == -1)
                dacmne = "end"
             dacmne = getval("\tMnemonic", dacmne)
             if (dacmne == "delete")
                continue
             if (dacmne == "end")
                break
             if ((dacdev = list_getpar(DAC_LIST, dacmne, "dev")) == -1) 
                dacdev = ""
             dacdev = getval("\tDevice name", dacdev)
             if ((factor = list_getpar(DAC_LIST, dacmne, "factor")) == -1) 
                factor = 1
             factor = getval("\tScale factor", factor)
             list_add(daclist, dacmne)
             list_setpar(daclist, dacmne, "dev",   dacdev)
             list_setpar(daclist, dacmne, "factor",factor)
          }
          print
          dacreset
          for (i = 1; i <= list_n(daclist); i++) {
             dacmne = list_item(daclist, i)
             dacdev = list_getpar(daclist, i, "dev")
             factor = list_getpar(daclist, i, "factor")
             dac_add(dacmne, dacdev, factor)
          }
          dacon
      }
   } else {
      dacoff
      dacno = int($#)/2
      split("$*",params)
      dacreset
      for (i = 0; i < dacno; i++) {
          dacmne = params[i*2]
          dacdev = params[i*2 + 1]
          dac_add(dacmne, dacdev)
      }
      dacon
   }
}
'

#%UU%
#%MDESC%
def dacreset '
    dacoff
    list_init DAC_LIST
'

#%UU
#%MDESC%
#  Shows current DAC pseudomotors definition.
#
def dacshow '{
    local dacno mne

    if (dacno = list_n(DAC_LIST)) {
 
       printf("\nDAC pseudomotors are \"%s\"\n\n",DACON?"ACTIVE":"NOT ACTIVE")
       printf("\t   Motor               Device          Config      Factor\n") 
       printf("\t---------------------------------------------------------\n") 
       for (i=1; i<=dacno; i++) {
            mne =  list_item(DAC_LIST, i)
            printf("\t%8s %20s", mne, list_getpar(DAC_LIST, i, "dev")) 
            if (motor_num(mne) < 0) 
                printf(" %15s","NOT DEFINED")
            else 
                printf(" %15s","OK")
            printf("%12s\n", list_getpar(DAC_LIST, i, "factor")) 
       }
       printf("\n")
    } else {
       printf("Nothing defined for DAC\n")
    }
}' 

#%UU% 
#%MDESC% 
#  Deactivates definitions for DAC pseudomotors.
def dacoff '
{
    local dacno

    dacno = list_n(DAC_LIST)

    for (i = 1; i <= dacno; i++) {
        cdef("", "", list_item(DAC_LIST, i), "delete")
    } 
    DACON=0
}'

#%IU% 
#%MDESC%
#  Activates definitions for DAC pseudomotors.
#
def dacon '{
    local dacno num mne

    dacno = list_n(DAC_LIST)

    for (i=1; i <= dacno; i++) {
        num = motor_num(mne = list_item(DAC_LIST, i))
        if (num != -1 ) {
        cdef("user_checkall",sprintf("dacmove %s\n", mne), mne, 0x01)
        cdef("user_getpangles",sprintf("dacgetangles %s\n", mne), mne, 0x01)
        }
    } 
    DACON=1
    printf("DAC pseudomotors are now ACTIVE\n")
}'

#%IU%
#
def dacmove '
{
   local device factor

   device = list_getpar(DAC_LIST, "$1", "dev")
   factor = list_getpar(DAC_LIST, "$1", "factor")
   esrf_io(device, "DevSetValue", A[$1]*factor)
}'

#%IU%
#
def dacgetangles '
{
   local device angle factor

   device = list_getpar(DAC_LIST, "$1", "dev")
   factor = list_getpar(DAC_LIST, "$1", "factor")

# The Icv716 card will not allow us to read the value back - would
# that not be something to be changed in the server.
   ESRF_ERR=-1 ; angle=esrf_io(device,"DevReadValue")
   if (angle != -1) A[$1]=angle/factor
}'

#%IU% 
#%MDESC% For blmenu (use blmenu.mac).
def dacbody(mode) '{

   if (mode == 1) {
      if (DACON) {
         dacoff
      }
      else {
         dacon
      }
      
   }
   return(sprintf("%s",DACON?"ON":"OFF"))
}'


#%MACROS%
#%IMACROS%
#%DEPENDENCIES%
#  To use a dac as a motor the following conditions have to be fulfilled:
#%UL%
#%LI% The file dac.mac has to be read in               done by: startup s.
#    (this file needs: pseudo.mac , stchanges.mac)
#%LI% the dac motors have to be configured            done by: SPECADM
#       (Controller NONE, mnemonic as in startupscript)
#%LI% setup the dacs ( with dacsetup )              done by: startup s.
#%XUL%
#%AUTHOR%
#%TOC%