Skip to content
Snippets Groups Projects
Configuration.py 9.1 KiB
Newer Older
BRONES Romain's avatar
BRONES Romain committed
###################################################################################################
#    CONFIGURATION FUNCTIONS
###################################################################################################
#
# Contains functions to configure blocks of the FOFB application.
#
###################################################################################################

import tango
import logging
import numpy as np
BRONES Romain's avatar
BRONES Romain committed
import FofbTool.Utils


# Get the module logger
logger = logging.getLogger("FofbTool")

def trycatch_tango_devfailed(func):
    def inner_function(*args, **kwargs):
        try:
BRONES Romain's avatar
BRONES Romain committed
            return func(*args, **kwargs)
        except tango.DevFailed as e:
            logger.error("DevFailed met during configuration")
            logger.debug(e)
            return False
    return inner_function

###################################################################################################
BRONES Romain's avatar
BRONES Romain committed
#    CONFIGURE COM BPM
###################################################################################################

@trycatch_tango_devfailed
BRONES Romain's avatar
BRONES Romain committed
def cellnode_configure_combpm(node_tangopath, bpmallowed):
BRONES Romain's avatar
BRONES Romain committed
    """
    Configure the combpm block of a CellNode.
BRONES Romain's avatar
BRONES Romain committed
    The BPM data allowed through are taken in the argument.
BRONES Romain's avatar
BRONES Romain committed

    PARAMETERS
    ----------
BRONES Romain's avatar
BRONES Romain committed
    node_tangopath: str
        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
BRONES Romain's avatar
BRONES Romain committed
    bpmallowed: [int], None
BRONES Romain's avatar
BRONES Romain committed
        List of BPMID allowed through the block.
BRONES Romain's avatar
BRONES Romain committed

    RETURN
    ------
    success: boolean
        True if configuration is a success
    """

    # Get device proxy
    try:
BRONES Romain's avatar
BRONES Romain committed
        prx=tango.DeviceProxy(node_tangopath)
        prx.ping()
    except tango.DevFailed:
        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
        return None
BRONES Romain's avatar
BRONES Romain committed

