Add Simulink Library Blocks to Model by MATLAB codes

Sometimes, we need to (repeatedly) apply some same blocks in a Simulink model. It would be convenient to create a custom library that consisting the blocks. The parameters can be set during this procedure. The library can then be added to Simulink models as you want, by drag and drop, or by MATLAB codes. Here, an example to create a flexible wire model based on lumped parameter method will be given.

At first, a Simulink library which can model an element of the wire should be created. The procedure of creating custom library can be referred to creating-block-libraries. SimMechanics is applied in this example and the resulted library is shown in the following figure.

Wire element library.

In this example, Spherical joints are applied to connect the element. The coordinates of the two element ends are defined by the Rigid Transform blocks. Then, the following MATLAB codes can be applied to create the wire model:

new_system('wireModel');
load_system('wireModel');
add_block('built-in/Subsystem','wireModel/wire');
set_param('wireModel/wire','position',[300,60,350,110]);

for i=1:numElement
    blockname=['wireModel/wire/wireElement' num2str(i)];
    block=add_block('wireElement_lib/wireElement',blockname);
    pos = [50+60*(i-1), 50, 100+60*(i-1) 100];
    set_param(blockname,'position',pos);
    nameL = ['element{' num2str(i) '}.L'];
    set_param(blockname,'L',nameL);

    if(i==1)
        continue;
    else
        c2=['wireElement' num2str(i) '/LConn1'];
        c1=['wireElement' num2str(i-1) '/LConn2'];
        add_line('wireModel/wire',c1,c2);
    end
end
add_block('nesl_utility/Connection Port','wireModel/wire/Conn1');
set_param('wireModel/wire/Conn1','position',[0,20,15,30]);
add_block('nesl_utility/Connection Port','wireModel/wire/Conn2');
set_param('wireModel/wire/Conn2','position',[1400,20,1415,30]);
add_line('wireModel/wire','wireElement1/LConn1','Conn1/RConn1');
add_line('wireModel/wire','wireElement20/LConn2','Conn2/RConn1');

save_system('wireModel.slx');

Note that the number of the elements should be defined before execute the above codes. The generated Simulink model can be illustrated as follows:

Scenery of the wire model.

After adding other required blocks, such as the World Frame, Solver, and Mechanism Configuration, the model can be run properly.

By changing the custom Simulink library, the aerodynamics can also be taken into account during the simulation. The library considering the aerodynamics based on quasi-steady theory (by S-function) is illustrated in the following figure.

wire element considering external loads.

Leave a Comment

Your email address will not be published. Required fields are marked *