esrf

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


#%TITLE% linkam.mac
#%NAME%
#  LINKAM.MAC
#%CATEGORY%
#%DESCRIPTION%
#
#%EXAMPLE%
#%DL%
#%DT%XXXsetup%DD%
#%SETUP%
#%UL%
#%LI%
#%XUL%


global LINKAM_PAR[]

# LINKAM_PAR["device"]
# LINKAM_PAR["on"]
# LINKAM_PAR["temp"]
# LINKAM_PAR["cnt_mne"]
# LINKAM_PAR[""]
# LINKAM_PAR[""]


def linkam_setup '{
    global LINKAM_PAR[]
    local _nb_param

    _nb_param = $#

    if (_nb_param == 1) {
        LINKAM_PAR["device"] = "$1"
    }
    else if(_nb_param > 1 && _nb_param <= 3){
        LINKAM_PAR["device"] = "$1"
        LINKAM_PAR["cnt_mne"]      = "$2"
        LINKAM_PAR["scale_factor"] = "$3"

        _cmd = sprintf( "linkam_gettemp %s \n", LINKAM_PAR["cnt_mne"])
        cdef("user_getcounts", _cmd, LINKAM_PAR["cnt_mne"], 0x02)

        linkam_on
    }
    else {
        print "Usage: $0 <device_name> [<counter_mnemonic> [<scale_factor>] ]"
        exit
    }
    setup_tail("linkam_")

}'


def linkam_unsetup '{
    linkam_off

    if (LINKAM_PAR["cnt_mne"] != 0){
        counter_par(LINKAM_PAR["cnt_mne"], "disable" , 1)
    }
    cdef("user_getcounts", "", LINKAM_PAR["cnt_mne"], "delete")
    unglobal LINKAM_PAR
}'


def linkam_on '{
    LINKAM_PAR["on"] = 1
    counter_par(LINKAM_PAR["cnt_mne"], "disable" , 0)
    printf( "LINKAM : pseudo counter \"%s\" is now ON\n", LINKAM_PAR["cnt_mne"])
}'


def linkam_off '{
    if (LINKAM_PAR["cnt_mne"] != 0){
        LINKAM_PAR["on"] = 0
        S[cnt_num(LINKAM_PAR["cnt_mne"])] = 0
        counter_par(LINKAM_PAR["cnt_mne"], "disable" , 1)
        printf( "LINKAM : pseudo counter \"%s\" is now OFF\n", LINKAM_PAR["cnt_mne"])
    }
}'


def linkam_status '{
    print "LINKAM : STATUS : pseudo counter : ", LINKAM_PAR["on"] ? "ON" : "OFF"
    print "LINKAM : STATUS : Cell :", _linkam_status()
}'


#%IU% <pseudo_counter_mnemonic>
#%MDESC%
#  Reads the Linkam temperature and stores it in the corresponding
#  global variable.
#
def linkam_gettemp '{
    local _sf _mne

    _mne = "$1"
    if(cnt_num(_mne) > -1){
        _sf  = LINKAM_PAR["scale_factor"] ? LINKAM_PAR["scale_factor"]  : 1
        S[cnt_num(_mne)] = _linkam_get_temperature() * _sf
    }
    else{
        print " LINKAM : ERROR : " _mne " does not exists"
    }
}'



######################################################################
#####################                             ####################
#####################  User level linkam control  ####################
#####################                             ####################
######################################################################


#%UU%
#%MDESC% 
#
def linkam_save_status '{
    local my_status
    if( $# != 0 ) {
        printf("usage: %s (prints status to datafile)\n", "$0")
        exit
    }

    my_status = _linkam_status()
    fprintf(DATAFILE,"#C Linkam Temperature is %.2f C\n",  _linkam_get_temperature())

    if(substr(my_status,1,7) == "Holding") {
        fprintf(DATAFILE,"#C Linkam is holding\n")
    }
    else  {
        fprintf(DATAFILE,"#C Linkam is %s to %.2f at %.2f C/s\n",       \
                my_status, linkam_get_setpoint(), _linkam_get_rate())
    }
}'


#%UU% [param = setpoint = target temperature = limit (in degrees C)]
#%MDESC% 
#
def linkam_set_temperature '{
    if( $# != 1) {
        printf("usage: %s setpoint\n", "$0")
        exit
    }

    if($1 < -100 || $1 > 600 ) {
        printf("Temperature %f out of limits\n", $1)
        exit
    }

    _linkam_set_temperature( $1 )

}'


