Reuse Custom Code in Stateflow Charts

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 leverage the software to take advantage of your preexisting code.

Stateflow charts call custom code functions by using the same syntax as other reusable functions:

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

Note

Do not share fixed-point data between your custom code and your Stateflow chart. Data returned from custom code must be of type double.

Choose the Appropriate Procedure for Simulation

To choose the right procedure for simulation, click the highlighted block that best describes your goal.

Use Custom Code to Define Global Constants

This example shows how to use custom C code to define constants that apply to all charts in your model.

The model contains a Stateflow® chart with an input, which you can set to 0 or 1 by toggling the manual switch in the model during simulation.

The chart contains two states A and B. In this example, you define two constants named TRUE and FALSE to guard the transitions between the states in the chart, instead of using the values 1 and 0. These custom definitions improve the readability of your chart actions. TRUE and FALSE are not Stateflow data objects.

To enter the custom code that defines the two constants:

  1. Open the Model Configuration Parameters dialog box.

  2. Select the Simulation Target pane.

  3. In the Header file subpane, enter #define and #include statements. For instance, in this example, you define the global constants with this code:

#define TRUE 1
#define FALSE 0

Because the two custom definitions appear at the top of the generated machine header file sf_custom_code_global_constants_sfun.h, you can use TRUE and FALSE in all charts that belong to this model.

Use Custom Code to Define Constants, Variables, and Functions

This example show how to use custom C code to define constants, variables, and functions that apply to all charts in your model.

The model contains a Stateflow chart with an input signal from a Sine Wave block.

The chart contains two states A and B, and three data objects: input_data, local_data, and out_data. The chart accesses a custom variable named my_global and calls a custom function named my_function.

To configure the model to access the custom code:

  1. Open the Model Configuration Parameters dialog box.

  2. Select the Simulation Target pane.

  3. In the Header file subpane, enter #define and #include statements. When you include a custom header file, you must enclose the file name in double quotes.

  4. In the Include directories subpane, enter the name of the folder that contains your custom code files. To access custom code files in a subfolder of the model folder, use a relative path name of the form .\subfolder_name.

  5. In the Source files subpane, enter the name of the source file that contains your custom code. To access a source file that resides in a subfolder of the model folder, use a relative path name of the form .\subfolder_name\source_file.c.

In this example, the custom code defines three constants, a variable, and a function by using these configurations:

  • The Header file subpane contains this statement: #include "sf_custom_code_constants_vars_fcns_hdr.h"

  • The Include directories subpane contains a single period (.) to indicate that all of the custom code files reside in the same folder as the model.

  • The Source file subpane contains this file name: sf_custom_code_constants_vars_fcns_src.c

The custom header file sf_custom_code_constants_vars_fcns_hdr.h contains the definitions of three constants:

#define TRUE 1
#define FALSE 0
#define MAYBE 2

The header file also contains declarations for the variable my_global and the function my_function:

extern int myglobal;
extern int my_function(int var1, double var2);

The custom source file sf_custom_code_constants_vars_fcns_src.c is compiled in conjunction with the Stateflow generated code into a single S-function MEX file.

Because the custom definitions appear at the top of the generated machine header file sf_custom_code_constants_vars_fcns_sfun.h, you can access them in all charts that belong to this model.

Related Topics