Main Content

Variant Subsystems

该模型说明了Simulink®变体子系统。金宝app变体子系统可让您为一个子系统提供多个实现,其中在模拟过程中只有一个实现活动。您可以通过编程方式交换活动实现,并在不修改模型的情况下将其替换为其他实现之一。

变体子系统的概述

一个变体子系统块包含两个或多个儿童子系统,其中一个孩子在模型执行过程中处于活动状态。活跃的儿童子系统称为主动变体。您可以通过更改全局工作空间中变量的值或使用“变体子系统块”对话框的变量选择,通过更改变量的值来编程切换变体子系统块的活动变体。这积极的变体在模型汇编过程中通过Simulink编程与变体子系统的内部和外口块相连。金宝app

以编程方式控制变体选择,一个Simulink.Variant对象与“变体子系统块”对话框中的每个子子系统关联。Simulink.Variant对象是created in the MATLAB® global workspace. These objects have a property named健康)状况, which is an expression, that evaluates to a boolean value and is used to determine the active variant child subsystem.

笔记:You can specify variant controls in the MATLAB® global workspace, model workspace, mask workspace, or a data dictionary.

例如,定义VSS_LINEAR_CONTROLLER=Simulink.Variant('VSS_MODE==1');

in the global workspace creates aSimulink.Variantobject where the constructor argument('VSS_MODE==1')定义何时活跃。使用变体子系统对话框,然后关联VSS_LINEAR_CONTROLLERwith one of the child subsystems within the Variant Subsystem. Defining

VSS_MODE=1

在全球工作区中,激活VSS_LINEAR_CONTROLLER变体。条件参数可以是simple表达consisting of scalar variables, enumerations, equality, inequality, &&, | | , and ~. Parenthesis () can be used for precedence grouping.

Using Variant Subsystems

此示例中的模型使用以下变体对象和变体控制变量,该变量在MATLAB全局工作区中定义:

VSS_LINEAR_CONTROLLER=Simulink.Variant('VSS_MODE==1');

vss_nonlinear_controller 金宝app= simulink.variant('vss_mode == 2');

VSS_MODE=2;

Opening the example modelsldemo_variant_subsystems运行预载体defined in文件 - > modelProperties->回调。This populates the global workspace with the variables for the Variant Subsystem block named Controller:

图1:这example model,sldemo_variant_subsystems

To specify theSimulink.Variant控制器子系统的对象关联,右键单击控制器子系统,然后选择Subsystem Parameters, which will open the控制器子系统块对话框

控制器子系统块对话框指定了两个潜在变体。这两个变体又与两个变体相关联Simulink.Variant对象VSS_LINEAR_CONTROLLERandVSS_NONLINEAR_CONTROLLER在全球存在的工作区。这些objects have a property named健康)状况, an expression that evaluates to a boolean and that determines which variant is active. The condition is also shown in the Variant Subsystem block dialog. In this example, the Condition properties ofVSS_LINEAR_CONTROLLERandVSS_NONLINEAR_CONTROLLERVSS_MODE== 1andVSS_MODE== 2, respectively. The variableVSS_MODEresides in the global workspace, and can be a standard MATLAB variable or aSimulink.Parameter

If there is no associated variant object or a '%' (comment) character prefixes the variant object in the Variant Subsystem parameters dialog box, then the child subsystem is considered commented out and is not used during model execution.

图2:控制器子系统块的内容

在变体子系统块中,您可以放置​​Inport,Outport和子系统块。在此示例中,Linear Controller子系统块与变体对象关联,VSS_LINEAR_CONTROLLER,和非线性控制器子系统块与变体对象关联,VSS_NONLINEAR_CONTROLLER

在变体子系统中不允许信号连接。金宝app在模拟模型时,Simulink编程将内部和外口块固定到活动变体中。

Switching Active Variants

To simulate using theLinear Controller变体,定义VSS_MODE=1在全局工作区中,然后模拟模型。

To simulate using the非线性控制器, defineVSS_MODE=2在全局工作区中,然后模拟模型。

枚举和重复使用

sldemo_variant_subsystems_enummodel illustrates the followingSimulink.Variant功能:

1。Enumerations:MATLAB enumeration classes can be used to improve readability in the conditions of the variant object.

2。Reuse:Simulink.Variant对象可以在不同的变体子系统块中重复使用。

This example uses following variables which are defined in the MATLAB global workspace:

vsse_linear_controller 金宝app= simulink.variant(...'vsse_mode == sldemo_vss_controller_type.linear')

VSSE_NONLINEAR_CONTROLLER=Simulink.Variant( ... 'VSSE_MODE==sldemo_vss_CONTROLLER_TYPE.NONLINEAR')

VSSE_MODE=sldemo_vss_CONTROLLER_TYPE.LINEAR

vsse_protype = 金宝appsimulink.variant(...'vsse_mode_build == sldemo_vss_build_type.prototype')

vsse_production 金宝app= simulink.variant(...'vsse_mode_build == sldemo_vss_build_type.production')

VSSE_MODE_BUILD=sldemo_vss_BUILD_TYPE.PRODUCTION

在这些Simulink.Variant对象, we use the enumeration classes,sldemo_vss_BUILD_TYPE.m, andsldemo_vss_controller_type.m定义Simulink.Variant健康)状况可提高可读性的参数。

三个滤镜变体子系统块,滤镜1,滤镜2和滤镜3都使用VSSE_PrototypeandVSSE_PRODUCTIONSimulink.Variant对象。

笔记:这name of the enumeration class must be unique among data type names and global workspace variable names, and is case-sensitive.

Opening the example modelsldemo_variant_subsystems_enum运行预载体defined in文件 - > modelProperties->回调。This populates the global workspace with variables for the Variant Subsystem blocks:

Figure 3:这example model,sldemo_variant_subsystems_enum

Code Generation Using Enumerated types as Variant Control Variables

You can use enumerated types to give meaningful names to integers used as variant control values.

考虑模型rtwdemo_preprocessor_subsys

In the MATLAB Editor, define the classes that map enumerated values to meaningful names.

如果未正确定义枚举类型,将显示错误。这是几个场景,导致错误。

Invalid definition1:In this case, theSimulink.IntEnumType没有定义。

Invalid definition2:In this case, the variables are not initialized.

Enter the variant control expression as shown in the next example:

Figure 4:Block Parameters

Define the value of V in the global workspace. For example, V=2;. The value can be a normal MATLAB variable or aSimulink.Parameter目的。但是,该值不能是枚举类型。

Now generate code with变体激活时间设置代码编译。Sample code is as shown below.

Figure 5:生成的代码

有关使用的信息Simulink.VariantorSimulink.Parameter对象或MATLAB变量作为变体控制变量,请参阅Approaches for Specifying Variant Controlssection inIntroduction to Variant Controls

有关变体子系统代码生成的更多信息,请参见嵌入式编码器文档。

更多关于

Variant System Design