matrix-inertia.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
11 
12 #include <fstream>
13 #include <map>
14 #include <vector>
15 // #include <jrl/dynamics/Joint.h>
16 // #include <jrl/dynamics/HumanoidDynamicMultiBody.h>
17 #include <abstract-robot-dynamics/robot-dynamics-object-constructor.hh>
18 #include <sot/core/debug.hh>
19 
20 using namespace dynamicsJRLJapan;
21 using namespace dynamicgraph::sot;
22 using namespace dynamicgraph;
23 
24 static matrix3d skewSymmetric(const vector3d& v) {
25  matrix3d res;
26 
27  res(0, 0) = 0.0;
28  res(1, 1) = 0.0;
29  res(2, 2) = 0.0;
30 
31  res(0, 1) = -v[2];
32  res(0, 2) = v[1];
33 
34  res(1, 0) = v[2];
35  res(1, 2) = -v[0];
36 
37  res(2, 0) = -v[1];
38  res(2, 1) = v[0];
39 
40  return res;
41 }
42 
43 /* -------------------------------------------------------------------------- */
44 /* -------------------------------------------------------------------------- */
45 /* -------------------------------------------------------------------------- */
46 
47 MatrixInertia::MatrixInertia(CjrlHumanoidDynamicRobot* aHDR)
48  : aHDR_(aHDR), aHDMB_(0x0) {
49  sotDEBUGIN(25);
50  if (aHDR != NULL) init(aHDR);
51  sotDEBUGOUT(25);
52 }
53 
54 void MatrixInertia::init(CjrlHumanoidDynamicRobot* aHDR) {
55  sotDEBUGIN(25);
56  aHDR_ = aHDR;
57  aHDMB_ = dynamic_cast<dynamicsJRLJapan::HumanoidDynamicMultiBody*>(aHDR_);
58 
59  /* STEP 2: get the joints and resize internal vectors according to
60  * number of joints. */
61  joints_ = aHDMB_->jointVector();
62  sotDEBUG(25) << "Joints:" << joints_.size() << endl;
63 
64  parentIndex_.resize(joints_.size());
65  inertia_.resize(joints_.size() + 5, joints_.size() + 5);
66  phi.resize(joints_.size());
67  iVpi.resize(joints_.size());
68  iVpiT.resize(joints_.size());
69  Ic.resize(joints_.size());
70 
71  /* STEP 3: create the index of parents. */
72  initParents();
73 
74  /* STEP 4: initialize phi (dof table) for each joint. */
75  initDofTable();
76  sotDEBUGOUT(25);
77 }
78 
80  sotDEBUGIN(25);
81  std::map<CjrlJoint*, int> m;
82  for (size_t i = 0; i < joints_.size(); ++i) m[joints_[i]] = i;
83 
84  sotDEBUG(15) << "Parent Indexes:" << std::endl;
85  for (size_t i = 0; i < joints_.size(); ++i) {
86  if (joints_[i]->parentJoint() == 0x0) {
87  parentIndex_[i] = -1;
88  sotDEBUG(15) << "parent of\t" << i << "\t("
89  << static_cast<Joint*>(joints_[i])->getName() << "):\t" << -1
90  << std::endl;
91  } else {
92  parentIndex_[i] = m[joints_[i]->parentJoint()];
93  sotDEBUG(15) << "parent of\t" << i << ":\t("
94  << static_cast<Joint*>(joints_[i])->getName() << "):\t"
95  << m[joints_[i]->parentJoint()] << "\t("
96  << static_cast<Joint*>(joints_[m[joints_[i]->parentJoint()]])
97  ->getName()
98  << ")" << std::endl;
99  }
100  }
101  sotDEBUGOUT(25);
102 }
103 
105  sotDEBUGIN(25);
106  for (size_t i = 0; i < joints_.size(); ++i) {
107  Joint* j = static_cast<Joint*>(joints_[i]);
108  vector3d z = j->axe();
109  dynamicgraph::Vector& phi_i = phi[i];
110  phi_i.resize(6);
111  phi_i.fill(0.);
112  for (int n = 0; n < 3; ++n) phi_i(n + 3) = z(n);
113  sotDEBUG(25) << phi_i << endl;
114  }
115  sotDEBUGOUT(25);
116 }
117 
119 
120 /* -------------------------------------------------------------------------- */
121 /* -------------------------------------------------------------------------- */
122 /* -------------------------------------------------------------------------- */
124  sotDEBUGIN(25);
125  const vectorN& currentConf = aHDMB_->currentConfiguration();
126  sotDEBUG(45) << "q = [ " << currentConf << endl;
127 
128  sotDEBUG(15) << "Spatial transforms:" << std::endl;
129  const size_t SIZE = joints_.size();
130  for (size_t i = 0; i < SIZE; ++i) {
131  sotDEBUG(25) << "Joint " << i
132  << ": rank = " << joints_[i]->rankInConfiguration() << endl;
133  Joint* j = static_cast<Joint*>(joints_[i]);
134 
135  /* iRpi: Rotation from joint i to parent of i. */
136  MatrixRotation piRi;
137  {
138  /* phi_[1] = phi_(1:3) is the rotation axis of the joint. */
139  matrix3d Ri;
140  j->getStaticRotation(Ri);
141  vector3d axeJ = Ri * j->axe();
142  const double& q = currentConf(joints_[i]->rankInConfiguration());
143  sotDEBUG(45) << "q" << i << " = " << q << endl;
144  matrix3d piRi_tmp;
145  j->RodriguesRotation(axeJ, q, piRi_tmp);
146  for (unsigned int loopi = 0; loopi < 3; ++loopi)
147  for (unsigned int loopj = 0; loopj < 3; ++loopj)
148  piRi(loopi, loopj) = piRi_tmp(loopi, loopj);
149  }
150  dynamicgraph::Vector piTi(3);
151  {
152  vector3d piTi_tmp;
153  j->getStaticTranslation(piTi_tmp);
154  for (unsigned int loopi = 0; loopi < 3; ++loopi)
155  piTi(loopi) = piTi_tmp(loopi);
156  }
157  /* Twist matrix iXpi: transfo of the velocities between i en pi (parent i).
158  * iXpi = [ iRpi Skew( iRpi.t )*iRpi ]
159  * [ 0 iRpi ] */
160  MatrixHomogeneous piMi;
161  piMi.buildFrom(piRi, piTi);
162  MatrixHomogeneous iMpi;
163  piMi.inverse(iMpi);
164  sotDEBUG(45) << "piMi = " << piMi << endl;
165  sotDEBUG(45) << "iMpi = " << iMpi << endl;
166 
167  iVpi[i].buildFrom(iMpi);
168  iVpi[i].transpose(iVpiT[i]);
169  sotDEBUG(25) << "iVpi" << i << " = " << iVpi[i] << endl;
170  }
171  sotDEBUGOUT(25);
172 }
173 
175  sotDEBUGIN(25);
176  inertia_.fill(0.0);
177 
178  const size_t SIZE = joints_.size();
179 
180  /* Compute the local 6D inertia matrices. */
181  for (size_t i = 0; i < SIZE; ++i) {
182  /* Position of the mass in the joint frame. */
183  vector3d com = joints_[i]->linkedBody()->localCenterOfMass();
184  matrix3d Sc = skewSymmetric(com);
185  /* Inertia of the link. */
186  matrix3d Icm = joints_[i]->linkedBody()->inertiaMatrix();
187 
188  double m = joints_[i]->linkedBody()->mass();
189 
190  /* Ic_ is the inertia 6D matrix of the joint in the joint frame.
191  * Ic = [ Ai+mSc.Sc' mSc ]
192  * [ mSc' mId ]
193  */
194  sotDEBUG(45) << "com" << i << " = [ " << com << "]" << endl;
195  sotDEBUG(45) << "Sc" << i << " = [ " << Sc << "]" << endl;
196  sotDEBUG(45) << "Icm" << i << " = [ " << Icm << "]" << endl;
197  matrix3d Sct = Sc.Transpose();
198  matrix3d Irr = Sc * Sct;
199  dynamicgraph::Matrix& Ici = Ic[i];
200  Ici.resize(6, 6);
201  for (unsigned int loopi = 0; loopi < 3; ++loopi)
202  for (unsigned int loopj = 0; loopj < 3; ++loopj) {
203  /*TT*/ if (loopi == loopj)
204  Ici(loopi, loopj) = m;
205  else
206  Ici(loopi, loopj) = 0.;
207  /*TR*/ Ici(loopi, loopj + 3) = m * Sct(loopi, loopj);
208  /*RT*/ Ici(loopi + 3, loopj) = m * Sc(loopi, loopj);
209  /*RR*/ Ici(loopi + 3, loopj + 3) =
210  m * Irr(loopi, loopj) + Icm(loopi, loopj);
211  }
212 
213  sotDEBUG(25) << "Ic" << i << " = " << Ici;
214  }
215 
216  dynamicgraph::Vector Fi(6);
217  dynamicgraph::Matrix iVpiT_Ici(6, 6);
218  dynamicgraph::Matrix iVpiT_Ici_iVpi(6, 6);
219 
220  for (int i = SIZE - 1; i >= 1; --i) {
221  const unsigned int iRank = joints_[i]->rankInConfiguration();
222 
223  dynamicgraph::Matrix& Ici = Ic[i];
224  sotMatrixTwist& iVpii = iVpi[i];
225  MatrixForce& iVpiiT = iVpiT[i];
226  dynamicgraph::Vector& phii = phi[i];
227  /* F = Ic_i . phi_i */
228  Fi = Ici * phii;
229  /* H_ii = phi_i' . F */
230  /*DEBUGinertia_(i + 5, i + 5) = phii.scalarProduct(Fi);*/
231 
232  inertia_(iRank, iRank) = phii.scalarProduct(Fi);
233  sotDEBUG(30) << "phi" << i << " = " << phii;
234  sotDEBUG(35) << "Fi" << i << " = " << Fi << endl;
235  sotDEBUG(25) << "IcA" << i << " = " << Ici << endl;
236  sotDEBUG(45) << "Joint " << i << " in " << iRank << endl;
237 
238  /* Ic_pi = Ic_pi + iXpi' Ic_i iXpi */
239  iVpiiT.multiply(Ici, iVpiT_Ici);
240  iVpiT_Ici.multiply(iVpii, iVpiT_Ici_iVpi);
241 
242  sotDEBUG(45) << "Icpi" << parentIndex_[i] << " = " << Ic[parentIndex_[i]];
243  Ic[parentIndex_[i]] += iVpiT_Ici_iVpi;
244 
245  sotDEBUG(45) << "Icpi" << parentIndex_[i] << "_" << i << " = "
246  << Ic[parentIndex_[i]];
247  sotDEBUG(45) << "Vpi" << i << " = " << iVpii;
248 
249  size_t j = i;
250  while (parentIndex_[j] != 0) {
251  /* F = jXpj' . F */
252  Fi = iVpiT[j] * Fi;
253  /* j = pj */
254  j = parentIndex_[j];
255  /* Hij = Hji = F' phi_j */
256  inertia_(iRank, joints_[j]->rankInConfiguration()) = inertia_(
257  joints_[j]->rankInConfiguration(), iRank) = Fi.scalarProduct(phi[j]);
258  sotDEBUG(35) << "Fi = " << Fi << endl;
259  sotDEBUG(35) << "FiXphi = " << inertia_(j + 5, i + 5) << endl;
260  }
261 
262  /* When parentIndex_[j] == 0: FREE FLYER. */
263  Fi = iVpiT[j] * Fi;
264  for (size_t k = 0; k < 6; ++k) {
265  inertia_(iRank, k) = inertia_(k, iRank) = Fi(k);
266  }
267  }
268 
269  /* --- FREE FLYER = Ic0 --- */
270  const dynamicgraph::Matrix& Ic0 = Ic[0];
271  for (size_t i = 0; i < 6; ++i)
272  for (size_t j = 0; j < 6; ++j) {
273  inertia_(i, j) = Ic0(i, j);
274  }
275 
276  sotDEBUGOUT(25);
277 }
278 
279 /* -------------------------------------------------------------------------- */
280 /* -------------------------------------------------------------------------- */
281 /* -------------------------------------------------------------------------- */
282 
284  for (size_t i = 0; i < (joints_.size() + 5); ++i)
285  for (size_t j = 0; j < (joints_.size() + 5); ++j)
286  A[i * (joints_.size() + 5) + j] = inertia_(i, j);
287 }
288 
289 const maal::boost::Matrix& MatrixInertia::getInertiaMatrix(void) {
290  return inertia_;
291 }
dynamicgraph::sot::MatrixInertia::inertia_
dynamicgraph::Matrix inertia_
Definition: matrix-inertia.h:69
m
float m
dynamicgraph::sot::MatrixHomogeneous
Eigen::Transform< double, 3, Eigen::Affine > SOT_CORE_EXPORT MatrixHomogeneous
matrix-inertia.h
dynamicgraph
i
int i
dynamicgraph::sot::MatrixInertia::iVpiT
std::vector< MatrixForce > iVpiT
Definition: matrix-inertia.h:68
debug.hh
dynamicgraph::Matrix
Eigen::MatrixXd Matrix
dynamicgraph::sot::MatrixInertia::initDofTable
void initDofTable(void)
Definition: matrix-inertia.cpp:104
dynamicgraph::sot::MatrixInertia::aHDR_
CjrlHumanoidDynamicRobot * aHDR_
Definition: matrix-inertia.h:60
dynamicgraph::sot::MatrixInertia::getInertiaMatrix
const maal::boost::Matrix & getInertiaMatrix(void)
Definition: matrix-inertia.cpp:289
res
res
sotDEBUGOUT
#define sotDEBUGOUT(level)
sotDEBUGIN
#define sotDEBUGIN(level)
dynamicgraph::sot::MatrixInertia::~MatrixInertia
~MatrixInertia(void)
Definition: matrix-inertia.cpp:118
Joint
JointTpl< double > Joint
dynamicgraph::sot::MatrixInertia::aHDMB_
dynamicsJRLJapan::HumanoidDynamicMultiBody * aHDMB_
Definition: matrix-inertia.h:61
dynamicgraph::sot::MatrixInertia::init
void init(CjrlHumanoidDynamicRobot *aHDR)
Definition: matrix-inertia.cpp:54
q
q
dynamicgraph::sot::MatrixInertia::joints_
std::vector< CjrlJoint * > joints_
Definition: matrix-inertia.h:62
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::MatrixInertia::computeInertiaMatrix
void computeInertiaMatrix()
Definition: matrix-inertia.cpp:174
dynamicgraph::sot::MatrixInertia::parentIndex_
std::vector< int > parentIndex_
Definition: matrix-inertia.h:63
dynamicgraph::sot::MatrixInertia::initParents
void initParents(void)
Definition: matrix-inertia.cpp:79
dynamicgraph::sot
x0
x0
v
v
dynamicgraph::sot::MatrixRotation
Eigen::Matrix< double, 3, 3 > SOT_CORE_EXPORT MatrixRotation
skewSymmetric
static matrix3d skewSymmetric(const vector3d &v)
Definition: matrix-inertia.cpp:24
dynamicgraph::sot::MatrixInertia::Ic
std::vector< dynamicgraph::Matrix > Ic
Definition: matrix-inertia.h:65
dynamicgraph::sot::MatrixForce
Eigen::Matrix< double, 6, 6 > SOT_CORE_EXPORT MatrixForce
dynamicgraph::sot::MatrixInertia::iVpi
std::vector< MatrixTwist > iVpi
Definition: matrix-inertia.h:67
dynamicgraph::sot::MatrixInertia::phi
std::vector< dynamicgraph::Vector > phi
Definition: matrix-inertia.h:66
A
A
dynamicgraph::sot::MatrixInertia::update
void update(void)
Definition: matrix-inertia.cpp:123
n
Vec3f n
com
com
sotDEBUG
#define sotDEBUG(level)


sot-dynamic-pinocchio
Author(s): Olivier Stasse
autogenerated on Fri Jul 28 2023 02:10:01