How can I edit SimMechanics block parameters programmatically?

13 views (last 30 days)
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 Comments
Kaustubha Govind
Kaustubha Govind on 29 Jun 2012
What about if you use SET_PARAM after calling add_block('mylib/Body','K/Body')?
Matthew Eicholtz
Matthew Eicholtz on 29 Jun 2012
I tried using SET_PARAM as you suggest, but it seemed to produce strange results.
When I selected View->Block Data Tip Options->Parameter Names and Values and hovered over the object, it looked like the changes took effect, but it didn't show up when I double-clicked on the block itself.

Sign in to comment.

Accepted Answer

Ryan G
Ryan G on 29 Jun 2012
It looks like you found that string in the model file. There is a similar string for 'WorkingFrames'
'Left$CS1$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none#Right$CS2$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none'
So I can set
x = 'Left$CS1$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none#Right$CS2$[0 0 0]$CG$CG$m$[0 0 0]$Euler X-Y-Z$deg$WORLD$true$none';
and do this
add_block('mblibv1/Bodies/Body','K/Body',... 'Mass','0',... 'Inertia','zeros(3)',... 'CGPos','[0 0 0]',... 'WorkingFrames',x);
but you need to set all your frames in that one big string.

More Answers (2)

Falk
Falk on 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 Comment
Ryan G
Ryan G on 31 Jan 2013
This is a very good point in general, but remember he is building the model programatically!

Sign in to comment.


Parham Sagharichi ha
Parham Sagharichi ha on 17 Feb 2015
i wanna change number of ports for joint revloute , what should i do ?
  1 Comment
Szymon Maczka
Szymon Maczka on 8 May 2019
Good question... I am trying to do the same with 'Spring and Damper Force' for changing stiffness parameter during simulation #2018b

Sign in to comment.

Categories

Find more on Simulation and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!