MATLAB Answers

How can I find the center of gravity of a machine in SimMechanics 3.1.1 (R2009b)?

20 views (last 30 days)
I have multiple bodies at various locations in my SimMechanics model and I wish to know what the center of gravity is of the entire machine defined by the bodies. In other words, I want to know the World-reference CG location of all of the bodies multiplied by their masses. How can I do this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Apr 2019
Edited: MathWorks Support Team on 4 Apr 2019
The ability to do detect the center of gravity of a machine automatically is not availalble in SimMechanics 3.2 (R2010a_pre). You can use the following approach as a workaround:
You can define all bodies with their CG defined in the World Reference plain and then write a MATLAB script to manually perform this computation. This would use the FIND_SYSTEM and GET_PARAM commands to query this information as such;
% Get the Body blocks in the current model.
h=find_system(gcs,'ClassName','Body')
% Extract a vector of mass values.
masses=str2num(cell2mat(get_param(h,'Mass')))
% Extract a vector of CG locations.
for i=1:length(h)
CGstring=get_param(h{i},'CG')
p=textscan(CGstring,'%s','delimiter','$')
CGs(i,:)=str2num(p{1}{3})
end
% Take the weighted average of the CGs with the mass values.
machineCG = [0 0 0];
for i = 1:length(h)
machineCG = machineCG + masses(i)*CGs(i,:);
end
machineCG = machineCG/sum(masses);
In the case where all of the body CGs are not necessarily defined in world coordinates you may use the Body Sensor block to do this conversion for you but this requires hooking this block up manually to every body block in your model. Connect the body selector to the CG of the body block and select World Coordinate system for the output of position and send this information to workspace or wherever you wish to compute the CG.

  2 Comments

Stephanie
Stephanie on 14 Apr 2014
How do you define the CG for second generation simmechanics for the line of code
CGstring=get_param(h{i},'CG')
there isnt a body sensor block and I've tried changing CG to 'Center of Mass'
Thanks!
Matin Saeedkondori
Matin Saeedkondori on 29 Oct 2015
Hi, i'm trying to execute this code, put have some fundamental problem. would you mind explain it more? I need to get center of gravity of a 2D image of human body,

Sign in to comment.

More Answers (1)

Peter Kövesdi
Peter Kövesdi on 18 Dec 2019
Is there any update to this? In Matlab 2017b, not even the command
h=find_system(gcs,'ClassName','Body')
works any more.

  0 Comments

Sign in to comment.

Sign in to answer this question.