Main Content

生成子系统与C代码嵌套的变体ode Compile and Startup Activation

This example illustrates howcode compileandstartupvariant activation time can be used together in two nested variant subsystems.

Model

Open the modelslexNestedVSSWithCCAndST.slx. The model contains a Variant Subsystem blockEngine Controllerwith two choicesDiesel EngineandPetrol Enginewith the conditionsENGINE_TYPE == 1andENGINE_TYPE == 2respectively. TheVariant activation timeparameter of theEngine Controllerblock is set tocode compile.The two variantsDiesel EngineandPetrol Enginehavestartupas theVariant activation timeparameter. Each engine type has two choicesEffficient ModeandNormal Mode. ThePetrol Enginehas the conditionsPMODE == 1andPMODE == 2, and theDiesel Enginehas the conditionsDMODE == 1andDMODE == 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 compileandstartupvariant 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 Controllerappear with the preprocessor#ifconditions and the conditions related to the mode appear as regularif.

The mix ofcode compileandstartup变体激活*使您可以微调the variant selection. In the generated executable, you can have either thePetrol Enginecontroller or theDiesel Enginecontroller. At startup, you can decide if you want theEfficient Modeor theNormal Modeof eitherPetrol EngineorDiesel Enginecontrollers. To do this, write a custom code which can help to select different variants atstartupactivation time in theSystem Initializeblock. To select different variants, navigate toModel Settings > Code Generation > Custom Code. Specify theHeader fileandSource fileas#include "ChangeDieselController.h"andChangeDieselController.crespectively.

TheChangeDieselController.cis 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_initializefunction and generate code.

voidslexNestedVSSWithCCAndST_initialize(void){ ChangeDieselEngineCOntroller(); utAssert((DMODE == 1) + (DMODE == 2) == 1); utAssert((PMODE == 1) + (PMODE == 2) <= 1); slexNestedVSSWithCCAndST_DW.systemEnable = 1; }