Search code examples
matlabsimulinks-function

Same S-Function used twice, one works one doesn't


I have a simulink model and I made a simple s-function block to add to my model and it works. The catch if if I copy or use that same s-function twice in my model, when I run it only the last s-function called will do what it's supposed to do, the other will output a 0.

#define S_FUNCTION_NAME DivByZero 
#define S_FUNCTION_LEVEL 2
#define NUMBER_OF_INPUTS 1
#define NUMBER_OF_OUTPUTS 1

#include "DivByZero.h"
#include "mex.h"

#if !defined(MATLAB_MEX_FILE)
/*
 * This file cannot be used directly
*/
# error This_file_can_be_used_only_during_simulation_inside_Simulink
#endif

float32 DivByZeroInput;
float32 DivByZeroOutput;

const void * inputPorts[NUMBER_OF_INPUTS];
const void * outputPorts[NUMBER_OF_OUTPUTS];

static void mdlInitializeSizes(SimStruct *S)
{
    uint8 i;
    ssSetNumSFcnParams(S, 0);
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
    {
        return; /* Parameter mismatch reported by the Simulink engine */
    }
    if (!ssSetNumInputPorts(S, NUMBER_OF_INPUTS)) return;


    ssSetInputPortWidth(S, 0, 1);
    ssSetInputPortDataType(S, 0, SS_SINGLE);
    for (i = 0; i < NUMBER_OF_INPUTS; i++)
    {
        ssSetInputPortDirectFeedThrough(S, i, 1); /* Set direct feedthrough flag */
        ssSetInputPortRequiredContiguous(S, i, 1); /*direct input signal access*/
    }

    if (!ssSetNumOutputPorts(S, NUMBER_OF_OUTPUTS)) return;

    ssSetOutputPortWidth(S, 0, 1);
    ssSetOutputPortDataType(S, 0, SS_SINGLE);

    /* Set Sample Time */
    ssSetNumSampleTimes(S, 1);

    /* Specify the sim state compliance to be same as a built-in block */
    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);

    ssSetOptions(S, SS_OPTION_ALLOW_PORT_SAMPLE_TIME_IN_TRIGSS);
    ssSupportsMultipleExecInstances(S, true); //set so the s-function can be used in a for each subsystem block
}

static void mdlInitializeSampleTimes(SimStruct *S)
{
    /* Inherits sample time from the driving block */
    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
    ssSetModelReferenceSampleTimeDefaultInheritance(S);
}

#define MDL_START
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
    uint8 i;

    /* Save Input ports */
    for (i = 0; i < NUMBER_OF_INPUTS; i++)
    {
        inputPorts[i] = ssGetInputPortSignal(S, i);
    }

    /* Save Output ports */
    for (i = 0; i < NUMBER_OF_OUTPUTS; i++)
    {
        outputPorts[i] = ssGetOutputPortRealSignal(S, i);
    }

}
#endif 

static void mdlOutputs(SimStruct *S, int_T tid)
{
    memcpy(&DivByZeroInput, inputPorts[0], sizeof(DivByZeroInput));//gets the input port info
    if (fabs(DivByZeroInput) > 0.00001f)
    {
        DivByZeroOutput = DivByZeroInput;
    }
    else
    {
        if (DivByZeroInput >= 0.0f)
        {
            DivByZeroOutput = 0.00001f;
        }
        else
        {
            DivByZeroOutput= -0.00001f;
        }
    }
    memcpy(outputPorts[0], &DivByZeroOutput, sizeof(DivByZeroOutput));//sets the output port
}

static void mdlTerminate(SimStruct *S)
{
    /* MODEL TERMINATE */
}

/* END OF TEMPLATE */

#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif

The s-function is supposed to provide a simple division by zero protection as you can see from the code. In the image below, if I delete S-Function1, S-Function will then work as intended. I tried putting it in a library block and just put 2 library blocks and I got the same result enter image description here


Solution

  • You are defining these variables:

    float32 DivByZeroInput;
    float32 DivByZeroOutput;
    
    const void * inputPorts[NUMBER_OF_INPUTS];
    const void * outputPorts[NUMBER_OF_OUTPUTS];
    

    These variables are shared across both instances, which means both S-Functions write to the same output addresses.

    Side note: Do you really have to use an S-Function? Creating the same from a few native simulink blocks is probably much easier.