## Doubts on getting the center of mass and inertia matrix

Need help, or want to share a macro? Post here!
CharlieMAC
Posts: 219
Joined: Fri Apr 25, 2014 1:59 am

### Doubts on getting the center of mass and inertia matrix

Hi guys!

I'm trying to model the parts of a biped robot, but for simulation purposes using Simmechanics it is required to get the inertia tensor and the center of gravity of the parts. I found here in the forum that there're some instructions that can do the work, but I have some doubts about the results I'm getting.

The first thing I saw here in the forum was this topic: viewtopic.php?f=10&t=330&start=20 where I found the following code:

Code: Select all

``````>>> import Part
>>> b = Part.makeBox(10,10,10)
>>> s = Part.Solid(b)
>>> s.CenterOfMass
Vector (5, 5, 5)
>>>``````
What does this makeBox exactly do? I mean, I tried to use it with different parts but the center of mass was always the same (5,5,5). Later, using the Python console I found there's a MatrixOfInertia instruction that allows to calculate the inertia tensor of the part, but I also read here in the forum that Freecad lacks of material properties for the solids, so, with this on mind, my suspicion is that there's no density. So, concretely, how reliable is this Freecad's inertia tensor and center of mass calculation? Do I have to specify the dimensions of the solid to makeBox to get a more accurate result?

Here is my Freecad's info and one of the parts I'm trying to get the calculations from:

OS: Ubuntu 14.04 LTS
Platform: 64-bit
Version: 0.14.3672 (Git)
Branch: master
Hash: b0c157a5ea97dd2bd11658f98f53fc1edefcd456
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
SoQt version: 1.6.0a
OCC version: 6.7.0

Best regards,

Charlie
Attachments
Pendulo_2.fcstd
ickby
Posts: 2982
Joined: Wed Oct 05, 2011 7:36 am

### Re: Doubts on getting the center of mass and inertia matrix

they are very reliable, if the solid is valid. We encountered negative masses already on solids with flipped surfaces etc. so you need to make sure your solid is ok before calculating anything.

Part.makeBox() actually makes a shape of a box, or a cube or whatever you want to call it. And it creates a solid, so the call to Part.Solid is not needed.to show the box you need to atach the shape to a document object, the simplest way is:

Code: Select all

``````box = Part.makeBox(10,10,10)
Part.show(box)
``````
The box is created with one corner in the origin, and if every dimension is 10 long then the center of mass is of course at the coordinate (5,5,5). If you make your box with 20 on each side it will be at (10,10,10) etc.

Also the material is irrelevant. The calculated mass and inertia is only valid for homogenous materials and then density is only a factor for mass and inertia. Freecad uses density = 1, so just multiply your value to get your specific result.

Mass and inertia tensorare properties of the shape. So to access those properties for any part (gui constructed or scripted or whatever) you simply get the shape of the solid and aceess it's properties:

Code: Select all

``````list = App.ActiveDocument.getObjectsByLabel("Pendulo_Completo")
shape = list[0].Shape                     #we received a list of objects, need the first entry
solid_shape = shape.Solids[0]        #fusions are compounds, we need to get the solid shape
solid_shape.Mass
solid_shape.CenterOfMass
solid_shape.MatrixOfInertia
``````
CharlieMAC
Posts: 219
Joined: Fri Apr 25, 2014 1:59 am

### Re: Doubts on getting the center of mass and inertia matrix

Thanks for your complete answer. One last thing: What are the units of the density value Freecad uses? Because I'm using milimeters as my length unit, so I don't to mess the calculation due to this

Best regards,

Charlie
shoogen
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

### Re: Doubts on getting the center of mass and inertia matrix

I would just pretend that you get a specific moment of inertia (L^5) / (mm^5).
Then you multiply it by some density (M L^-3) / (kg mm^-3) and get your usual Moment of inertia (M L^2) in (kg mm²)
CharlieMAC
Posts: 219
Joined: Fri Apr 25, 2014 1:59 am

### Re: Doubts on getting the center of mass and inertia matrix

CharlieMAC
Posts: 219
Joined: Fri Apr 25, 2014 1:59 am

### Re: Doubts on getting the center of mass and inertia matrix

How can create a a function like Calc_Inertia("part",density) to automate the process of the calculation? I have the following code into a .py file, but it seems I'm doing something wrong.

Code: Select all

``````def Calc_Inercia(parte,densidad):

list=App.ActiveDocument.getObjectsByLabel(parte)
shape=list[0].Shape
solid_shape=shape.Solids[0]
print "Masa:" + str(solid_shape.Mass)
print "Centro de Masa:" + str(solid_shape.CenterOfMass)
print "Tensor de Inercia:" + str(solid_shape.MatrixOfInertia)
``````
But I get this error:

Code: Select all

``````>>> import Inercia
>>> Calc_Inercia("Soporte_Superior",1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'Calc_Inercia' is not defined
``````
What am I doing wrong?

Best regards,
Charlie
vrytar
Posts: 28
Joined: Wed Nov 21, 2012 10:08 pm

### Re: Doubts on getting the center of mass and inertia matrix

You have to call
Inercia.Calc_Inercia("Soporte_Superior",1)
or use
from Inercia import Calc_Inercia
CharlieMAC
Posts: 219
Joined: Fri Apr 25, 2014 1:59 am

### Re: Doubts on getting the center of mass and inertia matrix

Thanks! It is also important to write this:

Code: Select all

``import FreeCAD as App``
Otherwise it'll give an error concerning App.
kwahoo
Posts: 309
Joined: Fri Nov 29, 2013 3:09 pm
Contact:

### Re: Doubts on getting the center of mass and inertia matrix

Or you can write:

Code: Select all

``import FreeCAD``
and then

Code: Select all

``list=FreeCAD.ActiveDocument.getObjectsByLabel(parte)``