#%UU%
#%MDESC% 
#
def linkam_get_temperature '{
    if( $# != 0) {
        printf("usage: %s\n", "$0")
        exit
    }

    printf("Current Temperature is %f C\n", _linkam_get_temperature())
}'


#%UU% [param = ramp rate in degrees per minute]
#%MDESC% 
#
def linkam_set_rate '{
    if( $# != 1) {
        printf("usage: %s <rate>\n", "$0")
        exit
    }

    _linkam_set_rate( $1 )
}'


#%UU%
#%MDESC% 
#
def linkam_get_rate '{
    if( $# != 0) {
        printf("usage: %s\n", "$0")
        exit
    }

    printf("Current Rate is %f C/min\n", _linkam_get_rate())
}'


#%UU%
#%MDESC% 
#
def linkam_get_status '{
    if( $# != 0) {
        printf("usage: %s\n", "$0")
        exit
    }

    printf("Status is \'%s\' \n", _linkam_status())
}'


#%UU%
#%MDESC% 
#
def linkam_get_setpoint '{
    if( $# != 0) {
        printf("usage: %s\n", "$0")
        exit
    }

    printf("Setpoint is \'%s\' \n", _linkam_get_setpoint())
}'


#%UU% [1st param = setpoint, 2nd if given = waiting time in sec to reach it]
#%MDESC% 
#
def linkam_stabilise '{
    # go to a temperature (= limit) and assure to be holding at limit
    # by reading out status and temperature.

    if( $# < 1) {
        printf("usage: %s setpoint <max waiting time in seconds>\n", "$0")
        exit
    }

    if($1 < -100 || $1 > 600 ) {
        printf("Temperature %f out of limits\n", $1)
        exit
    }

    if ($# == 1) {
	wait_time = 300   # 5 minutes
	printf("Wait time not given; 5 minutes set\n")
    } else {
	wait_time = $2
    }

    _linkam_set_temperature ( $1 )

    for(tries=0;tries < wait_time; tries++){
        my_status=_linkam_status()
        printf("Temperature is %6.1f, Linkam is %s \r",  _linkam_get_temperature(), my_status)
        if(substr(my_status,1,7) == "Holding") {
            printf("\n")
            break
        }
        sleep(1)
    }
}'



#%UU% [param = waiting time in a loop]
#%MDESC% 
#
def linkam_watch '{
    while(1){
        my_status=_linkam_status()
        printf("%s: Temperature is %6.1fC, Linkam is %s ",              \
               date(), _linkam_get_temperature(), _linkam_status())
        if(substr(my_status,1,7) != "Holding") {
            printf("at %4.1f C/min\n", _linkam_get_rate())
        } else {
            printf("    \r")
        }
        if($# > 0 && $1 > 0 ){
           sleep($1)
        }
        else {
            sleep(1)
        }
    }
}'


#%UU% 
#%MDESC% Checking if linkam is in Holding state
#
def linkam_isholding() '{
    local my_status
    my_status=_linkam_status()
    if(substr(my_status,1,7) != "Holding") {
        return 0
    } else {
        return 1
    }
}'


#%UU% [1st param = high T, 2nd param = low T] 
#%MDESC% 
#
def linkam_pulse '{
    local _nb_param

    _nb_param = $#

    if(_nb_param != 2){
        printf("usage: %s T_hot T_cool\n", "$0")
        exit
    }

    linkam_set_rate 200
    linkam_stabilise $1
    linkam_stabilise $2
}'


#%UU% [param]
#%MDESC% 
#
def linkam_cool '{

    linkam_set_rate 200
    linkam_set_temperature 25
    linkam_watch
}'

#%UU%
#%MDESC%
#
def cool '{
    # to be typed rapidly
    linkam_cool
}'

#%UU%
#%MDESC%
#
def linkam_info '{
    print "-----------===== linkam infos =====---------"
    linkam_get_temperature
    linkam_get_status
    linkam_get_rate
    print "--------------------------------------------"
}'


#%UU%
#%MDESC%
#
def linkam_pumpauto '{
    print "-----------===== linkam pump set to auto mode =====---------"
    _linkam_set_pump_auto
}'


#%UU%
#%MDESC%
#
#def linkam_pumpmanual '{
#    print "-----------===== linkam pump set to manual mode =====---------"
#    print "-----------===== Please use linkam_set_pump_speed 0...30 =====---------"
#    _linkam_set_pump_manual
#}'


