Guy on Simulink

金宝app基于模型的设计

统一MATLAB和SIMULINK:用户金宝app故事第2部分

在我的上一篇文章中,我描述了如何使用MATLAB对象参数化simulink模型。金宝app老实说,本身没有什么魔术或革命性的。我之所以决定在一系列帖子中介绍此主题的原因是因为这能做到这一点。
在这篇文章中,我正在建立一个框架的基础,我认为,发现并与Simulink模型进行互动非常方便。金宝app

The Building Blocks

This framework will be centered around a fundamental unit I decided to call a 金宝app 部分 或者 slPart 为短。我用“部分”,因为我喜欢这个词think of this as the mechanical parts you can find on the shelves of a garage or a machine shop: gears, nuts, bolts, etc. Parts can be made of other parts, like a car is made of an engine, suspensions, and wheels, and like a suspension is made of springs and dampers.
该框架的基本单元围绕两个基本构建块建造:MATLAB类和一个Simulink掩盖子系统。金宝app

超类:slpart

Let's begin with the MATLAB class, which I will name slPart ,如 金宝app模拟部分
This class allows me to define properties and methods common to all parts. I am making it a handle class ,这允许MATLAB代码和Simulink块可以参考并与金宝app slPart objects back and forth.
Through this series of posts, I will incrementally add functionalities to this class, but as a starting point, here is what the slPart 课堂看起来像:
Classdefslpart <句柄
properties(隐藏= true)
区块路Char
结尾
methods
功能obj = maskInit(obj,blk)
obj.blockpath = blk;
结尾
结尾
结尾
它有一个财产 区块路 ,,,,where I will store the path of the block where the object is being used and one method maskInit which, as its name implies, will be used for mask initialization.

模板块

For the block, I created a "template" empty Subsystem and stored it in a library.
Using the Mask Editor, I defined one parameter named "obj":
To make sure that only objects of class slPart 被传递给此参数,我创建了一个参数约束。
我将蒙版类型设置为“ slpart”(这将帮助我在将来找到此块的实例)
我指定了 maskInit method defined in slPart 作为掩码初始化代码:
最后,由于我希望用户编辑从此模板创建的块,因此当我创建此块的副本时,我会打破到库的链接。为此,我利用了 ClipboardFCN回调
With that setup, I can copy this library block into a model and begin creating my first Simulink Part!

Creating a First slPart

For this first example, I will reuse the same 春天 Class as in my previous post. The only difference is that it now subclasses slPart
Classdef春天< slPart
properties
m
k
C
结尾
methods
功能obj = spring()
OBJ.M = 0.5;
obj.k = 5;
obj.c = 3;
结尾
结尾
结尾
For convenience, I stored this 春天 Class in a package folder 命名 +Springlib 。I will describe why below. For now this means that I will refer to this class as 春天Lib.spring
在Simuli金宝appnk侧,我将模板块的副本拖到模型中,打开空子系统并使用“ OBJ”的属性作为块参数实现我的算法:
I then instantiate a 春天 目的:
myspring = springlib.spring
myspring=
春天with properties: m: 0.5000 k: 5 c: 3
并指定 myspring 在“块”对话框中:
With that set up, I can simulate the model and once the simulation completes, myspring knows where it has been simulated.
sim('spring_sim');
myspring.blockpath
ans ='Spring_sim/我的第一个春天'
I will leverage this capability in following posts.

创建数据变体

我们可以做的下一件事是创建一个弹簧库,就像您可以从销售泉水公司的目录中购买的弹簧图书馆一样。他们都将使用相同的算法,但具有不同的参数值。为此,我们可以使用我们的通用 春天 类作为创建多个的模板 subclasses inheriting from the 春天 superclass
This can quickly give you a catalog of springs. You can use these springs in all sorts of component models, or share this library of springs with collaborators. Those collaborators pick a spring from the catalog without worrying about the parameters or the algorithm under the mask.
For convenience, it is possible to place all those data variants classes in a package folder
This creates a namespace that enables you to "scan" the catalog using tab-complete:
With this library or catalog of springs, it becomes easy to simulate a model with different parameterizations:
在(1:2)= si金宝appmulink.simulationInput('spring_sim');
in(1) = in(1).setVariable(',Springlib.springn1234);
in(2) = in(2).setVariable(',springlib.spring_supplierb_code5);
out = sim(in,“表演”,,,,'off');

现在轮到你了

At this point, it's probably fair to say that I have not introduced anything really game changing. What I did so far is create the foundation of a framework that I will extend in the following posts by adding methods and properties to the slPart Class that I created today.
您可以下载项目的当前状态 here
如果您已经看到了类似的框架或以上关于您希望看到的内容的想法,请在下面的评论中告诉我。
|

注释

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.