In at present’s submit, I’ll add options to the framework introduced within the earlier submit to assist controlling variants.
In case you missed the earlier posts on this sequence, listed here are hyperlinks to all posts:
Variant slPart Block
When I’ll copy this block to fashions, I will add to it as many algorithm variants as I would like.
Including Variant Specification to slPart
The objective right here is to pick the lively variant primarily based on the category of the article, which is specified as a masks parameter. For that, I add the next code to the maskInit methodology of slPart:
classdef slPart < deal with
properties (Hidden = true)
BlockPath char
finish
strategies
operate obj = maskInit(obj,blk)
obj.BlockPath = blk;
% If the block has variants, set lively variant primarily based on the article class
if strcmp(get_param(blk,‘Variant’),‘on’)
variantChoices = get_param(blk,‘VariantChoices’); % Get all variant selections
variantChoices = {variantChoices.Title}’; % Extract the labels
classList = [class(obj);superclasses(obj)]; % Get the listing of sophistication and superclasses
matchingChoices = intersect(classList,variantChoices); % Discover the label that matches the category or closest superclass
set_param(blk,‘LabelModeActiveChoice’,matchingChoices{1}); % Apply the primary match
finish
finish
finish
finish
Implementing a Variant slPart
Let’s hold constructing on the spring instance from the earlier submit. I add my template block to a mannequin and create two variants, one is similar linear spring as within the earlier instance, and the opposite is a non-linear model:
I create a brand new slPart class for the non-linear model and put it aside within the +springLib package deal created within the earlier submit. Any more, I’ll seek advice from this class as springLib.springNonLinear:
classdef springNonLinear < slPart
properties
m
displacementBreakPoints;
stiffnessTable;
velocityBreakPoints;
dampingTable;
finish
strategies
operate obj = springNonLinear()
obj.m = 1;
obj.displacementBreakPoints = -5:5;
obj.stiffnessTable = tanh(-5:5);
obj.velocityBreakPoints = -5:5;
obj.dampingTable = 2*tanh(-5:5);
finish
finish
finish
Now, the important thing factor for the masks initialization methodology to have the ability to set the right variant is to set the label of every variant to the identify of corresponding class.
Extra Variant Parameterizations
With these saved within the +springLib package deal folder, I now have doubled the scale of my library of springs, which I can nonetheless entry by tab-completion:
Choosing a Half and Simulating the Corresponding Variant
As soon as the mannequin is correctly configured and the MATLAB lessons have been created, I can assign an object of the specified kind to the variable specified within the block masks dialog and Simulink will routinely use the suitable variant subsystem throughout simulation.
I like the truth that, if I give this mannequin to somebody, all that individual must do is to select a spring from the springLib package deal. No have to know or care about the truth that it is likely to be activating a distinct variant subsystem beneath the hood.
That method, you inherit a element the place the inside mechanics have been abstracted so you possibly can concentrate on utilizing it and never be distracted by implementation particulars.
Now it is your flip
Clearly, what I confirmed right here is the naked minimal to get this system working. For a sensible challenge, I might add validation code and error checking to higher information the person in case one thing goes fallacious.
What do you consider this method to variants? Did you or your organization got here up with different methods to systematically management variants? I might have an interest to listen to how the usability compares with what I described on this submit.
Tell us within the feedback under.