How can I edit SimMechanics block parameters programmatically?

3 次查看(过去 30 天)
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 个评论
Kaustubha Govind
Kaustubha Govind 2012-6-29
What about if you use SET_PARAM after calling add_block('mylib/Body','K/Body')?
Matthew Eicholtz
Matthew Eicholtz 2012-6-29
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.

请先登录,再进行评论。

采纳的回答

Ryan G
Ryan G 2012-6-29
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.

更多回答(2 个)

Falk
Falk 2013-1-30
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.

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by