esrf

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

#%TITLE% HDH.MAC 
#%NAME%
#  Macros to read the Heidenhain encoders (RON 806) through 
#  a serial line.
#
#%CATEGORY% Positioning
#
#%DESCRIPTION%
#  Macros to define and control Haidenhein encoders
#  as pseudomotors
#%BR%  These hdh are assumed to be controlled through
#  a serial line
#%BR%  Through an Hdh, we can access 2 values.
#
#%BR%
#%BR%  Serial line protocol:
#%UL% 
#%LI% ^B  to ask for a value
#%LI% The response of the serial line is :
#%UL% 
#%LI% X=`sign'    <floating value> R <CR><LF>
#%LI% Y=`sign'    <floating value> R <CR><LF><LF><LF><LF><LF>
#%XUL%
#%XUL%
#
#  NOTE: For the following macros, the HDH_DEV_index
#        represents the index 0..n in the list of 
#        HDH devices defined by the user with hdhsetup
#        If this parameter is omitted, as it is always
#        the last parameter in the list of the macro,
#        Spec will replace it by 0. So omitting it
#        allows to work with the first HDH of the list.
#

#%AUTHOR%
#  March 1st 1994  MCD

#%DEPENDENCIES%
#  To use an HDH encoder:
#%UL%
#%LI% load the macro file : hdh.mac
#%LI% setup the encoder(s) : with hdhsetup
#%LI% configure your encoder(s) :
#%UL%
#%LI% controller NONE
#%LI% mnemonic corresponding to the one provided in hdhsetup
#%XUL%
#%XUL%

#%END%
#  hdhsetup nb dev_name mne cha ncha dev_name mne cha ...
#
#     setup of the devices
#
#  hdh_flush  HDH_DEV_index
#
#     print what is on the serial line buffer for one device
#
#  hdh_read n  HDH_DEV_index
#
#      prints the position in channel `n' (1 or 2 or 0=both)
#      of one device
#
#  hdh1_read n  HDH_DEV_index
#
#      one channel hdh
#      prints the position 
#      of one device
#
#  hdh2_read n  HDH_DEV_index
#
#      two channel hdh (same as hdh_read)
#      prints the position in channel `n' (1 or 2 or 0=both)
#      of one device
#
#  hdh3_read n  HDH_DEV_index
#
#      two channel hdh in channel `n' (1 or 2 or 0=both)
#      prints the position 
#      of one device
#
#  hdh1_read3  mne  n  HDH_DEV_index 
# 
#      for pseudo motor mecanism, reads the value
#      of one pseudo motor (hdh 1 channel)
#      It puts also the result in HDH_VAL
#
#  hdh2_read3  mne  n  HDH_DEV_index 
# 
#      for pseudo motor mecanism, reads the value
#      of one pseudo motor (hdh 2 channel)
#      It puts also the result in HDH_VAL
#
#  hdh3_read3  mne  n  HDH_DEV_index 
# 
#      for pseudo motor mecanism, reads the value
#      of one pseudo motor (hdh 3 channel)
#      It puts also the result in HDH_VAL
#
#  Macros:
#
#  Author: MCD
#  $Revision: 3.1 $

#----------
#  hdhsetup nb dev_name1 pseudo_mne1 ch1 nch1 dev_name2 ch22 pseudo_mne2 ...
#
#           `nb          : nb of hdh pseudomotors defined
#           Dev_name`i'  : Name of the MAXE device containing
#                          the CC133 encoder for the `i'th pseudom.
#           pseudo_mne`i': mnemonic associated to that pseudomotor
#           ch`i'        : channel number (1 for X) or (2 for Y)
#           nch`i'	 : number of channels of the equipement
# 
#  when called without arguments, this macro asks interactively
#  for the values
#
#----------
#%UU% <nb> <dev_name1> <pseudo_mne1> <ch1> <nch1> <dev_name2> <ch2> <pseudo_mne2> ...
#%MDESC%
# setup of the devices :
#%UL%
#%LI% nb           : nb of hdh pseudomotors defined
#%LI% dev_name`i'  : Name of the MAXE device containing
# the CC133 encoder for the `i'th pseudom.
#%LI% pseudo_mne`i': mnemonic associated to that pseudomotor
#%LI% ch`i'        : channel number (1 for X) or (2 for Y)
#%LI% nch`i'	   : number of channels of the equipement
#%XUL%
#when called without arguments, this macro asks interactively
#  for the values
#%BR% This macro allows to define some HDH encoders as pseudomotors.
#%BR% For each encoder, the user has to provide the mnemonic for this
# encoder (free), the server device name for the hardware 
# access to the encoder, an integer indicating which
# channel he wants to read, and the number of channels managed by
# the equipement. Each encoder has a number, from 0 to 
# [nb]-1.
#%BR% In the following functions,  HDH_DEV_index represents this number.

