Main Content

Use a Bus Signal with S-Function Builder to Create an S-Function

This topic will show you how to create a bus signal and connect it to an S-Function Builder. At the end, you will use the S-Function Builder to build a simple C MEX S-function.

The S-Function Builder is a Simulink®block that integrates C/C++ code to build an S-function from specifications and C code that you supply. The S-Function Builder also serves as a wrapper for the generated S-function in models that use the S-function. To learn more on S-Function Builder, seeBuild S-Functions Automatically Using S-Function Builder. To see more examples on how to use S-Function Builder, typesfundemosin the MATLAB®Command Window, then navigate toC-file S-functions>S-function Builder.

  1. Start building your S-function by setting the current folder to the folder in which you want to create an S-function. Then, add this folder to the MATLAB path.

    mkdirnewSfunaddpath(fullfile(pwd,'newSfun')) cd('newSfun')

  2. If you wish to connect a bus signal to the Input or Output port of the S-Function Builder, you must first create a bus object. You can create a bus object interactively using the Simulink Bus Editor (seeCreate and Specify Simulink.Bus Objects). Alternatively, you can useSimulink.Bus:

    1. In the MATLAB Command Window, enter:

      a = Simulink.Bus

      As a result, theHeaderFilefor the bus defaults to the empty character vector:

      a = Simulink.Bus Description: '' HeaderFile: '' Elements: [0x1 double]
    2. If you want to specify the header file for the bus, then at the MATLAB Command Window, enter the following:

      a.Headerfile = 'Busdef.h'

      If you do not specify a header file, Simulink automatically generatesSfunctionname_bus.h

    For a demonstration on how to use the S-Function Builder with a bus, enter the following command at the MATLAB Command Window:

    open_system(fullfile(matlabroot,'/toolbox/simulink/simdemos/simfeatures/sfbuilder_bususage'))

  3. Create a new Simulink model. Click on the canvas and type S-Function Builder to create an instance of theS-Function Builderblock. Alternatively, drag an S-Function Builder block from theUser-Defined Functionslibrary in the Library Browser into the new model.

  4. Click on the canvas and type S-Function Builder or copy an instance of the S-Function Builder block from theUser-Defined Functionslibrary into the new model.

  5. Double-click to open the S-Function Builder editor.

  6. Use the specification and code entry panes on the S-Function Builder dialog box to enter information and custom source code required to tailor the generated S-function to your application.

  7. Check whether the bus signal is in between the specified upper and lower saturation limits. To do so this, create two input and two output ports, and assign the ports according to the table below:

  8. Fill theOutputs_wrapperfunction with the logic of your code. See the example code:

    void sfbuilder_bus_Outputs_wrapper(const SFB_COUNTERBUS *u0, const int32_T *u1, const SFB_COUNTERBUS *y0, int32_T *y1) { /* Output_BEGIN */ int32_T limit; boolean_T inputGElower; /* limit is sum of SFB_SIGNALBUS.input and the second input(u1) */ limit = u0->inputsignal.input + *u1; /* check if SFB_SIGNALBUS.limit is >= LIMITBUS.lower_saturation_limit */ inputGElower = (limit >= u0->limits.lower_saturation_limit); if((u0->limits.upper_saturation_limit >= limit) && inputGElower) { *y1 = limit; } else { if(inputGElower) { limit = u0->limits.upper_saturation_limit; } else { limit = u0->limits.lower_saturation_limit; } *y1 = limit; } y0->inputsignal.input = *y1; y0->limits = u0->limits; /* Output_END */ }
  9. Click the arrow underBuildand SelectGenerate TLC wrapperto create a wrapper for your S-function for code generation.

  10. ClickBuildon the S-Function Builder to start the build process.

    The S-Function Builder builds a MEX file that implements the specified S-function and saves the file in the current folder (seeHow the S-Function Builder Builds an S-Function).

  11. Save the model containing the S-Function Builder block.

How the S-Function Builder Builds an S-Function

s函数构建器构建一个S-function by generating the following source files in the current folder:

  • sfun.c— This file contains the C source code representation of the standard portions of the generated S-function.

  • sfun_wrapper.c— This file contains the custom code that you entered in the S-Function Builder dialog box.

  • sfun.tlc— This file permits the generated S-function to run in SimulinkRapid Acceleratormode and allows for inlining the S-function during code generation. In addition, this file generates code for the S-function inAcceleratormode which allows the model to run faster.

  • sfun_bus.h— If you specify anyInput portorOutput portas a bus in thePorts and Parameterstable, but do not specify a header file, then the S-Function Builder automatically generates this header file.

After generating the S-function source code, the S-Function Builder uses themexcommand to build the MEX file representation of the S-function from the generated source code and any external custom source code and libraries that you specified.

Deploy the Generated S-Function

To use the generated S-function in another model, first check to ensure that the folder containing the generated S-function is on the MATLAB path. Then, copy the S-Function Builder block from the model used to create the S-function into the target model. If necessary, set the target model's parameters to the values required by the target model.

Alternatively, you can deploy the generated S-function without using the S-Function Builder block or exposing the underlying C source file.

  1. Open the Simulink model that will include the S-function.

  2. Click the canvas to create an instance of the S-function block, or copy an S-Function block from theUser-Defined Functionslibrary in the Library Browser into the model.

  3. Double-click on the S-Function block.

  4. In the Block Parameters dialog box that opens, in theS-function nameedit field, enter the name of the executable file generated by the S-Function Builder.

  5. Enter any parameters for the S-function into theS-function parametersedit field. Enter the parameters in the order in which they appear in the S-Function Builder dialog box.

  6. ClickOKon the S-Function Block Parameters dialog box.

By creating a block from a generated S-function, you can also create a mask for yourS-Functionblock. To learn more about how to add a mask for your S-function, seeCreate Block Masks. To see an example of how block masks are added to an S-function, enter the following command on the MATLAB Command Window.

open_system(fullfile(matlabroot,'/toolbox/simulink/simdemos/simfeatures/sfcndemo_matadd'));

You can use the generated executable file in any S-Function block in any model as long as the executable file is on the MATLAB path.

See Also

|

Related Topics