Learning goals :
- Use simulink Encapsulating a subsystem , And put the encapsulation subsystem into the custom library , Available for reuse in modeling
function : Package an edge detection module which can detect the jumping of the falling edge of the input signal , The module can support double-click to modify internal parameters . When the packaging is complete , Add the encapsulated module to the custom simulink In the library , It is convenient for modeling and daily use
Matlab edition :2018a
1、 The creation of the encapsulation subsystem
1.1 The construction of function model
Create the model shown above , The module realizes the logic function of falling edge detection , among date store memory The module is used to store the last state of the input signal . When the input signal value is 0, And the last input signal saved is 1 when , Detected as falling edge , Output is 1. Because we need to get the last state of the input signal first , So you should first read the saved state of the last input signal , therefore date store memroy The priority of the module must be set to read first and then write , In other words, the above read module has higher priority than saving the current state to date store memory Modules should be high .
The priority can be set by right clicking the module to be modified , Select the next to last line of the pop-up options bar Properties, The dialog box as shown in the following figure will pop up , stay Priority Options to set , The smaller the priority value, the higher the priority . The setting of priority can also be designed by the method described later .
(PS: The color of the module can be customized , Right mouse click ——》 Pop up bar Format——》Background Color)
1.2 Subsystem creation Mask
establish Mask There are a lot of benefits to , For example, when using a module, you can double-click the encapsulated module , In the pop-up dialog box, set some internal parameters . Like the bottom edge detection module to be packaged now , When using, we want to save the state of the last input signal data store memory Module name . here , We can create mask Create a new input field in the , This input field will get the input string and the module internal date store memory Names are linked together . Every time you use it , Double click the module to pop up Mask Enter the input field of the window to Data store memory Name from , It automatically acts inside the subsystem data store memory.
establish Mask front , You have to put 1.1 The function model is encapsulated into a subsystem , adopt Ctrl+G The shortcut key completes . After creating the subsystem , Delete the I / O ports outside the subsystem , As shown below :
Select the subsystem , adopt Ctrl+M Shortcut key creation Mask, Pop up the corresponding mask Setup dialog , The content of the pop-up dialog box is set below
1) Set up Icon&Ports Options
① Block frame: Set whether the frame of the module is visible
② Icon transparency: Set whether the icon is transparent
③ Icon uinits: Set icon units , Normalization is chosen here , That is, scope [0 1]
④ Icon Drawing command settings , Use plot Draw the drop arrow icon in the lower left corner of the dialog box . The drawing command can be supported as follows :
Draw command | explain | Syntax example |
color | Change the drawing color of the subsequent encapsulation icon drawing command | color('red'); port_label('input',1,'In') |
disp | Show text on the package icon | disp('Simulink') |
dpoly | Show the pass through function on the package icon | dpoly([0 0 1], [1 2 1], 'z') |
droots | Show the pass through function on the package icon | droots([-1], [-2 -3], 4) |
fprinf | Center the variable text on the wrapper icon | fprintf('Sum = %d', 7) |
image | Show... On the package icon RGB Images | image('xxx.jpg') |
patch | Draws a color patch of the specified shape on the package icon | patch([0 10 20 30 30 0], [10 30 20 25 10 10],[1 0 0]) |
plot | Draw a graph with a series of points on the package icon | plot([10 20 30 40], [10 20 10 15]) |
port_label | Draw port labels on the package icon | port_label('output', 1, 'out') |
text |
Display text at a specific location on the wrapper icon . You must be in Icon units Box |
text(5,10, 'Simulink') |
block_icon | Promote the icon of the module contained in the subsystem to the subsystem encapsulation |
|
2) Set up Parameters & Dialog Options
① stay Mask Add... To the dialog box Edit
② Set up Edit The prompt in the dialog box
③ establish Edit Dialog variables , Input Edit The contents of the dialog will be saved in this variable
④ Edit The initial value of the dialog variable
⑤ Don't check
⑥ Tunable: Parameters cannot be modified when setting simulation
⑦ Set up Edit Callback function for ,EF_DataStore_cbFn(gcb),gcb Represents the present block Handle
Create a EF_DataStore_cbFn.m file ,Edit The callback function is as follows :
function EF_DataStore_cbFn(currentBlock)
maskDSName = get_param(currentBlock,'dsName');
set_param([currentBlock '/Data Store Read'], 'DataStoreName',maskDSName);
set_param([currentBlock '/Data Store Read'], 'Priority','1');
set_param([currentBlock '/Data Store Write'],'DataStoreName',maskDSName);
set_param([currentBlock '/Data Store Write'],'Priority','2');
end
The callback function is called when the parameters in the dialog box change , Get... In the callback function Edit Enter the name of , Assign the name to Data Store memory Read and write module of , And set the priority of reading and writing , For example, after manually setting priority before , It's not necessary to set up again this time .
⑧ Edit The position of the cue , Choice in Edit On top of
3) Set up Documentation Options
Documentation Options are some descriptions of the function of the encapsulation module , When you double-click the package module , Filled with mask The dialog box will display the description information .help You can only view the package module's help Option , To be seen
4) establish Mask The effect is
2、 Add the encapsulation module to the custom library
1) Create a new one simulink Lib, Copy the encapsulated module to the new library 2) Revise mask Parameters
Check Mask Of Initialization Options allow library block to modify its contents
After checking this option , When the module in the library is used for modeling , Can pass Mask Created in the Edit To save the signal in the descent edge Data Store memory Rename
notes : If it's in simulink Create packaging modules directly in the library , Instead of using the above first in model Create a module in , Then copy the package module to the library , You can create mask You can check this item directly
3) Modify the display name of the package module
When modeling , Multiple falling edge detection modules may be used in the model , In order to observe whether there are different falling edge detection module uses the same name Data Store Memory, And then it makes unnecessary mistakes . Can be set to use Edit Enter a name as the module display name , Set up by following two steps :
3)-1 Right click the encapsulation module , choice Format——》Show Block Name——》off
3)-2 Right click the encapsulation module , choice Properties——》Block Annotation——》%dSName
The modified display is shown in the following figure :
3) Add custom library to Simulink Library
establish slblocks.m file
function blkStruct = slblocks blkStruct.Browser(1).Library = 'LogicLib';------------------>① blkStruct.Browser(1).Name = 'ucLogicLib';---------------->② end
Matlab Search to add directory named slblocks Of m file , Use the information in this file in Simulink Library Browser Add custom library to panel
① Name of the code customization library file
② Custom library in Simulink Library Browser The panel shows the name
4) Custom library file content storage form
To better manage custom Libraries , We usually put custom inventory in a feature folder , The above contents can be stored according to this structure
MyLib |------slblocks.m |------Lib |------LogicLib.slx |------mask |------EF_DataStore_cbFn.m
Be careful , Be sure to add the above path to Matlab Of Path In the path , Otherwise, there will be a path condition that cannot be found
4) Add completion effect
① Simulink Library Browser The custom library name appears in
② The custom library contains the Edge_Fall modular
3、 Use the library , Modeling generation code validation
Using custom Libraries , Build a simple model , Used to verify the accuracy of Library encapsulation
3.1 Simple model building
Analog buttons Press process detection : When the key is pressed , Connect the buttons GPIO From high to low , Produce a falling edge , Detection of this state means that the key is pressed .
2) Code generation
After code generation configuration , Generate the following code :
The generated code logic is verified to be correct , The encapsulated subsystem conforms to the logic function we want to implement , The goal of reusing encapsulation sub function is achieved
About code generation configuration , Refer to the previous two articles on code generation :