pinocchio  2.4.4
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
joint-composite.hpp
1 //
2 // Copyright (c) 2016-2020 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_joint_composite_hpp__
6 #define __pinocchio_joint_composite_hpp__
7 
8 #include "pinocchio/multibody/joint/fwd.hpp"
9 #include "pinocchio/multibody/joint/joint-collection.hpp"
10 #include "pinocchio/multibody/joint/joint-basic-visitors.hpp"
11 #include "pinocchio/container/aligned-vector.hpp"
12 #include "pinocchio/spatial/act-on-set.hpp"
13 
14 #include "pinocchio/serialization/fwd.hpp"
15 
16 namespace pinocchio
17 {
18 
19  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
21 
22  template<typename _Scalar, int _Options, template<typename S, int O> class JointCollectionTpl>
23  struct traits< JointCompositeTpl<_Scalar,_Options,JointCollectionTpl> >
24  {
25  typedef _Scalar Scalar;
26 
27  enum {
28  Options = _Options,
29  NQ = Eigen::Dynamic,
30  NV = Eigen::Dynamic
31  };
32 
33  typedef JointCollectionTpl<Scalar,Options> JointCollection;
40 
41  // [ABA]
42  typedef Eigen::Matrix<Scalar,6,Eigen::Dynamic,Options> U_t;
43  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Options> D_t;
44  typedef Eigen::Matrix<Scalar,6,Eigen::Dynamic,Options> UD_t;
45 
46  PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
47 
48  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1,Options> ConfigVector_t;
49  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1,Options> TangentVector_t;
50  };
51 
52  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
53  struct traits< JointModelCompositeTpl<Scalar,Options,JointCollectionTpl> >
55 
56  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
57  struct traits< JointDataCompositeTpl<Scalar,Options,JointCollectionTpl> >
59 
60  template<typename _Scalar, int _Options, template<typename S, int O> class JointCollectionTpl>
62  : public JointDataBase< JointDataCompositeTpl<_Scalar,_Options,JointCollectionTpl> >
63  {
64  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
65 
68  PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE(JointDerived);
69  PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
70 
71  typedef JointCollectionTpl<Scalar,Options> JointCollection;
72  typedef JointDataTpl<Scalar,Options,JointCollectionTpl> JointDataVariant;
73 
74  typedef PINOCCHIO_ALIGNED_STD_VECTOR(JointDataVariant) JointDataVector;
75 
76  // JointDataComposite() {} // can become necessary if we want a vector of JointDataComposite ?
77 
79  : joints()
80  , iMlast(0)
81  , pjMi(0)
82  , S(0)
83  , M(Transformation_t::Identity())
84  , v(Motion_t::Zero())
85  , c(Motion_t::Zero())
86  , U(6,0), Dinv(0,0), UDinv(6,0)
87  , StU(0,0)
88  {}
89 
90 
91  JointDataCompositeTpl(const JointDataVector & joint_data, const int /*nq*/, const int nv)
92  : joints(joint_data), iMlast(joint_data.size()), pjMi(joint_data.size())
93  , S(Constraint_t::Zero(nv))
94  , M(Transformation_t::Identity())
95  , v(Motion_t::Zero())
96  , c(Motion_t::Zero())
97  , U(U_t::Zero(6,nv))
98  , Dinv(D_t::Zero(nv,nv))
99  , UDinv(UD_t::Zero(6,nv))
100  , StU(D_t::Zero(nv,nv))
101  {}
102 
104  JointDataVector joints;
105 
107  PINOCCHIO_ALIGNED_STD_VECTOR(Transformation_t) iMlast;
108 
110  PINOCCHIO_ALIGNED_STD_VECTOR(Transformation_t) pjMi;
111 
112  Constraint_t S;
113  Transformation_t M;
114  Motion_t v;
115  Bias_t c;
116 
117  // // [ABA] specific data
118  U_t U;
119  D_t Dinv;
120  UD_t UDinv;
121 
122  D_t StU;
123 
124  static std::string classname() { return std::string("JointDataComposite"); }
125  std::string shortname() const { return classname(); }
126 
127  };
128 
129  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
130  inline std::ostream & operator <<(std::ostream & os, const JointDataCompositeTpl<Scalar,Options,JointCollectionTpl> & jdata)
131  {
133 
134  os << "JointDataComposite containing following models:\n" ;
135  for (typename JointDataVector::const_iterator it = jdata.joints.begin();
136  it != jdata.joints.end(); ++it)
137  os << " " << shortname(*it) << std::endl;
138  return os;
139  }
140 
141 
142  template<typename NewScalar, typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
143  struct CastType< NewScalar, JointModelCompositeTpl<Scalar,Options,JointCollectionTpl> >
144  {
146  };
147 
148  template<typename _Scalar, int _Options, template<typename S, int O> class JointCollectionTpl>
150  : public JointModelBase< JointModelCompositeTpl<_Scalar,_Options,JointCollectionTpl> >
151  {
152  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
153 
156  PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(JointDerived);
157 
158  typedef JointCollectionTpl<Scalar,Options> JointCollection;
159  typedef JointModelTpl<Scalar,Options,JointCollectionTpl> JointModelVariant;
160 
161  typedef SE3Tpl<Scalar,Options> SE3;
164 
165  typedef PINOCCHIO_ALIGNED_STD_VECTOR(JointModelVariant) JointModelVector;
166 
167  using Base::id;
168  using Base::idx_q;
169  using Base::idx_v;
170  using Base::setIndexes;
171  using Base::nq;
172  using Base::nv;
173 
176  : joints()
177  , jointPlacements()
178  , m_nq(0)
179  , m_nv(0)
180  , njoints(0)
181  {}
182 
184  JointModelCompositeTpl(const size_t size)
185  : joints()
186  , jointPlacements()
187  , m_nq(0)
188  , m_nv(0)
189  , njoints(0)
190  {
191  joints.reserve(size); jointPlacements.reserve(size);
192  m_idx_q.reserve(size); m_idx_v.reserve(size);
193  m_nqs.reserve(size); m_nvs.reserve(size);
194  }
195 
202  template<typename JointModel>
204  const SE3 & placement = SE3::Identity())
205  : joints(1,(JointModelVariant)jmodel.derived())
206  , jointPlacements(1,placement)
207  , m_nq(jmodel.nq())
208  , m_nv(jmodel.nv())
209  , m_idx_q(1,0), m_nqs(1,jmodel.nq())
210  , m_idx_v(1,0), m_nvs(1,jmodel.nv())
211  , njoints(1)
212  {}
213 
220  : Base(other)
221  , joints(other.joints)
222  , jointPlacements(other.jointPlacements)
223  , m_nq(other.m_nq)
224  , m_nv(other.m_nv)
225  , m_idx_q(other.m_idx_q), m_nqs(other.m_nqs)
226  , m_idx_v(other.m_idx_v), m_nvs(other.m_nvs)
227  , njoints(other.njoints)
228  {}
229 
230 
239  template<typename JointModel>
240  JointModelDerived & addJoint(const JointModelBase<JointModel> & jmodel,
241  const SE3 & placement = SE3::Identity())
242  {
243  joints.push_back((JointModelVariant)jmodel.derived());
244  jointPlacements.push_back(placement);
245 
246  m_nq += jmodel.nq(); m_nv += jmodel.nv();
247 
248  updateJointIndexes();
249  njoints++;
250 
251  return *this;
252  }
253 
254  JointDataDerived createData() const
255  {
256  typename JointDataDerived::JointDataVector jdata(joints.size());
257  for (int i = 0; i < (int)joints.size(); ++i)
258  jdata[(size_t)i] = ::pinocchio::createData<Scalar,Options,JointCollectionTpl>(joints[(size_t)i]);
259  return JointDataDerived(jdata,nq(),nv());
260  }
261 
262  template<typename, int, template<typename S, int O> class, typename>
263  friend struct JointCompositeCalcZeroOrderStep;
264 
265  template<typename ConfigVectorType>
266  void calc(JointDataDerived & data, const Eigen::MatrixBase<ConfigVectorType> & qs) const;
267 
268  template<typename, int, template<typename S, int O> class, typename, typename>
269  friend struct JointCompositeCalcFirstOrderStep;
270 
271  template<typename ConfigVectorType, typename TangentVectorType>
272  void calc(JointDataDerived & data,
273  const Eigen::MatrixBase<ConfigVectorType> & qs,
274  const Eigen::MatrixBase<TangentVectorType> & vs) const;
275 
276  template<typename Matrix6Like>
277  void calc_aba(JointDataDerived & data,
278  const Eigen::MatrixBase<Matrix6Like> & I,
279  const bool update_I) const
280  {
281  data.U.noalias() = I * data.S.matrix();
282  data.StU.noalias() = data.S.matrix().transpose() * data.U;
283 
284  // compute inverse
285 // data.Dinv.setIdentity();
286 // data.StU.llt().solveInPlace(data.Dinv);
287  internal::PerformStYSInversion<Scalar>::run(data.StU,data.Dinv);
288  data.UDinv.noalias() = data.U * data.Dinv;
289 
290  if (update_I)
291  PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I).noalias() -= data.UDinv * data.U.transpose();
292  }
293 
294  int nv_impl() const { return m_nv; }
295  int nq_impl() const { return m_nq; }
296 
300  void setIndexes_impl(JointIndex id, int q, int v)
301  {
302  Base::setIndexes_impl(id, q, v);
303  updateJointIndexes();
304  }
305 
306  static std::string classname() { return std::string("JointModelComposite"); }
307  std::string shortname() const { return classname(); }
308 
309  JointModelCompositeTpl & operator=(const JointModelCompositeTpl & other)
310  {
311  Base::operator=(other);
312  m_nq = other.m_nq;
313  m_nv = other.m_nv;
314  m_idx_q = other.m_idx_q;
315  m_idx_v = other.m_idx_v;
316  m_nqs = other.m_nqs;
317  m_nvs = other.m_nvs;
318  joints = other.joints;
319  jointPlacements = other.jointPlacements;
320  njoints = other.njoints;
321 
322  return *this;
323  }
324 
325  using Base::isEqual;
326  bool isEqual(const JointModelCompositeTpl & other) const
327  {
328  return Base::isEqual(other)
329  && nq() == other.nq()
330  && nv() == other.nv()
331  && m_idx_q == other.m_idx_q
332  && m_idx_v == other.m_idx_v
333  && m_nqs == other.m_nqs
334  && m_nvs == other.m_nvs
335  && joints == other.joints
336  && jointPlacements == other.jointPlacements
337  && njoints == other.njoints;
338  }
339 
341  template<typename NewScalar>
343  {
345  ReturnType res((size_t)njoints);
346  res.setIndexes(id(),idx_q(),idx_v());
347  res.m_nq = m_nq;
348  res.m_nv = m_nv;
349  res.m_idx_q = m_idx_q;
350  res.m_idx_v = m_idx_v;
351  res.m_nqs = m_nqs;
352  res.m_nvs = m_nvs;
353  res.njoints = njoints;
354 
355  res.joints.resize(joints.size());
356  res.jointPlacements.resize(jointPlacements.size());
357  for(size_t k = 0; k < jointPlacements.size(); ++k)
358  {
359  res.joints[k] = joints[k].template cast<NewScalar>();
360  res.jointPlacements[k] = jointPlacements[k].template cast<NewScalar>();
361  }
362 
363  return res;
364  }
365 
367  JointModelVector joints;
369  PINOCCHIO_ALIGNED_STD_VECTOR(SE3) jointPlacements;
370 
371  template<typename D>
372  typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
373  jointConfigSelector(const Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_q,nq()); }
374  template<typename D>
375  typename SizeDepType<NQ>::template SegmentReturn<D>::Type
376  jointConfigSelector( Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_q,nq()); }
377 
378  template<typename D>
379  typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
380  jointVelocitySelector(const Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_v,nv()); }
381  template<typename D>
382  typename SizeDepType<NV>::template SegmentReturn<D>::Type
383  jointVelocitySelector( Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_v,nv()); }
384 
385  template<typename D>
386  typename SizeDepType<NV>::template ColsReturn<D>::ConstType
387  jointCols(const Eigen::MatrixBase<D>& A) const { return A.middleCols(Base::i_v,nv()); }
388  template<typename D>
389  typename SizeDepType<NV>::template ColsReturn<D>::Type
390  jointCols(Eigen::MatrixBase<D>& A) const { return A.middleCols(Base::i_v,nv()); }
391 
392  template<typename D>
393  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::ConstType
394  jointConfigSelector_impl(const Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_q,nq()); }
395  template<typename D>
396  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::Type
397  jointConfigSelector_impl(Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_q,nq()); }
398  template<typename D>
399  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::ConstType
400  jointVelocitySelector_impl(const Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_v,nv()); }
401  template<typename D>
402  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::Type
403  jointVelocitySelector_impl(Eigen::MatrixBase<D>& a) const { return a.segment(Base::i_v,nv()); }
404 
405  template<typename D>
406  typename SizeDepType<Eigen::Dynamic>::template ColsReturn<D>::ConstType
407  jointCols_impl(const Eigen::MatrixBase<D>& A) const { return A.middleCols(Base::i_v,nv()); }
408  template<typename D>
409  typename SizeDepType<Eigen::Dynamic>::template ColsReturn<D>::Type
410  jointCols_impl(Eigen::MatrixBase<D>& A) const { return A.middleCols(Base::i_v,nv()); }
411 
412 
413  protected:
414 
415  friend struct Serialize<JointModelCompositeTpl>;
416 
417  template<typename, int, template<typename,int> class>
418  friend struct JointModelCompositeTpl;
419 
423  {
424  int idx_q = this->idx_q();
425  int idx_v = this->idx_v();
426 
427  m_idx_q.resize(joints.size());
428  m_idx_v.resize(joints.size());
429  m_nqs.resize(joints.size());
430  m_nvs.resize(joints.size());
431 
432  for(size_t i = 0; i < joints.size(); ++i)
433  {
434  JointModelVariant & joint = joints[i];
435 
436  m_idx_q[i] = idx_q; m_idx_v[i] = idx_v;
437  ::pinocchio::setIndexes(joint,i,idx_q,idx_v);
438  m_nqs[i] = ::pinocchio::nq(joint);
439  m_nvs[i] = ::pinocchio::nv(joint);
440  idx_q += m_nqs[i]; idx_v += m_nvs[i];
441  }
442  }
443 
444 
446  int m_nq, m_nv;
447 
449 
451  std::vector<int> m_idx_q;
453  std::vector<int> m_nqs;
455  std::vector<int> m_idx_v;
457  std::vector<int> m_nvs;
458 
459  public:
461  int njoints;
462  };
463 
464 
465  template<typename Scalar, int Options, template<typename,int> class JointCollectionTpl>
466  inline std::ostream & operator <<(std::ostream & os, const JointModelCompositeTpl<Scalar,Options,JointCollectionTpl> & jmodel)
467  {
469 
470  os << "JointModelComposite containing following models:\n" ;
471  for (typename JointModelVector::const_iterator it = jmodel.joints.begin();
472  it != jmodel.joints.end(); ++it)
473  os << " " << shortname(*it) << std::endl;
474 
475  return os;
476  }
477 
478 } // namespace pinocchio
479 
480 #include <boost/type_traits.hpp>
481 
482 namespace boost
483 {
484  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
485  struct has_nothrow_constructor< ::pinocchio::JointModelCompositeTpl<Scalar,Options,JointCollectionTpl> >
486  : public integral_constant<bool,true> {};
487 
488  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
489  struct has_nothrow_copy< ::pinocchio::JointModelCompositeTpl<Scalar,Options,JointCollectionTpl> >
490  : public integral_constant<bool,true> {};
491 
492  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
493  struct has_nothrow_constructor< ::pinocchio::JointDataCompositeTpl<Scalar,Options,JointCollectionTpl> >
494  : public integral_constant<bool,true> {};
495 
496  template<typename Scalar, int Options, template<typename S, int O> class JointCollectionTpl>
497  struct has_nothrow_copy< ::pinocchio::JointDataCompositeTpl<Scalar,Options,JointCollectionTpl> >
498  : public integral_constant<bool,true> {};
499 }
500 
501 /* --- Details -------------------------------------------------------------- */
502 /* --- Details -------------------------------------------------------------- */
503 /* --- Details -------------------------------------------------------------- */
504 #include "pinocchio/multibody/joint/joint-composite.hxx"
505 
506 #endif // ifndef __pinocchio_joint_composite_hpp__
int nv(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointNvVisitor to get the dimension of the joint tangent space...
std::vector< int > m_nvs
Dimension of the segment in the tangent vector.
JointModelCompositeTpl< NewScalar, Options, JointCollectionTpl > cast() const
int idx_q(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointIdxQVisitor to get the index in the full model configuration space...
std::vector< int > m_idx_v
Index in the tangent vector.
Definition: casadi.hpp:13
void updateJointIndexes()
Update the indexes of the joints contained in the composition according to the position of the joint ...
int idx_v(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointIdxVVisitor to get the index in the full model tangent space corre...
int nq(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointNqVisitor to get the dimension of the joint configuration space...
JointDataTpl< Scalar, Options, JointCollectionTpl > createData(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through CreateData visitor to create a JointDataTpl.
std::vector< int > m_idx_q
Keep information of both the dimension and the position of the joints in the composition.
JointModelCompositeTpl(const JointModelBase< JointModel > &jmodel, const SE3 &placement=SE3::Identity())
Constructor with one joint.
JointModelCompositeTpl()
Default contructor.
JointDataVector joints
Vector of joints.
std::string shortname(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointShortnameVisitor to get the shortname of the derived joint model...
void setIndexes_impl(JointIndex id, int q, int v)
Update the indexes of subjoints in the stack.
JointModelDerived & addJoint(const JointModelBase< JointModel > &jmodel, const SE3 &placement=SE3::Identity())
Add a joint to the vector of joints.
JointModelCompositeTpl(const JointModelCompositeTpl &other)
Copy constructor.
std::vector< int > m_nqs
Dimension of the segment in the config vector.
JointModelCompositeTpl(const size_t size)
Default contructor with a defined size.
Main pinocchio namespace.
Definition: treeview.dox:24
JointModelVector joints
Vector of joints contained in the joint composite.
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:35
int m_nq
Dimensions of the config and tangent space of the composite joint.
Type of the cast of a class C templated by Scalar and Options, to a new NewScalar type...
Definition: fwd.hpp:46
void calc_aba(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel, JointDataTpl< Scalar, Options, JointCollectionTpl > &jdata, const Eigen::MatrixBase< Matrix6Type > &I, const bool update_I)
Visit a JointModelTpl and the corresponding JointDataTpl through JointCalcAbaVisitor to...
int njoints
Number of joints contained in the JointModelComposite.