esrf

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

#%TITLE% k487.mac
#%NAME%
#  Keithley 487 used as a voltage source.  Use multim.mac for
#  standard reading
#
#%CATEGORY% Detection, multimeter
#
#%DESCRIPTION%
#%PRE%
######################################################################
##########                                                  ##########
########## k487 used as a voltage source using a pseudo     ##########
########## motor                                            ##########
##########                                                  ##########
########## These macros are intended to talk to a k487 or   ##########
########## a k6487 configured in KSCPI gpib labguage        ##########
##########                                                  ##########
########## To set the KSCPI language:                       ##########
##########      K6487 :                                     ##########
##########        + hit "config" key                        ##########
##########        + hit "comm" key                          ##########
##########        + use arriw key up/down in "V-SOURCE"     ##########
##########          panel untill see "LANG: ...".           ##########
##########          "LANG" should blink                     ##########
##########        + use left/right arrows below to make     ##########
##########          the selected language blinking          ##########
##########        + use the up/down arrows marked "RANGE"   ##########
##########          to select "SCPI"                        ##########
##########        + hit "ENTER" key                         ##########
##########                                                  ##########
##########                                                  ##########
##########                                                  ##########
##########                                                  ##########
##########                                                  ##########
######################################################################
#%END%

#-------------------------------------------------------------------------------------------------------------
# "$Header: /segfs/bliss/source/spec/macros/RCS/k487.mac,v 1.7 2016/10/19 10:45:58 homsrego Exp $"
#-------------------------------------------------------------------------------------------------------------
# 2011/04/21 rh - in k487del and k487setrange
#   changed (mne in K487_PAR) by (mne in K487_MNE)
# 2011/05/02 rh - k487_setoff, included the check of the gpib listener
# 2016/10/19 rh - add newlines at the end of the file
#-------------------------------------------------------------------------------------------------------------


#%UU% <motor mne> <gpib addr>
#%MDESC%
#    Adds a k487 pseudo motor. motor mne should already exists in SPEC config
def k487add '{
  local addr mne num exist
  global K487_PAR[]
  global K487_MNE[]

  if ($# != 2) {
    print "Usage : k487add <motor mne> <gpib addr>"
    exit
  }

  mne = "$1"
  addr = "$2"

  if ((num = motor_num(mne)) == -1) {
    printf("motor\"%s\" does not exist, exit !!!\n", mne)
    exit
  }

  exist = -1
  if (mne in K487_MNE) {
    exist = K487_PAR[mne]["on"]
    k487off num
  }

  K487_MNE[mne] = mne
  K487_PAR[mne]["addr"] = addr

  if ((exist == -1) || (exist == 1)) {
    if (exist == -1)
      K487_PAR["nb"]++
    k487on num
  }
}'

#
# delete a k87 pseudo motor already configured
#
def k487del '{
  local mne

  if ($# != 1) {
    print "Usage: k487del [motor mne.]"
    exit
  }

  mne = "$1"

# 2011/04/21 rh - was written (mne in K487_PAR)
  if (mne in K487_MNE) {
    delete K487_MNE[mne]
    delete K487_PAR[mne]["addr"]
    if (K487_PAR[mne]["on"] == 1) {
      K487_PAR["nbon"]--
      K487_PAR["nb"]--
    }
    delete K487_PAR[mne]["on"]
    delete K487_PAR[mne]["old"]
  }
}'

#
# set all k487 pseudo motors configured to ON if no parameters
# set k487 pseudo motor defined by the first parameter as motor num to ON
#
def k487on '{
  local motmne motnum

  if (($# != 0) && ($# != 1)) {
    print "Usage: k487on             ==> set all pseudo on"
    print "       k487on [motor num] ==> set [motor Num] on"
    exit
  }

  if ($# == 1) {
    motnum = $1
    motmne = motor_name(motnum)

    if (motmne in K487_MNE) {
        if (!K487_PAR[motmne]["on"]) {
          K487_PAR[motmne]["on"] = 1
          K487_PAR["nbon"]++
	}
	k487_seton motnum
    } else {
        printf("Unknown k487 \"%s\", exit !!!\n", motmne)
        exit
    }
  }
  else {
      K487_PAR["nbon"] = K487_PAR["nb"]
      for (motmne in K487_MNE) {
          K487_PAR[motmne]["on"] = 1
          motnum = motor_num(motmne)
          k487_seton motnum
      }
  }

  if (K487_PAR["nbon"] != 0) {
    cdef("user_checkall", "k487_checkall\n", "K487")
    cdef("user_getpangles", "k487_getangles\n", "K487")
  }
}'


