Skip to content

Commit e639239

Browse files
committed
[Improvement] matrix inversion and the for-loop for computing centroidal inertia.
* Improve the average velocity computation: ci.llt().solveInPlace(av) * Simplify the for loop computation with the intermediate term: X_com_i
1 parent b076d81 commit e639239

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/RBDyn/Momentum.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ void computeCentroidalInertia(const MultiBody & mb,
1717
const MultiBodyConfig & mbc,
1818
const Eigen::Vector3d & com,
1919
Eigen::Matrix6d & ci,
20-
Eigen::Vector6d & cm)
20+
Eigen::Vector6d & av)
2121
{
2222

2323
using namespace Eigen;
2424

2525
const std::vector<Body> & bodies = mb.bodies();
26-
cm = Vector6d::Zero();
26+
av = Vector6d::Zero();
2727

2828
ci = Matrix6d::Identity();
2929

@@ -33,14 +33,17 @@ void computeCentroidalInertia(const MultiBody & mb,
3333
// body momentum in body coordinate
3434
sva::ForceVecd hi = bodies[i].inertia() * mbc.bodyVelB[i];
3535

36-
// momentum at CoM for link i : {}^iX_{com}^T {}^iI_i {}^iV_i
37-
cm += (mbc.bodyPosW[i] * X_com_0).transMul(hi).vector();
38-
39-
// sum: X^T_com_i*I_i*X_com_i
4036
// X_com_i = X_i_0 * X_com_0
41-
ci += ((mbc.bodyPosW[i] * X_com_0).matrix().transpose()) * (bodies[i].inertia().matrix())
42-
* (mbc.bodyPosW[i] * X_com_0).matrix();
37+
auto X_com_i = mbc.bodyPosW[i] * X_com_0;
38+
39+
// Momentum at CoM for link i : {}^iX_{com}^T {}^iI_i {}^iV_i
40+
av += X_com_i.transMul(hi).vector();
41+
42+
// Sum: X^T_com_i*I_i*X_com_i
43+
ci += X_com_i.matrix().transpose() * bodies[i].inertia().matrix() * X_com_i.matrix();
4344
}
45+
46+
ci.llt().solveInPlace(av);
4447
}
4548

4649
void computeCentroidalInertia(const MultiBody & mb,
@@ -50,9 +53,9 @@ void computeCentroidalInertia(const MultiBody & mb,
5053
Eigen::Vector6d & cm,
5154
Eigen::Vector6d & av)
5255
{
53-
computeCentroidalInertia(mb, mbc, com, ci, cm);
54-
// Compute the average velocity: inertia.inverse()*momentum
55-
av = ci.llt().solve(cm);
56+
computeCentroidalInertia(mb, mbc, com, ci, av);
57+
// Compute the centroidal momentum := inertia * average velocity
58+
cm = ci * av;
5659
}
5760

5861
sva::ForceVecd computeCentroidalMomentum(const MultiBody & mb, const MultiBodyConfig & mbc, const Eigen::Vector3d & com)

src/RBDyn/RBDyn/Momentum.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ using Blocks = std::vector<Block>;
2222
class Jacobian;
2323

2424
/**
25-
* Compute the centroidal inertia (ci), and centroidal momentum (cm)
25+
* Compute the centroidal inertia (ci), centroidal momentum (cm),
26+
* and the average velocity (av = ci^-1*cm)
2627
* at the CoM frame as describe in [Orin and Gosawami 2008].
2728
* @param mb MultiBody used has model.
2829
* @param mbc Use bodyPosW, bodyVelB.
2930
* @param com CoM position.
3031
* @param ci Centroidal inertia at the Centroidal frame.
3132
* @param cm centroidal momentum at the Centroidal frame.
32-
* @param av Average velocity is inverse(centroidalInertia)*centroidalMomentum
33+
* @param av Average velocity is: av = ci.inverse()*cm
3334
*/
3435
RBDYN_DLLAPI void computeCentroidalInertia(const MultiBody & mb,
3536
const MultiBodyConfig & mbc,
3637
const Eigen::Vector3d & com,
3738
Eigen::Matrix6d & ci,
38-
Eigen::Vector6d & cm);
39-
39+
Eigen::Vector6d & cm,
40+
Eigen::Vector6d & av);
4041
/**
4142
* Compute the centroidal inertia (ci), centroidal momentum (cm),
4243
* and the average velocity (av = ci^-1*cm)
@@ -45,14 +46,14 @@ RBDYN_DLLAPI void computeCentroidalInertia(const MultiBody & mb,
4546
* @param mbc Use bodyPosW, bodyVelB.
4647
* @param com CoM position.
4748
* @param ci Centroidal inertia at the Centroidal frame.
48-
* @param cm centroidal momentum at the Centroidal frame.
4949
* @param av Average velocity is inverse(centroidalInertia)*centroidalMomentum
50+
*
51+
* If the Centroidal Momentum is needed, we can easily compute it as: cm = ci*av.
5052
*/
5153
RBDYN_DLLAPI void computeCentroidalInertia(const MultiBody & mb,
5254
const MultiBodyConfig & mbc,
5355
const Eigen::Vector3d & com,
5456
Eigen::Matrix6d & ci,
55-
Eigen::Vector6d & cm,
5657
Eigen::Vector6d & av);
5758

5859
/**

0 commit comments

Comments
 (0)