esrf

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

#%TITLE% hp8116a.MAC
#
#%NAME%
#  hp8116a.mac - Macros to control pulse function generator HP8116a
#
#%OVERVIEW%
#  
#
#%END%



### start of setup

#%UU%
#%MDESC%
# Syntax : h8116a_setup.%BR%
# Set the global variables for pulse function generator HP8116a
#
def h8116a_setup '{
    global H8116A_PAR[]
    #setup_tail("h8116a_")
}'

#%UU%
#%MDESC%
# Syntax : h8116a_gpib_setup <GPIB unit> <GPIB number>.%BR%
# Set the global variables used for all gpib access: H8116A_PAR["comm_mode"] and H8116A_PAR["addr"]
#
def h8116a_gpib_setup '{ 
    local gpib_unit gpib_addr
     
    if ($# != 2) {
        printf("Usage: h8116a_gpib_setup <Spec GPIB unit range> <Instrument GPIB number>")
    } else {
        gpib_unit = $1 ; gpib_addr = $2
        H8116A_PAR["addr"] = sprintf("%1d:%1d",gpib_unit,gpib_addr)
        H8116A_PAR["comm_mode"] = "GPIB"
    }
}'

### end of setup



### start of basic communication functions

#%IU%
#%MDESC%
# Syntax : _h8116a_send()%BR%
# Internal function to send command string to the hp8116a
#
def _h8116a_send(command) '{
    local numsent
    local opc

    opc=" *OPC"
    command = command opc

    if (H8116A_PAR["comm_mode"] == "GPIB") {
	numsent = gpib_put(H8116A_PAR["addr"],command)
	if (numsent == 0) {
            print "Number of chars sent = 0!!"
	    return "H8116AERROR"
        } else {
	    return numsent
        }
    }
}'


#%IU%
#%MDESC%
# Syntax : _h8116a_receive()%BR%
# Internal function to read string from the hp8116a
#
def _h8116a_receive() '{
    local ans
    ans = "H8116AERROR"

    if (H8116A_PAR["comm_mode"] == "GPIB") {
        ans = gpib_get(H8116A_PAR["addr"])
        if (ans == "") {
            print "Got empty answer!!"
            ans = "H8116AERROR"
        }
    }
    return ans 
}'


#%IU%
#%MDESC%
# Syntax : _h8116a_comm()%BR%
# Internal function to send a command and read it back from hp8116a
#
def _h8116a_comm(command) '{
    local numsent
    local ans
    numsent = _h8116a_send(command)
    if (numsent == "H8116AERROR") {
        printf("Sending %s failed\n", command)
        exit
    }
    ans = _h8116a_receive()
    if (ans == "H8116AERROR") {
        printf("Reading from hp8116a failed\n")
        exit
    }
    return ans 
}'

### end of basic communication functions


### start of different commands related macros and functions

#%UU%
#%MDESC%
# Syntax : h8116_current_settings%BR%
# Display the device indentification string of the hp8116a.
#
def  h8116a_current_settings '{
    local settings
    
    settings = _h8116a_get_current_settings()
    printf("HP8116A current settings are:\n %s\n",settings)
}'


#%IU%
#%MDESC%
# Syntax : _h8116a_get_current_settings()%BR%
# Internal function which gets the hp8116a identification string from
# the instrument and returns it.
#
def _h8116a_get_current_settings() '{
    local ans
    
    ans = _h8116a_comm("CST")
    return ans
}'


#%UU%
#%MDESC%
# Syntax : h8116_lol%BR%
# Sets base signal level
#
def h8116a_base '{
    local tmpbase
    local cmdstr
    local ans 

    local ret
    ret = _h8116a_get_current_settings()
    if (index(ret,"C0") > 0) {
        # Normal output
        if ($# == 0) {
	    # Default value = 0
            tmpbase = 0
        } else if ($# == 1) {
            tmpbase = $1
        } else {
            printf("Usage: %s <base voltage level(Volts)>\n")
            exit
        }
        ans = _h8116a_comm("IHIL")
        local nf, arr[], currhigh
        nf = split(ans,arr)
        currhigh = sprintf("%f",arr[1])
        if (tmpbase >= currhigh) {
            printf("New wanted base level (LOL) is above or equal current high level\n")
            cmdstr = sprintf("HIL " tmpbase+0.1 " V")
            ans = _h8116a_comm(cmdstr)
	    status = _h8116a_comm("IHIL")
            printf("High signal voltage level reset:")
            printf(status)
            printf("\n")
        }
        H8116A_PAR["base"] = tmpbase 
        cmdstr = sprintf("LOL %f V",H8116A_PAR["base"])
        ans = _h8116a_comm(cmdstr)
        printf("Base signal voltage level set to %f Volts\n", H8116A_PAR["base"])
	status = _h8116a_comm("ILOL")
	printf(status)
    } else {
        # Complementary output
        if ($# == 0) {
	    # Default value = 1 V
            tmpbase = 1
        } else if ($# == 1) {
            tmpbase = $1
        } else {
            printf("Usage: %s <base voltage level(Volts)>\n")
            exit
        }
        ans = _h8116a_comm("ILOL")
        local nf, arr[], currlow
        nf = split(ans,arr)
        currlow = sprintf("%f",arr[1])
        if (tmpbase <= currlow) {
            printf("New wanted base level (HIL) is below or equal current low level\n")
            cmdstr = sprintf("LOL " tmpbase-0.1 " V")
            ans = _h8116a_comm(cmdstr)
	    status = _h8116a_comm("ILOL")
            printf("Low signal voltage level reset:")
            printf(status)
	    printf(" \n")
        }
        H8116A_PAR["base"] = tmpbase 
        cmdstr = sprintf("HIL %f V",H8116A_PAR["base"])
        ans = _h8116a_comm(cmdstr)
        printf("Base signal voltage level set to %f Volts\n", H8116A_PAR["base"])
	status = _h8116a_comm("IHIL")
	printf(status)
    }
}'

