ocra-recipes
Doxygen documentation for the ocra-recipes repository
Model3T.cpp
Go to the documentation of this file.
1 #include "Model3T.h"
2 
3 using namespace Eigen;
4 
5 #include <string>
6 #include <sstream>
7 
8 #include <iostream>
9 
10 
11 #include <Eigen/Lgsm>
12 #include<Eigen/StdVector> //Mandatory for vector fixed-size Eigen element (like Twistd)
13  //else it raise assertion of bad alignement
14 
15 #ifndef __EIGENDEF_FOR_VECTOR__
16 #define __EIGENDEF_FOR_VECTOR__
17 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Twistd)
18 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix<double, 6, 6>)
19 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Rotation3d)
20 #endif
21 
22 extern "C"
23 {
24  ocra::Model* Create(const std::string& robotName)
25  {
26  return new Model3T(robotName);
27  }
28 }
29 
31 {
32 public:
33  VectorXd q;
34  VectorXd dq;
35  Displacementd Hroot;
36  Twistd Troot;
37 
38  VectorXd actuatedDofs;
39  VectorXd lowerLimits;
40  VectorXd upperLimits;
41 
42  Vector3d comPosition;
43  Vector3d comVelocity;
44  Vector3d comJdotQdot;
45  Matrix< double,3, Dynamic > comJacobian;
46  Matrix< double,3, Dynamic > comJacobianDot;
47 
48  MatrixXd M;
49  MatrixXd Minv;
50  MatrixXd B;
51  VectorXd n;
52  VectorXd l;
53  VectorXd g;
54 
55  std::vector< Displacementd > segPosition;
56  std::vector< Twistd > segVelocity;
57  std::vector< Matrix<double,6,Dynamic> > segJacobian;
58  std::vector< Matrix<double,6,Dynamic> > segJdot;
59  std::vector< Twistd > segJdotQdot;
60  std::vector< Matrix<double,6,Dynamic> > jointJacobian;
61  std::vector< double > segMass;
62  std::vector< Vector3d > segCoM;
63  std::vector< Matrix<double, 6, 6> > segMassMatrix;
64  std::vector< Matrix<double, 6, 6> > segNLEffects;
65  std::vector< Vector3d > segMomentsOfInertia;
66  std::vector< Rotation3d > segInertiaAxes;
67  std::vector< std::string > segName;
68 
69  Pimpl(const std::string& robotName)
70  {
71  q = VectorXd::Zero(3);
72  dq = VectorXd::Zero(3);
73  Hroot = Displacementd(0,0,0); //CONSTANT
74  Troot = Twistd(0,0,0,0,0,0); //CONSTANT
75 
76  actuatedDofs = VectorXd::Ones(3); //CONSTANT
77  lowerLimits = VectorXd::Constant(3, -10.); //CONSTANT
78  upperLimits = VectorXd::Constant(3, 10.); //CONSTANT
79 
80  comPosition = Vector3d(0,0,0);
81  comVelocity = Vector3d(0,0,0);
82  comJdotQdot = Vector3d(0,0,0);
83  comJacobian = Matrix<double,3,3>::Zero(); //CONSTANT
84  comJacobianDot = Matrix<double,3,3>::Zero(); //CONSTANT
85 
86  M = MatrixXd::Zero(3,3);
87  Minv = MatrixXd::Zero(3,3);
88  B = MatrixXd::Identity(3,3) * 0.001;
89  n = VectorXd::Zero(3);
90  l = VectorXd::Zero(3);
91  g = VectorXd::Zero(3);
92 
93  for (int i=0; i<4; i++)
94  {
95  segPosition.push_back(Displacementd(0,0,0));
96  segVelocity.push_back(Twistd(0,0,0,0,0,0));
97  segJacobian.push_back( Matrix<double,6,3>::Zero() ); //CONSTANT
98  segJdot.push_back( Matrix<double,6,3>::Zero() ); //CONSTANT
99  segJdotQdot.push_back(Twistd(0,0,0,0,0,0)); //CONSTANT
100  jointJacobian.push_back( Matrix<double,6,3>::Zero() ); //CONSTANT
101  segMass.push_back(1.); //CONSTANT
102  segCoM.push_back( Vector3d(0,0,0) ); //CONSTANT
103  segMassMatrix.push_back( Matrix<double,6,6>::Zero() ); //CONSTANT
104  segMomentsOfInertia.push_back( Vector3d(0.1,0.1,0.1) ); //CONSTANT
105  segInertiaAxes.push_back( Rotation3d(1,0,0,0) ); //CONSTANT
106 
107  std::stringstream name;
108  name <<robotName<<".segment_"<<i;
109  segName.push_back( name.str() ); //CONSTANT
110 
111  segNLEffects.push_back( Matrix<double,6,6>::Zero() ); //CONSTANT
112  }
113 
114 
116 
117  segJacobian[0] << 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0;
118  jointJacobian[0] << 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0;
119 
120  segJacobian[1] << 0,0,0, 0,0,0, 0,0,0, 1,0,0, 0,0,0, 0,0,0;
121  jointJacobian[1] << 0,0,0, 0,0,0, 0,0,0, 1,0,0, 0,0,0, 0,0,0;
122 
123  segJacobian[2] << 0,0,0, 0,0,0, 0,0,0, 1,0,0, 0,1,0, 0,0,0;
124  jointJacobian[2] << 0,0,0, 0,0,0, 0,0,0, 1,0,0, 0,1,0, 0,0,0;
125 
126  segJacobian[3] << 0,0,0, 0,0,0, 0,0,0, 1,0,0, 0,1,0, 0,0,1;
127  jointJacobian[3] << 0,0,0, 0,0,0, 0,0,0, 1,0,0, 0,1,0, 0,0,1;
128 
129  for (int i=0; i<4; i++)
130  {
131  segMassMatrix[i].diagonal() << segMomentsOfInertia[i][0], segMomentsOfInertia[i][1], segMomentsOfInertia[i][2], segMass[i], segMass[i], segMass[i];
132  }
133 
134  // We assume that the gravity is along -Z and is -9.81!
135  g << 0, 0, 9.80665*segMass[3];
136 
137  for (int i=1; i<4; i++)
138  {
139  M += segJacobian[i].transpose() * segMassMatrix[i] * segJacobian[i];
140  }
141 
142  Minv = M.inverse();
143 
144  comJacobian = ( segMass[0]*segJacobian[0].bottomRows<3>()
145  + segMass[1]*segJacobian[1].bottomRows<3>()
146  + segMass[2]*segJacobian[2].bottomRows<3>()
147  + segMass[3]*segJacobian[3].bottomRows<3>()
148  )/( segMass[0]+segMass[1]+segMass[2]+segMass[3] );
149  }
150 
151 };
152 
153 
154 
155 
156 Model3T::Model3T(const std::string& robotName)
157  : ocra::Model(robotName, 3, false)
158  , pimpl( new Pimpl(robotName))
159 {
160  update();
161 }
162 
164 {
165 
166 }
167 
168 
171 const VectorXd& Model3T::getJointPositions() const
172 {
173  return pimpl->q;
174 }
175 
176 const VectorXd& Model3T::getJointVelocities() const
177 {
178  return pimpl->dq;
179 }
180 
181 const Displacementd& Model3T::getFreeFlyerPosition() const
182 {
183  return pimpl->Hroot;
184 }
185 
186 const Twistd& Model3T::getFreeFlyerVelocity() const
187 {
188  return pimpl->Troot;
189 }
190 
192 {
193  return 4;
194 }
195 
196 const VectorXd& Model3T::getActuatedDofs() const
197 {
198  return pimpl->actuatedDofs;
199 }
200 
201 const VectorXd& Model3T::getJointLowerLimits() const
202 {
203  return pimpl->lowerLimits;
204 }
205 
206 const VectorXd& Model3T::getJointUpperLimits() const
207 {
208  return pimpl->upperLimits;
209 }
210 
211 //CoM
212 double Model3T::getMass() const
213 {
214  double mass=0;
215  for (int i=0; i<nbSegments(); i++)
216  {
217  mass += getSegmentMass(i);
218  }
219  return mass;
220 }
221 
222 const Vector3d& Model3T::getCoMPosition() const
223 {
224  return pimpl->comPosition;
225 }
226 
227 const Vector3d& Model3T::getCoMVelocity() const
228 {
229  return pimpl->comVelocity;
230 }
231 
232 const Vector3d& Model3T::getCoMJdotQdot() const
233 {
234  return pimpl->comJdotQdot;
235 }
236 
237 const Matrix<double,3,Dynamic>& Model3T::getCoMJacobian() const
238 {
239  return pimpl->comJacobian;
240 }
241 
242 const Matrix<double,3,Dynamic>& Model3T::getCoMJacobianDot() const
243 {
244  return pimpl->comJacobianDot;
245 }
246 
247 //dynamic/static equation terms
248 const MatrixXd& Model3T::getInertiaMatrix() const
249 {
250  return pimpl->M;
251 }
252 
253 const MatrixXd& Model3T::getInertiaMatrixInverse() const
254 {
255  return pimpl->Minv;
256 }
257 
258 const MatrixXd& Model3T::getDampingMatrix() const
259 {
260  return pimpl->B;
261 }
262 
263 const VectorXd& Model3T::getNonLinearTerms() const
264 {
265  return pimpl->n;
266 }
267 
268 const VectorXd& Model3T::getLinearTerms() const
269 {
270  return pimpl->l;
271 }
272 
273 const VectorXd& Model3T::getGravityTerms() const
274 {
275  return pimpl->g;
276 }
277 
278 //segment data
279 const Displacementd& Model3T::getSegmentPosition(int index) const
280 {
281  return pimpl->segPosition[index];
282 }
283 
284 const Twistd& Model3T::getSegmentVelocity(int index) const
285 {
286  return pimpl->segVelocity[index];
287 }
288 
289 const Matrix<double,6,Dynamic>& Model3T::getSegmentJacobian(int index) const
290 {
291  return pimpl->segJacobian[index];
292 }
293 
294 const Matrix<double,6,Dynamic>& Model3T::getSegmentJdot(int index) const
295 {
296  return pimpl->segJdot[index];
297 }
298 
299 const Twistd& Model3T::getSegmentJdotQdot(int index) const
300 
301 {
302  return pimpl->segJdotQdot[index];
303 }
304 
305 const Matrix<double,6,Dynamic>& Model3T::getJointJacobian(int index) const
306 
307 {
308  return pimpl->jointJacobian[index];
309 }
310 
311 double Model3T::getSegmentMass(int index) const
312 
313 {
314  return pimpl->segMass[index];
315 }
316 
317 const Vector3d& Model3T::getSegmentCoM(int index) const
318 
319 {
320  return pimpl->segCoM[index];
321 }
322 
323 const Matrix<double, 6, 6>& Model3T::getSegmentMassMatrix(int index) const
324 
325 {
326  return pimpl->segMassMatrix[index];
327 }
328 
329 const Vector3d& Model3T::getSegmentMomentsOfInertia(int index) const
330 
331 {
332  return pimpl->segMomentsOfInertia[index];
333 }
334 
335 
336 const Rotation3d& Model3T::getSegmentInertiaAxes(int index) const
337 
338 {
339  return pimpl->segInertiaAxes[index];
340 }
341 
342 
343 
344 void Model3T::doSetJointPositions(const VectorXd& q)
345 {
346  pimpl->q = q;
347  update();
348 }
349 
350 void Model3T::doSetJointVelocities(const VectorXd& q_dot)
351 {
352  pimpl->dq = q_dot;
353  update();
354 }
355 
356 void Model3T::doSetFreeFlyerPosition(const Displacementd& H_root)
357 {
358  pimpl->Hroot = H_root;
359 
360  update();
361 }
362 
363 void Model3T::doSetFreeFlyerVelocity(const Twistd& T_root)
364 {
365  pimpl->Troot = T_root;
366 
367  update();
368 }
369 
370 int Model3T::doGetSegmentIndex(const std::string& name) const
371 {
372  for ( int i=0; i<nbSegments(); i++ )
373  {
374  if (pimpl->segName[i] == name)
375  {
376  return i;
377  }
378  }
379 
380  throw( std::string("error, cannot find segment with name ") + name );
381 }
382 
383 const std::string& Model3T::doGetSegmentName(int index) const
384 {
385  return pimpl->segName[index];
386 }
387 
388 
389 
390 
394 
396 {
397  // update segments positions
398  pimpl->segPosition[1].x() =pimpl->q[0];
399 
400  pimpl->segPosition[2].x() =pimpl->q[0];
401  pimpl->segPosition[2].y() =pimpl->q[1];
402 
403  pimpl->segPosition[3].x() =pimpl->q[0];
404  pimpl->segPosition[3].y() =pimpl->q[1];
405  pimpl->segPosition[3].z() =pimpl->q[2];
406 
407  // update segments velocities
408  pimpl->segVelocity[1].vx() =pimpl->dq[0];
409 
410  pimpl->segVelocity[2].vx() =pimpl->dq[0];
411  pimpl->segVelocity[2].vy() =pimpl->dq[1];
412 
413  pimpl->segVelocity[3].vx() =pimpl->dq[0];
414  pimpl->segVelocity[3].vy() =pimpl->dq[1];
415  pimpl->segVelocity[3].vz() =pimpl->dq[2];
416 
417  //
418  pimpl->comPosition = ( pimpl->segMass[0] * pimpl->segPosition[0].getTranslation()
419  + pimpl->segMass[1] * pimpl->segPosition[1].getTranslation()
420  + pimpl->segMass[2] * pimpl->segPosition[2].getTranslation()
421  + pimpl->segMass[3] * pimpl->segPosition[3].getTranslation()
422  ) / getMass();
423 
424  pimpl->comVelocity = ( pimpl->segMass[0] * pimpl->segVelocity[0].getLinearVelocity()
425  + pimpl->segMass[1] * pimpl->segVelocity[1].getLinearVelocity()
426  + pimpl->segMass[2] * pimpl->segVelocity[2].getLinearVelocity()
427  + pimpl->segMass[3] * pimpl->segVelocity[3].getLinearVelocity()
428  ) / getMass();
429 
430 }
431 
432 
433 
437 
439 {
440  std::cout<<"nb segments: "<<nbSegments()<<"\n"; //CONSTANT
441  std::cout<<"actuated: "<<getActuatedDofs()<<"\n"; //CONSTANT
442  std::cout<<"lower: "<<getJointLowerLimits()<<"\n"; //CONSTANT
443  std::cout<<"upper: "<<getJointUpperLimits()<<"\n"; //CONSTANT
444 
445  std::cout<<"total mass: "<<getMass()<<"\n";
446 
447  std::cout<<"M: "<<getInertiaMatrix()<<"\n";
448  std::cout<<"Minv: "<<getInertiaMatrixInverse()<<"\n";
449  std::cout<<"B: "<<getDampingMatrix()<<"\n";
450  std::cout<<"l: "<<getLinearTerms()<<"\n";
451 
452  for (int i=0; i<nbSegments(); i++)
453  {
454  std::cout<<"name ("<<i<<"): "<<getSegmentName(i)<<"\n";
455  std::cout<<"index ("<<i<<"): "<<getSegmentIndex(getSegmentName(i))<<"\n";
456  std::cout<<"Jseg ("<<i<<"): "<<getSegmentJacobian(i)<<"\n";
457  std::cout<<"dJseg ("<<i<<"): "<<getSegmentJdot(i)<<"\n";
458  std::cout<<"dJsegdq ("<<i<<"): "<<getSegmentJdotQdot(i)<<"\n";
459  std::cout<<"Jjoint ("<<i<<"): "<<getJointJacobian(i)<<"\n";
460  std::cout<<"segmass ("<<i<<"): "<<getSegmentMass(i)<<"\n";
461  std::cout<<"segcom ("<<i<<"): "<<getSegmentCoM(i)<<"\n";
462  std::cout<<"segMassMat ("<<i<<"): "<<getSegmentMassMatrix(i)<<"\n";
463  std::cout<<"segMoI ("<<i<<"): "<<getSegmentMomentsOfInertia(i)<<"\n";
464  std::cout<<"segIAxes ("<<i<<"): "<<getSegmentInertiaAxes(i)<<"\n";
465  }
466 }
467 
468 
469 
471 {
472  std::cout<<"q: "<<getJointPositions()<<"\n";
473  std::cout<<"dq: "<<getJointVelocities()<<"\n";
474  //std::cout<<"Hroot: "<<getFreeFlyerPosition()<<"\n"; // meaningless because fixed root
475  //std::cout<<"Troot: "<<getFreeFlyerVelocity()<<"\n"; //
476 
477  std::cout<<"compos: "<<getCoMPosition()<<"\n";
478  std::cout<<"comvel: "<<getCoMVelocity()<<"\n";
479 
480  for (int i=0; i<nbSegments(); i++)
481  {
482  std::cout<<"segpos ("<<i<<"): "<<getSegmentPosition(i)<<"\n";
483  std::cout<<"segvel ("<<i<<"): "<<getSegmentVelocity(i)<<"\n";
484  }
485 
486  std::cout<<"n: "<<getNonLinearTerms()<<"\n";
487  std::cout<<"g: "<<getGravityTerms()<<"\n";
488 }
489 
490 
491 
492 
493 
494 
virtual void doSetJointPositions(const Eigen::VectorXd &q)
Definition: Model3T.cpp:344
virtual const Eigen::Rotation3d & getSegmentInertiaAxes(int index) const
Definition: Model3T.cpp:336
VectorXd l
Definition: Model3T.cpp:52
virtual const Eigen::Vector3d & getCoMVelocity() const
Definition: Model3T.cpp:227
virtual const Eigen::Matrix< double, 3, Eigen::Dynamic > & getCoMJacobian() const
Definition: Model3T.cpp:237
virtual const Eigen::MatrixXd & getInertiaMatrix() const
Definition: Model3T.cpp:248
virtual const Eigen::VectorXd & getJointPositions() const
Definition: Model3T.cpp:171
virtual const Eigen::Twistd & getSegmentVelocity(int index) const
Definition: Model3T.cpp:284
virtual ~Model3T()
Definition: Model3T.cpp:163
std::vector< Matrix< double, 6, Dynamic > > segJdot
Definition: Model3T.cpp:58
Vector3d comVelocity
Definition: Model3T.cpp:43
virtual const Eigen::Matrix< double, 6, Eigen::Dynamic > & getJointJacobian(int index) const
Definition: Model3T.cpp:305
Definition: Model3T.h:7
virtual void update()
Definition: Model3T.cpp:395
virtual double getMass() const
Definition: Model3T.cpp:212
virtual const Eigen::Vector3d & getSegmentMomentsOfInertia(int index) const
Definition: Model3T.cpp:329
Matrix< double, 3, Dynamic > comJacobianDot
Definition: Model3T.cpp:46
virtual const Eigen::MatrixXd & getDampingMatrix() const
Definition: Model3T.cpp:258
std::vector< Matrix< double, 6, 6 > > segNLEffects
Definition: Model3T.cpp:64
void testVariableData()
Definition: Model3T.cpp:470
virtual int doGetSegmentIndex(const std::string &name) const
Definition: Model3T.cpp:370
Pimpl(const std::string &robotName)
Definition: Model3T.cpp:69
Model3T(const std::string &robotName)
Definition: Model3T.cpp:156
Model class.
Definition: Model.h:38
ocra::Model * Create(const std::string &robotName)
Definition: Model3T.cpp:24
Matrix< double, 3, Dynamic > comJacobian
Definition: Model3T.cpp:45
virtual const Eigen::Vector3d & getCoMJdotQdot() const
Definition: Model3T.cpp:232
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual const Eigen::Matrix< double, 6, Eigen::Dynamic > & getSegmentJacobian(int index) const
Definition: Model3T.cpp:289
virtual const Eigen::VectorXd & getGravityTerms() const
Definition: Model3T.cpp:273
void testConstantData()
Definition: Model3T.cpp:438
Vector3d comPosition
Definition: Model3T.cpp:42
virtual const Eigen::VectorXd & getJointLowerLimits() const
Definition: Model3T.cpp:201
std::vector< Rotation3d > segInertiaAxes
Definition: Model3T.cpp:66
VectorXd upperLimits
Definition: Model3T.cpp:40
int getSegmentIndex(const std::string &name) const
Definition: Model.cpp:192
std::vector< std::string > segName
Definition: Model3T.cpp:67
virtual const std::string & doGetSegmentName(int index) const
Definition: Model3T.cpp:383
std::vector< Matrix< double, 6, Dynamic > > segJacobian
Definition: Model3T.cpp:57
virtual const Eigen::VectorXd & getActuatedDofs() const
Definition: Model3T.cpp:196
std::vector< Twistd > segVelocity
Definition: Model3T.cpp:56
VectorXd lowerLimits
Definition: Model3T.cpp:39
virtual const Eigen::VectorXd & getJointUpperLimits() const
Definition: Model3T.cpp:206
virtual void doSetFreeFlyerPosition(const Eigen::Displacementd &H_root)
Definition: Model3T.cpp:356
MatrixXd Minv
Definition: Model3T.cpp:49
virtual const Eigen::Twistd & getFreeFlyerVelocity() const
Definition: Model3T.cpp:186
Vector3d comJdotQdot
Definition: Model3T.cpp:44
virtual const Eigen::Displacementd & getSegmentPosition(int index) const
Definition: Model3T.cpp:279
virtual const Eigen::Vector3d & getSegmentCoM(int index) const
Definition: Model3T.cpp:317
virtual const Eigen::VectorXd & getJointVelocities() const
Definition: Model3T.cpp:176
VectorXd q
Definition: Model3T.cpp:33
VectorXd g
Definition: Model3T.cpp:53
const std::string & getSegmentName(int index) const
Definition: Model.cpp:197
std::vector< Twistd > segJdotQdot
Definition: Model3T.cpp:59
std::vector< double > segMass
Definition: Model3T.cpp:61
virtual void doSetJointVelocities(const Eigen::VectorXd &q_dot)
Definition: Model3T.cpp:350
virtual int nbSegments() const
Definition: Model3T.cpp:191
VectorXd dq
Definition: Model3T.cpp:34
virtual const Eigen::Matrix< double, 6, 6 > & getSegmentMassMatrix(int index) const
Definition: Model3T.cpp:323
virtual const Eigen::Matrix< double, 6, Eigen::Dynamic > & getSegmentJdot(int index) const
Definition: Model3T.cpp:294
MatrixXd M
Definition: Model3T.cpp:48
VectorXd actuatedDofs
Definition: Model3T.cpp:38
virtual const Eigen::MatrixXd & getInertiaMatrixInverse() const
Definition: Model3T.cpp:253
virtual const Eigen::VectorXd & getNonLinearTerms() const
Definition: Model3T.cpp:263
virtual const Eigen::Twistd & getSegmentJdotQdot(int index) const
Definition: Model3T.cpp:299
std::vector< Vector3d > segMomentsOfInertia
Definition: Model3T.cpp:65
Twistd Troot
Definition: Model3T.cpp:36
std::vector< Matrix< double, 6, 6 > > segMassMatrix
Definition: Model3T.cpp:63
virtual const Eigen::Matrix< double, 3, Eigen::Dynamic > & getCoMJacobianDot() const
Definition: Model3T.cpp:242
virtual void doSetFreeFlyerVelocity(const Eigen::Twistd &T_root)
Definition: Model3T.cpp:363
std::vector< Matrix< double, 6, Dynamic > > jointJacobian
Definition: Model3T.cpp:60
std::vector< Vector3d > segCoM
Definition: Model3T.cpp:62
std::vector< Displacementd > segPosition
Definition: Model3T.cpp:55
virtual const Eigen::Vector3d & getCoMPosition() const
Definition: Model3T.cpp:222
virtual double getSegmentMass(int index) const
Definition: Model3T.cpp:311
Displacementd Hroot
Definition: Model3T.cpp:35
MatrixXd B
Definition: Model3T.cpp:50
VectorXd n
Definition: Model3T.cpp:51
virtual const Eigen::Displacementd & getFreeFlyerPosition() const
Definition: Model3T.cpp:181
virtual const Eigen::VectorXd & getLinearTerms() const
Definition: Model3T.cpp:268