Main Content

Implement Function-Call Subsystems with S-Functions

您可以创建一个子系统,其executi触发on is determined by logic internal to a C MEX S-function instead of by the value of a signal. A subsystem so configured is called afunction-call subsystem. To implement a function-call subsystem:

  • In the Trigger block, selectfunction-callas theTrigger typeparameter.

  • 有趣的ction, use thessEnableSystemWithTidandssDisableSystemWithTidto enable or disable the triggered subsystem and thessCallSystemWithTidmacro to call the triggered subsystem.

  • In the model, connect the S-Function block output directly to the trigger port.

    Note

    Function-call connections can only be performed on the first output port.

Function-call subsystems are not executed directly by the Simulink®engine; rather, the S-function determines when to execute the subsystem. When the subsystem completes execution, control returns to the S-function. This figure illustrates the interaction between a function-call subsystem and an S-function.

In this figure,ssCallSystemWithTidexecutes the function-call subsystem that is connected to the first output port element.ssCallSystemWithTidreturns 0 if an error occurs while executing the function-call subsystem or if the output is unconnected. After the function-call subsystem executes, control is returned to your S-function.

Function-call subsystems can only be connected to S-functions that have been properly configured to accept them.

To configure an S-function to call a function-call subsystem:

  • InmdlInitializeSizes, set the data type of the S-function first output port to function-call by specifying

    ssSetOutputPortDataType(S, 0, SS_FCN_CALL);

  • Specify the elements that are to execute the function-call subsystem inmdlInitializeSampleTimes. For example:

    ssSetCallSystemOutput(S,0); /* call on first element */ ssSetCallSystemOutput(S,1); /* call on second element */
  • Specify inmdlInitializeSampleTimeswhether you want the S-function to be able to enable or disable the function-call subsystem. Only S-functions that explicitly enable and disable the function-call subsystem can reset the states and outputs of the subsystem, as determined by the function-call subsystem'sTriggerandOutportblocks. For example, the code

    ssSetExplicitFCSSCtrl(S, 1);

    inmdlInitializeSampleTimesspecifies that the S-function can enable and disable the function-call subsystem. In this case, the S-function must invokessEnableSystemWithTidbefore executing the subsystem usingssCallSystemWithTid.

  • Execute the subsystem in the appropriatemdlOutputsormdlUpdateS-function routine. For example:

    static void mdlOutputs(...) { if (((int)*uPtrs[0]) % 2 == 1) { if (!ssCallSystemWithTid(S,0,tid)) { /* Error occurred, which will be reported by */ /*the Simulink engine*/ return; } } else { if (!ssCallSystemWithTid(S,1,tid)) { /* Error occurred, which will be reported by */ /*the Simulink engine*/ return; } } ... }

Seesfun_fcncall.cfor an example that executes a function-call subsystem on the first and second elements of the first S-function output. The following Simulink model (sfcndemo_sfun_fcncall) uses this S-function.

The first function-call subsystem provides a sine wave output. The second function-call subsystem is a simple feedback loop containing a Unit Delay block.

When the Pulse Generator emits its upper value, the function-call subsystem connected to the first element of the first S-function output port is triggered. Similarly, when the Pulse Generator emits its lower value, the function-call subsystem connected to the second element is triggered. The simulation output is shown on the following Scope.

Function-call subsystems are a powerful modeling construct. You can configure Stateflow®blocks to execute function-call subsystems, thereby extending the capabilities of the blocks. For more information, see the Stateflow documentation.

See Also

||

Related Topics