Improve Code Readability of Variant Parameters Using Enumerated Types
You can use enumerated types to improve code readability of variant control values in variant parameters. Simulink Enumerations improve readability because enumerated types of variant controls values are represented as meaningful names instead of integers in the generated code.
You can define enumerated types of variant control values in the storage locations listed in Storage Locations for Variant Control Variables (Operands) in Variant Parameters.
For variant parameters with startup
activation time, only enumerations that are defined using these techniques are supported:
Using the function
Simulink.defineIntEnumType
By subclassing built-in integer data types
int8
,int16
,int32
,uint8
, oruint16
, or by subclassingSimulink.IntEnumType
These enumerations are also supported when permanently stored in a Simulink® data dictionary. See Enumerations in Data Dictionary.
Model
Consider the example model slexVariantsWithStartup
.
This model represents a fog lamp controller system that operates in driver control mode and in sensor connected mode. In driver control mode, the intensity of the fog lamps is controlled by the driver. In sensor connected mode, the sensor adjusts the intensity of different types of fog lamps based on the intensity of ambient light in the external environment.
In this example, lightVolt
is a variant parameter that represents fog lamp intensities. The sensor voltage from Signal Editor block is the intensity of ambient light in the external environment. The LIGHT_TYPE
is the variant control variable that represents different types of fog lamps. LIGHT_TYPE
enables you to switch between fog lamp intensities, lightVolt
, for the given intensity of ambient light. LIGHT_TYPE
can have two values, LightType.Type1
and LightType.Type2
.
When LIGHT_TYPE == LightType.Type1
evaluates to true
, a fog lamp intensity from [0 0 0 0.7 0.7] that corresponds to the given ambient light becomes active. When LIGHT_TYPE == LightType.Type2
evaluates to true
, a fog lamp intensity from [0 0 0 1 1] that corresponds to the given ambient light becomes active. For example, if the ambient light has an intensity 4
and the type of the fog lamp is Type1
, the fog lamp is illuminated with intensity 0.7
. Similarly, if the ambient light has an intensity 4
and the type of the fog lamp is Type2
, the fog lamp is illuminated with intensity 1
. For more information on this fog lamp controller system, see Run Iterative Simulations Without Recompiling Model for Variant Systems Using Fast Restart.
Fog Lamp Types in a Enumeration Class
In this example, the enumeration LightType
, saved in LightType.m
, derives from the built-in class Simulink.IntEnumType
. It defines two enumeration members, Type1
and Type2
, with underlying integer values 1
and 2
.
type LightType.m
classdef LightType < Simulink.IntEnumType % ControllerType - enumeration class used by % slexVariantsWithStartup % Copyright 2021-2023 The MathWorks, Inc. enumeration Type1 (1) Type2 (2) end methods (Static) function val = getDataScope() val = 'Imported'; end function val = getHeaderFile() val = 'LightType.h'; end end end
Simulate Fog Lamp Controller for Different Types of Fog Lamps in Sensor Connected Mode
1. In the MATLAB® Command Window:
a. Set CTRL_MODE
to 1
to activate the Sensors Connected.
CTRL_MODE.Value = int32(1);
b. Set LIGHT_TYPE
to LightType.Type1
to represent Type1
type of fog lamps.
LIGHT_TYPE.Value = LightType.Type1;
Check that the activation time of the LIGHT_TYPE
is set to startup
.
2. Run the simulation. Use the Scope block to visualize the results. For details about the signals that the Scope block plots, see the Model Inputs and Outputs section of Run Iterative Simulations Without Recompiling Model for Variant Systems Using Fast Restart.
3. Change the value of LIGHT_TYPE
to LightType.Type2
and run the simulation again. Use the Scope to visualize the results.
LIGHT_TYPE.Value = LightType.Type2;
Generate Code for Enumerated Type of Fog Lamps
Before you generate code from the model, make sure that you have write permission in your current folder.
To generate code using Simulink® Coder™, the configuration parameters in Model Settings > Code Generation are set as:
1. The System target file is specified as grt
.
2. On the Custom Code pane, the header file is specified as ReadLightControllerValues.h
and the source file is specified as ReadLightControllerValues.c
. This setting inserts the ReadLightControllerValues()
function in the model source file, slexVariantsWithStartup.c
, of the generated code.
3. In the Apps gallery of the model toolstrip, click Simulink Coder. On the C Code tab, click Generate Code. For more information, see Generate Code Using Simulink Coder (Simulink Coder).
slbuild('slexVariantsWithStartup');
### Starting build procedure for: slexVariantsWithStartup ### Successful completion of build procedure for: slexVariantsWithStartup Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================================== slexVariantsWithStartup Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 27.292s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 32.821s
Review Generated Code
1. In the C Code tab, select Open Report.
2. From the Generated Code pane of the report, select the slexVariantsWithStartup.c
file. The slexVariantsWithStartup_initialize
function contains the ReadLightControllerValues()
function to read the values of CTRL_MODE
and LIGHT_TYPE
for determining the mode of operation and the type of the fog lamp. The value of LIGHT_TYPE
read using the function is compared to the enumerated names Type1
and Type2
that maps to the values 1
and 2
of the enumeration LightType
. Based on the condition that evaluates to true
, the set of fog lamp intensities to activate is determined.
void slexVariantsWithStartup_initialize(void) { ... /* user code (Initialize function Body) */
/* System '<Root>' */ ReadValueOfVariantControls();
/* Variant Parameters startup activation time */ if (LIGHT_TYPE == Type1) { for (i = 0; i < 5; i++) { slexVariantsWithStartup_P.lightVolt[i] = (real_T)tmp[i]; } } else if (LIGHT_TYPE == Type2) { for (i = 0; i < 5; i++) { slexVariantsWithStartup_P.lightVolt[i] = tmp_0[i]; } } ... }
See Also
Enumerated Types to Improve Code Readability of Variant Control Variables of Variant Parameters