## how to find the center of mass of a assembly

### how to find the center of mass of a assembly

Hi,

i have built the quadruped robot using a cad tool and i have imported it to coppeliasim, know i want to find the COM of the robot so that i can capture the acceleration, velocity and position of the center of mass. how to do it can you pls explain the procedure for it ?

i have built the quadruped robot using a cad tool and i have imported it to coppeliasim, know i want to find the COM of the robot so that i can capture the acceleration, velocity and position of the center of mass. how to do it can you pls explain the procedure for it ?

### Re: how to find the center of mass of a assembly

The center of mass \(\mathbf{C}\) of a composite body is given by the weighted average of the individual centers of mass, i.e.:

\(\mathbf{M} = \sum_{i=1}^{n} m_i\)

\(\mathbf{C} = \frac{1}{\mathbf{M}}\sum_{i=1}^{n} m_i \cdot \mathbf{c}_i\)

where \(m_i\) is the mass of the i-th body, and \(\mathbf{c}_i\) its center of mass in global coordinates.

To retrieve \(m_i\) and \(\mathbf{c}_i\) the functions to use are sim.getShapeMass and sim.getShapeInertia, e.g.:

\(\mathbf{M} = \sum_{i=1}^{n} m_i\)

\(\mathbf{C} = \frac{1}{\mathbf{M}}\sum_{i=1}^{n} m_i \cdot \mathbf{c}_i\)

where \(m_i\) is the mass of the i-th body, and \(\mathbf{c}_i\) its center of mass in global coordinates.

To retrieve \(m_i\) and \(\mathbf{c}_i\) the functions to use are sim.getShapeMass and sim.getShapeInertia, e.g.:

Code: Select all

```
local shapes = {shape1handle, shape2handle, ...}
local M = 0
local C = Vector {0, 0, 0}
for i, shape in ipairs(shapes) do
local m = sim.getShapeMass(shape)
M = M + m
local ii, com = sim.getShapeInertia(shape)
local c = Matrix(3, 4, com):col(4) -- 4th column of com is the position of center of mass
c = sim.multiplyVector(sim.getObjectMatrix(shape), c) -- in global coords
C = C + m * c
end
C = C / M
```

### Re: how to find the center of mass of a assembly

https://drive.google.com/file/d/1ySF7z- ... sp=sharing

i am couldn't able t get the com value , can you pls help me atleast for one leg . i have added a child script to the code in which i have coded using ur example. and the result is : "Center of Mass for Leg 1:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 2:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 3:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 4:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 1:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 2:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 3:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 4:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 1:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 2:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 3:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 4:",{-nan(ind), -nan(ind), -nan(ind)}

i am couldn't able t get the com value , can you pls help me atleast for one leg . i have added a child script to the code in which i have coded using ur example. and the result is : "Center of Mass for Leg 1:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 2:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 3:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 4:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 1:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 2:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 3:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 4:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 1:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 2:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 3:",{-nan(ind), -nan(ind), -nan(ind)}

"Center of Mass for Leg 4:",{-nan(ind), -nan(ind), -nan(ind)}

### Re: how to find the center of mass of a assembly

You are not accessing objects correctly.

See Accessing scene objects programmatically.

See Accessing scene objects programmatically.

### Re: how to find the center of mass of a assembly

https://drive.google.com/file/d/1qrZW4s ... sp=sharing

i have done adding the handles for the objects but now if i change the shape reference frame the COM values are changing. what is the correct way of doing it ?. or any changes is require in the model.

i have done adding the handles for the objects but now if i change the shape reference frame the COM values are changing. what is the correct way of doing it ?. or any changes is require in the model.

### Re: how to find the center of mass of a assembly

your calculation of "totalCenterOfMass" is not correct.

you probably want to do:

you probably want to do:

Code: Select all

```
local allHandles = {
body,
hip_1, upper_dynamic1, Dynamic_lower_leg1,
hip_2, upper_dynamic2, Dynamic_lower_leg2,
hip_3, upper_dynamic3, Dynamic_lower_leg3,
hip_4, upper_dynamic4, Dynamic_lower_leg4,
}
local totalCenterOfMass = calculateCenterOfMass(allHandles)
```

### Re: how to find the center of mass of a assembly

https://drive.google.com/file/d/1_ZGjA2 ... sp=sharing

hi, thank you for identifying the problem . now even after correcting that, the center of mass is far away from the body of the robot . i checked the mass of all the objects like, legs and body its all coming correct . what do you think the reason could be ?, i have chosen shape reference as the mesh center. apart from that any other suggestion for making the model for dynamics use is appreciated.

Thank you

hi, thank you for identifying the problem . now even after correcting that, the center of mass is far away from the body of the robot . i checked the mass of all the objects like, legs and body its all coming correct . what do you think the reason could be ?, i have chosen shape reference as the mesh center. apart from that any other suggestion for making the model for dynamics use is appreciated.

Thank you

### Re: how to find the center of mass of a assembly

The error is in the code to transform the

You first have to extract the position vector, and then transform it using sim.multiplyVector:

`c`

(shape center of mass) vector in global coords.You first have to extract the position vector, and then transform it using sim.multiplyVector:

Code: Select all

```
local ii, com = sim.getShapeInertia(shape) -- Get inertia and center of mass
local c = {com[4], com[8], com[12]} -- Extract position of center of mass
c = sim.multiplyVector(sim.getObjectMatrix(shape, -1), c) -- Transform to global coords
...
```

### Re: how to find the center of mass of a assembly

Have also a look at the model

Cheers

*models/tools/center of mass visualization tool.ttm*Cheers