コード例 #1
0
def getData(projectPath, outputNames, simulationTime):
    """
    Function to exchange data with a running model.

    :param projectPath: Path to the project file.
    :param outputNames: Output signal names of the  model.
    :param simulationTime: Model simulation time.

    """

    # Wait prior to getting the outputs
    sleep(TIMEOUT)

    ## Connect to a running model using its name.
    projectName = os.path.abspath(projectPath)
    #log.info("=====Path to the project={!s}".format(projectName))

    start = datetime.now()
    RtlabApi.OpenProject(projectName)
    if (simulationTime < RtlabApi.GetStopTime()
            or RtlabApi.GetStopTime() <= 0.0):

        #log.info ("=====The connection with {!s} is completed.".format(projectName))
        try:
            ## Get the model state and the real simulationTime mode
            modelState, realTimeMode = RtlabApi.GetModelState()
            ## Print the model state
            log.info("=====getData(): The model state is {!s}.".format(
                RtlabApi.OP_MODEL_STATE(modelState)))

            ## If the model is running
            if modelState == RtlabApi.MODEL_RUNNING:
                ## Exchange data
                try:
                    ctlr = True
                    RtlabApi.GetSystemControl(ctlr)
                    #log.info ("=====The system control is acquired.")
                    RtlabApi.Pause()
                    #log.info ("=====The model is paused.")
                    s = "=====getData(): outputs to be get are={!s}".format(
                        outputNames)
                    log.info(s)
                    #RtlabApi.GetSignalControl(True)
                    outputValues = RtlabApi.GetSignalsByName(outputNames)
                    timeFactor = 1
                    RtlabApi.Execute(timeFactor)
                    log.info("=====getData(): The model is executed.")
                    ## Release signal control after changing values
                    ctlr = False
                    RtlabApi.GetSystemControl(ctlr)
                    #log.info ("=====The system control is released.")
                except Exception:
                    ## Ignore error 11 which is raised when
                    ## RtlabApi.DisplayInformation is called whereas there is no
                    ## pending message
                    info = sys.exc_info()
                    if info[1][
                            0] != 11:  # 'There is currently no data waiting.'
                        ## If a exception occur: stop waiting
                        log.error ("=====getData(): An error occured at simulationTime={!s} while getting the " \
                               "output values for the output names={!s}.".format(simulationTime, outputNames))
                        raise

            ## if the model is not running
            else:
                ## Print the model state
                log.error(
                    "=====getData(): The model is not running. Simulation will be terminated."
                )
                raise

            end = datetime.now()
            log.info(
                '=====getData(): Get values={!s} of outputs with names={!s} in {!s} seconds.'
                .format(outputValues, outputNames,
                        (end - start).total_seconds()))
        finally:
            ## Always disconnect from the model when the connection
            ## is completed
            RtlabApi.Disconnect()
        return outputValues
    else:
        ctlr = True
        #RtlabApi.GetSystemControl (ctlr)
        #RtlabApi.Reset()
        ctlr = False
        #RtlabApi.GetSystemControl (ctlr)
        RtlabApi.Disconnect()
        log.info ("=====getData(): The simulation stoptime={!s} is reached. "\
               "The model is reset and the connection is closed.".format(RtlabApi.GetStopTime()))
        return zeroOutputValues(outputNames)
コード例 #2
0
def setData(projectPath, inputNames, inputValues, simulationTime):
    """
    Function to exchange data with a running model.

    :param projectPath: Path to the project file.
    :param inputNames: Input signal names of the  model.
    :param inputValues: Input signal values of the  model.
    :param simulationTime: Model simulation time.

    """

    # Wait prior to setting the inputs
    sleep(TIMEOUT)
    projectName = os.path.abspath(projectPath)
    #log.info("=====Path to the project={!s}".format(projectName))

    start = datetime.now()
    RtlabApi.OpenProject(projectName)
    if (simulationTime < RtlabApi.GetStopTime()
            or RtlabApi.GetStopTime() <= 0.0):
        #log.info ("=====The connection with {!s} is completed.".format(projectName))
        try:
            ## Get the model state and the real simulationTime mode
            modelState, realTimeMode = RtlabApi.GetModelState()
            ## If the model is running
            if modelState == RtlabApi.MODEL_RUNNING:
                ## Set input data
                ########Setting inputs of the model
                try:
                    #signalNames = (signalName1, signalName2, ...)
                    #signalValues = (value1, value2, ...)
                    #RtlabApi.SetSignalsByName(signalNames, signalValues)
                    ## Get signal control before changing values
                    ctlr = True
                    RtlabApi.GetSystemControl(ctlr)
                    #log.info ("=====The system control is acquired.")
                    RtlabApi.GetSignalControl(ctlr)
                    #log.info ("=====The signal control is acquired.")
                    RtlabApi.Pause()
                    #log.info ("=====The model is paused.")
                    RtlabApi.SetSignalsByName(inputNames, inputValues)
                    #log.info ("=====The signals are set.")
                    timeFactor = 1
                    RtlabApi.Execute(timeFactor)
                    #log.info ("=====The model is executed.")
                    ## Release signal control after changing values
                    ctlr = False
                    RtlabApi.GetSignalControl(ctlr)
                    #log.info ("=====The signal control is released.")
                    RtlabApi.GetSystemControl(ctlr)
                    #log.info ("=====The system control is released.")
                except Exception:
                    ## Ignore error 11 which is raised when
                    ## RtlabApi.DisplayInformation is called whereas there is no
                    ## pending message
                    info = sys.exc_info()
                    if info[1][
                            0] != 11:  # 'There is currently no data waiting.'
                        ## If a exception occur: stop waiting
                        log.error ("=====setData(): An error occured at simulationTime={!s} while setting the " \
                               "input values for the input names={!s}.".format(simulationTime, inputNames))
                        raise
            ## if the model is not running
            else:
                ## Print the model state
                log.error(
                    "=====setData(): The model state is not running. Simulation will be terminated."
                )
                raise
            end = datetime.now()
            log.info(
                '==========setData(): Send values={!s} of inputs with names={!s} in {!s} seconds.'
                .format(inputValues, inputNames,
                        (end - start).total_seconds()))
        finally:
            ## Always disconnect from the model when the connection
            RtlabApi.Disconnect()
    else:
        RtlabApi.Disconnect()
        log.info ("=====setData(): The simulation stoptime={!s} is reached. "\
               " the connection is closed.".format(RtlabApi.GetStopTime()))