BRONES Romain's avatar
BRONES Romain committed
    logger.debug("Activate BPMs {} in the filter for {}".format(bpmallowed, node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    f = prx["combpm_filter_table"].value
    f[:] = 0
    f[np.array(bpmallowed)] = 0x80
    prx["combpm_filter_table"] = f
BRONES Romain's avatar
BRONES Romain committed
    logger.info("Configuration of ComBpm done on {}.".format(node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    return True
###################################################################################################
#    CONFIGURE COM LBP
###################################################################################################

@trycatch_tango_devfailed
BRONES Romain's avatar
BRONES Romain committed
def cellnode_configure_comlbp(node_tangopath, seqoffset=0):
    """
    Configure the comlbp block of a CellNode.
    For now, nothing done

    PARAMETERS
    ----------
BRONES Romain's avatar
BRONES Romain committed
    node_tangopath: str
        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
BRONES Romain's avatar
BRONES Romain committed
    seqoffset: int
        Sequence offset to configure

    RETURN
    ------
    success: boolean
        True if configuration is a success
    """

BRONES Romain's avatar
BRONES Romain committed
    # Get device proxy
    try:
BRONES Romain's avatar
BRONES Romain committed
        prx=tango.DeviceProxy(node_tangopath)
        prx.ping()
    except tango.DevFailed:
        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
        return None
BRONES Romain's avatar
BRONES Romain committed

BRONES Romain's avatar
BRONES Romain committed
    logger.debug("Program sequence offset {} in {}".format(seqoffset, node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    for n in range(4):
        prx["comlbp{}_seqoffset".format(n)]=seqoffset

BRONES Romain's avatar
BRONES Romain committed
    logger.info("Configuration of ComLBP done on {}.".format(node_tangopath))

###################################################################################################
BRONES Romain's avatar
BRONES Romain committed
#    CONFIGURE CCN: COM CELLNODES
###################################################################################################


@trycatch_tango_devfailed
BRONES Romain's avatar
BRONES Romain committed
def cellnode_configure_ccn(node_tangopath, nbpm, npsc):
BRONES Romain's avatar
BRONES Romain committed
    Configure the ComCellNode block on a cellnode.
    Automatically set the number of bpm/psc packets and MAC length.

    PARAMETERS
    ----------
BRONES Romain's avatar
BRONES Romain committed
    node_tangopath: str
        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
    nbpm:
        Number of BPM allowed by the filter, hence the number of expected BPM packets.
    npsc:
        Number of total PSC, hence the number of expected PSC packets.

BRONES Romain's avatar
BRONES Romain committed
    RETURN
    ------
    success: boolean
        True if configuration is a success
BRONES Romain's avatar
BRONES Romain committed
    # Get device proxy
    try:
BRONES Romain's avatar
BRONES Romain committed
        prx=tango.DeviceProxy(node_tangopath)
        prx.ping()
    except tango.DevFailed:
        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
        return None
BRONES Romain's avatar
BRONES Romain committed

BRONES Romain's avatar
BRONES Romain committed
    logger.debug("{} bpm allowed in the ethernet frame on {}".format(nbpm, node_tangopath))
    logger.debug("{} psc expected in the ethernet frame on {}".format(nbpm, node_tangopath))

    maclen = 10*nbpm+10
    logger.debug("Configure packeter framesize (mac length) to {}".format(maclen))
BRONES Romain's avatar
BRONES Romain committed
    prx["ccnpack0_framesize"] = maclen

    logger.debug("Configure packeter npackets to {}".format(nbpm-1))
BRONES Romain's avatar
BRONES Romain committed
    prx["ccnpack0_npackets"] = nbpm-1

    maclen = npsc*6+10
    logger.debug("Configure unpacketer framesize (mac length) to {}".format(maclen))
BRONES Romain's avatar
BRONES Romain committed
    prx["ccnunpack0_framesize"] = maclen
BRONES Romain's avatar
BRONES Romain committed
    logger.info("Configuration of CCN done on {}.".format(node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    return True
@trycatch_tango_devfailed
BRONES Romain's avatar
BRONES Romain committed
def centralnode_configure_ccn(node_tangopath, nbpm, npsc):
BRONES Romain's avatar
BRONES Romain committed
    Configure the ComCellNode block on the centralnode.
    Automatically set the number of bpm/psc packets and MAC length.

    PARAMETERS
    ----------
BRONES Romain's avatar
BRONES Romain committed
    node_tangopath: str
        The target fofbnode tango path, ie 'ans/dg/fofb-centralnode'
    nbpm: list(int)
        Number of BPM packet received on each interface.
    npsc:
        Number of total PSC, hence the number of expected PSC packets.

BRONES Romain's avatar
BRONES Romain committed
    RETURN
    ------
    success: boolean
        True if configuration is a success
BRONES Romain's avatar
BRONES Romain committed
    # Get device proxy
BRONES Romain's avatar
BRONES Romain committed
    try:
BRONES Romain's avatar
BRONES Romain committed
        prx=tango.DeviceProxy(node_tangopath)
        prx.ping()
    except tango.DevFailed:
        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
        return None
    for n in range(len(nbpm)):
        maclen = npsc*6+10
        logger.debug("Configure packeter {} framesize (mac length) to {}".format(n, maclen))
BRONES Romain's avatar
BRONES Romain committed
        prx["ccnpack{}_framesize".format(n)] = maclen

        logger.debug("Configure packeter {}  npackets to {}".format(n, npsc-1))
BRONES Romain's avatar
BRONES Romain committed
        prx["ccnpack{}_npackets".format(n)] = npsc-1

        maclen = 10*nbpm[n]+10
        logger.debug("Configure unpacketer {} framesize (mac length) to {}".format(n, maclen))
BRONES Romain's avatar
BRONES Romain committed
        prx["ccnunpack{}_framesize".format(n)] = maclen
BRONES Romain's avatar
BRONES Romain committed
    logger.info("Configuration of CCN done on {}.".format(node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    return True
BRONES Romain's avatar
BRONES Romain committed
###################################################################################################
#    CONFIGURE COM CORR
###################################################################################################
@trycatch_tango_devfailed
BRONES Romain's avatar
BRONES Romain committed
def cellnode_configure_comcorr(node_tangopath, pscid, enable):
BRONES Romain's avatar
BRONES Romain committed
    """
    Configure the comcorr block of a CellNode.
    The PSC ID either taken in the argument, or default to nominal values.
BRONES Romain's avatar
BRONES Romain committed
    PARAMETERS
    ----------
BRONES Romain's avatar
BRONES Romain committed
    node_tangopath: str
        The target fofbnode tango path, ie 'ans/dg/fofb-cellnode-c09'
BRONES Romain's avatar
BRONES Romain committed
    pscid: [int], None
        List of 32 PSCID to program on the output board. If None, default to nominal values.

    enable: boolean
        Enable or not the hardware outputs.

    RETURN
    ------
    success: boolean
        True if configuration is a success
    """
    # Get device proxy
    try:
BRONES Romain's avatar
BRONES Romain committed
        prx=tango.DeviceProxy(node_tangopath)
        prx.ping()
    except tango.DevFailed:
        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
        return None
BRONES Romain's avatar
BRONES Romain committed

BRONES Romain's avatar
BRONES Romain committed
    logger.debug("Set PSCIDs {} in the filter for {}".format(pscid, node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    f= prx["comcorr_line_id"].value
    f[:] = pscid
    f[:] = f+ 0x10000*bool(enable)
BRONES Romain's avatar
BRONES Romain committed
    logger.debug("Set comcorr_line_id {} for {}".format(f, node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    prx["comcorr_line_id"] = f

BRONES Romain's avatar
BRONES Romain committed
    logger.info("Configuration of ComCorr done on {}.".format(node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    return True
BRONES Romain's avatar
BRONES Romain committed
###################################################################################################
#    CONFIGURE MATRIX CORRECTOR
###################################################################################################
@trycatch_tango_devfailed
BRONES Romain's avatar
BRONES Romain committed
def centralnode_configure_corr(node_tangopath, numbpm, pscid, k1x, k1y, k2x, k2y):
BRONES Romain's avatar
BRONES Romain committed
    """
    Configure the correction algorithm on the centralnode.
BRONES Romain's avatar
BRONES Romain committed

    PARAMETERS
    ----------
    node_tangopath: str
        The target fofbnode tango path, ie 'ans/dg/fofb-centralnode'
    k1x, k1y, k2x, k2y: list(int)
        List of coefficient for each filter. 4 coefficients : ABCD
BRONES Romain's avatar
BRONES Romain committed
    """
BRONES Romain's avatar
BRONES Romain committed

    # Get device proxy
BRONES Romain's avatar
BRONES Romain committed
    try:
BRONES Romain's avatar
BRONES Romain committed
        prx=tango.DeviceProxy(node_tangopath)
        prx.ping()
    except tango.DevFailed:
        logger.error("Failed to obtain tango proxy or to ping to {}".format(node_tangopath))
        return None

    for i,l in enumerate(["a","b","ic","d"]):
        prx["corr_k1{}_x".format(l)]=k1x[i]
        prx["corr_k2{}_x".format(l)]=k2x[i]
        prx["corr_k1{}_y".format(l)]=k1y[i]
        prx["corr_k2{}_y".format(l)]=k2y[i]
BRONES Romain's avatar
BRONES Romain committed

    f= prx["corr_pscid"].value
BRONES Romain's avatar
BRONES Romain committed
    f[0:100] = pscid
BRONES Romain's avatar
BRONES Romain committed
    prx["corr_pscid"] = f
BRONES Romain's avatar
BRONES Romain committed
    prx["corr_num_bpm"] = numbpm
BRONES Romain's avatar
BRONES Romain committed
    logger.info("Configuration of Corr done on {}.".format(node_tangopath))
BRONES Romain's avatar
BRONES Romain committed
    return True