Documentation

System Design in Simulink Using System Objects

System Design and Simulation in Simulink

You can use System objects in your model to simulate in Simulink®.

  1. Create a System object™ to be used in your model. SeeDefine New System Objects for Use in Simulinkfor information.

  2. Test your new System object in MATLAB®. SeeTest New System Objects in MATLAB

  3. Add the System object to your model by using theMATLAB Systemblock. SeeAdd System Objects to Your Simulink Modelfor information.

  4. Add other Simulink blocks as needed and connect the blocks to construct your system.

  5. Run the system

Define New System Objects for Use in Simulink

A System object is a component you can use to create your system in MATLAB. You can write the code in MATLAB and use that code to create a block in Simulink. To define your own System object, you write a class definition file, which is a text-based MATLAB file that contains the code defining your object. SeeSystem Object Integration.

Define System Object with Block Customizations

Create a System object for use in Simulink. The example performs system identification using a least mean squares (LMS) adaptive filter.

创建一个类定义文本文件来定义your System object. The code in this example creates a least mean squares (LMS) filter and includes customizations to the block icon and dialog box appearance. It is similar to theSystem Identification Using MATLAB System BlocksSimulink example.

Note

Instead of manually creating your class definition file, you can use theNew>System Object>Simulink Extensionmenu option to open a template. This template includes customizations of the System object for use in the SimulinkMATLAB Systemblock. You edit the template file, using it as guideline, to create your own System object.

On the first line of the class definition file, specify the name of your System object and subclass from bothmatlab.Systemandmatlab.system.mixin.CustomIcon. Thematlab.Systembase class enables you to use all the basic System object methods and specify the block input and output names, title, and property groups. TheCustomIconmixin class enables the method that lets you specify the block icon.

Add the appropriate basic System object methods to set up, reset, set the number of inputs and outputs, and run your algorithm. See the reference pages for each method and the full class definition file below for the implementation of each of these methods.

  • Use thesetupImplmethod to perform one-time calculations and initialize variables.

  • Use thestepImplmethod to implement the block’s algorithm.

  • Use theresetImplmethod to reset the state properties orDiscreteStateproperties.

  • Use thegetNumInputsImplandgetNumOutputsImplmethods to specify the number of inputs and outputs, respectively.

Add the appropriateCustomIconmethods to define the appearance of theMATLAB Systemblock in Simulink. See the reference pages for each method and the full class definition file below for the implementation of each of these methods.

  • Use thegetHeaderImplmethod to specify the title and description to display on the block dialog box.

  • Use thegetPropertyGroupsImplmethod to specify groups of properties to display on the block dialog box.

  • Use thegetIconImplmethod to specify the text to display on the block icon.

  • Use thegetInputNamesImplandgetOutputNamesImplmethods to specify the labels to display for the block input and output ports.

The full class definition file for the least mean squares filter is:

classdeflmsSysObj < matlab.System &...matlab.system.mixin.CustomIcon% lmsSysObj Least mean squares (LMS) adaptive filtering.% #codegenproperties% Mu Step sizeMu = 0.005;endproperties(Nontunable)% Weights Filter weightsWeights = 0;% N Number of filter weightsN = 32;endproperties(DiscreteState) X; H;endmethods(一个ccess = protected)functionsetupImpl(obj) obj.X = zeros(obj.N,1); obj.H = zeros(obj.N,1);endfunction[y, e_norm] = stepImpl(obj,d,u) tmp = obj.X(1:obj.N-1); obj.X(2:obj.N,1) = tmp; obj.X(1,1) = u; y = obj.X'*obj.H; e = d-y; obj.H = obj.H + obj.Mu*e*obj.X; e_norm = norm(obj.Weights'-obj.H);endfunctionresetImpl(obj) obj.X = zeros(obj.N,1); obj.H = zeros(obj.N,1);endend% Block icon and dialog customizationsmethods(Static, Access = protected)functionheader = getHeaderImpl header = matlab.system.display.Header(...'lmsSysObj',...'Title','LMS Adaptive Filter');endfunctiongroups = getPropertyGroupsImpl upperGroup = matlab.system.display.SectionGroup(...'Title','General',...'PropertyList',{'Mu'});lowerGroup = matlab.system.display.SectionGroup(...'Title','Coefficients',...'PropertyList',{'Weights',“N”});组= [upperGroup, lowerGroup];endendmethods(一个ccess = protected)functionicon = getIconImpl(~) icon = sprintf('LMS Adaptive\nFilter');endfunction[in1name, in2name] = getInputNamesImpl(~) in1name ='Desired'; in2name ='Actual';endfunction[out1name, out2name] = getOutputNamesImpl(~) out1name ='Output'; out2name ='EstError';endendend

