how to find the center of mass of a assembly

Typically: "How do I... ", "How can I... " questions
Post Reply
sunihsn
Posts: 18
Joined: 17 Apr 2023, 08:09

how to find the center of mass of a assembly

Post by sunihsn »

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 ?

fferri
Posts: 1230
Joined: 09 Sep 2013, 19:28

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

Post by fferri »

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.:

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

sunihsn
Posts: 18
Joined: 17 Apr 2023, 08:09

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

Post by sunihsn »

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)}

fferri
Posts: 1230
Joined: 09 Sep 2013, 19:28

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

Post by fferri »

You are not accessing objects correctly.
See Accessing scene objects programmatically.

sunihsn
Posts: 18
Joined: 17 Apr 2023, 08:09

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

Post by sunihsn »

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.

fferri
Posts: 1230
Joined: 09 Sep 2013, 19:28

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

Post by fferri »

your calculation of "totalCenterOfMass" is not correct.

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)

sunihsn
Posts: 18
Joined: 17 Apr 2023, 08:09

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

Post by sunihsn »

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

fferri
Posts: 1230
Joined: 09 Sep 2013, 19:28

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

Post by fferri »

The error is in the code to transform the 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
...

coppelia
Site Admin
Posts: 10375
Joined: 14 Dec 2012, 00:25

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

Post by coppelia »

Have also a look at the model models/tools/center of mass visualization tool.ttm

Cheers

sunihsn
Posts: 18
Joined: 17 Apr 2023, 08:09

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

Post by sunihsn »

https://drive.google.com/file/d/1JJFscw ... sp=sharing
i have corrected it as you have explained . can you pls check or verify if its correct or not , still i am not getting the expected results in co-simulation.

and also how to give these values: Frictional Force
Coefficient of Static Friction=mu_s=0.9;
Coefficient of Dynamic Friction=mu_k=0.7;
Critical Velocity=mu_vth=0.01;

contact_stiffness=500;
contact_damping=50;
how to give and where to give it , any specific dynamics simulator you suggest for this like bullet, newton , MUjuco etc?

Post Reply