Represent Variant Condition Values of Enumerated Type in Generated Code
You can generate C code from a Simulink® model with variant condition variables or values of Simulink-supported enumeration types. Enumerated types improve code readability because the condition values and variables in control expressions are represented as meaningful names instead of integers. In this section, you will learn:
How to generate C code from a model with variant condition values of enumerated types.
How the variant condition values are represented in the generated code.
Use Enumerated Type Derived From Built-In Integer
This example shows how to generate C code from a Simulink model with variant condition values of enumerated type derived from the
built-in integer int32
.
Open the slexVariantSetupUsingIntEnums
model. This model runs the
PostLoadFcn
defined in the File > ModelProperties > Callbacks dialog. This function populates the base workspace with the control variables
for the variant blocks.
In this model, the Variant Sink and the Variant Source
blocks specify two potential variants for the Linear controller and
Nonlinear controller subsystems. The control expression for the
Linear controller subsystem is V ==
ControllerChoice.Linear
. The control expression for the Nonlinear
controller subsystem is V == ControllerChoice.NonLinear
. Here,
ControllerChoice
is an integer-based enumeration class derived from the
built-in data type, int32
. The class is defined in the ControllerChoice.m
file. The class has two enumeration values,
Linear
and Nonlinear
. These enumerated values have
underlying integer values 0
and 1
.
Switch Between Variant Choices
Click Simulation > Run and see the variant conditions being propagated to the blocks. By default, the model simulates for the Linear controller subsystem.
To modify the active choice, set the value of V to
ControllerChoice.Nonlinear
, then simulate the model again. The model simulates for the Nonlinear controller subsystem.
Generate Code from Model
Before you generate code from the model, you must have write permission in your current folder.
To generate code, in the Apps gallery of the model toolstrip, click Embedded Coder. The C Code tab appears.
Click the Generate Code > Build icon. (
).
Review Generated Code
In the C Code tab of the toolstrip, select Open Report.
In the generated code, the variant control values
ControllerChoice.Linear
andControllerChoice.Nonlinear
are converted toControllerChoice_Linear
andControllerChoice_Nonlinear
, respectively. A valid C processor recognizes this format. The code also includes the definitions of macros corresponding to these variants. The active variant is determined by using preprocessor conditionals (#if
) on the macros (#define
)ControllerChoice_Linear
andControllerChoice_Nonlinear
.#if V == ControllerChoice_Linear slexVariantSetupUsingIntEnums_B.VariantMerge_For_Variant_Source = slexVariantSetupUsingIntEnums_P.DiscreteTransferFcn_NumCoef_a * slexVariantSetupUsingIntEnum_DW.DiscreteTransferFcn_states_o; #endif #if V == ControllerChoice_Nonlinear slexVariantSetupUsingIntEnums_B.VariantMerge_For_Variant_Source = look1_binlxpw(slexVariantSetupUsingIntEnums_P.DiscreteTransferFcn_NumCoef_l * slexVariantSetupUsingIntEnum_DW.DiscreteTransferFcn_states_n, slexVariantSetupUsingIntEnums_P.uDLookupTable_bp01Data, slexVariantSetupUsingIntEnums_P.uDLookupTable_tableData, 10U); #endif
To view the definitions of macros,
ControllerChoice_Linear
andControllerChoice_Nonlinear
, click theController.h
file in the Generated Code pane of the report.#define ControllerChoice_Linear (0) /* Default value */ #define ControllerChoice_Nonlinear (1) #endif /* RTW_HEADER_Controller_h_ */
To view the definition of the variant control variable,
V
, click theslexVariantSetupUsingIntEnums_types.h
file.#ifndef V #define V ControllerChoice_Linear #endif
Use In-Memory Enumerated Type Derived From Simulink.IntEnumType
The Simulink model in this example shows how to generate C code from a model having variant control variables of enumerated type derived from Simulink.IntEnumType.
Open the
slexVariantSetupUsingInMemoryEnums
model. The model runs thePreLoadFcn
defined in the File > ModelProperties > Callbacks dialog. This populates the base workspace with the control variables for the variant blocks.Generate C code. The variant control variables
Controller.Linear
andController.Nonlinear
are represented asController_Linear
andController_Nonlinear
in the generated code, as the above example also shows.The macros
Controller_Linear
andController_Nonlinear
are defined in theController.h
file.#define Controller_Linear (0) /* Default value */ #define Controller_Nonlinear (1) #endif /* RTW_HEADER_Controller_h_ */
The variant control variable is defined in the
slexVariantSetupUsingInMemoryEnums_types.h
file.#ifndef V #define V Controller_Linear #endif