针对支持和不支持的模块和参数定义模型顾问检查
您可以创建模型顾问检查,检查模块是否使用特定的模块或参数值。您可以为以下各项指定约束:
支持或不支持的模块参数值
支持或不支持的模型参数值
支持或不支持的模块
模块或参数是否满足约束组合
您也可以使用 addPreRequisiteConstraintID
函数添加前提条件约束,这些约束必须在模型顾问检查实际约束之前通过。您可以在编辑时或通过从模型顾问运行检查来对照这些约束检查模型。
示例
sldemo_bounce
模型对在地面上弹跳的弹球进行仿真。在此示例中,您创建两个由约束组成的模型顾问检查,然后对照这些约束检查模型。
创建对支持或不支持的模块参数的检查
首先,创建一个模型顾问检查,该检查包含三个模块参数约束 c1
、c2
和 c3
,它们指定支持和不支持的模块参数值。
1.定义一个新函数。
function constraints = createConstraints_Check1() end
2.在该函数内部,创建两个模块参数约束 c1
和 c2
。
function constraints = createConstraints_Check1() c1=Advisor.authoring.PositiveBlockParameterConstraint; c1.ID='ID_1'; c1.BlockType='Gain'; c1.ParameterName='Gain'; c1.SupportedParameterValues={'-0.7'}; c1.ValueOperator='eq'; % equal to c2=Advisor.authoring.NegativeBlockParameterConstraint; c2.ID='ID_2'; c2.BlockType='InitialCondition'; c2.ParameterName='Value'; c2.UnsupportedParameterValues={'0'}; c2.ValueOperator='le'; % less than or equal to end
约束 c1
指定 Gain 模块的值必须等于 -0.7
。约束 c2
指定不支持值小于或等于零的 Initial Condition 模块。
3.创建一个正向模块约束 c3
,并将 constraints
设置为一个包含约束 c1
、c2
和 c3
的元胞数组。
function constraints = createConstraints_Check1() c1=Advisor.authoring.PositiveBlockParameterConstraint; c1.ID='ID_1'; c1.BlockType='Gain'; c1.ParameterName='Gain'; c1.SupportedParameterValues={'-0.7'}; c1.ValueOperator='eq'; % equal to c2=Advisor.authoring.NegativeBlockParameterConstraint; c2.ID='ID_2'; c2.BlockType='InitialCondition'; c2.ParameterName='Value'; c2.UnsupportedParameterValues={'0'}; c2.ValueOperator='le'; % less than or equal to c3=Advisor.authoring.PositiveBlockTypeConstraint; c3.ID='ID_3'; s1=struct('BlockType','Constant','MaskType',''); s2=struct('BlockType','SubSystem','MaskType',''); s3=struct('BlockType','InitialCondition','MaskType',''); s4=struct('BlockType','Gain','MaskType',''); s5=struct('BlockType','Memory','MaskType',''); s6=struct('BlockType','SecondOrderIntegrator','MaskType',''); s7=struct('BlockType','Terminator','MaskType',''); c3.SupportedBlockTypes={s1;s2;s3;s4;s5;s6;s7;}; constraints = {c1,c2,c3}; end
约束 c3
指定支持的模块。constraints
是模块约束的元胞数组。
4.通过创建另一个函数 check1
来定义新的模型顾问检查。使用函数 Advisor.authoring.createBlockConstraintCheck
创建具有这些模块约束的模型顾问检查 rec
。然后使用 mdladvRoot.register(rec)
向模型顾问注册模块约束检查。
function check1() rec = Advisor.authoring.createBlockConstraintCheck('mathworks.check_0001',... 'Constraints',@createConstraints_Check1); rec.Title = 'Example 1: Check three block parameter constraints'; rec.TitleTips = 'Example check three block parameter constraints'; mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec,'Example: My Group') end function constraints = createConstraints_Check1() c1=Advisor.authoring.PositiveBlockParameterConstraint; c1.ID='ID_1'; c1.BlockType='Gain'; c1.ParameterName='Gain'; c1.SupportedParameterValues={'-0.7'}; c1.ValueOperator='eq'; % equal to c2=Advisor.authoring.NegativeBlockParameterConstraint; c2.ID='ID_2'; c2.BlockType='InitialCondition'; c2.ParameterName='Value'; c2.UnsupportedParameterValues={'0'}; c2.ValueOperator='le'; % less than or equal to c3=Advisor.authoring.PositiveBlockTypeConstraint; c3.ID='ID_3'; s1=struct('BlockType','Constant','MaskType',''); s2=struct('BlockType','SubSystem','MaskType',''); s3=struct('BlockType','InitialCondition','MaskType',''); s4=struct('BlockType','Gain','MaskType',''); s5=struct('BlockType','Memory','MaskType',''); s6=struct('BlockType','SecondOrderIntegrator','MaskType',''); s7=struct('BlockType','Terminator','MaskType',''); c3.SupportedBlockTypes={s1;s2;s3;s4;s5;s6;s7;}; constraints = {c1,c2,c3}; end
为复合约束创建检查
接下来,创建一个模型顾问检查,其中包含三个模块参数约束 cc1
、cc2
和 cc
。约束 cc1
和 cc2
指定支持哪些模块参数,约束 cc
是复合约束,包含 cc1
和 cc2
。
1.定义一个新函数。
function constraints = createConstraints_Check2() end
2.创建两个模块参数约束 cc1
和 cc2
以及一个复合约束 cc
。将 constraints
设置为包含约束 cc1
、cc2
和 cc
的元胞数组。
function constraints = createConstraints_Check2() cc1=Advisor.authoring.PositiveBlockParameterConstraint; cc1.ID='ID_cc1'; cc1.BlockType='SecondOrderIntegrator'; cc1.ParameterName='UpperLimitX'; cc1.SupportedParameterValues={'inf'}; cc1.ValueOperator='eq'; % equal to cc2=Advisor.authoring.PositiveBlockParameterConstraint; cc2.ID='ID_cc2'; cc2.BlockType='SecondOrderIntegrator'; cc2.ParameterName='LowerLimitX'; cc2.SupportedParameterValues={'0.0'}; cc2.ValueOperator='eq'; % equal to cc=Advisor.authoring.CompositeConstraint; cc.addConstraintID('ID_cc1'); cc.addConstraintID('ID_cc2'); cc.CompositeOperator='and'; % Model Advisor checks multiple constraints constraints = {cc1,cc2,cc}; end
约束 cc1
指定对于 Second-Order Integrator 模块,UpperLimitX 参数的值必须等于 inf
。约束 cc2
还指定 LowerLimitX 参数的值必须等于零。约束 cc
指定要通过此检查,cc1
和 cc2
必须同时通过。constriants
是模块约束的元胞数组。
3.在新函数 check2
中定义一个新的模型顾问检查。使用函数 Advisor.authoring.createBlockConstraintCheck
为函数 createConstraints_Check2
定义的模块约束创建一个模型顾问检查。
function check2() rec = Advisor.authoring.createBlockConstraintCheck('mathworks.check_0002',... 'Constraints',@createConstraints_Check2); rec.Title = 'Example 2: Check three block parameter constraints'; rec.TitleTips = 'Example check three block parameter constraints'; mdladvRoot = ModelAdvisor.Root; mdladvRoot.publish(rec,'Example: My Group') end function constraints = createConstraints_Check2() cc1=Advisor.authoring.PositiveBlockParameterConstraint; cc1.ID='ID_cc1'; cc1.BlockType='SecondOrderIntegrator'; cc1.ParameterName='UpperLimitX'; cc1.SupportedParameterValues={'inf'}; cc1.ValueOperator='eq'; cc2=Advisor.authoring.PositiveBlockParameterConstraint; cc2.ID='ID_cc2'; cc2.BlockType='SecondOrderIntegrator'; cc2.ParameterName='LowerLimitX'; cc2.SupportedParameterValues={'0.0'}; cc2.ValueOperator='eq'; cc=Advisor.authoring.CompositeConstraint; cc.addConstraintID('ID_cc1'); cc.addConstraintID('ID_cc2'); cc.CompositeOperator='and'; constraints = {cc1,cc2,cc}; end
创建和运行模型顾问检查
1.要注册新检查,请使用 sl_customization.m
文件。对于此示例,将 sl_customization_DefineChecks
函数和文件重命名为 sl_customization
。
function sl_customization(cm) % register custom checks. cm.addModelAdvisorCheckFcn(@check1); cm.addModelAdvisorCheckFcn(@check2);
2.在命令提示符下,通过键入以下命令创建 Example 1:Check block parameter constraints 和 Example 2:Check block parameter constraints 检查:
Advisor.Manager.refresh_customizations
3.在命令提示符下,打开模型 sldemo_bounce
。
open_system('sldemo_bounce')
4.在建模选项卡中,选择模型顾问以打开模型顾问。
5.在左窗格中,选择按产品 > 示例:My Group。
6.点击运行检查。
Example 1:Check three block parameter constraints 检查会产生警告,因为 Gain 模块的值为 -0.8
。Example 2:Check three block parameter constraints 检查通过,因为 Second-Order Integrator 模块同时满足这两个约束。
使用约束创建模型顾问编辑时检查
您可以使用编辑时检查来突出显示模型画布中违反了模块约束的模块。通过在模型顾问配置编辑器中选择所需的检查并保存自定义配置,可以选择在编辑时检查期间使用哪些模型顾问检查。
1.要打开模型顾问配置编辑器,请打开模型顾问并选择打开 > 打开配置编辑器。
2.您创建的检查出现在按产品 > 示例:My Group 中。
3.对于此示例,删除除示例:我的组文件夹以外的所有文件夹。
4.点击保存并将配置保存为 my_config.json
。
5.在对话框中,点击否,因为您不希望将此配置设置为默认配置。
6.在 Simulink 模型编辑器的建模选项卡上,点击模型顾问 > 编辑时检查。
7.对于模型顾问配置文件参数,点击浏览,然后选择 my_config.json
。
8.选择编辑时检查参数,并关闭“配置参数”对话框。
9.点击模型中突出显示的模块以查看编辑时警告。
您可以在编辑时检查诊断窗口中编辑模块参数值,方法是点击修复按钮,或点击不支持参数的超链接以打开模块参数窗口。
另请参阅
PositiveBlockParameterConstraint
| NegativeBlockParameterConstraint
| PositiveModelParameterConstraint
| NegativeModelParameterConstraint
| PositiveBlockTypeConstraint
| NegativeBlockTypeConstraint
| Advisor.authoring.generateBlockConstraintsDataFile