How can I use MATLAB functions to generate a Simscape Model?

22 vues (au cours des 30 derniers jours)
I want to generate a Simscape model using a MATLAB script because it will cut down on my work time significantly if I can generate it procedurally. I have been able to generate a Simulink model using commands like 'add_block', 'add_line', and 'set_param', but am having trouble generating a Simscape model the same way. How might I go about generating a Simscape model this way?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 13 Sep 2024
Modifié(e) : MathWorks Support Team le 13 Sep 2024
I have created an example script to generate a simple Simscape Electrical circuit programmatically. The script can be found below and contains inline comments explaining each section. I have provided additional explanation and linked resources below.
Script for Generating Simple Model
clear close all clc % Create the model modelName = 'testModel'; open_system(new_system(modelName)) % Add blocks to the model res1 = add_block("fl_lib/Electrical/Electrical Elements/Resistor", [modelName '/MyResistor']); volt1 = add_block("fl_lib/Electrical/Electrical Sources/AC Voltage Source", [modelName '/MyVoltage']); voltSensor1 = add_block("fl_lib/Electrical/Electrical Sensors/Voltage Sensor", [modelName '/MyVoltageSensor']); solvConf = add_block("nesl_utility/Solver Configuration", [modelName '/MySolv']); ref1 = add_block("fl_lib/Electrical/Electrical Elements/Electrical Reference", [modelName '/MyRef']); % Get block ports -- the LConn and RConn ports are the physical ports ResBlkH = get_param([modelName '/MyResistor'], 'PortHandles'); VoltBLkH = get_param([modelName '/MyVoltage'], 'PortHandles'); VoltSensorBlkH = get_param([modelName '/MyVoltageSensor'], 'PortHandles'); SolvBlkH = get_param([modelName '/MySolv'], 'PortHandles'); RefBlkH = get_param([modelName '/MyRef'], 'PortHandles'); % The following adds lines between Physical Ports % Use physical port handles to add lines add_line(modelName, ResBlkH.RConn(1), VoltBLkH.RConn(1)) add_line(modelName, SolvBlkH.RConn(1), VoltBLkH.RConn(1)) add_line(modelName, RefBlkH.LConn(1), VoltBLkH.RConn(1)) % Use physical port names to add lines add_line(modelName, 'MyResistor/LConn1', 'MyVoltage/LConn1') add_line(modelName, 'MyResistor/LConn1', 'MyVoltageSensor/LConn1') add_line(modelName, 'MyResistor/RConn1', 'MyVoltageSensor/RConn2') % Set the parameters for each of the Simscape blocks. These parameter % names can be found by opening the parameters for the block in Simulink, % selecting the 'Description' tab and clicking the 'Source Code' link. The % parameter names are contained in the 'parameters' section of the source % code. set_param(res1, 'R', '100') set_param(volt1, 'amp', '5')
'add_block' function
You can follow the following steps to know the source path of the block - 
1) Navigate to the block you want to add in the Simulink Library Browser.
2) Hover over the block.
3) A box will appear containing the block information.
4) On the top of the box, the path of the block is written in bold letters.
5) You can note down this path and provide the same to the 'source' argument in 'add_block' function.
I have attached a screenshot of the hovered box that appears and encircled the path you can refer for source:
For example, if you want to add the box hovered in the attached image in a model named 'mDemo' with the block name 'myBlock', you can use the following command.
add_block('sps_lib/Passives/Three-Phase Series RLC Branch', 'dd/myblock');
'add_line' function
To use the 'add_line' function with Simscape, the physical connection ports for the Simscape blocks to connect must be provided. The port handles of any Simulink or Simscape block can be found by using the 'get_param' function as follows:
thesePorts = get_param('block path/block name', 'PortHandles')
A list of the types of ports that Simulink and Simscape blocks can have can be found by referring to the 'Ports' or 'PortHandles' rows of the link below:
For Simscape blocks, the ports of interest are the physical connection ports that are called 'LConn' and 'RConn'. There are two ways to use these ports in the 'add_line' function. The first method is to use the port handles that were found using the 'get_param' function above:
add_line('model name', thesePorts.RConn(1), thesePorts.LConn(1));
This function connects the right and left connection ports of the block.
Alternatively, the port names can be used. The 'add_line' function can use the port names like this:
add_line('model name', 'block name/RConn1', 'block name/LConn1');
This is equivalent to the use of the handles above. The port names are 'RConn' and 'LConn' followed by the integer index of the right or left port that should be referenced. For example, if there are two left ports, the two port names would be 'LConn1' and 'LConn2'.
More information about the 'add_line' function can be found by referring to the link below:
More information about using the 'add_line' function with Simscape Electrical can be found by referring to the link below:
'set_param' function
The 'set_param' function can be used to set the parameters for Simscape Electrical blocks. The parameter names to use in the 'set_param' function can be found by opening the parameters for the desired block in Simulink, clicking the 'Description' tab, then opening the 'Source Code' link. The 'parameters' section of the source code will contain the names of the block parameters. The value of the parameters should be entered as a character vector:
set_param('block name', 'parameter name', '100')
More information about the 'set_param' function can be found by referring to the link below:
Alternatively, the following command will produce a list of the parameters set by the UI that can be updated using the 'set_param' command:
get_param(blockHandle, 'DialogParameters');
More information about 'get_param' can be found by referring to the link below:

Plus de réponses (0)

Catégories

En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by