pinocchio  UNKNOWN
joint-translation.hpp
1 //
2 // Copyright (c) 2015-2018 CNRS
3 // Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 // This file is part of Pinocchio
6 // Pinocchio is free software: you can redistribute it
7 // and/or modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation, either version
9 // 3 of the License, or (at your option) any later version.
10 //
11 // Pinocchio is distributed in the hope that it will be
12 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Lesser Public License for more details. You should have
15 // received a copy of the GNU Lesser General Public License along with
16 // Pinocchio If not, see
17 // <http://www.gnu.org/licenses/>.
18 
19 #ifndef __se3_joint_translation_hpp__
20 #define __se3_joint_translation_hpp__
21 
22 #include "pinocchio/macros.hpp"
23 #include "pinocchio/multibody/joint/joint-base.hpp"
24 #include "pinocchio/multibody/constraint.hpp"
25 #include "pinocchio/spatial/inertia.hpp"
26 #include "pinocchio/spatial/skew.hpp"
27 
28 namespace se3
29 {
30 
31  template<typename Scalar, int Options> struct MotionTranslationTpl;
32 
33  template<typename _Scalar, int _Options>
34  struct traits< MotionTranslationTpl<_Scalar,_Options> >
35  {
36  typedef _Scalar Scalar;
37  enum { Options = _Options };
38  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
39  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
40  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
41  typedef typename EIGEN_REF_CONSTTYPE(Vector6) ToVectorConstReturnType;
42  typedef typename EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
43  typedef Vector3 AngularType;
44  typedef Vector3 LinearType;
45  typedef const Vector3 ConstAngularType;
46  typedef const Vector3 ConstLinearType;
47  typedef Matrix6 ActionMatrixType;
49  enum {
50  LINEAR = 0,
51  ANGULAR = 3
52  };
53  }; // traits MotionTranslationTpl
54 
55  template<typename _Scalar, int _Options>
56  struct MotionTranslationTpl : MotionBase< MotionTranslationTpl<_Scalar,_Options> >
57  {
58  MOTION_TYPEDEF_TPL(MotionTranslationTpl);
59 
60  MotionTranslationTpl() : v (Motion::Vector3 (NAN, NAN, NAN)) {}
61  template<typename Vector3Like>
62  MotionTranslationTpl(const Eigen::MatrixBase<Vector3Like> & v) : v (v) {}
63 
64  MotionTranslationTpl(const MotionTranslationTpl & other) : v (other.v) {}
65 
66  Vector3 & operator()() { return v; }
67  const Vector3 & operator()() const { return v; }
68 
69  operator MotionPlain() const
70  {
71  return MotionPlain(v,MotionPlain::Vector3::Zero());
72  }
73 
74  MotionTranslationTpl & operator=(const MotionTranslationTpl & other)
75  {
76  v = other.v;
77  return *this;
78  }
79 
80  template<typename Derived>
81  void addTo(MotionDense<Derived> & v_) const
82  {
83  v_.linear() += v;
84  }
85 
86  // data
87  Vector3 v;
88 
89  }; // struct MotionTranslationTpl
90 
91  template<typename S1, int O1, typename MotionDerived>
92  inline typename MotionDerived::MotionPlain operator+(const MotionTranslationTpl<S1,O1> & m1, const MotionDense<MotionDerived> & m2)
93  {
94  return typename MotionDerived::MotionPlain(m2.linear() + m1.v, m2.angular());
95  }
96 
97  template<typename Scalar, int Options> struct ConstraintTranslationTpl;
98 
99  template<typename _Scalar, int _Options>
100  struct traits< ConstraintTranslationTpl<_Scalar,_Options> >
101  {
102  typedef _Scalar Scalar;
103  enum { Options = _Options };
104  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
105  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
106  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
107  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
108  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
109  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
110  typedef Matrix3 Angular_t;
111  typedef Vector3 Linear_t;
112  typedef const Matrix3 ConstAngular_t;
113  typedef const Vector3 ConstLinear_t;
114  typedef Matrix6 ActionMatrix_t;
115  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
116  typedef SE3Tpl<Scalar,Options> SE3;
120  enum {
121  LINEAR = 0,
122  ANGULAR = 3
123  };
124  typedef Eigen::Matrix<Scalar,3,1,Options> JointMotion;
125  typedef Eigen::Matrix<Scalar,3,1,Options> JointForce;
126  typedef Eigen::Matrix<Scalar,6,3,Options> DenseBase;
127  typedef DenseBase MatrixReturnType;
128  typedef const DenseBase ConstMatrixReturnType;
129  }; // traits ConstraintTranslationTpl
130 
131  template<typename _Scalar, int _Options>
132  struct ConstraintTranslationTpl : ConstraintBase< ConstraintTranslationTpl<_Scalar,_Options> >
133  {
134  SPATIAL_TYPEDEF_TEMPLATE(ConstraintTranslationTpl);
135 
136  enum { NV = 3, Options = traits<ConstraintTranslationTpl>::Options };
137  typedef typename traits<ConstraintTranslationTpl>::JointMotion JointMotion;
138  typedef typename traits<ConstraintTranslationTpl>::JointForce JointForce;
139  typedef typename traits<ConstraintTranslationTpl>::DenseBase DenseBase;
140 
142 
143  template<typename S1, int O1>
144  Motion operator*(const MotionTranslationTpl<S1,O1> & vj) const
145  { return Motion(vj(), Motion::Vector3::Zero()); }
146 
147  template<typename Vector3Like>
149  operator*(const Eigen::MatrixBase<Vector3Like> & v) const
150  {
151  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3);
153  }
154 
155  int nv_impl() const { return NV; }
156 
158  {
159  const ConstraintTranslationTpl & ref;
160  ConstraintTranspose(const ConstraintTranslationTpl & ref) : ref(ref) {}
161 
162  template<typename Derived>
164  operator* (const ForceDense<Derived> & phi)
165  {
166  return phi.linear();
167  }
168 
169  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
170  template<typename MatrixDerived>
171  const typename SizeDepType<3>::RowsReturn<MatrixDerived>::ConstType
172  operator*(const Eigen::MatrixBase<MatrixDerived> & F) const
173  {
174  assert(F.rows()==6);
175  return F.derived().template middleRows<3>(LINEAR);
176  }
177 
178  }; // struct ConstraintTranspose
179 
180  ConstraintTranspose transpose () const { return ConstraintTranspose(*this); }
181 
182  DenseBase matrix_impl() const
183  {
184  DenseBase S;
185  S.template middleRows<3>(LINEAR).setIdentity();
186  S.template middleRows<3>(ANGULAR).setZero();
187  return S;
188  }
189 
190  template<typename S1, int O1>
191  Eigen::Matrix<S1,6,3,O1> se3Action(const SE3Tpl<S1,O1> & m) const
192  {
193  Eigen::Matrix<S1,6,3,O1> M;
194  M.template middleRows<3>(LINEAR) = m.rotation ();
195  M.template middleRows<3>(ANGULAR).setZero ();
196 
197  return M;
198  }
199 
200  template<typename MotionDerived>
201  DenseBase motionAction(const MotionDense<MotionDerived> & m) const
202  {
203  const typename MotionDerived::ConstAngularType w = m.angular();
204 
205  DenseBase res;
206  skew(w,res.template middleRows<3>(LINEAR));
207  res.template middleRows<3>(ANGULAR).setZero();
208 
209  return res;
210  }
211 
212  }; // struct ConstraintTranslationTpl
213 
214  template<typename MotionDerived, typename S2, int O2>
215  inline typename MotionDerived::MotionPlain
216  operator^(const MotionDense<MotionDerived> & m1,
217  const MotionTranslationTpl<S2,O2> & m2)
218  {
219  typedef typename MotionDerived::MotionPlain ReturnType;
220  return ReturnType(m1.angular().cross(m2.v),
221  ReturnType::Vector3::Zero());
222  }
223 
224  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
225  template<typename S1, int O1, typename S2, int O2>
226  inline Eigen::Matrix<S2,6,3,O2>
227  operator*(const InertiaTpl<S1,O1> & Y,
229  {
230  typedef ConstraintTranslationTpl<S2,O2> Constraint;
231  Eigen::Matrix<S2,6,3,O2> M;
232  alphaSkew(Y.mass(),Y.lever(),M.template middleRows<3>(Constraint::ANGULAR));
233  M.template middleRows<3>(Constraint::LINEAR).setZero();
234  M.template middleRows<3>(Constraint::LINEAR).diagonal().fill(Y.mass ());
235 
236  return M;
237  }
238 
239  /* [ABA] Y*S operator*/
240  template<typename M6Like, typename S2, int O2>
241  inline const typename SizeDepType<3>::ColsReturn<M6Like>::ConstType
242  operator*(const Eigen::MatrixBase<M6Like> & Y,
244  {
245  typedef ConstraintTranslationTpl<S2,O2> Constraint;
246  return Y.derived().template middleCols<3>(Constraint::LINEAR);
247  }
248 
249  namespace internal
250  {
251  template<typename S1, int O1>
252  struct SE3GroupAction< ConstraintTranslationTpl<S1,O1> >
253  { typedef Eigen::Matrix<S1,6,3,O1> ReturnType; };
254 
255  template<typename S1, int O1, typename MotionDerived>
256  struct MotionAlgebraAction< ConstraintTranslationTpl<S1,O1>,MotionDerived >
257  { typedef Eigen::Matrix<S1,6,3,O1> ReturnType; };
258  }
259 
260 
261  template<typename Scalar, int Options> struct JointTranslationTpl;
262 
263  template<typename _Scalar, int _Options>
264  struct traits< JointTranslationTpl<_Scalar,_Options> >
265  {
266  enum {
267  NQ = 3,
268  NV = 3
269  };
270  typedef _Scalar Scalar;
271  enum { Options = _Options };
277  typedef BiasZero Bias_t;
278  typedef Eigen::Matrix<Scalar,6,NV,Options> F_t;
279 
280  // [ABA]
281  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
282  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
283  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
284 
285  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
286  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
287  }; // traits JointTranslationTpl
288 
289  template<typename Scalar, int Options>
290  struct traits< JointDataTranslationTpl<Scalar,Options> >
292 
293  template<typename Scalar, int Options>
294  struct traits< JointModelTranslationTpl<Scalar,Options> >
296 
297  template<typename _Scalar, int _Options>
298  struct JointDataTranslationTpl : public JointDataBase< JointDataTranslationTpl<_Scalar,_Options> >
299  {
301  SE3_JOINT_TYPEDEF_TEMPLATE;
302 
303  Constraint_t S;
304  Transformation_t M;
305  Motion_t v;
306  Bias_t c;
307 
308  F_t F; // TODO if not used anymore, clean F_t
309 
310  // [ABA] specific data
311  U_t U;
312  D_t Dinv;
313  UD_t UDinv;
314 
315  JointDataTranslationTpl () : M(1), U(), Dinv(), UDinv() {}
316 
317  }; // struct JointDataTranslationTpl
318 
319  template<typename _Scalar, int _Options>
320  struct JointModelTranslationTpl : public JointModelBase< JointModelTranslationTpl<_Scalar,_Options> >
321  {
323  SE3_JOINT_TYPEDEF_TEMPLATE;
324 
329 
330  JointDataDerived createData() const { return JointDataDerived(); }
331 
332  template<typename ConfigVector>
333  void calc(JointDataDerived & data,
334  const typename Eigen::MatrixBase<ConfigVector> & qs) const
335  {
336  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ConfigVector_t,ConfigVector);
337  data.M.translation(qs.template segment<NQ>(idx_q()));
338  }
339 
340  template<typename ConfigVector, typename TangentVector>
341  void calc(JointDataDerived & data,
342  const typename Eigen::MatrixBase<ConfigVector> & qs,
343  const typename Eigen::MatrixBase<TangentVector> & vs) const
344  {
345  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TangentVector_t,TangentVector);
346  calc(data,qs.derived());
347 
348  data.v() = vs.template segment<NQ>(idx_v());
349  }
350 
351  template<typename S2, int O2>
352  void calc_aba(JointDataDerived & data, Eigen::Matrix<S2,6,6,O2> & I, const bool update_I) const
353  {
354  data.U = I.template middleCols<3>(Inertia::LINEAR);
355  data.Dinv = data.U.template middleRows<3>(Inertia::LINEAR).inverse();
356  data.UDinv.template middleRows<3>(Inertia::LINEAR).setIdentity(); // can be put in data constructor
357  data.UDinv.template middleRows<3>(Inertia::ANGULAR).noalias() = data.U.template middleRows<3>(Inertia::ANGULAR) * data.Dinv;
358 
359  if (update_I)
360  {
361  I.template block<3,3>(Inertia::ANGULAR,Inertia::ANGULAR)
362  -= data.UDinv.template middleRows<3>(Inertia::ANGULAR) * I.template block<3,3>(Inertia::LINEAR, Inertia::ANGULAR);
363  I.template middleCols<3>(Inertia::LINEAR).setZero();
364  I.template block<3,3>(Inertia::LINEAR,Inertia::ANGULAR).setZero();
365  }
366  }
367 
368  Scalar finiteDifferenceIncrement() const
369  {
370  using std::sqrt;
371  return sqrt(Eigen::NumTraits<Scalar>::epsilon());
372  }
373 
374  static std::string classname() { return std::string("JointModelTranslation"); }
375  std::string shortname() const { return classname(); }
376 
377  }; // struct JointModelTranslationTpl
378 
379 } // namespace se3
380 
381 #endif // ifndef __se3_joint_translation_hpp__
JointDataVariant createData(const JointModelVariant &jmodel)
Visit a JointModelVariant through CreateData visitor to create a JointDataVariant.
void alphaSkew(const Scalar alpha, const Eigen::MatrixBase< Vector3 > &v, const Eigen::MatrixBase< Matrix3 > &M)
Computes the skew representation of a given 3d vector multiplied by a given scalar. i.e. the antisymmetric matrix representation of the cross product operator ( )
Definition: skew.hpp:116
void calc_aba(const JointModelVariant &jmodel, JointDataVariant &jdata, Inertia::Matrix6 &I, const bool update_I)
Visit a JointDataVariant and the corresponding JointModelVariant through JointCalcAbaVisitor to...
void skew(const Eigen::MatrixBase< Vector3 > &v, const Eigen::MatrixBase< Matrix3 > &M)
Computes the skew representation of a given 3d vector, i.e. the antisymmetric matrix representation o...
Definition: skew.hpp:34
ConstLinearType linear() const
Return the linear part of the force vector.
Definition: force-base.hpp:60
std::string shortname(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointShortnameVisitor to get the shortname of the derived joint mod...
int idx_q(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointIdxQVisitor to get the index in the full model configuration s...
Eigen::VectorXd finiteDifferenceIncrement(const Model &model)
Computes the finite difference increments for each degree of freedom according to the current joint c...
int idx_v(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointIdxVVisitor to get the index in the full model tangent space c...