In my earlier publish, I described how one can parameterize a Simulink mannequin utilizing MATLAB objects. To be trustworthy, there’s nothing magic or revolutionary to that in itself. The explanation why I made a decision to cowl this subject in a sequence of posts is due to what this permits.
On this publish, I’m constructing the inspiration of a framework that, in my view, makes it very handy to find and work together with a Simulink mannequin.
Listed below are hyperlinks to different posts on this sequence:
The Constructing Blocks
This framework will likely be centered round a elementary unit I made a decision to name a Simulink Half or slPart for brief. I exploit the phrase “half” as a result of I like to consider this because the mechanical elements yow will discover on the cabinets of a storage or a machine store: gears, nuts, bolts, and so on. Components could be product of different elements, like a automotive is product of an engine, suspensions, and wheels, and like a suspension is product of springs and dampers.
The fundamental unit of this framework is constructed round two elementary constructing blocks: a MATLAB class and a Simulink masked subsystem.
The superclass: slPart
Let’s start with the MATLAB class, which I’ll title slPart, as in Simulink Half.
Via this sequence of posts, I’ll incrementally add functionalities to this class, however as a place to begin, here’s what the slPart class seems to be like:
classdef slPart < deal with
properties (Hidden = true)
operate obj = maskInit(obj,blk)
obj.BlockPath = blk;
It has one property BlockPath, the place I’ll retailer the trail of the block the place the article is getting used and one technique maskInit which, as its title implies, will likely be used for masks initialization.
The Template Block
For the block, I created a “template” empty Subsystem and saved it in a library.
Utilizing the Masks Editor, I outlined one parameter named “obj”:
To make it possible for solely objects of sophistication slPart are handed to this parameter, I created a Parameter Constraint.
I set the Masks kind to be “slPart” (This may assist me discover situations of this block sooner or later)
and I specify the maskInit technique outlined in slPart as Masks Initialization code:
With that setup, I can copy this library block right into a mannequin and start creating my first Simulink Half!
Making a First slPart
For this primary instance, I’ll reuse the identical spring class as in my earlier publish. The one distinction is that it now subclasses slPart:
classdef spring < slPart
operate obj = spring()
obj.m = 0.5;
obj.okay = 5;
obj.c = 3;
On the Simulink aspect, I drag a duplicate of the template block right into a mannequin, open the empty subsystem and implement my algorithm utilizing the properties of “obj” as block parameters:
I then instantiate a spring object:
mySpring = springLib.spring
and specify mySpring within the block dialog:
With that arrange, I can simulate the mannequin and as soon as the simulation completes, mySpring is aware of the place it has been simulated.
I’ll leverage this functionality in following posts.
Creating Information Variants
This could shortly offer you a catalog of springs. You should use these springs in all types of element fashions, or share this library of springs with collaborators. These collaborators decide a spring from the catalog with out worrying in regards to the parameters or the algorithm below the masks.
This creates a namespace that lets you “scan” the catalog utilizing tab-complete:
With this library or catalog of springs, it turns into simple to simulate a mannequin with completely different parameterizations:
in(1:2) = Simulink.SimulationInput(‘spring_sim’);
in(1) = in(1).setVariable(‘mySpring’,springLib.springN1234);
in(2) = in(2).setVariable(‘mySpring’,springLib.spring_SupplierB_code5);
out = sim(in,‘ShowProgress’,‘off’);
Now it is your Flip
At this level, it is in all probability honest to say that I’ve not launched something actually sport altering. What I did thus far is create the inspiration of a framework that I’ll prolong within the following posts by including strategies and properties to the slPart class that I created at present.
When you’ve got seen the same framework or if the above offers you concepts on what you wish to see added to it, let me know within the feedback under.