#
# set all k487 pseudo motors configured to OFF if no parameters
# set k487 pseudo motor defined by the first parameter as motor num to OFF
#
def k487off '{
  local motmne motnum

  if (($# != 0) && ($# != 1)) {
    print "Usage: k487off             ==> set all pseudo off"
    print "       k487off [motor num] ==> set [motor num] off"
    exit
  }

  if ($# == 1) {
    motnum = $1
    motmne = motor_name(motnum)

    if (motmne in K487_MNE) {
    	if (K487_PAR[motmne]["on"]) {
            K487_PAR[motmne]["on"] = 0
            K487_PAR["nbon"]--
        }

        k487_setoff motnum
    }
    else {
        printf("Unknown k487 \"%s\", exit !!!\n", motmne)
        exit
    }
  }
  else {
      K487_PAR["nbon"] = 0

      for (motmne in K487_MNE) {
          K487_PAR[motmne]["on"] = 0
          motnum = motor_num(motmne)
          k487_setoff motnum
      }
  }

  if (K487_PAR["nbon"] == 0) {
    cdef("user_checkall", "", "K487", "delete")
    cdef("user_getpangles", "", "K487", "delete")
  }
}'


#
# read all k487 pseudo motors set to ON
#
def k487_getangles '{
  local mne num res_str
  local comm
  local addr

  for (mne in K487_MNE) {
    if (K487_PAR[mne]["on"]) {
      num = motor_num(mne)
      addr = K487_PAR[mne]["addr"]
      comm = sprintf("SOURCE1:VOLTAGE:LEVEL:IMMEDIATE:AMPLITUDE?")

      gpib_put(addr, comm)
      A[num] = gpib_get(addr) + 0

      K487_PAR[mne]["old"] = A[num]
    }
  }
}'

#
# move all k487 pseudo motors set to ON if necessary
#
def k487_checkall '{
  local mne num
  local comm
  local addr

  for (mne in K487_MNE) {

    if (K487_PAR[mne]["on"]) {
      num = motor_num(mne)

      if (K487_PAR[mne]["old"] != A[num]) {
        addr = K487_PAR[mne]["addr"]
        comm = sprintf("SOURCE1:VOLTAGE:LEVEL:IMMEDIATE:AMPLITUDE %g", A[num])

        gpib_put(addr, comm)
      }
    }
  }
}'

#
# set range (10-50-500) for the motor given by the first parameter
#
def k487setrange '{
    local num range
    local comm
    local addr

    if ($# != 2) {
        print "Usage: k487setrange [motor num] [range (10/50/500)] "
        exit
    }

    num   = $1
    range = $2

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }

    if ((range != 10) && (range != 50) && (range != 500)) {
        printf("Range \"%f\" not in (10/50/500), exit !!!\n", range)
        exit
    }

# 2011/04/21 rh - was written (mne in K487_PAR)
    if (!(mne in K487_MNE)) {
        printf("motor \"%s\" is not defined as K487, exit !!!\n", mne)
        exit
    }

    addr = K487_PAR[mne]["addr"]
    comm = sprintf("SOURCE1:VOLTAGE:RANGE %d", range)

    gpib_put(addr, comm)
}'


def k487getrange '{
    local num range

    if ($# != 1) {
        print "Usage: k487getrange [motor num]"
        exit
    }

    num   = $1

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }

    range = _k487getrange(mne)
    printf("\"%s\" Output Range : %d V\n", mne, range)
}'

def _k487getrange(motmne) '{
    local addr comm ret

    addr = K487_PAR[motmne]["addr"]
    comm = sprintf("SOURCE1:VOLTAGE:RANGE?")

    gpib_put(addr, comm)

    ret = gpib_get(addr, comm)

    return(ret)
}'

def k487_seton '{
    local num mne
    local comm
    local addr

    if ($# != 1) {
        print "Usage: k487_seton [motor num]"
        exit
    }

    num   = $1

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }


    addr = K487_PAR[mne]["addr"]
    comm = sprintf("SOURCE1:VOLTAGE:STATE ON")

    gpib_put(addr, comm)
}'

