How can I edit SimMechanics block parameters programmatically?

조회 수: 3 (최근 30일)
Matthew Eicholtz
Matthew Eicholtz 2012년 6월 29일
댓글: Szymon Maczka 2019년 5월 8일
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.
  댓글 수: 1
Ryan G
Ryan G 2013년 1월 31일
This is a very good point in general, but remember he is building the model programatically!

댓글을 달려면 로그인하십시오.


Parham Sagharichi ha
Parham Sagharichi ha 2015년 2월 17일
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