Main Content

Code Generation Using Variant Transitions

With variant transitions, you can create Stateflow® charts in Simulink® models that generate code that can be used in a variety of different software situations. Variant transitions allow you to model a full subset of variations within one Stateflow chart so that you can choose which variation you want when you generate code or when you compile the generated code.

Variant transitions are guarded by Simulink parameters or MATLAB® variables in a condition and connect states within your chart that are variations from the core chart configuration. Variant transition conditions are evaluated during code compile or code generation instead of at runtime. In addition to this, the generated code only contains the data members needed to execute the currently selected software configuration.

Chart variations are supported only in code generation. Stateflow charts that you develop with variant transitions simulate according to which portion of your chart is active.

Variant transitions can not be used in conjunction with actions on default transition paths of a state or a chart.

Create a Variant Configuration

Variant transitions connect variant configurations to your core Stateflow chart. Variant configurations are only active when the variant transition condition is true.

In this example, you will add variant configurations to a basic control system that turns a lamp on and off. The variant configurations allow the lamp to dim in multiple ways. Once you add in the variant configurations to your Stateflow chart, you can choose to generate code for any configuration.

Initially, your Stateflow chart includes two states, Off and On. The light turns on when the event, SWITCH_ON, is sent to the chart. The light turns off when the event, SWITCH_OFF, is sent to the chart.

Some of the lamps have the ability to be dimmed. Within that subset, there are lamps that can be set high or low and lamps that can only be set to high, medium, or low. To add these capabilities to the Stateflow chart, you add in three variations: Dimmer1, Dimmer2, and NoDimmer. Dimmer1 has three substates, High, Medium, and Low. Dimmer2 has two substates, High and Low.

Entry to the variant configurations are guarded by a transition condition that uses a Simulink parameter or MATLAB variable that is defined in the base workspace. To change the transition to a variant transition, click the transition. In the Transition tab, select Variant Transition. The transition appears on the chart with a # symbol, which indicates that the transition is a variant transition.

In this example, the variant configurations are guarded by a condition with a Simulink parameter. In this case, HAS_DIMMER1 is a parameter object. In order for the variant configuration to be active, the condition [HAS_DIMMER1 == 1] must be true. For more information about Simulink parameters, see Set Block Parameter Values (Simulink).

During simulation, your Stateflow chart executes only the active portion of your Stateflow chart. The inactive portion appears grayed out on the Stateflow canvas. To change which portion of the chart is currently active, update the guarding parameter in the base workspace.

Generate Code for Variant Configurations

You must have Simulink Coder™ or Embedded Coder® to generate code from your Stateflow chart. By default, the generated code only contains the active variant state machine.

If you are using Embedded Coder, you can choose to include a preprocessor conditional statement in your generated code. In the Stateflow editor, on the Modeling tab, under Design Data, select Property Inspector. Under Advanced, select Generate preprocessor conditionals.

For more information about generating code, see Generate Code Using Simulink Coder (Simulink Coder) and Generate Code Using Embedded Coder (Embedded Coder).

Using the Variant Manager

The Variant Manager is a central tool that allows you to manage variant configurations in your system model. In the Stateflow editor, on the Modeling tab, under Design Data, select Variant Manager.

For more information about the Variant Manager, see Variant Manager Overview (Simulink).

Related Topics