定义与间接直通的系统对象

Create a System object for use in Simulink. The example performs system identification using a least mean squares (LMS) adaptive filter and uses feedback loops.

创建一个类定义文本文件来定义your System object. The code in this example creates an integer delay and includes feedback loops, and customizations to the block icon. For information on feedback loops, seeUse System Objects in Feedback Loops. This example implements a System object that you can use for nondirect feedthrough. It is similar to theSystem Identification Using MATLAB System BlocksSimulink example.

On the first line of the class definition file, subclass frommatlab.Systemandmatlab.system.mixin.CustomIcon. Thematlab.Systembase class enables you to use all the basic System object methods and specify the block input and output names, title, and property groups. TheCustomIconmixin class enables the method that lets you specify the block icon. TheNondirectmixin enables the methods that let you specify how the block is updated and what it outputs.

Add the appropriate basic System object methods to set up and reset the object and set and validate the properties. Since this object supports nondirect feedthrough, you do not implement thestepImplmethod. You implement theupdateImplandoutputImplmethods instead. See the reference pages for each method and the full class definition file below for the implementation of each of these methods.

  • Use thesetupImplmethod to initialize some of the object’s properties.

  • Use theresetImplmethod to reset the property states.

  • Use thevalidatePropertiesImplmethod to check that the property values are valid.

Add the followingNondirectmixin class methods instead of thestepImplmethod to specify how the block updates its state and its output. See the reference pages and the full class definition file below for the implementation of each of these methods.

  • Use theoutputImplmethod to implement code to calculate the block output.

  • Use theupdateImplmethod to implement code to update the block’s internal states.

  • Use theisInputDirectFeedthroughImplmethod to specify that the block is not direct feedthrough. Its inputs do not directly affect its outputs.

Add thegetIconImplmethod to define the block icon when it is used in Simulink via the MATLAB System block. See the reference page and the full class definition file below for the implementation of this method.

The full class definition file for the delay is:

classdefintDelaySysObj < matlab.System &...matlab.system.mixin.Nondirect &...matlab.system.mixin.CustomIcon% intDelaySysObj Delay input by specified number of samples.% #codegenproperties% InitialOutput Initial outputInitialOutput = 0;endproperties(Nontunable)% NumDelays Number of delaysNumDelays = 1;endproperties(DiscreteState) PreviousInput;endmethods(一个ccess = protected)functionsetupImpl(obj, ~) obj.PreviousInput = ones(1,obj.NumDelays)*obj.InitialOutput;endfunction[y] = outputImpl(obj, ~)% Output does not directly depend on inputy = obj.PreviousInput(end);endfunctionupdateImpl(obj, u) obj.PreviousInput = [u obj.PreviousInput(1:end-1)];endfunctionflag = isInputDirectFeedthroughImpl(~,~) flag = false;endfunctionvalidatePropertiesImpl(obj)if((numel(obj.NumDelays)>1) || (obj.NumDelays <= 0)) error('Number of delays must be positive non-zero ...scalarvalue.');endif(numel(obj.InitialOutput)>1) error('Initial output must be scalar value.');endendfunctionresetImpl(obj) obj.PreviousInput = ones(1,obj.NumDelays)*obj.InitialOutput;endfunctionicon = getIconImpl(~) icon = sprintf('Integer\nDelay');endendend

Test New System Objects in MATLAB

  1. Create an instance of your new System object. For example, create an instance of thelmsSysObj.

    s = lmsSysObj;

  2. Run the object multiple times with different inputs. Doing this step tests for syntax errors and other possible issues before you add it to Simulink. For example,

    desired = 0; actual = 0.2; s(desired,actual);

Add System Objects to Your Simulink Model

  1. Add your System objects to your Simulink model by using theMATLAB Systemblock as described inMapping System Objects to Block Dialog Box.

  2. Add other Simulink blocks, connect them, and configure any needed parameters to complete your model as described in the Simulink documentation. See theSystem Identification for an FIR System Using MATLAB System BlocksSimulink example.

  3. Run your model in the same way you run any Simulink model.

Was this topic helpful?