生成子系统与C代码嵌套的变体ode Compile and Startup Activation
This example illustrates howcode compile
andstartup
variant activation time can be used together in two nested variant subsystems.
Model
Open the modelslexNestedVSSWithCCAndST.slx
. The model contains a Variant Subsystem blockEngine Controller
with two choicesDiesel Engine
andPetrol Engine
with the conditionsENGINE_TYPE == 1
andENGINE_TYPE == 2
respectively. TheVariant activation time
parameter of theEngine Controller
block is set tocode compile
.The two variantsDiesel Engine
andPetrol Engine
havestartup
as theVariant activation time
parameter. Each engine type has two choicesEffficient Mode
andNormal Mode
. ThePetrol Engine
has the conditionsPMODE == 1
andPMODE == 2
, and theDiesel Engine
has the conditionsDMODE == 1
andDMODE == 2
. One of the engine types can be selected while building the generated code and one of the controller modes can be selected before the start of execution of the code.
Both the variant subsystems havePropagate conditions outside of variant subsystemandAllow zero active variant controlsturned off. When these flags are turned on, this model will error out during compilation as the conditions withcode compile
andstartup
variant activation time will get mixed for the Inport and Outport blocks. For more information, seeConsiderations and Limitations for startup Variant Activation Time
open_system('slexNestedVSSWithCCAndST.slx');
Generate Code
Click onGenerate Code Using Embedded Coderto generate code with ERT target. In the generated code, you can see the variant conditions related toEngine Controller
appear with the preprocessor#if
conditions and the conditions related to the mode appear as regularif
.
The mix ofcode compile
andstartup
变体激活*使您可以微调the variant selection. In the generated executable, you can have either thePetrol Engine
controller or theDiesel Engine
controller. At startup, you can decide if you want theEfficient Mode
or theNormal Mode
of eitherPetrol Engine
orDiesel Engine
controllers. To do this, write a custom code which can help to select different variants atstartup
activation time in theSystem Initialize
block. To select different variants, navigate toModel Settings > Code Generation > Custom Code. Specify theHeader file
andSource file
as#include "ChangeDieselController.h"
andChangeDieselController.c
respectively.
TheChangeDieselController.c
is responsible for selecting the startup variant in the initialize function.
#include "rtwtypes.h" extern int32_T DMODE; void ChangeDieselEngineController() { // select the diesel controller at startup. // This is just an example, so the value is hard coded. // In practical scenario, this can read value from sensor and // decide which controller you want at startup. DMODE = 1; }
With the help of this code, you can select the startup variant in themodel_initialize
function and generate code.
voidslexNestedVSSWithCCAndST_initialize(void){ ChangeDieselEngineCOntroller(); utAssert((DMODE == 1) + (DMODE == 2) == 1); utAssert((PMODE == 1) + (PMODE == 2) <= 1); slexNestedVSSWithCCAndST_DW.systemEnable = 1; }