def h8116a_status '{
    local ret
    ret = _h8116a_get_current_settings()
    if (index(ret,"C0") > 0) {
	printf("In normal mode for positive p-jumps\n")
	status = _h8116a_comm("ILOL")
	printf("Output signal is equal to LOL: ")
        printf(status)
	printf(" \n")
	status = _h8116a_comm("IHIL")
	printf(status)

    } else {
        printf("In complement mode for negative p-jumps\n")
	status = _h8116a_comm("IHIL")
	printf("Output signal is equal to HIL:")
        printf(status)
	printf(" \n")
	status = _h8116a_comm("ILOL")
	printf(status)
    }
}'

def h8116a_enable '{
    ans = _h8116a_comm("D0")
}'


def h8116a_disable '{
    ans = _h8116a_comm("D1")
}'

def h8116a_toggle '{
    local ret
    ret = _h8116a_get_current_settings()
    if (index(ret,"C0") > 0) {
        # Normal output; change to complementary
        ans = _h8116a_comm("ILOL")
        local nf, arr[], newbase
        nf = split(ans,arr)
        newbase = sprintf("%f",arr[1])
        cmdstr = "HIL " newbase " V,LOL " newbase-0.1 " V"
        p cmdstr
        ans = _h8116a_comm(cmdstr)
	ans = _h8116a_comm("C1")
    } else {
        # Complementary output; change to normal
        ans = _h8116a_comm("IHIL")
        local nf, arr[], newbase
        nf = split(ans,arr)
        newbase = sprintf("%f",arr[1])
        cmdstr = "HIL " newbase+0.1 " V,LOL " newbase " V"
        p cmdstr
        ans = _h8116a_comm(cmdstr)
	ans = _h8116a_comm("C0")
    }
}'


def h8116a_pulse '{
    local cmdstr
    local ans

    if ($# == 0) {
        H8116A_PAR["amp"] = 5   # 5 Volts  
        H8116A_PAR["wid"] = 1   # 1 second
    } else if ($# == 1) {
        H8116A_PAR["amp"] = $1
        H8116A_PAR["wid"] = 1   # 1 second
    } else if ($# == 2) {
        H8116A_PAR["amp"] = $1
        if ($2 == 0) {
            printf("Pulse width cannot be zero\n")
            exit
        }
        H8116A_PAR["wid"] = 1/$2
    } else {
        printf("Usage: %s <High voltage level(Volts)> <pulse width(sec)>\n","$0")
        exit
    }

    local ret
    ret = _h8116a_get_current_settings()
    if (H8116A_PAR["amp"] > 0) {
        if (index(ret,"C1") > 0) {
            # Complementary --> toggle to normal output
            h8116a_toggle
        } 
        cmdstr = sprintf("HIL %f V",H8116A_PAR["base"]+H8116A_PAR["amp"])
        ans = _h8116a_comm(cmdstr)     
        printf("Pressure jump amplitude set to %f Volts\n", H8116A_PAR["amp"])
    } else {
        if (index(ret,"C0") > 0) {
            # Normal --> toggle to complementary output
            h8116a_toggle
        } 
        cmdstr = sprintf("LOL %f V",H8116A_PAR["base"]+H8116A_PAR["amp"])
        ans = _h8116a_comm(cmdstr)     
        printf("Pressure jump amplitude set to %f Volts\n", H8116A_PAR["amp"])
    }

    cmdstr = sprintf("FRQ %f HZ",H8116A_PAR["wid"])
    ans = _h8116a_comm(cmdstr)     
    printf("Pulse width set to %f sec\n", 1/H8116A_PAR["wid"])

}'




#%MACROS%
#%IMACROS%
#%DEPENDENCIES%
#%AUTHOR%
#A. Mauro, BCU%BR%
#%BR%
#$Date: 2016/05/15 17:26:48 $%BR%
#$Revision: 1.0 $%BR%
#$State: Exp $
#%BUGS%