Configure Variant Parameter Values for AUTOSAR Elements
AUTOSAR software components can flexibly specify the parameter value of an AUTOSAR element by using variant parameters. To model AUTOSAR elements with variable parameter values in Simulink®:
Create blocks that represent AUTOSAR elements.
Represent varying parameter values by adding
Simulink.VariantVariabledata objects.Model AUTOSAR system constants by using
AUTOSAR.Parameterdata objects. The AUTOSAR data objects represent the condition values that determine the active value of variant parameters.Associate an activation time with the AUTOSAR system constants by using
Simulink.VariantControldata objects. The activation time determines at which stage of code generation you can modify the variant parameter values.
With variant parameters, you can modify parameter values prior to code compile or at model startup. When you build the model, the generated C code contains values and conditions corresponding to variant parameters. The exported ARXML code contains the variant choices as VARIATION-POINT-PROXY entries and the variant control variable as a system constant representing the associated condition value.
Specify Variant Parameters at Pre-compile Time
This example shows how to generate a code for an AUTOSAR element that contains variant parameters without needing to regenerate the code for each set of values. In the generated code, the variant parameter values are enclosed in preprocessor conditionals #if and #elif that enable you to switch between the values prior to code compile.
For example, here is an AUTOSAR component model that contains a variant parameter, k, which models multiple values for the Gain block.
open_system('mAutosarVariantParameter')
This parameter defines multiple values for the Gain parameter and associates each value with variant condition logic. You can specify the variant condition logic as an expression or a Simulink.VariantExpression object containing an expression.
ADAPTIVE = Simulink.VariantExpression('MySys == 10'); LINEAR = Simulink.VariantExpression('MySys == 1'); NONLINEAR = Simulink.VariantExpression('MySys == 2'); k = Simulink.VariantVariable('Choices',{'ADAPTIVE', 10 ,'LINEAR',1,'NONLINEAR',3});
To model an AUTOSAR system constant, the model defines the AUTOSAR.Parameter data object tmpSysCon.
tmpSysCon = AUTOSAR.Parameter(int32(1)); tmpSysCon.CoderInfo.StorageClass = 'Custom'; tmpSysCon.CoderInfo.CustomStorageClass = 'SystemConstant';
The value of tmpSysCon determines the active value of k.
When you generate code for the model:
In the ARXML code, the variant choices appear as
VARIATION-POINT-PROXYentries with short-namesADAPTIVE,LINEAR, andNONLINEAR.MySysappears as a system constant representing the associated condition value.
<VARIATION-POINT-PROXYS>
<VARIATION-POINT-PROXY UUID="744b1a40-2029-54ae-fba9-79a6ca104b8c">
<SHORT-NAME>ADAPTIVE</SHORT-NAME>
<CATEGORY>CONDITION</CATEGORY>
<CONDITION-ACCESS BINDING-TIME="PRE-COMPILE-TIME"><SYSC-REF DEST="SW-SYSTEMCONST">/DataTypes/SystemConstants/MySys</SYSC-REF> == 10</CONDITION-ACCESS>
</VARIATION-POINT-PROXY>
<VARIATION-POINT-PROXY UUID="af1f057b-45e6-58f7-7e12-b66857813de6">
<SHORT-NAME>LINEAR</SHORT-NAME>
<CATEGORY>CONDITION</CATEGORY>
<CONDITION-ACCESS BINDING-TIME="PRE-COMPILE-TIME"><SYSC-REF DEST="SW-SYSTEMCONST">/DataTypes/SystemConstants/MySys</SYSC-REF> == 1</CONDITION-ACCESS>
</VARIATION-POINT-PROXY>
<VARIATION-POINT-PROXY UUID="6ba924d2-49e1-5948-cbd1-c0990240bb21">
<SHORT-NAME>NONLINEAR</SHORT-NAME>
<CATEGORY>CONDITION</CATEGORY>
<CONDITION-ACCESS BINDING-TIME="PRE-COMPILE-TIME"><SYSC-REF DEST="SW-SYSTEMCONST">/DataTypes/SystemConstants/MySys</SYSC-REF> == 2</CONDITION-ACCESS>
</VARIATION-POINT-PROXY>
</VARIATION-POINT-PROXYS>
In the RTE compatible C code, the values of
kare enclosed in preprocessor conditionals#ifand#elif. When you compile this code, Simulink® evaluates the condition expressions. Based on the condition expression that evaluates totrue, the gain value associated with that condition logic becomes active and compiles the code only for that gain value. You can then change the value of the variant control variableMySys to compile the code for a different gain parameter value. You are not required to regenerate the code for a different value of gain.
Parameters rtP = {
#if Rte_SysCon_ADAPTIVE || Rte_SysCon_LINEAR || Rte_SysCon_NONLINEAR
/* Variable: k
* Referenced by: '<Root>/Gain'
*/
#if Rte_SysCon_ADAPTIVE
10.0
#elif Rte_SysCon_LINEAR
1.0
#elif Rte_SysCon_NONLINEAR
3.0
#endif
#define PARAMETERS_VARIANT_EXISTS
#endif
#ifndef PARAMETERS_VARIANT_EXISTS
0
#endif /* PARAMETERS_VARIANT_EXISTS undefined */
};
Specify Variant Parameters at Postbuild Time
This example shows how to generate a runnable for an AUTOSAR element that runs for different sets of variant parameter values without needing to recompile the code for each set of values. In the generated code, the variant parameter values are enclosed in regular if conditions that enable you to switch between the values at model startup.
For example, here is an AUTOSAR component model that contains a variant parameter, k, which models multiple values for the Gain block. Open the model by entering the following code at the MATLAB® command line:

This parameter defines multiple values for the Gain parameter and associates each value with a variant condition logic. You can specify the variant condition logic as an expression or a Simulink.VarianExpression object containing an expression.
ADAPTIVE = Simulink.VariantExpression('MyPBCrit == 10'); LINEAR = Simulink.VariantExpression('MyPBCrit == 1'); NONLINEAR = Simulink.VariantExpression('MyPBCrit == 2'); k = Simulink.VariantVariable('Choices',{'ADAPTIVE', 10 ,'LINEAR',1,'NONLINEAR',3});
The value of MyPBCrit determines the active value of k.
MyPBCrit = Simulink.VariantControl('Value', 1, 'ActivationTime', 'startup');
When you generate code for the model:
In the ARXML code, the variant choices appear as
VARIATION-POINT-PROXYentries with short-namesADAPTIVE,LINEAR, andNONLINEAR.MyPBCritappears as a system constant representing the associated condition value.<VARIATION-POINT-PROXYS> <VARIATION-POINT-PROXY UUID="e773053e-d2a7-568c-768b-fee924d1fad6"> <SHORT-NAME>ADAPTIVE</SHORT-NAME> <CATEGORY>CONDITION</CATEGORY> <POST-BUILD-VARIANT-CONDITIONS> <POST-BUILD-VARIANT-CONDITION> <MATCHING-CRITERION-REF DEST="POST-BUILD-VARIANT-CRITERION">/DataTypes/PostBuildCriterions/MyPBCrit</MATCHING-CRITERION-REF> <VALUE>10</VALUE> </POST-BUILD-VARIANT-CONDITION> </POST-BUILD-VARIANT-CONDITIONS> </VARIATION-POINT-PROXY> <VARIATION-POINT-PROXY UUID="3ce69abb-b974-591d-c7a8-180c64bedfb5"> <SHORT-NAME>LINEAR</SHORT-NAME> <CATEGORY>CONDITION</CATEGORY> <POST-BUILD-VARIANT-CONDITIONS> <POST-BUILD-VARIANT-CONDITION> <MATCHING-CRITERION-REF DEST="POST-BUILD-VARIANT-CRITERION">/DataTypes/PostBuildCriterions/MyPBCrit</MATCHING-CRITERION-REF> <VALUE>1</VALUE> </POST-BUILD-VARIANT-CONDITION> </POST-BUILD-VARIANT-CONDITIONS> </VARIATION-POINT-PROXY> <VARIATION-POINT-PROXY UUID="b4b96126-4744-5093-360b-3965883aeeda"> <SHORT-NAME>NONLINEAR</SHORT-NAME> <CATEGORY>CONDITION</CATEGORY> <POST-BUILD-VARIANT-CONDITIONS> <POST-BUILD-VARIANT-CONDITION> <MATCHING-CRITERION-REF DEST="POST-BUILD-VARIANT-CRITERION">/DataTypes/PostBuildCriterions/MyPBCrit</MATCHING-CRITERION-REF> <VALUE>2</VALUE> </POST-BUILD-VARIANT-CONDITION> </POST-BUILD-VARIANT-CONDITIONS> </VARIATION-POINT-PROXY> </VARIATION-POINT-PROXYS>In the RTE compatible C code, the values of
kare enclosed in regularifconditions. When you execute the runnable built from this code, Simulink evaluates the condition expressions. Based on the condition expression that evaluates totrue, the gain value associated with that condition logic becomes active and the runnable executes only for that gain value. You can then change the value of the variant control variableMyPBCritto execute the runnable for a different gain parameter value. You are not required to recompile the code to build the runnable for a different gain parameter value.void mBasic_Init(void) { /* Variant Parameters startup activation time */ if (Rte_PbCon_ADAPTIVE()) { rtP.k = 10.0; } else if (Rte_PbCon_LINEAR()) { rtP.k = 1.0; } else if (Rte_PbCon_NONLINEAR()) { rtP.k = 3.0; } }
See Also
Use Variant Parameters to Reuse Block Parameters with Different Values