pinocchio  2.2.1-dirty
joint-planar.hpp
1 //
2 // Copyright (c) 2015-2019 CNRS INRIA
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_joint_planar_hpp__
7 #define __pinocchio_joint_planar_hpp__
8 
9 #include "pinocchio/macros.hpp"
10 #include "pinocchio/multibody/joint/joint-base.hpp"
11 #include "pinocchio/spatial/cartesian-axis.hpp"
12 #include "pinocchio/multibody/constraint.hpp"
13 #include "pinocchio/spatial/motion.hpp"
14 #include "pinocchio/spatial/inertia.hpp"
15 
16 namespace pinocchio
17 {
18 
19  template<typename Scalar, int Options = 0> struct MotionPlanarTpl;
21 
22  template<typename Scalar, int Options>
23  struct SE3GroupAction< MotionPlanarTpl<Scalar,Options> >
24  {
26  };
27 
28  template<typename Scalar, int Options, typename MotionDerived>
29  struct MotionAlgebraAction< MotionPlanarTpl<Scalar,Options>, MotionDerived>
30  {
32  };
33 
34  template<typename _Scalar, int _Options>
35  struct traits< MotionPlanarTpl<_Scalar,_Options> >
36  {
37  typedef _Scalar Scalar;
38  enum { Options = _Options };
39  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
40  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
41  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
42  typedef typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Vector6) ToVectorConstReturnType;
43  typedef typename PINOCCHIO_EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
44  typedef Vector3 AngularType;
45  typedef Vector3 LinearType;
46  typedef const Vector3 ConstAngularType;
47  typedef const Vector3 ConstLinearType;
48  typedef Matrix6 ActionMatrixType;
50  typedef MotionPlain PlainReturnType;
51  enum {
52  LINEAR = 0,
53  ANGULAR = 3
54  };
55  }; // traits MotionPlanarTpl
56 
57  template<typename _Scalar, int _Options>
58  struct MotionPlanarTpl
59  : MotionBase< MotionPlanarTpl<_Scalar,_Options> >
60  {
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62  MOTION_TYPEDEF_TPL(MotionPlanarTpl);
63 
64  typedef CartesianAxis<2> AxisZ;
65 
66  MotionPlanarTpl() {}
67 
68  MotionPlanarTpl(const Scalar & x_dot, const Scalar & y_dot,
69  const Scalar & theta_dot)
70  : m_x_dot(x_dot), m_y_dot(y_dot), m_theta_dot(theta_dot)
71  {}
72 
73  template<typename Vector3Like>
74  MotionPlanarTpl(const Eigen::MatrixBase<Vector3Like> & vj)
75  : m_x_dot(vj[0]), m_y_dot(vj[1]), m_theta_dot(vj[2])
76  {
77  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3);
78  }
79 
80  inline PlainReturnType plain() const
81  {
82  return PlainReturnType(typename PlainReturnType::Vector3(m_x_dot,m_y_dot,Scalar(0)),
83  typename PlainReturnType::Vector3(Scalar(0),Scalar(0),m_theta_dot));
84  }
85 
86  template<typename Derived>
87  void addTo(MotionDense<Derived> & other) const
88  {
89  other.linear()[0] += m_x_dot;
90  other.linear()[1] += m_y_dot;
91  other.angular()[2] += m_theta_dot;
92  }
93 
94  template<typename MotionDerived>
95  void setTo(MotionDense<MotionDerived> & other) const
96  {
97  other.linear() << m_x_dot, m_y_dot, (Scalar)0;
98  other.angular() << (Scalar)0, (Scalar)0, m_theta_dot;
99  }
100 
101  template<typename S2, int O2, typename D2>
102  void se3Action_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
103  {
104  v.angular().noalias() = m.rotation().col(2) * m_theta_dot;
105  v.linear().noalias() = m.translation().cross(v.angular());
106  v.linear() += m.rotation().col(0) * m_x_dot;
107  v.linear() += m.rotation().col(1) * m_y_dot;
108  }
109 
110  template<typename S2, int O2>
111  MotionPlain se3Action_impl(const SE3Tpl<S2,O2> & m) const
112  {
113  MotionPlain res;
114  se3Action_impl(m,res);
115  return res;
116  }
117 
118  template<typename S2, int O2, typename D2>
119  void se3ActionInverse_impl(const SE3Tpl<S2,O2> & m, MotionDense<D2> & v) const
120  {
121  // Linear
122  // TODO: use v.angular() as temporary variable
123  Vector3 v3_tmp;
124  AxisZ::alphaCross(m_theta_dot,m.translation(),v3_tmp);
125  v3_tmp[0] += m_x_dot; v3_tmp[1] += m_y_dot;
126  v.linear().noalias() = m.rotation().transpose() * v3_tmp;
127 
128  // Angular
129  v.angular().noalias() = m.rotation().transpose().col(2) * m_theta_dot;
130  }
131 
132  template<typename S2, int O2>
133  MotionPlain se3ActionInverse_impl(const SE3Tpl<S2,O2> & m) const
134  {
135  MotionPlain res;
136  se3ActionInverse_impl(m,res);
137  return res;
138  }
139 
140  template<typename M1, typename M2>
141  void motionAction(const MotionDense<M1> & v, MotionDense<M2> & mout) const
142  {
143  // Linear
144  AxisZ::alphaCross(-m_theta_dot,v.linear(),mout.linear());
145 
146  typename M1::ConstAngularType w_in = v.angular();
147  typename M2::LinearType v_out = mout.linear();
148 
149  v_out[0] -= w_in[2] * m_y_dot;
150  v_out[1] += w_in[2] * m_x_dot;
151  v_out[2] += -w_in[1] * m_x_dot + w_in[0] * m_y_dot ;
152 
153  // Angular
154  AxisZ::alphaCross(-m_theta_dot,v.angular(),mout.angular());
155  }
156 
157  template<typename M1>
158  MotionPlain motionAction(const MotionDense<M1> & v) const
159  {
160  MotionPlain res;
161  motionAction(v,res);
162  return res;
163  }
164 
165  // data
166  Scalar m_x_dot;
167  Scalar m_y_dot;
168  Scalar m_theta_dot;
169 
170  }; // struct MotionPlanarTpl
171 
172  template<typename Scalar, int Options, typename MotionDerived>
173  inline typename MotionDerived::MotionPlain
174  operator+(const MotionPlanarTpl<Scalar,Options> & m1, const MotionDense<MotionDerived> & m2)
175  {
176  typename MotionDerived::MotionPlain result(m2);
177  result.linear()[0] += m1.m_x_dot;
178  result.linear()[1] += m1.m_y_dot;
179 
180  result.angular()[2] += m1.m_theta_dot;
181 
182  return result;
183  }
184 
185  template<typename Scalar, int Options> struct ConstraintPlanarTpl;
186 
187  template<typename _Scalar, int _Options>
188  struct traits< ConstraintPlanarTpl<_Scalar,_Options> >
189  {
190  typedef _Scalar Scalar;
191  enum { Options = _Options };
192  enum {
193  LINEAR = 0,
194  ANGULAR = 3
195  };
197  typedef Eigen::Matrix<Scalar,3,1,Options> JointForce;
198  typedef Eigen::Matrix<Scalar,6,3,Options> DenseBase;
199  typedef DenseBase MatrixReturnType;
200  typedef const DenseBase ConstMatrixReturnType;
201  }; // struct traits ConstraintPlanarTpl
202 
203  template<typename _Scalar, int _Options>
204  struct ConstraintPlanarTpl
205  : ConstraintBase< ConstraintPlanarTpl<_Scalar,_Options> >
206  {
207  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
208  PINOCCHIO_CONSTRAINT_TYPEDEF_TPL(ConstraintPlanarTpl)
209 
210  enum { NV = 3 };
211 
212  ConstraintPlanarTpl() {};
213 
214  template<typename Vector3Like>
215  JointMotion __mult__(const Eigen::MatrixBase<Vector3Like> & vj) const
216  {
217  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3);
218  return JointMotion(vj);
219  }
220 
221  int nv_impl() const { return NV; }
222 
224  {
225  const ConstraintPlanarTpl & ref;
226  ConstraintTranspose(const ConstraintPlanarTpl & ref) : ref(ref) {}
227 
228  template<typename Derived>
230  {
231  typedef typename ForceDense<Derived>::Vector3 Vector3;
232  return Vector3(phi.linear()[0], phi.linear()[1], phi.angular()[2]);
233  }
234 
235  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
236  template<typename Derived>
237  friend typename Eigen::Matrix <typename Eigen::MatrixBase<Derived>::Scalar, 3, Derived::ColsAtCompileTime>
238  operator*(const ConstraintTranspose &, const Eigen::MatrixBase<Derived> & F)
239  {
240  typedef typename Eigen::MatrixBase<Derived>::Scalar Scalar;
241  typedef Eigen::Matrix<Scalar, 3, Derived::ColsAtCompileTime> MatrixReturnType;
242  assert(F.rows()==6);
243 
244  MatrixReturnType result(3, F.cols ());
245  result.template topRows<2>() = F.template topRows<2>();
246  result.template bottomRows<1>() = F.template bottomRows<1>();
247  return result;
248  }
249 
250  }; // struct ConstraintTranspose
251 
252  ConstraintTranspose transpose () const { return ConstraintTranspose(*this); }
253 
254  DenseBase matrix_impl() const
255  {
256  DenseBase S;
257  S.template block<3,3>(Inertia::LINEAR, 0).setZero ();
258  S.template block<3,3>(Inertia::ANGULAR, 0).setZero ();
259  S(Inertia::LINEAR + 0,0) = Scalar(1);
260  S(Inertia::LINEAR + 1,1) = Scalar(1);
261  S(Inertia::ANGULAR + 2,2) = Scalar(1);
262  return S;
263  }
264 
265  template<typename S1, int O1>
266  DenseBase se3Action(const SE3Tpl<S1,O1> & m) const
267  {
268  DenseBase X_subspace;
269 
270  // LINEAR
271  X_subspace.template block <3,2>(Motion::LINEAR, 0) = m.rotation ().template leftCols <2> ();
272  X_subspace.template block <3,1>(Motion::LINEAR, 2) = m.translation().cross(m.rotation ().template rightCols<1>());
273 
274  // ANGULAR
275  X_subspace.template block <3,2>(Motion::ANGULAR, 0).setZero ();
276  X_subspace.template block <3,1>(Motion::ANGULAR, 2) = m.rotation ().template rightCols<1>();
277 
278  return X_subspace;
279  }
280 
281  template<typename S1, int O1>
282  DenseBase se3ActionInverse(const SE3Tpl<S1,O1> & m) const
283  {
284  DenseBase X_subspace;
285 
286  // LINEAR
287  X_subspace.template block <3,2>(Motion::LINEAR, 0) = m.rotation().transpose().template leftCols <2>();
288  X_subspace.template block <3,1>(Motion::ANGULAR,2) = m.rotation().transpose() * m.translation(); // tmp variable
289  X_subspace.template block <3,1>(Motion::LINEAR, 2) = -X_subspace.template block <3,1>(Motion::ANGULAR,2).cross(m.rotation().transpose().template rightCols<1>());
290 
291  // ANGULAR
292  X_subspace.template block <3,2>(Motion::ANGULAR, 0).setZero();
293  X_subspace.template block <3,1>(Motion::ANGULAR, 2) = m.rotation().transpose().template rightCols<1>();
294 
295  return X_subspace;
296  }
297 
298  template<typename MotionDerived>
299  DenseBase motionAction(const MotionDense<MotionDerived> & m) const
300  {
301  const typename MotionDerived::ConstLinearType v = m.linear();
302  const typename MotionDerived::ConstAngularType w = m.angular();
303  DenseBase res(DenseBase::Zero());
304 
305  res(0,1) = -w[2]; res(0,2) = v[1];
306  res(1,0) = w[2]; res(1,2) = -v[0];
307  res(2,0) = -w[1]; res(2,1) = w[0];
308  res(3,2) = w[1];
309  res(4,2) = -w[0];
310 
311  return res;
312  }
313  }; // struct ConstraintPlanarTpl
314 
315  template<typename MotionDerived, typename S2, int O2>
316  inline typename MotionDerived::MotionPlain
317  operator^(const MotionDense<MotionDerived> & m1, const MotionPlanarTpl<S2,O2> & m2)
318  {
319  return m2.motionAction(m1);
320  }
321 
322  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
323  template<typename S1, int O1, typename S2, int O2>
324  inline typename Eigen::Matrix<S1,6,3,O1>
326  {
327  typedef InertiaTpl<S1,O1> Inertia;
328  typedef typename Inertia::Scalar Scalar;
329  typedef Eigen::Matrix<S1,6,3,O1> ReturnType;
330 
331  ReturnType M;
332  const Scalar mass = Y.mass();
333  const typename Inertia::Vector3 & com = Y.lever();
334  const typename Inertia::Symmetric3 & inertia = Y.inertia();
335 
336  M.template topLeftCorner<3,3>().setZero();
337  M.template topLeftCorner<2,2>().diagonal().fill(mass);
338 
339  const typename Inertia::Vector3 mc(mass * com);
340  M.template rightCols<1>().template head<2>() << -mc(1), mc(0);
341 
342  M.template bottomLeftCorner<3,2>() << (Scalar)0, -mc(2), mc(2), (Scalar)0, -mc(1), mc(0);
343  M.template rightCols<1>().template tail<3>() = inertia.data().template tail<3>();
344  M.template rightCols<1>()[3] -= mc(0)*com(2);
345  M.template rightCols<1>()[4] -= mc(1)*com(2);
346  M.template rightCols<1>()[5] += mass*(com(0)*com(0) + com(1)*com(1));
347 
348  return M;
349  }
350 
351  /* [ABA] Y*S operator (Inertia Y,Constraint S) */
352  // inline Eigen::Matrix<double,6,1>
353 
354  template<typename M6Like, typename S2, int O2>
355  inline Eigen::Matrix<S2,6,3,O2>
356  operator*(const Eigen::MatrixBase<M6Like> & Y,
358  {
359  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(M6Like,6,6);
360  typedef Eigen::Matrix<S2,6,3,O2> Matrix63;
361 
362  Matrix63 IS;
363  IS.template leftCols<2>() = Y.template leftCols<2>();
364  IS.template rightCols<1>() = Y.template rightCols<1>();
365 
366  return IS;
367  }
368 
369  template<typename S1, int O1>
371  { typedef Eigen::Matrix<S1,6,3,O1> ReturnType; };
372 
373  template<typename S1, int O1, typename MotionDerived>
374  struct MotionAlgebraAction< ConstraintPlanarTpl<S1,O1>,MotionDerived >
375  { typedef Eigen::Matrix<S1,6,3,O1> ReturnType; };
376 
377  template<typename Scalar, int Options> struct JointPlanarTpl;
378 
379  template<typename _Scalar, int _Options>
380  struct traits< JointPlanarTpl<_Scalar,_Options> >
381  {
382  enum {
383  NQ = 4,
384  NV = 3
385  };
386  enum { Options = _Options };
387  typedef _Scalar Scalar;
394 
395  // [ABA]
396  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
397  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
398  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
399 
400  PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
401 
402  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
403  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
404  };
405 
406  template<typename Scalar, int Options>
408  template<typename Scalar, int Options>
410 
411  template<typename _Scalar, int _Options>
412  struct JointDataPlanarTpl : public JointDataBase< JointDataPlanarTpl<_Scalar,_Options> >
413  {
414  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
416  PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE(JointDerived);
417  PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
418 
419  Constraint_t S;
420  Transformation_t M;
421  Motion_t v;
422  Bias_t c;
423 
424  // [ABA] specific data
425  U_t U;
426  D_t Dinv;
427  UD_t UDinv;
428 
429  D_t StU;
430 
431  JointDataPlanarTpl () : M(1), U(), Dinv(), UDinv() {}
432 
433  static std::string classname() { return std::string("JointDataPlanar"); }
434  std::string shortname() const { return classname(); }
435 
436  }; // struct JointDataPlanarTpl
437 
438  PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelPlanarTpl);
439  template<typename _Scalar, int _Options>
440  struct JointModelPlanarTpl
441  : public JointModelBase< JointModelPlanarTpl<_Scalar,_Options> >
442  {
443  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
445  PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(JointDerived);
446 
448  using Base::id;
449  using Base::idx_q;
450  using Base::idx_v;
451  using Base::setIndexes;
452 
453  JointDataDerived createData() const { return JointDataDerived(); }
454 
455  template<typename ConfigVector>
456  inline void forwardKinematics(Transformation_t & M, const Eigen::MatrixBase<ConfigVector> & q_joint) const
457  {
458  const Scalar
459  & c_theta = q_joint(2),
460  & s_theta = q_joint(3);
461 
462  M.rotation().template topLeftCorner<2,2>() << c_theta, -s_theta, s_theta, c_theta;
463  M.translation().template head<2>() = q_joint.template head<2>();
464  }
465 
466  template<typename ConfigVector>
467  void calc(JointDataDerived & data,
468  const typename Eigen::MatrixBase<ConfigVector> & qs) const
469  {
470  typedef typename ConfigVector::Scalar Scalar;
471  typename ConfigVector::template ConstFixedSegmentReturnType<NQ>::Type & q = qs.template segment<NQ>(idx_q());
472 
473  const Scalar
474  &c_theta = q(2),
475  &s_theta = q(3);
476 
477  data.M.rotation().template topLeftCorner<2,2>() << c_theta, -s_theta, s_theta, c_theta;
478  data.M.translation().template head<2>() = q.template head<2>();
479 
480  }
481 
482  template<typename ConfigVector, typename TangentVector>
483  void calc(JointDataDerived & data,
484  const typename Eigen::MatrixBase<ConfigVector> & qs,
485  const typename Eigen::MatrixBase<TangentVector> & vs) const
486  {
487  calc(data,qs.derived());
488 
489  typename TangentVector::template ConstFixedSegmentReturnType<NV>::Type & q_dot = vs.template segment<NV>(idx_v ());
490 
491  data.v.m_x_dot = q_dot(0);
492  data.v.m_y_dot = q_dot(1);
493  data.v.m_theta_dot = q_dot(2);
494  }
495 
496  template<typename Matrix6Like>
497  void calc_aba(JointDataDerived & data,
498  const Eigen::MatrixBase<Matrix6Like> & I,
499  const bool update_I) const
500  {
501  data.U.template leftCols<2>() = I.template leftCols<2>();
502  data.U.template rightCols<1>() = I.template rightCols<1>();
503 
504  data.StU.template leftCols<2>() = data.U.template topRows<2>().transpose();
505  data.StU.template rightCols<1>() = data.U.template bottomRows<1>();
506 
507  // compute inverse
508 // data.Dinv.setIdentity();
509 // data.StU.llt().solveInPlace(data.Dinv);
510  internal::PerformStYSInversion<Scalar>::run(data.StU,data.Dinv);
511 
512  data.UDinv.noalias() = data.U * data.Dinv;
513 
514  if(update_I)
515  PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I).noalias() -= data.UDinv * data.U.transpose();
516  }
517 
518  static std::string classname() { return std::string("JointModelPlanar");}
519  std::string shortname() const { return classname(); }
520 
522  template<typename NewScalar>
524  {
525  typedef JointModelPlanarTpl<NewScalar,Options> ReturnType;
526  ReturnType res;
527  res.setIndexes(id(),idx_q(),idx_v());
528  return res;
529  }
530 
531  }; // struct JointModelPlanarTpl
532 
533 } // namespace pinocchio
534 
535 #include <boost/type_traits.hpp>
536 
537 namespace boost
538 {
539  template<typename Scalar, int Options>
540  struct has_nothrow_constructor< ::pinocchio::JointModelPlanarTpl<Scalar,Options> >
541  : public integral_constant<bool,true> {};
542 
543  template<typename Scalar, int Options>
544  struct has_nothrow_copy< ::pinocchio::JointModelPlanarTpl<Scalar,Options> >
545  : public integral_constant<bool,true> {};
546 
547  template<typename Scalar, int Options>
548  struct has_nothrow_constructor< ::pinocchio::JointDataPlanarTpl<Scalar,Options> >
549  : public integral_constant<bool,true> {};
550 
551  template<typename Scalar, int Options>
552  struct has_nothrow_copy< ::pinocchio::JointDataPlanarTpl<Scalar,Options> >
553  : public integral_constant<bool,true> {};
554 }
555 
556 #endif // ifndef __pinocchio_joint_planar_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...
JointModelPlanarTpl< NewScalar, Options > cast() const
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...
Return type of the ation of a Motion onto an object of type D.
JointDataTpl< Scalar, Options, JointCollectionTpl > createData(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through CreateData visitor to create a JointDataTpl.
void forwardKinematics(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< ConfigVectorType > &q)
Update the joint placements according to the current joint configuration.
std::string shortname(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointShortnameVisitor to get the shortname of the derived joint model...
ConstAngularType angular() const
Return the angular part of the force vector.
Definition: force-base.hpp:40
Main pinocchio namespace.
Definition: treeview.dox:24
Common traits structure to fully define base classes for CRTP.
Definition: spatial/fwd.hpp:32
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...
MultiplicationOp< InertiaTpl< Scalar, Options >, ConstraintDerived >::ReturnType operator*(const InertiaTpl< Scalar, Options > &Y, const ConstraintBase< ConstraintDerived > &constraint)