esrf

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

#=============================================================================
#    "$Source: /segfs/bliss/source/spec/macros/RCS/flowbus.mac,v $";
#    "$Revision: 1.4 $";    $Date: 2018/10/30 13:08:53 $";
#=============================================================================

#=============================================================================
# COUNTER CONFIGURATION
# <macrocounter channel>  = (10 * <function>) + <physical channel (0 .. 9)>
#                                    fn=0 -> mfc Current
#                                    fn=1 -> FluidTemperature
#                                    fn=2 -> Orifice
#                                    fn=3 -> PressureInlet
#                                    fn=4 -> PressureOutlet
#                                    fn=5 -> ValveOutput
#                                    fn=6 -> mfc Fmeasure
#=============================================================================

#=============================================================================
#$Log: flowbus.mac,v $
#Revision 1.4  2018/10/30 13:08:53  homsrego
#added write FSetpoint -> FSetpoing (206)
#cleaning and cosmetics
#
#Revision 1.3  2018/10/01 10:49:36  homsrego
#added function 6 - Fmeasure
#
#=============================================================================

#=============================================================================
# 2013/06/09 rh
#   ver 1.4 surfx
#   ver 1.6 mallorca
#   ID03flowbus -> flowbus_id03.mac
# 2018/10/01 rh
#   added the function 6 -> Fmeasure (205)
# 2018/10/30 rh
#   added write FSetpoint -> FSetpoing (206)
#   cleaning and cosmetics
#=============================================================================


#%IU%
#%MDESC%
# Flowbus macro motor config
#
def flowbus_config(mne, type, p1, p2, p3) '{
    if (type=="ctrl") {
        global FLOWBUS_MOT[] FLOWBUS_MOT_PAR[]
        local imot factor
        FLOWBUS_MOT["dev"]= flowbus_ADDR
        FLOWBUS_MOT["NbNodes"]= tango_get(FLOWBUS_MOT["dev"], "NbNodes")
        if (FLOWBUS_MOT["NbNodes"] < 0) {
            printf("Flowbus Motor device %s : NO connection !!\n", FLOWBUS_MOT["dev"])
            return ".error."
        }
        else if (FLOWBUS_MOT["NbNodes"] == 0) {
            printf("Flowbus Motor device %s : NO nodes defined !!\n", FLOWBUS_MOT["dev"])
            return ".error."
        }
        else {
            printf("Using Flowbus Motor device %s [%d]\n", FLOWBUS_MOT["dev"], FLOWBUS_MOT["NbNodes"])
            for (imot= 0; imot<FLOWBUS_MOT["NbNodes"]; imot++)
                FLOWBUS_MOT[imot]= -1
        }
    }

    if (type=="mot") {
        local chan fsp_mode
        chan= p3
        if (chan >= FLOWBUS_MOT["NbNodes"]) {
            print "Flowbus Motor ERROR [" motor_mne(mne) "] >> channel should be <", FLOWBUS_MOT["NbNodes"]
            return ".error."
        }
        FLOWBUS_MOT[motor_mne(mne)]= chan
        FLOWBUS_MOT[chan]= motor_mne(mne)
        # motor can have a scale factor to change from % to mBar, 0.08333333 converts from 0-100% to 0-1200mBar
        factor = motor_par(motor_mne(mne),"factor")
        if (factor == 0) factor=1
        FLOWBUS_MOT_PAR[motor_mne(mne)]["factor"] = factor
        fsp_mode = motor_par(motor_mne(mne),"fsp_mode")
        FLOWBUS_MOT_PAR[motor_mne(mne)]["fsp_mode"] = !!fsp_mode
    }
}'

