Reuse Custom Code in Stateflow Charts - MATLAB & Simulink (original) (raw)

You can integrate custom code written in C or C++ with Stateflow® charts in Simulink® models. By sharing data and functions between your custom code and your Stateflow chart, you can augment the capabilities of Stateflow and take advantage of your preexisting code.

Integrate Custom C Code in Stateflow Charts

This example shows how to use custom C code to define constants, variables, and functions that you can access in the charts in your model. For more information about integrating custom C++ code in your charts, see Access Custom C++ Code in Stateflow Charts.

In this example, a Stateflow chart calls a custom code function named custom_function. This function reads the chart input signal and the local data threshold, compares their values, and returns one of three custom global constants named HIGH, MEDIUM, and LOW. The chart uses the value of the function to determine whether to transition to a new state after it increments or decrements the value of a custom variable named custom_var.

To see the custom code that this chart accesses, open the Configuration Parameters dialog box and, in the Simulation Target pane, select the Code Information tab.

Both of these files are located in the same folder that contains the model. To access custom code files in a different folder, use relative path names. For more information, see Specify Relative Paths to Custom Code.

When you simulate the model, Stateflow compiles the source file and the chart into a single S-function MEX file. Because the custom definitions appear at the top of the generated machine header file sf_custom_header.h, every chart in the model can access the custom code during simulation.

Configure Custom Code

Specify Custom Code Settings for Simulation

The examples for specifying C source, header, and library files are based on this folder structure.

The MATLAB file browser shows three folders: "src", "includes" and "lib". "src" contains three C files. "includes" contains two header files and a "utilities" folder with one header file. "lib" contains a DLL library and a "additional libraries" folder with a DLL library.

To configure your model to access custom code during simulation, follow these steps.

  1. Open the Configuration Parameters dialog box. For information on how to do this, see Set Configuration Parameters by Using the Dialog Box (Simulink).
  2. In the Simulation Target pane, in theCode Information tab, specify these parameters:
    • Include headers — Enter the code to include at the top of the generated _`model`_.h header file, which declares custom functions and data in the generated code. The code appears at the top of all generated source code files and is visible to all generated code. For example, use this parameter to enter #include and #define statements. When you include a custom header file, you must enclose the file name in double quotes. For more information, see Include headers (Simulink).
      Note
      * The code you specify in this parameter can includeextern declarations of variables or functions, such as extern int x or extern void myfun(void). However, global variable or function definitions such as int x or void myfun(void) cause linking errors because the definitions appear multiple times in the source files of the generated code.

    The Simulation Target Custom Code "Include headers" tab has three lines of code: #include "util.h", #include "table.h", and #include "extra functions.h"

    • Include directories — Enter a list of the folder paths that contain custom header, source, and library files that you include either directly in the configuration parameters or indirectly in the compiled target. You can separate entries with newlines, spaces, or semicolons. For more information, see Include directories (Simulink).
      The Simulation Target Custom Code "Include directories" tab shows five lines: src, includes, includes/utilities, lib, "lib/additional libraries"
    • Source files — Enter a list of source files to compile and link into the target separated by newlines, spaces, or semicolons. For more information, seeSource files (Simulink).
      The Simulation Target Custom Code "Source files" tab shows three lines: util.c, table.c, and "extra functions.c"
    • Libraries — Enter a list of static libraries that contain custom object code to link into the target, separated by newlines, spaces, or semicolons. For more information, see Libraries (Simulink).
      The Simulation Target Custom Code "Libraries" tab shows two lines: addons.dll, and library.dll
    • Defines — Enter a space-separated list of preprocessor macro definitions to add to the generated code. For more information, see Defines (Simulink).
    • Compiler flags — Enter additional compiler flags to be added to the compiler command line when your custom code is compiled. For more information, seeCompiler flags (Simulink).
    • Linker flags — Enter additional linker flags to be added to the linker command line when your custom code is linked. For more information, see Linker flags (Simulink).
  3. Under Advanced parameters, check that Import custom code (Simulink) is selected. This parameter is enabled by default.

For information on setting simulation options by using the command-line API, see Set Configuration Parameters Programmatically.

Specify Custom Code Settings for Code Generation

To configure your model to access custom code for code generation, use the > pane of the Configuration Parameters dialog box. You can use the same custom code settings for code generation that you use for simulation or use unique custom code settings.

The Code Generation Custom Code pane of the Configuration Parameters dialog box shows the same custom code settings specified in the Simulation Target pane.

For more information, see Use the same custom code settings as Simulation Target (Simulink Coder) and Integrate External Code by Using Model Configuration Parameters (Simulink Coder).

Call Custom Code Functions in States and Transitions

You can call custom code functions from the actions of any state or transition or from other functions.

To call a custom code function, use the signature specified by the function declaration in your header file. Include an actual argument value for each formal argument in the function signature:

return_val = function_name(arg1,arg2,...)

Custom Code Configuration

Specify Relative Paths to Custom Code

When you update your model or start the simulation, the model searches for custom code files in these folders:

You can specify the location of your custom code by using paths relative to one of these folders. For instance, suppose that, in the previous example, you store the source and header files for your custom code in the model folder subfoldersCustomCode/SourceFiles and CustomCode/HeaderFiles. To access these files, use the Include directories parameter to specify the relative paths of the subfolders.

The Simulation Target pane of the Configuration Parameters dialog box shows the relative paths to two subfolders.

Alternatively, you can use relative pathnames to specify the header and source files individually:

#include "./CustomCode/HeaderFiles/sf_custom_code_constants_vars_fcns_hdr.h"  

Guidelines for Relative Path Syntax

When you construct relative paths for custom code, follow these syntax guidelines:

See Also

Topics