Main Content

Organize Variant Control Macros in Same Header File

This example explains how to aggregate multiple variant control macros (#define preprocessor directives) into a single generated header file to simplify the management of complex systems with multiple interacting variant conditions. Furthermore, you can use the custom storage classes to specify the header file name associated with the variant control variables. By applying the custom storage class, the header file name is automatically set for all parameter objects that represent the variant control variables. This approach eliminates the need to manually update the header file name for each parameter object, ensuring that any changes made to the header file name are reflected across all objects using the custom storage class.


We recommend completing the Implement Variations in Separate Hierarchy Using Variant Subsystems example to learn more about how to use variant blocks in Simulink.

Explore the Model

Open the slexVariantSubsystems model.

The slexVariantSubsystems model contains a Variant Subsystem block named Controller. The Controller block encapsulates two different implementations, Linear Controller and Nonlinear Controller, as variant choices. To specify appropriate variant conditions in the variant choices, run the prepare_preproc_subsys file.

model = "slexVariantSubsystems";

1. Navigate inside the Controller block to view its choices. Right-click the variant badge on the Controller block and select Block Parameters (Subsystem). In this example, the variant choices in the Controller block are associated with variant controls that are represented by the Simulink.VariantExpression objects VSS_LINEAR_CONTROLLER and VSS_NONLINEAR_CONTROLLER. The VSS_LINEAR_CONTROLLER object contains the control expression MODE_A == 0 || MODE_B == 0 and VSS_NONLINEAR_CONTROLLER contains the control expression MODE_A == 1 && MODE_B == 1. The control expressions are displayed in the Condition column of the Controller Block Parameters dialog box.

Here, the variant control variables MODE_A and MODE_B are Simulink.Parameter objects and their values are set to 1. The parameter objects use the custom storage class Define and are configured to appear in the generated code as C-code macros in a separate header file macros.h.

The variant control variables defined as Simulink.Parameter objects can have any of the storage classes listed in Storage Classes for Different Variant Activation Times.

MODE_A = Simulink.Parameter;
MODE_A.Value = 1;
MODE_A.DataType = "int32";
MODE_A.CoderInfo.StorageClass = "Custom";
MODE_A.CoderInfo.CustomStorageClass = "Define";
MODE_A.CoderInfo.CustomAttributes.HeaderFile = "macros.h";

MODE_B = Simulink.Parameter;
MODE_B.Value = 1;
MODE_B.DataType = "int32";
MODE_B.CoderInfo.StorageClass = "Custom";
MODE_B.CoderInfo.CustomStorageClass = "Define";
MODE_B.CoderInfo.CustomAttributes.HeaderFile = "macros.h";

VSS_LINEAR_CONTROLLER.Condition = "MODE_A == 0 || MODE_B == 0";
VSS_NONLINEAR_CONTROLLER.Condition = "MODE_A == 1 && MODE_B == 1";

Change Name of Generated Header File

To change the name of the generated header file from macros.h to variant_controls.h, perform these steps:

1. On the Modeling tab, select Model Explorer.

2. In the Model Hierarchy pane, select Simulink Root > Base Workspace. The Contents pane displays all the objects in the base workspace.

3. Select MODE_A, and then in the Simulink.Parameter:MODE_A pane, select the Code Generation tab.

4. In the HeaderFile parameter, specify variant_controls.h as the name of the file.

5. Repeat the steps to change the name of the generated header file for MODE_B to variant_controls.h.

Alternatively, you can change the file name programmatically, by using this command. For more information, see Simulink.CoderInfo.

Simulink.CoderInfo.CustomAttributes.HeaderFile = 'variant_controls.h';

6. To generate code, in the Apps tab of the toolstrip, navigate to Embedded Coder. In the C code tab, select Build > Generate code. Alternatively, enter this command in the Command Window. Observe that the variant control variables MODE_A and MODE_B appear as C-code macros in the header file variant_controls.h.

### Starting build procedure for: slexVariantSubsystems
### Successful completion of build procedure for: slexVariantSubsystems

Build Summary

Top model targets built:

Model                  Action                        Rebuild Reason                                    
slexVariantSubsystems  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 11.503s

Reduce Maintenance Effort by Creating Custom Storage Class

To change the name of a header file that is associated with multiple parameter objects, you must change the configuration of each parameter object individually. While the Model Data Editor allows for batch editing, this does not extend to new variant control variables (parameter objects) that you add. You must specify the header file name individually. Additionally, the Model Data Editor displays parameter objects for only a single model at a time.

A more efficient approach is to create a custom storage class. With the custom storage class, you are required to specify the header file name within the custom storage class definition only once. Consequently, this header file name automatically applies to all parameter objects linked to the custom storage class.

To create a custom storage class, perform these steps:

1. Copy the demodata package into your current folder as +myPackage.


2. Navigate inside the +myPackage folder and open Parameter.m.

3. Uncomment the methods section that defines the method setupCoderInfo. In the call to the function useLocalCustomStorageClasses, replace 'packageName' with 'myPackage'.

    function setupCoderInfo(h)
      % Use custom storage classes from this package
      useLocalCustomStorageClasses(h, 'myPackage');
  end % methods

4. Save and close the file.

5. Open the Custom Storage Class Designer.


6. Select the custom storage class Define.

7. Click Copy. A new custom storage class, Define_1, appears. Select this new custom storage class.

8. Set Name to VariantControlVar.

9. Set Header file to Specify. In the text box, enter variant_controls.h and save the changes.

10. At the command prompt, replace the Simulink.Parameter objects MODE_A and MODE_B with the myPackage.Parameter object. Apply the new custom storage class VariantControlVar.

MODE_A = myPackage.Parameter;
MODE_A.Value = 1;
MODE_A.DataType = "int32";
MODE_A.CoderInfo.StorageClass = "Custom";
MODE_A.CoderInfo.CustomStorageClass = "VariantControlVar";

MODE_B = myPackage.Parameter;
MODE_B.Value = 1;
MODE_B.DataType = "int32";
MODE_B.CoderInfo.StorageClass = "Custom";
MODE_B.CoderInfo.CustomStorageClass = "VariantControlVar";

11. Create a new parameter object MODE_C representing a variant control variable using the custom storage class VariantControlVar.

MODE_C.Value = 1;
MODE_C.DataType = "int32";
MODE_C.CoderInfo.StorageClass = "Custom";
MODE_C.CoderInfo.CustomStorageClass = "VariantControlVar";

Instead of modifying each parameter object individually, you can change the header file name for all of them once using the Custom Storage Class Designer.

See Also

(Embedded Coder) |