#%IU%
#%MDESC%
# Flowbus macro motor command
#
def flowbus_cmd(mne, key, p1, p2) '{
    local fnId ; fnId = "flowbus_cmd"
    local fsp_mode _mne _dev factor ch
    local float array fbargs[2] 

    #printf("%s> mne[%s] key[%s] p1[%s] p2[%s]\n", fnId, mne, key, p1, p2)

  
    if (key=="start_one") {
        _dev = motor_par(mne,"address")
        ch = motor_par(mne,"channel")
        fbargs[0]= ch
        fsp_mode = motor_par(mne,"fsp_mode")
        factor =   motor_par(mne,"factor")
        if(factor == 0) factor = 1.0


        if(fsp_mode) {
	        fbargs[1]= p1
        	tango_io(_dev, "WriteFSetPoint", fbargs)
        } else {
	        fbargs[1]= p1 * factor
        	tango_io(_dev, "WriteSetPoint", fbargs)
        }    

    }
    if (key=="preread_all") {
        FLOWBUS_MOT["doread"]= 1
    }
    if (key=="preread_one") {
        _flowbus_readsp()
    }
    if (key=="position") {
        ch = motor_par(mne,"channel")
        fsp_mode = motor_par(mne,"fsp_mode")
        factor =   motor_par(mne,"factor")
        if(factor == 0) factor = 1.0

        if (FLOWBUS_MOT["doread"]) {
            _flowbus_readsp()
            FLOWBUS_MOT["doread"]= 0
        }

        if(fsp_mode) {
            return FLOWBUS_FPOS[ch]
        } else {
            return FLOWBUS_POS[ch]/factor
        }    


        
    }
}'

#%IU%
#%MDESC%
# Flowbus macro counter config
#
def flowbuscnt_config(mne, type, p1, p2, p3) '{
    if (type=="ctrl") {
        global FLOWBUS_CNT[]
        local icnt

        FLOWBUS_CNT["dev"]= flowbuscnt_ADDR
        FLOWBUS_CNT["NbNodes"]= tango_get(FLOWBUS_CNT["dev"], "NbNodes")
        if (FLOWBUS_CNT["NbNodes"] < 0) {
            printf("Flowbus Counter device %s : NO connection !!\n", FLOWBUS_CNT["dev"])
            return ".error."
        }
        else if (FLOWBUS_CNT["NbNodes"] == 0) {
            printf("Flowbus Counter device %s : NO nodes defined !!\n", FLOWBUS_CNT["dev"])
            return ".error."
        }
        else {
            printf("Using Flowbus Counter device %s [%d]\n", FLOWBUS_CNT["dev"], FLOWBUS_CNT["NbNodes"])
            #for (icnt= 0; icnt<FLOWBUS_CNT["NbNodes"]; icnt++)
            #    FLOWBUS_CNT[icnt]= -1
        }
    }

    if (type=="cnt") {
        local chan
        fn= int(p3 / 10)
        chan= p3 %  10
        if (chan >= FLOWBUS_CNT["NbNodes"]) {
            print "Flowbus Counter ERROR [" cnt_mne(mne) "] >> channel should be <", FLOWBUS_CNT["NbNodes"]
            return ".error."
        }
        FLOWBUS_CNT[cnt_mne(mne)]["chan"]= chan
        FLOWBUS_CNT[cnt_mne(mne)]["fn"]= fn
        #FLOWBUS_CNT[chan]= cnt_mne(mne)
        #FLOWBUS_CNT[chan]["fn"]= fn
    }
}'

#%IU%
#%MDESC%
# Flowbus macro counter command
#
def flowbuscnt_cmd(mne, cmd, p1, p2, p3) '{
    local _mne, chan, fn, val
    if (cmd=="get_status") {
        return (0)
    }
    else if (cmd=="prestart_all") {
        FLOWBUS_CNT["doread"]= 1
    }
    else if (cmd=="counts") {
        if (FLOWBUS_CNT["doread"]) {
            _flowbus_readval()
            FLOWBUS_CNT["doread"]= 0
        }
    
        _mne = cnt_mne(mne)
        chan = FLOWBUS_CNT[_mne]["chan"]
        fn = FLOWBUS_CNT[_mne]["fn"]
    
        val = FLOWBUS_VAL2D[chan][fn]
        # printf("mne=%s chan=%d fn=%d V=%f\n", _mne, chan, fn, val)
        return val
    }
}'