def k487_setoff '{
    local num mne
    local comm
    local addr

    if ($# != 1) {
        print "Usage: k487_setoff [motor num]"
        exit
    }

    num   = $1

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }


    addr = K487_PAR[mne]["addr"]
            # test the gpib listener
    if(gpib_put(addr,"") == 1) {
        comm = sprintf("SOURCE1:VOLTAGE:STATE OFF")
        gpib_put(addr, comm)
    }

}'

def k487setgain '{
    local cntnum gain addr comm

  if ($# != 2) {
    print "Usage: k487setgain [counter num] [gain (0=auto 1=20mA  2=2mA  3=200uA"
    print "                                        4=20uA 5=2uA  6=200nA 7=20nA "
    print "                                        8=2nA)]"
    exit
  }

  cntnum = $1
  gain   = $2

  if ((gain!=0) && (gain!=1) && (gain!=2) && (gain!=3) && (gain!=4) && \
      (gain!=5) && (gain!=6) && (gain!=7) && (gain!=8)) {
    printf("Gain must be in:\n")
    printf("0=auto 1=20mA  2=2mA  3=200uA 4=20uA 5=2uA  6=200nA 7=20nA 8=2nA\n")
    exit
  }

  if ((cnt_mne(cntnum)) == "?") {
    printf("Counter num. \"%d\" not defined in Spec config, exit !!!\n", cntnum)
    exit
  }

  addr = counter_par(cntnum, "channel")
  if (gain != 0) {
    comm = sprintf(":CURRENT:RANGE %g", 2 * pow(10, -(gain+1)))
  } else {
    comm = sprintf(":CURRENT:RANGE:AUTO ON")
  }

  gpib_put(addr, comm)
}'



def k487set_source_current_limit '{
    local cntnum curr_limit addr comm

    if ($# != 2) {
        print "Usage: k487set_source_current_limit  <counter num> <curr_limit in ampers>"
        print "       curr_limit : 2.5e-2  2.5e-3  2.5e-4  2.5e-5"
        exit
    }

    cntnum = $1
    curr_limit = $2

    if ((curr_limit!=2.5e-2) && (curr_limit!=2.5e-3) && (curr_limit!=2.5e-4) && (curr_limit!=2.5e-5)) {
        printf("Curr_Limit must be in: 2.5e-2  2.5e-3  2.5e-4  2.5e-5\n" )
        exit
    }

    if ((cnt_mne(cntnum)) == "?") {
        printf("Counter num. \"%d\" not defined in Spec config, exit !!!\n", cntnum)
        exit
    }

    addr = counter_par(cntnum, "channel")

    if (curr_limit != 0) {
        comm = sprintf("SOURCE1:VOLTAGE:ILIM %g", curr_limit)
        gpib_put(addr, comm)
    }
    else {
        print "source current limnit error"
    }

}'


def k487configreset '{
    local num mne
    local addr

    if ($# != 1) {
        print "Usage: $0 motor_num"
        exit
    }

    num   = $1

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }

    addr = K487_PAR[mne]["addr"]
    if(yesno("Reset configuration to factory one?",0)) {
        gpib_put(addr, "*RST")
    }
}'

def k487configsave '{
    local num mne
    local addr
    local loc

    if ($# != 2) {
        print "Usage: $0 motor_num memory_location"
        exit
    }

    num   = $1
    loc   = $2

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }

    if((loc<0) || (loc>2)) {
        printf("Invalid memory location. Must be 0,1,2\n")
        exit
    }

    addr = K487_PAR[mne]["addr"]
    _k487configaction("SAV", addr, loc)
}'

def k487configrecall'{
    local num mne
    local addr
    local loc

    if ($# != 2) {
        print "Usage: $0 motor_num memory_location"
        exit
    }

    num   = $1
    loc   = $2

    if ((mne = motor_mne(num)) == "?") {
        printf("Motor num. \"%d\" not defined in Spec config, exit !!!\n", num)
        exit
    }

    if((loc<0) || (loc>2)) {
        printf("Invalid memory location. Must be 0,1,2\n")
        exit
    }

    addr = K487_PAR[mne]["addr"]
    if(yesno("Overwrite current configuration with stored one?",0)) {
        _k487configaction("RCL", addr, loc)
    }
}'

def _k487configaction(act, addr, loc) '{
    local comm
    comm = sprintf("*%s %d", act, loc)
    gpib_put(addr, comm)
}'