#%UU% [param = pump speed (0 --> 30), 0 = pump off]
#%MDESC%
#
#def linkam_set_pump_speed '{
#    local _nb_param
#    print "-----------===== linkam set pump speed for manual mode =====--------"
#
#    _nb_param = $#
#
#    if(_nb_param != 1){
#        printf("usage: %s speed [0-30]\n", "$0")
#        exit
#    }
#    if (($1 < 0) || ($1 > 30)) {
#        printf("%s: Pump speed %d is out of range [0-30]\n", "$0", $1)
#        exit
#    }
#
#    print "-----------===== linkam pump set to manual mode =====---------"
#    _linkam_set_pump_manual
#
#    printf("-----------===== linkam set pump speed to %d =====---------\n",$1)
#    _linkam_set_pump_speed $1
#}'

def linkam_stop '{
    # Important: do not apply next 2, in order to keep pump in automtic mode
    #_linkam_set_pump_manual
    #_linkam_set_pump_speed 0 
    _linkam_stop
}'

def linkam_hold '{
    _linkam_hold
}'


######################################################################
#####################                            #####################
#####################  Low level linkam control  #####################
#####################                            #####################
######################################################################


def _linkam_set_temperature( linkam_setpoint ) '{
    esrf_io(LINKAM_PAR["device"],"DevLinkamSetLimit",linkam_setpoint)
    # Need a sleep of 80ms according Linkam support
    sleep(0.1)
    esrf_io(LINKAM_PAR["device"],"DevLinkamStart")
}'

def _linkam_get_setpoint() '{
    return(esrf_io(LINKAM_PAR["device"],"DevLinkamGetLimit"))
}'

def _linkam_get_temperature() '{
    return(esrf_io(LINKAM_PAR["device"],"DevLinkamGetTemperature"))
}'

def _linkam_set_rate( linkam_rate ) '{
    esrf_io(LINKAM_PAR["device"],"DevLinkamSetRate",linkam_rate)
}'

def _linkam_get_rate() '{
    return(esrf_io(LINKAM_PAR["device"],"DevLinkamGetRate"))
}'

def linkam_dsc_clear '{
    esrf_io(LINKAM_PAR["device"],"DevLinkamDscClearBuffer")
    #printf("Linkam DSC buffer cleared\n")
}'

def linkam_get_dsc_temperature() '{
    local _dsc _old_temp _old_dsc _status

    _dsc[0] = -1
    _dsc[1] = -1

    linkam_dsc_clear

    _status = esrf_io(LINKAM_PAR["device"],"DevLinkamGetDscTemperature",_dsc)
    _dsc[1] = _dsc[1] & 0xffff

    while (_dsc[1] == 32767) {
        _status = esrf_io(LINKAM_PAR["device"],"DevLinkamGetDscTemperature",_dsc)
    }

    #printf("%04x %d\n",_dsc[1] , _dsc[1] & 0xffff)
    LINKAM_PAR["temp"] = _dsc[0]
    LINKAM_PAR["dsc"]  = _dsc[1]
    return
}'

def _linkam_status() '{
    local linkam_string
    linkam_string = esrf_io(LINKAM_PAR["device"],"DevStatus")
    # taking away \n in the end
    return(substr(linkam_string,1,length(linkam_string)-1))
}'


def _linkam_set_pump_auto '{
    ret = esrf_io(LINKAM_PAR["device"],"DevLinkamPumpAutomatic")
    if (ret != 0) {
        printf("Error setting linkam pump to automatic mode\n")
    }
}'

def _linkam_set_pump_manual '{
    ret = esrf_io(LINKAM_PAR["device"],"DevLinkamPumpManual")
    if (ret != 0) {
        printf("Error setting linkam pump to manual mode\n")
    }
}'

def _linkam_set_pump_speed '{
    ret = esrf_io(LINKAM_PAR["device"],"DevLinkamSetPumpSpeed",$1)
    if (ret != 0) {
        printf("Error setting linkam pump speed to %d for manual mode\n", $1)
    }
}'

def _linkam_stop '{
    ret = esrf_io(LINKAM_PAR["device"],"DevLinkamStop")
    if (ret != 0) {
        printf("Error stopping linkam\n")
    }
}'

def _linkam_hold '{
    ret = esrf_io(LINKAM_PAR["device"],"DevLinkamHold")
    if (ret != 0) {
        printf("Error setting linkam to hold state\n")
    }
}'

def _linkam_getmodel() '{
    local model_string
    model_string = esrf_io(LINKAM_PAR["device"],"DevLinkamGetModel")
    return(model_string)
}'