#%IU%
#%MDESC%
# Read setpoints
#
def _flowbus_readsp() '{
    if (FLOWBUS_MOT["NbNodes"]>0) {
        float array FLOWBUS_POS[FLOWBUS_MOT["NbNodes"]]
        float array FLOWBUS_FPOS[FLOWBUS_MOT["NbNodes"]]
        tango_get(FLOWBUS_MOT["dev"], "SetPoint", FLOWBUS_POS)
        tango_get(FLOWBUS_MOT["dev"], "FSetPoint", FLOWBUS_FPOS)
    }
}'

#%IU%
#%MDESC%
# Read current values
#
def _flowbus_readval() '{
    local ndim, i, fn
    ndim = FLOWBUS_CNT["NbNodes"]    
    if (ndim>0) {

        _flowbuscnt_check_fn
        
        float array FLOWBUS_VALX[ndim]
        float array FLOWBUS_VAL2D[ndim][7]

        fn = 0 # mfc Current
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "Current", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

        fn = 6  # mfc Fmeasure
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "Fmeasure", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

        fn = 5  # 5-> ValveOutput
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "ValveOutput", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

        fn = 1  # FluidTemperature"
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "FluidTemperature", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

        fn = 2  # Orifice
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "Orifice", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

        fn = 3  # PressureInlet
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "PressureInlet", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

        fn = 4  # PressureOutlet
        if(FLOWBUS_CNT["fnUsed"][fn]){        
            tango_get(FLOWBUS_CNT["dev"], "PressureOutlet", FLOWBUS_VALX)
            for(i = 0; i < ndim; i++) {FLOWBUS_VAL2D[i][fn] = FLOWBUS_VALX[i]} 
        }

    }
}'

#%UU%
#%MDESC%
# Print report on all setpoint and current values
#
def flowbus_show '{
    local idx ichan mout[] imax cname cfn

    local imot done

    done = 0

    print "CHAN    MOTOR SETPOINT"
    for(imot=1; imot < MOTORS; imot ++){
        if(motor_par(imot,"device_id") == "flowbus"){
            done = 1
            cname= motor_mne(imot)
            ichan= motor_par(imot, "channel")
            printf(" #%2d %8.8s %8.2g\n", ichan, \
                            cname, FLOWBUS_POS[ichan])
        }
    }



    local icnt
    _flowbus_readval()
    print "CHAN  FN  COUNTER      VAL"
    for(icnt=1; icnt < COUNTERS; icnt ++){
        if(counter_par(icnt,"device_id") == "flowbuscnt"){
            done = 1
            cname= cnt_mne(icnt)
            cfn= FLOWBUS_CNT[cname]["fn"]
            ichan= FLOWBUS_CNT[cname]["chan"]
            printf(" #%2d %3d %8.8s %8.2g\n", ichan, cfn, cname, FLOWBUS_VAL2D[ichan][cfn])
        }
    }

    if (done == 0) {
        print "No Flowbus Counter/Motor configured !!"
    }
}'

#%IU%
#%MDESC%
# counters / is function used?
#
def _flowbuscnt_check_fn '{
    local _i, _imax icnt cfn cname
    _imax = FLOWBUS_CNT["NbNodes"]

    for(_i = 0; _i<_imax; _i++) {
        FLOWBUS_CNT["fnUsed"][_i] = 0
    }

    for(icnt=1; icnt < COUNTERS; icnt ++){
        if(counter_par(icnt,"device_id") == "flowbuscnt"){
            cname= cnt_mne(icnt)
            cfn= FLOWBUS_CNT[cname]["fn"]
            FLOWBUS_CNT["fnUsed"][cfn] = 1
        }
    }

}'