How can I edit SimMechanics block parameters programmatically?
Ältere Kommentare anzeigen
I am writing a script to generate a SimMechanics model programmatically and need some help.
bdclose('all') % close all Simulink models
new_system('K') % start a new model called K
open_system('K') % open the new model called K
load_system('mylib') % loads my custom library of SimMechanics blocks (these are just the basic blocks: body, ground, revolute, prismatic, etc.)
For a block like ground, I can add it to the model and change parameters as follows:
add_block('mylib/Ground','K/Ground',...
'CoordPosition','[# # #]',...
'ShowEnvPort','on'); % where [# # #] is an arbitrary location
I can also add the machine environment block and modify it:
add_block('mylib/Machine Environment','K/Machine Environment',...
'Gravity','[0 0 0]');%,...
'Dimensionality','2D Only',...
'AnalysisType','Kinematics',...
'VisualizeMachine','off')
Here is the problem: I cannot seem to properly modify some the Body block parameters
add_block('mylib/Body','K/Body',...
'Mass','0',...
'Inertia','zeros(3)',...
'CGPos','[# # #]',...
'CS1Pos','[# # #]',...
'CS2Pos','[# # #]');
Obviously, replace the '#' with actual numbers. When I run the code, the CS positions are not altered when I double-click on the block in the model. I found that I can change the CG line as follows:
CGstr = ['Left$CG$[# # #]'$WORLD$WORLD$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$false$none'];
and replace...
'CGPos','[# # #]'
in the previous 'add_block' code with
'CG',CGstr
and it works. But I can't do it for the CS1, CS2, etc...Any suggestions?
2 Kommentare
Kaustubha Govind
am 29 Jun. 2012
What about if you use SET_PARAM after calling add_block('mylib/Body','K/Body')?
Matthew Eicholtz
am 29 Jun. 2012
Akzeptierte Antwort
Weitere Antworten (2)
Falk
am 30 Jan. 2013
There is a another way to change CS-values within a body-block via code.
First, you have to build a subsystem around the body-block. You have to build a mask for the subsystem. In the mask you can define parameters. These parameters you can use as variables in the CS- or CG-positions within the body-block-mask.
At this point, everything was stupidyclicki...
But the mask-parameters you can set via code, e.g.
set_param('myModel/SubsystemName','maskparameter',num2str(value))
Each occurence of 'maskparameter' within the underlying body-block will copy its value from the subsystem (one level above) to the variable in the body-block.
1 Kommentar
Ryan G
am 31 Jan. 2013
This is a very good point in general, but remember he is building the model programatically!
Parham Sagharichi ha
am 17 Feb. 2015
0 Stimmen
i wanna change number of ports for joint revloute , what should i do ?
1 Kommentar
Szymon Maczka
am 8 Mai 2019
Good question... I am trying to do the same with 'Spring and Damper Force' for changing stiffness parameter during simulation #2018b
Kategorien
Mehr zu Simulation and Analysis finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!