pinocchio  2.1.3
joint-prismatic-unaligned.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_joint_prismatic_unaligned_hpp__
7 #define __pinocchio_joint_prismatic_unaligned_hpp__
8 
9 #include "pinocchio/macros.hpp"
10 #include "pinocchio/multibody/joint/joint-base.hpp"
11 #include "pinocchio/multibody/joint/joint-translation.hpp"
12 #include "pinocchio/multibody/constraint.hpp"
13 #include "pinocchio/spatial/inertia.hpp"
14 
15 namespace pinocchio
16 {
17 
18  template<typename Scalar, int Options=0> struct MotionPrismaticUnalignedTpl;
20 
21  namespace internal
22  {
23  template<typename Scalar, int Options>
24  struct SE3GroupAction< MotionPrismaticUnalignedTpl<Scalar,Options> >
25  {
26  typedef MotionTpl<Scalar,Options> ReturnType;
27  };
28 
29  template<typename Scalar, int Options, typename MotionDerived>
30  struct MotionAlgebraAction< MotionPrismaticUnalignedTpl<Scalar,Options>, MotionDerived>
31  {
32  typedef MotionTpl<Scalar,Options> ReturnType;
33  };
34  }
35 
36  template<typename _Scalar, int _Options>
37  struct traits< MotionPrismaticUnalignedTpl<_Scalar,_Options> >
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  }; // traits MotionPrismaticUnalignedTpl
57 
58  template<typename _Scalar, int _Options>
59  struct MotionPrismaticUnalignedTpl : MotionBase < MotionPrismaticUnalignedTpl<_Scalar,_Options> >
60  {
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62  MOTION_TYPEDEF_TPL(MotionPrismaticUnalignedTpl);
63 
65 
66  template<typename Vector3Like, typename S2>
67  MotionPrismaticUnalignedTpl(const Eigen::MatrixBase<Vector3Like> & axis,
68  const S2 rate)
69  : axis(axis), rate(rate)
70  { EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3); }
71 
72 // operator MotionPlain() const
73 // { return MotionPlain(axis*rate,MotionPlain::Vector3::Zero());}
74 
75  template<typename Derived>
76  void addTo(MotionDense<Derived> & other) const
77  {
78  other.linear() += axis * rate;
79  }
80 
81  template<typename Derived>
82  void setTo(MotionDense<Derived> & other) const
83  {
84  other.linear().noalias() = axis*rate;
85  other.angular().setZero();
86  }
87 
88  template<typename S2, int O2, typename D2>
89  void se3Action_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
90  {
91  v.linear().noalias() = rate * (m.rotation() * axis); // TODO: check efficiency
92  v.angular().setZero();
93  }
94 
95  template<typename S2, int O2>
96  MotionPlain se3Action_impl(const SE3Tpl<S2,O2> & m) const
97  {
98  MotionPlain res;
99  se3Action_impl(m,res);
100  return res;
101  }
102 
103  template<typename S2, int O2, typename D2>
104  void se3ActionInverse_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
105  {
106  // Linear
107  v.linear().noalias() = rate * (m.rotation().transpose() * axis);
108 
109  // Angular
110  v.angular().setZero();
111  }
112 
113  template<typename S2, int O2>
114  MotionPlain se3ActionInverse_impl(const SE3Tpl<S2,O2> & m) const
115  {
116  MotionPlain res;
117  se3ActionInverse_impl(m,res);
118  return res;
119  }
120 
121  template<typename M1, typename M2>
122  void motionAction(const MotionDense<M1> & v, MotionDense<M2> & mout) const
123  {
124  // Linear
125  mout.linear().noalias() = v.angular().cross(axis);
126  mout.linear() *= rate;
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  Vector3 axis;
142  Scalar rate;
143  }; // struct MotionPrismaticUnalignedTpl
144 
145  template<typename Scalar, int Options, typename MotionDerived>
146  inline typename MotionDerived::MotionPlain
148  {
149  typedef typename MotionDerived::MotionPlain ReturnType;
150  return ReturnType(m1.rate*m1.axis + m2.linear(), m2.angular());
151  }
152 
153  template<typename Scalar, int Options> struct ConstraintPrismaticUnaligned;
154 
155  template<typename _Scalar, int _Options>
156  struct traits< ConstraintPrismaticUnaligned<_Scalar,_Options> >
157  {
158  typedef _Scalar Scalar;
159  enum { Options = _Options };
160  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
161  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
162  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
163  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
164  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
165  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
166  typedef Matrix3 Angular_t;
167  typedef Vector3 Linear_t;
168  typedef const Matrix3 ConstAngular_t;
169  typedef const Vector3 ConstLinear_t;
170  typedef Matrix6 ActionMatrix_t;
171  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
172  typedef SE3Tpl<Scalar,Options> SE3;
176  enum {
177  LINEAR = 0,
178  ANGULAR = 3
179  };
181  typedef Eigen::Matrix<Scalar,1,1,Options> JointForce;
182  typedef Eigen::Matrix<Scalar,6,1,Options> DenseBase;
183  typedef DenseBase MatrixReturnType;
184  typedef const DenseBase ConstMatrixReturnType;
185  }; // traits ConstraintPrismaticUnaligned
186 
187  template<typename _Scalar, int _Options>
188  struct ConstraintPrismaticUnaligned : ConstraintBase< ConstraintPrismaticUnaligned<_Scalar,_Options> >
189  {
190  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
191  SPATIAL_TYPEDEF_TEMPLATE(ConstraintPrismaticUnaligned);
192  enum { NV = 1, Options = _Options };
193 
195  typedef typename traits<ConstraintPrismaticUnaligned>::JointForce JointForce;
196  typedef typename traits<ConstraintPrismaticUnaligned>::DenseBase DenseBase;
197 
199 
200  template<typename Vector3Like>
201  ConstraintPrismaticUnaligned(const Eigen::MatrixBase<Vector3Like> & axis)
202  : axis(axis)
203  { EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3); }
204 
205  template<typename Vector1Like>
206  JointMotion __mult__(const Eigen::MatrixBase<Vector1Like> & v) const
207  {
208  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector1Like,1);
209  return JointMotion(axis,v[0]);
210  }
211 
212  template<typename S1, int O1>
213  Vector6 se3Action(const SE3Tpl<S1,O1> & m) const
214  {
215  Vector6 res;
216  res.template head<3>().noalias() = m.rotation()*axis;
217  res.template tail<3>().setZero();
218  return res;
219  }
220 
221  int nv_impl() const { return NV; }
222 
224  {
225  typedef typename traits<ConstraintPrismaticUnaligned>::Scalar Scalar;
226  typedef typename traits<ConstraintPrismaticUnaligned>::Force Force;
227  typedef typename traits<ConstraintPrismaticUnaligned>::Vector6 Vector6;
228 
229  const ConstraintPrismaticUnaligned & ref;
230  TransposeConst(const ConstraintPrismaticUnaligned & ref) : ref(ref) {}
231 
232  template<typename Derived>
233  Eigen::Matrix<
234  typename PINOCCHIO_EIGEN_DOT_PRODUCT_RETURN_TYPE(Vector3,typename ForceDense<Derived>::ConstLinearType),
235  1,1>
236  operator* (const ForceDense<Derived> & f) const
237  {
238  typedef Eigen::Matrix<
239  typename PINOCCHIO_EIGEN_DOT_PRODUCT_RETURN_TYPE(Vector3,typename ForceDense<Derived>::ConstLinearType),
240  1,1> ReturnType;
241 
242  ReturnType res; res[0] = ref.axis.dot(f.linear());
243  return res;
244  }
245 
246  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
247  template<typename D>
248  friend
249 #if EIGEN_VERSION_AT_LEAST(3,2,90)
250  const Eigen::Product<
251  Eigen::Transpose<const Vector3>,
252  typename Eigen::MatrixBase<const D>::template NRowsBlockXpr<3>::Type
253  >
254 #else
255  const typename Eigen::ProductReturnType<
256  Eigen::Transpose<const Vector3>,
257  typename Eigen::MatrixBase<const D>::template NRowsBlockXpr<3>::Type
258  >::Type
259 #endif
260  operator* (const TransposeConst & tc, const Eigen::MatrixBase<D> & F)
261  {
262  EIGEN_STATIC_ASSERT(D::RowsAtCompileTime==6,THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
263  /* Return ax.T * F[1:3,:] */
264  return tc.ref.axis.transpose () * F.template topRows<3> ();
265  }
266 
267  };
268  TransposeConst transpose() const { return TransposeConst(*this); }
269 
270 
271  /* CRBA joint operators
272  * - ForceSet::Block = ForceSet
273  * - ForceSet operator* (Inertia Y,Constraint S)
274  * - MatrixBase operator* (Constraint::Transpose S, ForceSet::Block)
275  * - SE3::act(ForceSet::Block)
276  */
277  DenseBase matrix_impl() const
278  {
279  DenseBase S;
280  S << axis, Vector3::Zero();
281  return S;
282  }
283 
284  template<typename MotionDerived>
285  DenseBase motionAction(const MotionDense<MotionDerived> & v) const
286  {
287  DenseBase res;
288  res << v.angular().cross(axis), Vector3::Zero();
289 
290  return res;
291  }
292 
293  // data
294  Vector3 axis;
295 
296  }; // struct ConstraintPrismaticUnaligned
297 
298 
299  template<typename MotionDerived, typename S2, int O2>
300  inline typename MotionDerived::MotionPlain
301  operator^(const MotionDense<MotionDerived> & m1,
303  {
304  return m2.motionAction(m1);
305  }
306 
307  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
308  template<typename S1, int O1, typename S2, int O2>
309  inline Eigen::Matrix<S1,6,1>
310  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismaticUnaligned<S2,O2> & cpu)
311  {
312  typedef InertiaTpl<S1,O1> Inertia;
313  /* YS = [ m -mcx ; mcx I-mcxcx ] [ v ; 0 ] = [ mv ; mcxv ] */
314  const S1 & m = Y.mass();
315  const typename Inertia::Vector3 & c = Y.lever();
316 
317  Eigen::Matrix<S1,6,1> res;
318  res.template head<3>().noalias() = m*cpu.axis;
319  res.template tail<3>() = c.cross(res.template head<3>());
320  return res;
321  }
322 
323  /* [ABA] Y*S operator (Inertia Y,Constraint S) */
324  template<typename M6, typename S2, int O2>
325 #if EIGEN_VERSION_AT_LEAST(3,2,90)
326  const typename Eigen::Product<
327  Eigen::Block<const M6,6,3>,
329  Eigen::DefaultProduct>
330 #else
331  const typename Eigen::ProductReturnType<
332  Eigen::Block<const M6,6,3>,
333  const typename ConstraintPrismaticUnaligned<S2,O2>::Vector3
334  >::Type
335 #endif
336  operator*(const Eigen::MatrixBase<M6> & Y, const ConstraintPrismaticUnaligned<S2,O2> & cpu)
337  {
338  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(M6,6,6);
339  return Y.template block<6,3> (0,Inertia::LINEAR) * cpu.axis;
340  }
341 
342 
343  namespace internal
344  {
345  template<typename Scalar, int Options>
346  struct SE3GroupAction< ConstraintPrismaticUnaligned<Scalar,Options> >
347  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
348 
349  template<typename Scalar, int Options, typename MotionDerived>
350  struct MotionAlgebraAction< ConstraintPrismaticUnaligned<Scalar,Options>,MotionDerived >
351  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
352  }
353 
354  template<typename Scalar, int Options> struct JointPrismaticUnalignedTpl;
355 
356  template<typename _Scalar, int _Options>
357  struct traits< JointPrismaticUnalignedTpl<_Scalar,_Options> >
358  {
359  enum {
360  NQ = 1,
361  NV = 1
362  };
363  typedef _Scalar Scalar;
364  enum { Options = _Options };
371  typedef Eigen::Matrix<Scalar,6,NV,Options> F_t;
372 
373  // [ABA]
374  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
375  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
376  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
377 
378  PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
379 
380  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
381  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
382  };
383 
384  template<typename Scalar, int Options>
385  struct traits< JointDataPrismaticUnalignedTpl<Scalar,Options> >
387 
388  template<typename _Scalar, int _Options>
390  : public JointDataBase< JointDataPrismaticUnalignedTpl<_Scalar,_Options> >
391  {
392  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
394  PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE;
395  PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
396 
397  Transformation_t M;
398  Constraint_t S;
399  Motion_t v;
400  Bias_t c;
401 
402  F_t F;
403 
404  // [ABA] specific data
405  U_t U;
406  D_t Dinv;
407  UD_t UDinv;
408 
410 
411  template<typename Vector3Like>
412  JointDataPrismaticUnalignedTpl(const Eigen::MatrixBase<Vector3Like> & axis)
413  : M()
414  , S(axis)
415  , v(axis,(Scalar)NAN)
416  , U(), Dinv(), UDinv()
417  {}
418 
419  static std::string classname() { return std::string("JointDataPrismaticUnaligned"); }
420  std::string shortname() const { return classname(); }
421 
422  }; // struct JointDataPrismaticUnalignedTpl
423 
424  template<typename Scalar, int Options>
425  struct traits< JointModelPrismaticUnalignedTpl<Scalar,Options> >
427 
428  PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelPrismaticUnalignedTpl);
429  template<typename _Scalar, int _Options>
431  : public JointModelBase< JointModelPrismaticUnalignedTpl<_Scalar,_Options> >
432  {
433  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
435  PINOCCHIO_JOINT_TYPEDEF_TEMPLATE;
436 
438  using Base::id;
439  using Base::idx_q;
440  using Base::idx_v;
441  using Base::setIndexes;
442 
443  typedef Eigen::Matrix<Scalar,3,1,_Options> Vector3;
444 
446  JointModelPrismaticUnalignedTpl(Scalar x, Scalar y, Scalar z)
447  : axis(x,y,z)
448  {
449  axis.normalize();
450  assert(axis.isUnitary() && "Translation axis is not unitary");
451  }
452 
453  template<typename Vector3Like>
454  JointModelPrismaticUnalignedTpl(const Eigen::MatrixBase<Vector3Like> & axis)
455  : axis(axis)
456  {
457  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Vector3Like);
458  assert(axis.isUnitary() && "Translation axis is not unitary");
459  }
460 
461  JointDataDerived createData() const { return JointDataDerived(axis); }
462 
463  using Base::isEqual;
464  bool isEqual(const JointModelPrismaticUnalignedTpl & other) const
465  {
466  return Base::isEqual(other) && axis == other.axis;
467  }
468 
469  template<typename ConfigVector>
470  void calc(JointDataDerived & data,
471  const typename Eigen::MatrixBase<ConfigVector> & qs) const
472  {
473  typedef typename ConfigVector::Scalar Scalar;
474  const Scalar & q = qs[idx_q()];
475 
476  data.M.translation().noalias() = axis * q;
477  }
478 
479  template<typename ConfigVector, typename TangentVector>
480  void calc(JointDataDerived & data,
481  const typename Eigen::MatrixBase<ConfigVector> & qs,
482  const typename Eigen::MatrixBase<TangentVector> & vs) const
483  {
484  calc(data,qs.derived());
485 
486  typedef typename TangentVector::Scalar S2;
487  const S2 & v = vs[idx_v()];
488  data.v.rate = v;
489  }
490 
491  template<typename Matrix6Like>
492  void calc_aba(JointDataDerived & data, const Eigen::MatrixBase<Matrix6Like> & I, const bool update_I) const
493  {
494  data.U.noalias() = I.template block<6,3> (0,Inertia::LINEAR) * axis;
495  data.Dinv[0] = Scalar(1)/axis.dot(data.U.template segment <3> (Inertia::LINEAR));
496  data.UDinv.noalias() = data.U * data.Dinv;
497 
498  if (update_I)
499  PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I) -= data.UDinv * data.U.transpose();
500  }
501 
502  Scalar finiteDifferenceIncrement() const
503  {
504  using math::sqrt;
505  return sqrt(Eigen::NumTraits<Scalar>::epsilon());
506  }
507 
508  static std::string classname() { return std::string("JointModelPrismaticUnaligned"); }
509  std::string shortname() const { return classname(); }
510 
512  template<typename NewScalar>
514  {
516  ReturnType res(axis.template cast<NewScalar>());
517  res.setIndexes(id(),idx_q(),idx_v());
518  return res;
519  }
520 
521  // data
522 
526  Vector3 axis;
527  }; // struct JointModelPrismaticUnalignedTpl
528 
529 } //namespace pinocchio
530 
531 #include <boost/type_traits.hpp>
532 
533 namespace boost
534 {
535  template<typename Scalar, int Options>
536  struct has_nothrow_constructor< ::pinocchio::JointModelPrismaticUnalignedTpl<Scalar,Options> >
537  : public integral_constant<bool,true> {};
538 
539  template<typename Scalar, int Options>
540  struct has_nothrow_copy< ::pinocchio::JointModelPrismaticUnalignedTpl<Scalar,Options> >
541  : public integral_constant<bool,true> {};
542 
543  template<typename Scalar, int Options>
544  struct has_nothrow_constructor< ::pinocchio::JointDataPrismaticUnalignedTpl<Scalar,Options> >
545  : public integral_constant<bool,true> {};
546 
547  template<typename Scalar, int Options>
548  struct has_nothrow_copy< ::pinocchio::JointDataPrismaticUnalignedTpl<Scalar,Options> >
549  : public integral_constant<bool,true> {};
550 }
551 
552 
553 #endif // ifndef __pinocchio_joint_prismatic_unaligned_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...
JointModelPrismaticUnalignedTpl< NewScalar, Options > 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
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...