Sunday, October 13, 2024
HomeMatlabUnifying MATLAB and Simulink: A Consumer Story Half 2 » Man on...

Unifying MATLAB and Simulink: A Consumer Story Half 2 » Man on Simulink


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.

This class permits me to outline properties and strategies frequent to all elements. I’m making it a deal with class, which permits each MATLAB code and Simulink blocks to consult with and work together with slPart objects forwards and backwards.

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)

BlockPath char

finish

strategies

operate obj = maskInit(obj,blk)

obj.BlockPath = blk;

finish

finish

finish

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:

Lastly, since I need customers to edit blocks created from this template, I break the hyperlink to the library once I create a duplicate of this block. For that, I leverage the ClipboardFcn callback:

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

properties

m

okay

c

finish

strategies

operate obj = spring()

obj.m = 0.5;

obj.okay = 5;

obj.c = 3;

finish

finish

finish

For comfort, I saved this spring class in a bundle folder named +springLib. I’ll describe why under. For now which means I’ll consult with this class as springLib.spring.

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.

mySpring.BlockPath

I’ll leverage this functionality in following posts.

Creating Information Variants

The following factor we are able to do is create a library of springs, like those you might purchase from the catalog of an organization promoting springs. They may all use the identical algorithm, however with completely different parameter values. For that, we are able to use our generic spring class as a template to create a number of subclasses inheriting from the spring superclass.

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.

For comfort, it’s doable to position all these knowledge variants lessons in a bundle folder.

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.

You possibly can obtain the present standing of the challenge right here.

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments