Create a variant parameter bank.
To specify code generation properties for the variant parameter bank, create an object of the Simulink.VariantBankCoderInfo
class.
You can control code placement by specifying the HeaderFile
and DefinitionFile
properties. You can specify the memory section to place the parameter values in the compiled code by providing code statements in the PreStatement
and PostStatement
properties.
Use the name of the Simulink.VariantBankCoderInfo
object to set the BankCoderInfo
property of the variant bank object.
Create a variant control variable V
using the Simulink.VariantControl
class. The Value
property of this object allows you to select an active value for a variant parameter and the ActivationTime
property allows you to specify a variant activation time. Use this variable to specify the variant condition expression for the choice values of a variant parameter.
Create two variant parameter objects K1
and K2
with variant activation time startup
and associate them with the variant parameter bank EngineParams
.
Generate code from the model using Embedded Coder®. For information on how to generate code, see Generate Code Using Embedded Coder (Embedded Coder).
Because K1
and K2
have variant activation time set to startup
, the code contains a structure EngineParams
. The name of this structure is the same as the Name
property of the parameter bank object. The structure definition is in the header file vparams.h
and the structure array is in the definition file vparams.c
.
/* vparams.h */
/* Variant parameter bank: EngineParams, for system '<Root>' */
typedef struct {
real_T K2[3]; /* Variable: K2
* Referenced by: '<Root>/Gain1'
*/
real_T K1; /* Variable: K1
* Referenced by: '<Root>/Gain'
*/
} EngineParams;
/* Variant parameter bank: EngineParams */
/* Variant parameter bank section */
#pragma data_seg(.mydata)
extern EngineParams EngineParams_ptr_impl[2];
extern EngineParams *EngineParams_ptr;
#pragma end
A structure array EngineParams_ptr_impl
of type EngineParams
contains choice values of K1
and K2
. The number of elements in the array depends on the number of variant conditions that you specify in the VariantConditions
property of the parameter bank object.
/* vparams.c */
#pragma data_seg(.mydata)
EngineParams EngineParams_ptr_impl[2] = {
{
/* Variable: K2
* Referenced by: '<Root>/Gain1'
*/
{ 5.0, 10.0, 15.0 },
/* Variable: K1
* Referenced by: '<Root>/Gain'
*/
6.0
}, {
/* Variable: K2
* Referenced by: '<Root>/Gain1'
*/
{ 3.0, 6.0, 9.0 },
/* Variable: K1
* Referenced by: '<Root>/Gain'
*/
3.0
}
};
EngineParams *EngineParams_ptr = &EngineParams_ptr_impl[1];
#pragma end
The var_param_bank_initialize
function initializes the structure pointer variable EngineParams_ptr
. The code uses the pointer variable to access the active value set from the array based on variant conditions.
/* model.c */
/* Model step function */
void var_param_bank_step(void)
{
/* Outport: '<Root>/Out2' incorporates:
* Gain: '<Root>/Gain1'
* Inport: '<Root>/Input1'
*/
var_param_bank_Y.Out2[0] = EngineParams_ptr->K2[0] * var_param_bank_U.Input1;
var_param_bank_Y.Out2[1] = EngineParams_ptr->K2[1] * var_param_bank_U.Input1;
var_param_bank_Y.Out2[2] = EngineParams_ptr->K2[2] * var_param_bank_U.Input1;
/* Outport: '<Root>/Out1' incorporates:
* Gain: '<Root>/Gain'
* Inport: '<Root>/Input'
*/
var_param_bank_Y.Out1 = EngineParams_ptr->K1 * var_param_bank_U.Input;
}
/* Model initialize function */
void var_param_bank_initialize(void)
{
/* Variant Parameters startup activation time */
if (V == EngType_Big) {
EngineParams_ptr = &EngineParams_ptr_impl[0U];
} else if (V == EngType_Small) {
EngineParams_ptr = &EngineParams_ptr_impl[1U];
}
var_param_startupVariantChecker();
}