Configure Parameters for C++ Interface Code Generation
When you generate a C++ class interface from a model, block parameters and other data elements associated with the model are represented in the model class (the C++ class generated from the Simulink® model). By default, Embedded Coder® optimizes the generated code, which can result in data elements getting removed from the generated code during optimization.
Based on your code interface requirements, you can configure the model to specify how Embedded Coder represents block parameters in the generated code.
You can configure parameters that are stored in the model class to be:
Instance-specific parameters - Different objects can have different parameter values.
Shared parameters - Different objects have the same parameter values.
Use model CppInterface
to explore configuration details and generated code for instance-specific and shared parameters.
Configure Instance-Specific Parameters
1. Open a model configured for C++ code generation. For this example, open CppInterface
.
open_system("CppInterface");
2. Save a working copy of the model and name it CppInterfaceInstanceSpecific
.
3. From the Apps tab, open the Embedded Coder app to access the C++ Code toolbar.
4. From the Modeling tab, in the Design section, open Model Explorer.
5. In Model Explorer, under Simulink Root > CppInterfaceInstanceSpecific > Model Workspace, select the Argument option for your parameters. For this example, select the Argument for the parameters Lower and Upper.
Close Model Explorer.
6. Under the C++ Code tab, open the Code Mappings Editor. Under the Data tab, configure the Model parameter arguments settings:
Set Data Visibility to
private
.Set Member Access Method to
Inlined method
.Point to the cell in the last column and click to set Data Access to
Direct
.
7. Save the model, and then generate code.
8. Once code generation is complete, open the generated file CppInterfaceInstancespecific.h
to inspect the model class CppInterfaceInstancespecific
and class members.
Block parameters
Lower
andUpper
are members of structureInstP_CppInterfaceInstanceSpe_T
.Block parameters are accessed by using get and set methods
get_InstP
andset_InstP
.The parameters are defined and initialized in the instance-specific
CppInterfaceInstanceSpeci_InstP
variable in the model class.
class CppInterfaceInstanceSpecific final { public: // instance parameters, for system '<Root>' struct InstP_CppInterfaceInstanceSpe_T { real_T LOWER; // Variable: LOWER real_T UPPER; // Variable: UPPER }; ... // get method for instance parameters const InstP_CppInterfaceInstanceSpe_T &get_InstP() const { return CppInterfaceInstanceSpeci_InstP; } // set method for instance parameters void set_InstP(const InstP_CppInterfaceInstanceSpe_T &CppInterfaceInstanceS_InstP_arg) { CppInterfaceInstanceSpeci_InstP = CppInterfaceInstanceS_InstP_arg; } ... private: ... // instance parameters InstP_CppInterfaceInstanceSpe_T CppInterfaceInstanceSpeci_InstP{ // Variable: LOWER -10.0, // Variable: UPPER 10.0 }; };
For more information, see Configure Instance-Specific Values for Block Parameters in a Referenced Model.
Configure Shared Parameters
Shared parameters appear as a structured data member with get and set access methods in the generated C++ model class.
1. Open a model configured for C++ code generation. For this example, open CppInterface
.
open_system("CppInterface");
2. Save a working copy of the model and name it CppInterfaceShared
.
3. From the Apps tab, open the Embedded Coder app to access the C++ Code toolbar.
4. From the C++ Code tab, open the configuration parameters by selecting Settings > C/C++ Code generation settings.
5. In the Configuration Parameters dialog box, under Code Generation > Optimization, from the Default parameter behavior list, select Tunable
.
Click Apply, and then click OK.
6. Under the Modeling tab, in the Design section, open Model Explorer.
7. In Model Explorer, under Simulink Root > CppInterfaceShared > Model Workspace, clear the Argument option for the parameters. For this example, clear the Argument for the parameters Lower and Upper.
Close Model Explorer.
8. Under the C++ Code tab, open the Code Mappings Editor. Under the Data tab, configure the Model parameters settings:
Set Data Visibility to
private
.Set Member Access Method to
Method
.
9. Save the model, and then generate code.
10. Once code generation is complete, open the generated file CppInterfaceShared.h
to inspect the model class (CppInterfaceShared
) and class members.
Block parameters
Lower
andUpper
are named variablesConstant2_Value
andConstant1_Value
in structureP_CppInterfaceShared_T
.Block parameters are accessed by using get and set methods
getBlockParameters
andsetBlockParameters
.The parameters are defined in the static
CppInterfaceShared_P
variable in the model class.
class CppInterfaceShared final { public: ... // Parameters (default storage) struct P_CppInterfaceShared_T { real_T Constant1_Value; // Expression: UPPER // Referenced by: '<Root>/Constant1' real_T Constant2_Value; // Expression: LOWER // Referenced by: '<Root>/Constant2' }; ... // Block parameters get method const P_CppInterfaceShared_T &getBlockParameters() const; // Block parameters set method void setBlockParameters(const P_CppInterfaceShared_T *pP_CppInterfaceShared_T) const; ... private: ... // Tunable parameters static P_CppInterfaceShared_T CppInterfaceShared_P; };
11. Open CppInterfaceShared_data.cpp
to inspect the initialization of parameters in CppInterfaceShared_P
.
#include "CppInterfaceShared.h" // Block parameters (default storage) CppInterfaceShared::P_CppInterfaceShared_T CppInterfaceShared:: CppInterfaceShared_P{ // Expression: UPPER 10.0, // Expression: LOWER -10.0 };