Simulink.VariantBankCoderInfo - Specify code generation properties for variant parameter bank - MATLAB (original) (raw)

Create a variant parameter bank.

EngineParams = Simulink.VariantBank(Name="EngineParams", ... Description="Engine parameters", ... VariantConditions={'V == EngType.Small','V == EngType.Big'});

Create a Simulink.VariantBankCoderInfo object to specify code generation properties for the variant parameter bank structure array that will represent EngineParams in the generated code. Use the name of the object to set the AllChoicesCoderInfo property of the EngineParams bank object.

EngineParamsAllChoicesCoderInfo = Simulink.VariantBankCoderInfo(HeaderFile="vparams.h", ... DefinitionFile="vparams.c", ... PreStatement='#pragma data_seg(.vparamdata)',PostStatement="#pragma end", ... Qualifier="Const"); EngineParams.AllChoicesCoderInfo = "EngineParamsAllChoicesCoderInfo";

Create a second Simulink.VariantBankCoderInfo object to specify code generation properties for the pointer variable used by the parameter bank. Use the name of the object to set the ActiveChoicesCoderInfo property of the EngineParams bank object.To apply the same code generation properties for both the structure array and the pointer variable, set the AllChoicesCoderInfo and ActiveChoiceCoderInfo properties to the same Simulink.VariantBankCoderInfo object.

EngineParamsActiveChoicesCoderInfo = Simulink.VariantBankCoderInfo(HeaderFile="vparams_ptr.h", ... DefinitionFile="vparams_ptr.c", ... PreStatement="#pragma data_seg(.pointerdata)",PostStatement="#pragma end", ... Qualifier="Const"); EngineParams.ActiveChoiceCoderInfo = "EngineParamsActiveChoicesCoderInfo";

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.

V = Simulink.VariantControl(Value=EngType.Small, ... ActivationTime="startup"); K1 = Simulink.VariantVariable(Choices={"V == EngType.Small",3,"V == EngType.Big",6}, ... Bank="EngineParams"); K2 = Simulink.VariantVariable(Choices={"V == EngType.Small", [3, 6, 9],"V == EngType.Big",[5, 10, 15]}, ... Bank="EngineParams");

Use the variant parameters K1 and K2 in your model and 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 '' / / Engine parameters */ typedef struct { real_T K2[3];
real_T K1;
} EngineParams;

/* Variant parameter bank: EngineParams / / Variant parameter bank section */ #pragma data_seg(.vparamdata) extern const EngineParams EngineParams_ptr_impl[2]; #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(.vparamdata) const EngineParams EngineParams_ptr_impl[2] = { { / Variable: K2 * Referenced by: '/Gain1' */ { 5.0, 10.0, 15.0 },

/* Variable: K1
 * Referenced by: '<Root>/Gain'
 */
6.0

}, { /* Variable: K2 * Referenced by: '/Gain1' */ { 3.0, 6.0, 9.0 },

/* Variable: K1
 * Referenced by: '<Root>/Gain'
 */
3.0

} }; #pragma end

Similarly, the header and definition files, vparams_ptr.h and vparams_ptr.c, contain the declaration and definition for the pointer variable.

/* vparams_ptr.h / / Variant parameter bank section */ #pragma data_seg(.pointerdata) extern const EngineParams *EngineParams_ptr; #pragma end

/* vparams_ptr.c */ #pragma data_seg(.pointerdata) const 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: '/Out2' incorporates:

/* Outport: '/Out1' incorporates:

/* 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(); }