def hdhsetup '
{
global HDH_NO HDH_DEV HDH_MNE HDH_CHA HDH_MAXNO HDH_PRINT HDH_NCHA
# HDH_VAL will receive the last read hdh value
global HDH_VAL  HDH_ERROR
global ppstring
local _pmne _pdev _chan nomne _nchan

HDH_PRINT = 1

HDH_MAXNO=5
_pdev[0]="$2"
_pdev[1]="$6"
_pdev[2]="$10"
_pdev[3]="$14"
_pdev[4]="$18"
_pmne[0]="$3"
_pmne[1]="$7"
_pmne[2]="$11"
_pmne[3]="$15"
_pmne[4]="$19"
_chan[0]="$4"
_chan[1]="$8"
_chan[2]="$12"
_chan[3]="$16"
_chan[4]="$20"
_nchan[0]="$5"
_nchan[1]="$9"
_nchan[2]="$13"
_nchan[3]="$17"
_nchan[4]="$21"

if ($# > HDH_MAXNO*4+1) {
  print "Usage: hdhsetup [nb of hdh] [dev name] [hdh mne] [hdh cha] [nb of cha]"
  print "                         [dev name] [hdh mne] [hdh cha] ..."
  exit
  }

nomne = int (($#-1)/2)

if ($# < 1) {
  HDH_NO = getval("How many Hdh devices do you want to use ",HDH_NO)
  }
else {
  HDH_NO = $1
  }

if (HDH_NO > HDH_MAXNO) {
  printf ("Just %d hdh allowed! Nothing done!\n",HDH_MAXNO)
  exit
  }


for (i=0; i< HDH_NO; i++) {
  if (nomne < i+1 && HDH_NO > i) {
    HDH_DEV[i]=getval(sprintf("What is the name of your Hdh device No %d ",i+1),HDH_DEV[i])
    }
  else if (HDH_NO > i) {
    HDH_DEV[i]=_pdev[i]
    }
  if (nomne < i+1 && HDH_NO > i) {
    HDH_MNE[i] = getval("What is the name of your pseudo motor hdh ",HDH_MNE[i])
    }
  else if (HDH_NO > i) {
    HDH_MNE[i]=_pmne[i]
    }
  if (nomne < i+1 && HDH_NO > i) {
    HDH_CHA[i] = getval("What is your Hdh channel [1 or 2]",HDH_CHA[i])
    }
  else if (HDH_NO > i) {
    HDH_CHA[i]=_chan[i]
    }
  if (nomne < i+1 && HDH_NO > i) {
    HDH_NCHA[i] = getval("How many channels in your equipement?",HDH_NCHA[i])
    }
  else if (HDH_NO > i) {
    HDH_NCHA[i]=_nchan[i]
    }
  }

for (i=0;i<HDH_NO; i++) {
  local ppstring
  if (HDH_NCHA[i] == 1) {
     ppstring=sprintf ("%s none none hdh1_read3 none none none none %d %d",HDH_MNE[i],HDH_CHA[i],i)
  }
  if (HDH_NCHA[i] == 2) {
     ppstring=sprintf ("%s none none hdh2_read3 none none none none %d %d",HDH_MNE[i],HDH_CHA[i],i)
  }
  if (HDH_NCHA[i] == 3) {
     ppstring=sprintf ("%s none none hdh3_read3 none none none none %d %d",HDH_MNE[i],HDH_CHA[i],i)
  }
  pseudosdef ppstring  
 }
}
'

#-------------------------------------------
#  hdh_flush  HDH_DEV_index
#
#  prints what is on the buffer for a device
#
#%UU% <HDH_DEV_index>
#%MDESC%
#  prints what is on the buffer for a device (flushes the serial line)
def hdh_flush '
print esrf_io(HDH_DEV[$1],"DevSerReadString",0)
'


#-------------------------------------------
#  hdh_read  `n'  HDH_DEV_index
#
#  If n is not provided, prints the values of 
#  the 2 channels of a device
#  If n is 1  : prints the value of X channel
#  If n is 2  : prints the value of Y channel
#
#  put the last read value in HDH_VAL
#%UU% <n>  <HDH_DEV_index>
#%MDESC%
#  reads a TWO channels equipement.
#%BR% reads the heidenhain channel value for the `HDH_DEV_index'th
#  encoder, according to `n'.
#%BR% If n is not provided, prints the values of 
#  the 2 channels of a device
#%BR% If n is 1  : prints the value of X channel and puts it in HDH_VAL
#%BR% If n is 2  : prints the value of Y channel and puts it in HDH_VAL
def hdh_read '
{
   local str1 str2 dummy valx valy len
   HDH_ERROR = 0
   esrf_io(HDH_DEV[$2],"DevSerWriteString","\2")

#  X: read until <CR>, 
   str1 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
   len = length(str1)
   if (len == 0) {
      print HDH_MNE[$2] X " : no data"
      HDH_ERROR = 1
   }
   else {
   if ($1 !=1) {
#     should be a valid Y value
      str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
      len = length(str2)
      if (len == 0) {
         print HDH_MNE[$2] Y " : no data"
         HDH_ERROR = 1
      }
   }
   else {
#     we read the present buffer, because <CR> may never appear
      str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",0)
   }

#  X value if request = 0 or 1
   if ($1 !=2 ) {
      sscanf(str1,"%s%f",dummy,valx)
      if (index(dummy,"-") !=0) {
        valx = -valx
      }
   print "X= " valx
   HDH_VAL = valx
   }

#  Y value if request = 0 or 2
   if ($1 !=1) {
      sscanf(str2,"%s%f",dummy,valy)
      if (index(dummy,"-") !=0) {
        valy = -valy
      }
      print "Y= " valy
      HDH_VAL = valy
   }
   }
}
'


#-------------------------------------------
#  hdh1_read  `n'  HDH_DEV_index
#
#  Reads a one channel only equipement
#
#
#  put the last read value in HDH_VAL
#%UU% <n>  <HDH_DEV_index>
#%MDESC%
#  reads a ONE channel equipement.
#%BR% reads the heidenhain channel value for the `HDH_DEV_index'th.
#%BR% `n' is unused.
#%BR% Puts the value in HDH_VAL and prints it.
def hdh1_read '
{
   local str1 str2 dummy valx valy len
   HDH_ERROR = 0
   esrf_io(HDH_DEV[$2],"DevSerWriteString","\2")

#  X: read until <CR>, 
   str1 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
   len = length(str1)
   if (len == 0) {
      print HDH_MNE[$2] " : no data"
      HDH_ERROR = 1
   }
   else {
#  we flush the present buffer. usefull?
   str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",0)
  
#  X value anyway
   sscanf(str1,"%s%f",dummy,valx)
   if (index(dummy,"-") !=0) {
     valx = -valx
   }

   if ( HDH_PRINT == 1) {
      print "X= " valx
   }
   HDH_VAL = valx
   }

}
'

#-------------------------------------------
#  hdh2_read  `n'  HDH_DEV_index
#
# Reads a 2 channels only equipement
#
#  If n is not provided, prints the values of 
#  the 2 channels of a device
#  If n is 1  : prints the value of X channel
#  If n is 2  : prints the value of Y channel
#
#  put the last read value in HDH_VAL
#%UU% <n>  <HDH_DEV_index>
#%MDESC%
#  reads a TWO channels equipement.
#%BR% reads the heidenhain channel value for the `HDH_DEV_index'th
#  encoder, according to `n'.
#%BR% If n is not provided, prints the values of 
#  the 2 channels of a device
#%BR% If n is 1  : prints the value of X channel and puts it in HDH_VAL
#%BR% If n is 2  : prints the value of Y channel and puts it in HDH_VAL
def hdh2_read '
{
   local str1 str2 dummy valx valy len
   HDH_ERROR = 0
   esrf_io(HDH_DEV[$2],"DevSerWriteString","\2")

#  X: read until <CR>, 
   str1 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
   len = length(str1)
   if (len == 0) {
      print HDH_MNE[$2] X " : no data"
      HDH_ERROR = 1
   }
   else {
   if ($1 !=1) {
#     should be a valid Y value
      str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
      len = length(str2)
      if (len == 0) {
         print HDH_MNE[$2] Y " : no data"
         HDH_ERROR = 1
      }
   }
   else {
#     we read the present buffer, because <CR> may never appear
      str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",0)
   }
   

#  X value if request = 0 or 1
   if ($1 !=2 ) {
      sscanf(str1,"%s%f",dummy,valx)
      if (index(dummy,"-") !=0) {
        valx = -valx
      }
   if ( HDH_PRINT == 1) {
      print "X= " valx
   }
   HDH_VAL = valx
   }

#  Y value if request = 0 or 2
   if ($1 !=1) {
      sscanf(str2,"%s%f",dummy,valy)
      if (index(dummy,"-") !=0) {
        valy = -valy
      }
      if ( HDH_PRINT == 1) {
         print "Y= " valy
      }
      HDH_VAL = valy
   }
   }
}
'
#-------------------------------------------
#  hdh3_read  `n'  HDH_DEV_index
#
#  Reads a 3 channels only equipement
#  If n is not provided, prints the values of 
#  the 2 channels of a device
#  If n is 1  : prints the value of X channel
#  If n is 2  : prints the value of Y channel
#
#  put the last read value in HDH_VAL
#%UU% <n>  <HDH_DEV_index>
#%MDESC%
#  reads a THREE channels equipement.
#%BR% reads the heidenhain channel value for the `HDH_DEV_index'th
#  encoder, according to `n'.
#%BR% If n is not provided, prints the values of 
#  the 2 channels of a device
#%BR% If n is 1  : prints the value of X channel and puts it in HDH_VAL
#%BR% If n is 2  : prints the value of Y channel and puts it in HDH_VAL
def hdh3_read '
{
   local str1 str2 dummy valx valy str3 len
   HDH_ERROR = 0
   esrf_io(HDH_DEV[$2],"DevSerWriteString","\2")

#  X: read until <CR>, 
   str1 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
   len = length(str1)
   if (len == 0) {
      print HDH_MNE[$2] X " : no data"
      HDH_ERROR = 1
   }
   else {
#  
   if ($1 !=1) {
#     should be a valid Y value
      str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",2)
      len = length(str2)
      if (len == 0) {
         print HDH_MNE[$2] Y " : no data"
         HDH_ERROR = 1
      }
   }
   else {
#     we read the present buffer, because <CR> may never appear
      str2 = esrf_io(HDH_DEV[$2],"DevSerReadString",0)
   }
   
# we flush the buffer 
      str3 = esrf_io(HDH_DEV[$2],"DevSerReadString",0)

#  X value if request = 0 or 1
   if ($1 !=2 ) {
      sscanf(str1,"%s%f",dummy,valx)
      if (index(dummy,"-") !=0) {
        valx = -valx
      }
   if ( HDH_PRINT == 1) {
      print "X= " valx
   }
   HDH_VAL = valx
   }

#  Y value if request = 0 or 2
   if ($1 !=1) {
      sscanf(str2,"%s%f",dummy,valy)
      if (index(dummy,"-") !=0) {
        valy = -valy
      }
      if ( HDH_PRINT == 1) {
         print "Y= " valy
      }
      HDH_VAL = valy
   }
   }
}
'
#-------------------------------------------
#  hdh1_read3  motor_mne  1/2  HDH_list_index 
#              
#       HDH_list_index:  index in HDH_xx list
#
#  macro for the pseudo_motor mecanism
#
#%UU% <motor_mne> <1/2>  <HDH_DEV_index>
#%MDESC%
#  macro for the pseudo_motor mecanism (ONE channel equipement)
#%UL%
#%LI% motor_mne     : motor mnemonic
#%LI% 1/2           : unused
#%LI% HDH_DEV_index : index in the devices list.
#%XUL%

def hdh1_read3'
   HDH_PRINT=0

   if (set_sim(-1) > 0) {
      HDH_VAL = A[$1]
   }
   else {
      hdh1_read $2 $3
      if (HDH_ERROR == 0) {
         A[$1]=HDH_VAL
      }
   }
   HDH_PRINT=1
'

#-------------------------------------------
#  hdh2_read3  motor_mne  1/2  HDH_list_index 
#              
#       HDH_list_index:  index in HDH_xx list
#       1 -> if interested by X value of Hdh
#       2 -> if interested by Y value of Hdh
#
#  macro for the pseudo_motor mecanism
#  put the value in HDH_VAL
#
#%UU% <motor_mne> <1/2>  <HDH_DEV_index>
#%MDESC%
#  macro for the pseudo_motor mecanism (TWO channel equipement)
#%UL%
#%LI% motor_mne     : motor mnemonic
#%LI% 1/2           : 1 if X channel, 2 if Y channel.
#%LI% HDH_DEV_index : index in the device list 
#%XUL%
def hdh2_read3'
   HDH_PRINT=0

   if (set_sim(-1) > 0) {
      HDH_VAL = A[$1]
   }
   else {
      hdh2_read $2 $3
      if (HDH_ERROR == 0) {
         A[$1]=HDH_VAL
      }
   }
   HDH_PRINT=1
'

#-------------------------------------------
#  hdh3_read3  motor_mne  1/2  HDH_list_index 
#              
#       HDH_list_index:  index in HDH_xx list
#       1 -> if interested by X value of Hdh
#       2 -> if interested by Y value of Hdh
#
#  macro for the pseudo_motor mecanism
#  put the value in HDH_VAL
#
#%UU% <motor_mne> <1/2>  <HDH_DEV_index>
#%MDESC%
#  macro for the pseudo_motor mecanism (THREE channels equipement)
#%UL%
#%LI% motor_mne     : motor mnemonic
#%LI% 1/2           : 1 if X channel, 2 if Y channel.
#%LI% HDH_DEV_index : index in the device list 
#%XUL%
def hdh3_read3'
   HDH_PRINT=0

   if (set_sim(-1) > 0) {
      HDH_VAL = A[$1]
   }
   else {
      hdh3_read $2 $3
      if (HDH_ERROR == 0) {
         A[$1]=HDH_VAL
      }
   }
   HDH_PRINT=1
'

#%MACROS%