Generate Separate Code for an Atomic Subchart

This example shows how to use atomic subcharts to generate code for individual parts of a Stateflow® chart. When you generate code for your chart, a separate file stores the code for the atomic subchart. Atomic subcharts are supported only in Stateflow charts in Simulink® models. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.

Original Model Without Atomic Subcharts

This model contains two Sine Wave blocks: one with a frequency of 1 radian per second, and the other with a frequency of 2 radians per second.

In the chart, each state uses saturator logic to convert the input sine wave to an output square wave of the same frequency.

Because this example does not use atomic subcharts, generated code for the entire model is stored in one file. To find code for a specific part of the chart, you have to look through the entire file.

In contrast, you can convert state A to an atomic subchart and specify that the code for the subchart appears in a separate file. This method of code generation enables unit testing for a specific part of a chart. You avoid searching through unrelated code and focus only on the code that interests you.

Edit Model to Use Atomic Subcharts

Step 1: Convert a State to an Atomic Subchart

Right-click state A and select Group & Subchart > Atomic Subchart. State A changes to an atomic subchart and displays the label Atomic in the upper-left corner.

Step 2: Set Up a Standalone C File for the Atomic Subchart

  1. Open the properties dialog box for subchart A by right-clicking the subchart and selecting Properties.

  2. Set the Code generation function packaging property to Reusable function.

  3. Set the Code generation file name options property to User specified.

  4. In the Code generation file name box, enter saturator as the name of the file.

  5. Click OK.

Step 3: Set Up the Code Generation Report

  1. In the Modeling tab, click Chart Properties > Model Settings to open the Configuration parameters dialog box.

  2. In the Code Generation tab, set the System target file parameter to ert.tlc.

  3. Under Code Generation > Report, select Create code generation report.

  4. Under Advanced parameters, select Model-to-code.

  5. Click Apply.

Step 4: Customize the Generated Function Names

In the Configuration Parameters dialog box, in the Code Generation > Identifiers tab, set the Subsystem methods parameter to the format scheme $R$N$M$F, where:

  • $R is the root model name.

  • $N is the block name.

  • $M is the mangle token.

  • $F is the type of interface function for the atomic subchart.

Generate Code

To generate code for your modelby using Embedded Coder®, press Ctrl+B.

The code generation report contains links to the code generated from the chart (Model files) and the atomic subchart (Subsystem files). To inspect the code for the subchart, click the saturator.c hyperlink.

Line 31 shows that the during function generated for the atomic subchart has the name ex_reuse_states_A_during. This name follows the format scheme $R$N$M$F specified for Subsystem methods:

  • The root model name is ex_reuse_states.

  • The block name is A.

  • The mangle token is empty.

  • The type of interface function for the atomic subchart is during.

Note: The line numbers that appear in your code generation report can differ from the numbers shown.

See Also

Related Topics