pinocchio  2.1.3
joint-prismatic.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_joint_prismatic_hpp__
7 #define __pinocchio_joint_prismatic_hpp__
8 
9 #include "pinocchio/macros.hpp"
10 #include "pinocchio/multibody/joint/joint-base.hpp"
11 #include "pinocchio/multibody/constraint.hpp"
12 #include "pinocchio/spatial/inertia.hpp"
13 #include "pinocchio/spatial/spatial-axis.hpp"
14 #include "pinocchio/utils/axis-label.hpp"
15 
16 namespace pinocchio
17 {
18 
19  template<typename Scalar, int Options, int _axis> struct MotionPrismaticTpl;
20 
21  namespace internal
22  {
23  template<typename Scalar, int Options, int axis>
24  struct SE3GroupAction< MotionPrismaticTpl<Scalar,Options,axis> >
25  {
26  typedef MotionTpl<Scalar,Options> ReturnType;
27  };
28 
29  template<typename Scalar, int Options, int axis, typename MotionDerived>
30  struct MotionAlgebraAction< MotionPrismaticTpl<Scalar,Options,axis>, MotionDerived>
31  {
32  typedef MotionTpl<Scalar,Options> ReturnType;
33  };
34  }
35 
36  template<typename _Scalar, int _Options, int _axis>
37  struct traits < MotionPrismaticTpl<_Scalar,_Options,_axis> >
38  {
39  typedef _Scalar Scalar;
40  enum { Options = _Options };
41  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
42  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
43  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
44  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Vector6) ToVectorConstReturnType;
45  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
46  typedef Vector3 AngularType;
47  typedef Vector3 LinearType;
48  typedef const Vector3 ConstAngularType;
49  typedef const Vector3 ConstLinearType;
50  typedef Matrix6 ActionMatrixType;
52  enum {
53  LINEAR = 0,
54  ANGULAR = 3
55  };
56  }; // struct traits MotionPrismaticTpl
57 
58  template<typename _Scalar, int _Options, int _axis>
59  struct MotionPrismaticTpl : MotionBase < MotionPrismaticTpl<_Scalar,_Options,_axis> >
60  {
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62  MOTION_TYPEDEF_TPL(MotionPrismaticTpl);
63 
64  enum { axis = _axis };
65 
66  typedef SpatialAxis<_axis+LINEAR> Axis;
67  typedef typename Axis::CartesianAxis3 CartesianAxis3;
68 
70  MotionPrismaticTpl(const Scalar & v) : rate(v) {}
71 
72 // inline operator MotionPlain() const { return Axis() * rate; }
73 
74  template<typename Derived>
75  void addTo(MotionDense<Derived> & other) const
76  {
77  typedef typename MotionDense<Derived>::Scalar OtherScalar;
78  other.linear()[_axis] += (OtherScalar) rate;
79  }
80 
81  template<typename MotionDerived>
82  void setTo(MotionDense<MotionDerived> & other) const
83  {
84  for(Eigen::DenseIndex k = 0; k < 3; ++k)
85  other.linear()[k] = k == axis ? rate : (Scalar)0;
86  other.angular().setZero();
87  }
88 
89  template<typename S2, int O2, typename D2>
90  void se3Action_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
91  {
92  v.angular().setZero();
93  v.linear().noalias() = rate * (m.rotation().col(axis));
94  }
95 
96  template<typename S2, int O2>
97  MotionPlain se3Action_impl(const SE3Tpl<S2,O2> & m) const
98  {
99  MotionPlain res;
100  se3Action_impl(m,res);
101  return res;
102  }
103 
104  template<typename S2, int O2, typename D2>
105  void se3ActionInverse_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
106  {
107  // Linear
108  v.linear().noalias() = rate * (m.rotation().transpose().col(axis));
109 
110  // Angular
111  v.angular().setZero();
112  }
113 
114  template<typename S2, int O2>
115  MotionPlain se3ActionInverse_impl(const SE3Tpl<S2,O2> & m) const
116  {
117  MotionPlain res;
118  se3ActionInverse_impl(m,res);
119  return res;
120  }
121 
122  template<typename M1, typename M2>
123  void motionAction(const MotionDense<M1> & v, MotionDense<M2> & mout) const
124  {
125  // Linear
126  CartesianAxis3::alphaCross(-rate,v.angular(),mout.linear());
127 
128  // Angular
129  mout.angular().setZero();
130  }
131 
132  template<typename M1>
133  MotionPlain motionAction(const MotionDense<M1> & v) const
134  {
135  MotionPlain res;
136  motionAction(v,res);
137  return res;
138  }
139 
140  //data
141  Scalar rate;
142  }; // struct MotionPrismaticTpl
143 
144  template<typename Scalar, int Options, int axis, typename MotionDerived>
145  typename MotionDerived::MotionPlain
146  operator+(const MotionPrismaticTpl<Scalar,Options,axis> & m1,
147  const MotionDense<MotionDerived> & m2)
148  {
149  typename MotionDerived::MotionPlain res(m2);
150  res += m1;
151  return res;
152  }
153 
154  template<typename MotionDerived, typename S2, int O2, int axis>
155  EIGEN_STRONG_INLINE
156  typename MotionDerived::MotionPlain
157  operator^(const MotionDense<MotionDerived> & m1, const MotionPrismaticTpl<S2,O2,axis> & m2)
158  {
159  return m2.motionAction(m1);
160  }
161 
162  template<typename Scalar, int Options, int axis> struct TransformPrismaticTpl;
163 
164  template<typename _Scalar, int _Options, int _axis>
165  struct traits< TransformPrismaticTpl<_Scalar,_Options,_axis> >
166  {
167  enum {
168  axis = _axis,
169  Options = _Options,
170  LINEAR = 0,
171  ANGULAR = 3
172  };
173  typedef _Scalar Scalar;
175  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
176  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
177  typedef typename Matrix3::IdentityReturnType AngularType;
178  typedef AngularType AngularRef;
179  typedef AngularType ConstAngularRef;
180  typedef Vector3 LinearType;
181  typedef const Vector3 LinearRef;
182  typedef const Vector3 ConstLinearRef;
183  typedef typename traits<PlainType>::ActionMatrixType ActionMatrixType;
184  typedef typename traits<PlainType>::HomogeneousMatrixType HomogeneousMatrixType;
185  }; // traits TransformPrismaticTpl
186 
187  namespace internal
188  {
189  template<typename Scalar, int Options, int axis>
190  struct SE3GroupAction< TransformPrismaticTpl<Scalar,Options,axis> >
191  { typedef typename traits <TransformPrismaticTpl<Scalar,Options,axis> >::PlainType ReturnType; };
192  }
193 
194  template<typename _Scalar, int _Options, int axis>
195  struct TransformPrismaticTpl : SE3Base< TransformPrismaticTpl<_Scalar,_Options,axis> >
196  {
197  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
198  PINOCCHIO_SE3_TYPEDEF_TPL(TransformPrismaticTpl);
200 
201  typedef SpatialAxis<axis+LINEAR> Axis;
202  typedef typename Axis::CartesianAxis3 CartesianAxis3;
203 
205  TransformPrismaticTpl(const Scalar & displacement)
206  : m_displacement(displacement)
207  {}
208 
209  PlainType plain() const
210  {
211  PlainType res(PlainType::Identity());
212  res.rotation().setIdentity();
213  res.translation()[axis] = m_displacement;
214 
215  return res;
216  }
217 
218  operator PlainType() const { return plain(); }
219 
220  template<typename S2, int O2>
222  se3action(const SE3Tpl<S2,O2> & m) const
223  {
225  ReturnType res(m);
226  res.translation()[axis] += m_displacement;
227 
228  return res;
229  }
230 
231  const Scalar & displacement() const { return m_displacement; }
232  Scalar & displacement() { return m_displacement; }
233 
234  ConstLinearRef translation() const { return CartesianAxis3()*displacement(); };
235  AngularType rotation() const { return AngularType(3,3); }
236 
237  protected:
238 
239  Scalar m_displacement;
240  };
241 
242  template<typename Scalar, int Options, int axis> struct ConstraintPrismatic;
243 
244  template<typename _Scalar, int _Options, int axis>
245  struct traits< ConstraintPrismatic<_Scalar,_Options,axis> >
246  {
247  typedef _Scalar Scalar;
248  enum { Options = _Options };
249  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
250  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
251  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
252  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
253  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
254  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
255  typedef Matrix3 Angular_t;
256  typedef Vector3 Linear_t;
257  typedef const Matrix3 ConstAngular_t;
258  typedef const Vector3 ConstLinear_t;
259  typedef Matrix6 ActionMatrix_t;
260  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
261  typedef SE3Tpl<Scalar,Options> SE3;
265  enum {
266  LINEAR = 0,
267  ANGULAR = 3
268  };
270  typedef Eigen::Matrix<Scalar,1,1,Options> JointForce;
271  typedef Eigen::Matrix<Scalar,6,1,Options> DenseBase;
272  typedef DenseBase MatrixReturnType;
273  typedef const DenseBase ConstMatrixReturnType;
274  }; // traits ConstraintRevolute
275 
276  template<typename _Scalar, int _Options, int axis>
277  struct ConstraintPrismatic : ConstraintBase < ConstraintPrismatic <_Scalar,_Options,axis> >
278  {
279  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
280  SPATIAL_TYPEDEF_TEMPLATE(ConstraintPrismatic);
281  enum { NV = 1, Options = _Options };
282 
284  typedef typename traits<ConstraintPrismatic>::JointForce JointForce;
285  typedef typename traits<ConstraintPrismatic>::DenseBase DenseBase;
286  typedef SpatialAxis<LINEAR+axis> Axis;
287 
288  template<typename Vector1Like>
289  JointMotion __mult__(const Eigen::MatrixBase<Vector1Like> & v) const
290  {
291  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector1Like,1);
292  assert(v.size() == 1);
293  return JointMotion(v[0]);
294  }
295 
296  template<typename S2, int O2>
297  DenseBase se3Action(const SE3Tpl<S2,O2> & m) const
298  {
299  DenseBase res;
300  MotionRef<DenseBase> v(res);
301  v.linear() = m.rotation().col(axis);
302  v.angular().setZero();
303  return res;
304  }
305 
306  int nv_impl() const { return NV; }
307 
309  {
310  const ConstraintPrismatic & ref;
311  TransposeConst(const ConstraintPrismatic & ref) : ref(ref) {}
312 
313  template<typename Derived>
314  typename ForceDense<Derived>::ConstLinearType::template ConstFixedSegmentReturnType<1>::Type
315  operator* (const ForceDense<Derived> & f) const
316  { return f.linear().template segment<1>(axis); }
317 
318  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
319  template<typename D>
320  friend typename Eigen::MatrixBase<D>::ConstRowXpr
321  operator*( const TransposeConst &, const Eigen::MatrixBase<D> & F )
322  {
323  assert(F.rows()==6);
324  return F.row(axis);
325  }
326 
327  }; // struct TransposeConst
328  TransposeConst transpose() const { return TransposeConst(*this); }
329 
330  /* CRBA joint operators
331  * - ForceSet::Block = ForceSet
332  * - ForceSet operator* (Inertia Y,Constraint S)
333  * - MatrixBase operator* (Constraint::Transpose S, ForceSet::Block)
334  * - SE3::act(ForceSet::Block)
335  */
336  DenseBase matrix_impl() const
337  {
338  DenseBase S;
340  v << Axis();
341  return S;
342  }
343 
344  template<typename MotionDerived>
345  DenseBase motionAction(const MotionDense<MotionDerived> & m) const
346  {
347  DenseBase res;
348  MotionRef<DenseBase> v(res);
349  v = m.cross(Axis());
350  return res;
351  }
352 
353  }; // struct ConstraintPrismatic
354 
355  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
356  template<typename S1, int O1, typename S2, int O2>
357  inline Eigen::Matrix<S1,6,1,O1>
358  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismatic<S2,O2,0> &)
359  {
360  /* Y(:,0) = ( 1,0, 0, 0 , z , -y ) */
361  const S1
362  &m = Y.mass(),
363  &y = Y.lever()[1],
364  &z = Y.lever()[2];
365  Eigen::Matrix<S1,6,1,O1> res;
366  res << m, S1(0), S1(0), S1(0), m*z, -m*y;
367  return res;
368  }
369  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
370  template<typename S1, int O1, typename S2, int O2>
371  inline Eigen::Matrix<S1,6,1,O1>
372  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismatic<S2,O2,1> & )
373  {
374  /* Y(:,1) = ( 0,1, 0, -z , 0 , x) */
375  const S1
376  &m = Y.mass(),
377  &x = Y.lever()[0],
378  &z = Y.lever()[2];
379  Eigen::Matrix<S1,6,1,O1> res;
380  res << S1(0), m, S1(0), -m*z, S1(0), m*x;
381  return res;
382  }
383  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
384  template<typename S1, int O1, typename S2, int O2>
385  inline Eigen::Matrix<S1,6,1,O1>
386  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismatic<S2,O2,2> & )
387  {
388  /* Y(:,2) = ( 0,0, 1, y , -x , 0) */
389  const S1
390  &m = Y.mass(),
391  &x = Y.lever()[0],
392  &y = Y.lever()[1];
393  Eigen::Matrix<S1,6,1,O1> res;
394  res << S1(0), S1(0), m, m*y, -m*x, S1(0);
395  return res;
396  }
397 
398  /* [ABA] operator* (Inertia Y,Constraint S) */
399  template<typename M6Like, typename S2, int O2, int axis>
400  inline const typename M6Like::ConstColXpr
401  operator*(const Eigen::MatrixBase<M6Like> & Y, const ConstraintPrismatic<S2,O2,axis> &)
402  {
403  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(M6Like,6,6);
404  return Y.derived().col(Inertia::LINEAR + axis);
405  }
406 
407  namespace internal
408  {
409  template<typename Scalar, int Options, int axis>
410  struct SE3GroupAction< ConstraintPrismatic<Scalar,Options,axis> >
411  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
412 
413  template<typename Scalar, int Options, int axis, typename MotionDerived>
414  struct MotionAlgebraAction< ConstraintPrismatic<Scalar,Options,axis>, MotionDerived >
415  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
416  }
417 
418  template<typename _Scalar, int _Options, int _axis>
420  {
421  typedef _Scalar Scalar;
422 
423  enum
424  {
425  Options = _Options,
426  axis = _axis
427  };
428  };
429 
430  template<typename _Scalar, int _Options, int axis>
431  struct traits< JointPrismaticTpl<_Scalar,_Options,axis> >
432  {
433  enum {
434  NQ = 1,
435  NV = 1
436  };
437  typedef _Scalar Scalar;
438  enum { Options = _Options };
445  typedef Eigen::Matrix<Scalar,6,NV,Options> F_t;
446 
447  // [ABA]
448  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
449  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
450  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
451 
452  PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
453 
454  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
455  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
456  };
457 
458  template<typename Scalar, int Options, int axis>
459  struct traits< JointDataPrismaticTpl<Scalar,Options,axis> >
461 
462  template<typename Scalar, int Options, int axis>
463  struct traits< JointModelPrismaticTpl<Scalar,Options,axis> >
465 
466  template<typename _Scalar, int _Options, int axis>
467  struct JointDataPrismaticTpl : public JointDataBase< JointDataPrismaticTpl<_Scalar,_Options,axis> >
468  {
469  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
471  PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE;
472  PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
473 
474  Constraint_t S;
475  Transformation_t M;
476  Motion_t v;
477  Bias_t c;
478 
479  F_t F;
480 
481  // [ABA] specific data
482  U_t U;
483  D_t Dinv;
484  UD_t UDinv;
485 
487 
488  static std::string classname() { return std::string("JointDataPrismatic"); }
489  std::string shortname() const { return classname(); }
490 
491  }; // struct JointDataPrismaticTpl
492 
493  template<typename NewScalar, typename Scalar, int Options, int axis>
494  struct CastType< NewScalar, JointModelPrismaticTpl<Scalar,Options,axis> >
495  {
497  };
498 
499  template<typename _Scalar, int _Options, int axis>
501  : public JointModelBase< JointModelPrismaticTpl<_Scalar,_Options,axis> >
502  {
503  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
504  typedef JointPrismaticTpl<_Scalar,_Options,axis> JointDerived;
505  PINOCCHIO_JOINT_TYPEDEF_TEMPLATE;
506 
508  using Base::id;
509  using Base::idx_q;
510  using Base::idx_v;
511  using Base::setIndexes;
512 
513  JointDataDerived createData() const { return JointDataDerived(); }
514 
515  template<typename ConfigVector>
516  void calc(JointDataDerived & data,
517  const typename Eigen::MatrixBase<ConfigVector> & qs) const
518  {
519  typedef typename ConfigVector::Scalar Scalar;
520  const Scalar & q = qs[idx_q()];
521  data.M.displacement() = q;
522  }
523 
524  template<typename ConfigVector, typename TangentVector>
525  void calc(JointDataDerived & data,
526  const typename Eigen::MatrixBase<ConfigVector> & qs,
527  const typename Eigen::MatrixBase<TangentVector> & vs) const
528  {
529  calc(data,qs.derived());
530 
531  typedef typename TangentVector::Scalar S2;
532  const S2 & v = vs[idx_v()];
533  data.v.rate = v;
534  }
535 
536  template<typename Matrix6Like>
537  void calc_aba(JointDataDerived & data, const Eigen::MatrixBase<Matrix6Like> & I, const bool update_I) const
538  {
539  data.U = I.col(Inertia::LINEAR + axis);
540  data.Dinv[0] = 1./I(Inertia::LINEAR + axis, Inertia::LINEAR + axis);
541  data.UDinv.noalias() = data.U * data.Dinv[0];
542 
543  if (update_I)
544  PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I) -= data.UDinv * data.U.transpose();
545  }
546 
547  Scalar finiteDifferenceIncrement() const
548  {
549  using math::sqrt;
550  return sqrt(Eigen::NumTraits<Scalar>::epsilon());
551  }
552 
553  static std::string classname()
554  {
555  return std::string("JointModelP") + axisLabel<axis>();
556  }
557  std::string shortname() const { return classname(); }
558 
560  template<typename NewScalar>
562  {
564  ReturnType res;
565  res.setIndexes(id(),idx_q(),idx_v());
566  return res;
567  }
568 
569  }; // struct JointModelPrismaticTpl
570 
574 
578 
582 
583 } //namespace pinocchio
584 
585 #include <boost/type_traits.hpp>
586 
587 namespace boost
588 {
589  template<typename Scalar, int Options, int axis>
590  struct has_nothrow_constructor< ::pinocchio::JointModelPrismaticTpl<Scalar,Options,axis> >
591  : public integral_constant<bool,true> {};
592 
593  template<typename Scalar, int Options, int axis>
594  struct has_nothrow_copy< ::pinocchio::JointModelPrismaticTpl<Scalar,Options,axis> >
595  : public integral_constant<bool,true> {};
596 
597  template<typename Scalar, int Options, int axis>
598  struct has_nothrow_constructor< ::pinocchio::JointDataPrismaticTpl<Scalar,Options,axis> >
599  : public integral_constant<bool,true> {};
600 
601  template<typename Scalar, int Options, int axis>
602  struct has_nothrow_copy< ::pinocchio::JointDataPrismaticTpl<Scalar,Options,axis> >
603  : public integral_constant<bool,true> {};
604 }
605 
606 #endif // ifndef __pinocchio_joint_prismatic_hpp__
int idx_q(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointIdxQVisitor to get the index in the full model configuration space...
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...
JointDataTpl< Scalar, Options, JointCollectionTpl > createData(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through CreateData visitor to create a JointDataTpl.
ModelTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType finiteDifferenceIncrement(const ModelTpl< Scalar, Options, JointCollectionTpl > &model)
Computes the finite difference increments for each degree of freedom according to the current joint c...
JointModelPrismaticTpl< NewScalar, Options, axis > cast() const
std::string shortname(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointShortnameVisitor to get the shortname of the derived joint model...
Main pinocchio namespace.
Definition: treeview.dox:24
Common traits structure to fully define base classes for CRTP.
Definition: spatial/fwd.hpp:29
ConstLinearType linear() const
Return the linear part of the force vector.
Definition: force-base.hpp:47
Type of the cast of a class C templated by Scalar and Options, to a new NewScalar type...
Definition: spatial/fwd.hpp:35
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...