pinocchio  UNKNOWN
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 // 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_prismatic_unaligned_hpp__
20 #define __se3_joint_prismatic_unaligned_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 
27 namespace se3
28 {
29 
30  template<typename Scalar, int Options> struct MotionPrismaticUnaligned;
31 
32  template<typename _Scalar, int _Options>
33  struct traits< MotionPrismaticUnaligned<_Scalar,_Options> >
34  {
35  typedef _Scalar Scalar;
36  enum { Options = _Options };
37  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
38  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
39  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
40  typedef typename EIGEN_REF_CONSTTYPE(Vector6) ToVectorConstReturnType;
41  typedef typename EIGEN_REF_TYPE(Vector6) ToVectorReturnType;
42  typedef Vector3 AngularType;
43  typedef Vector3 LinearType;
44  typedef const Vector3 ConstAngularType;
45  typedef const Vector3 ConstLinearType;
46  typedef Matrix6 ActionMatrixType;
48  enum {
49  LINEAR = 0,
50  ANGULAR = 3
51  };
52  }; // traits MotionPrismaticUnaligned
53 
54  template<typename _Scalar, int _Options>
55  struct MotionPrismaticUnaligned : MotionBase < MotionPrismaticUnaligned<_Scalar,_Options> >
56  {
57  MOTION_TYPEDEF_TPL(MotionPrismaticUnaligned);
58 
59  MotionPrismaticUnaligned () : axis(Vector3::Constant(NAN)), rate(NAN) {}
60 
61  template<typename Vector3Like, typename S2>
62  MotionPrismaticUnaligned (const Eigen::MatrixBase<Vector3Like> & axis, const S2 rate)
63  : axis(axis), rate(rate)
64  { EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3); }
65 
66  operator MotionPlain() const
67  { return MotionPlain(axis*rate,MotionPlain::Vector3::Zero());}
68 
69  template<typename Derived>
70  void addTo(MotionDense<Derived> & v) const
71  {
72  v.linear() += axis * rate;
73  }
74 
75  // data
76  Vector3 axis;
77  Scalar rate;
78  }; // struct MotionPrismaticUnaligned
79 
80  template<typename Scalar, int Options, typename MotionDerived>
81  inline typename MotionDerived::MotionPlain
83  {
84  typedef typename MotionDerived::MotionPlain ReturnType;
85  return ReturnType(m1.rate*m1.axis + m2.linear(), m2.angular());
86  }
87 
88  template<typename Scalar, int Options> struct ConstraintPrismaticUnaligned;
89 
90  template<typename _Scalar, int _Options>
91  struct traits< ConstraintPrismaticUnaligned<_Scalar,_Options> >
92  {
93  typedef _Scalar Scalar;
94  enum { Options = _Options };
95  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
96  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
97  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
98  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
99  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
100  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
101  typedef Matrix3 Angular_t;
102  typedef Vector3 Linear_t;
103  typedef const Matrix3 ConstAngular_t;
104  typedef const Vector3 ConstLinear_t;
105  typedef Matrix6 ActionMatrix_t;
106  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
107  typedef SE3Tpl<Scalar,Options> SE3;
111  enum {
112  LINEAR = 0,
113  ANGULAR = 3
114  };
115  typedef Eigen::Matrix<Scalar,1,1,Options> JointMotion;
116  typedef Eigen::Matrix<Scalar,1,1,Options> JointForce;
117  typedef Eigen::Matrix<Scalar,6,1,Options> DenseBase;
118  typedef DenseBase MatrixReturnType;
119  typedef const DenseBase ConstMatrixReturnType;
120  }; // traits ConstraintPrismaticUnaligned
121 
122  template<typename _Scalar, int _Options>
123  struct ConstraintPrismaticUnaligned : ConstraintBase< ConstraintPrismaticUnaligned<_Scalar,_Options> >
124  {
125  SPATIAL_TYPEDEF_TEMPLATE(ConstraintPrismaticUnaligned);
126 
127  enum { NV = 1, Options = _Options };
128  typedef typename traits<ConstraintPrismaticUnaligned>::JointMotion JointMotion;
129  typedef typename traits<ConstraintPrismaticUnaligned>::JointForce JointForce;
130  typedef typename traits<ConstraintPrismaticUnaligned>::DenseBase DenseBase;
131 
132  ConstraintPrismaticUnaligned() : axis(Vector3::Constant(NAN)) {}
133 
134  template<typename Vector3Like>
135  ConstraintPrismaticUnaligned(const Eigen::MatrixBase<Vector3Like> & axis)
136  : axis(axis)
137  { EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector3Like,3); }
138 
139  template<typename Derived>
140  MotionPrismaticUnaligned<Scalar,Options> operator*(const Eigen::MatrixBase<Derived> & v) const
141  {
142  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,1);
144  }
145 
146  template<typename S1, int O1>
147  Vector6 se3Action(const SE3Tpl<S1,O1> & m) const
148  {
149  Vector6 res;
150  res.template head<3>().noalias() = m.rotation()*axis;
151  res.template tail<3>().setZero();
152  return res;
153  }
154 
155  int nv_impl() const { return NV; }
156 
158  {
159  typedef typename traits<ConstraintPrismaticUnaligned>::Scalar Scalar;
160  typedef typename traits<ConstraintPrismaticUnaligned>::Force Force;
161  typedef typename traits<ConstraintPrismaticUnaligned>::Vector6 Vector6;
162 
163  const ConstraintPrismaticUnaligned & ref;
164  TransposeConst(const ConstraintPrismaticUnaligned & ref) : ref(ref) {}
165 
166  template<typename Derived>
167  Eigen::Matrix<
168  typename EIGEN_DOT_PRODUCT_RETURN_TYPE(Vector3,typename ForceDense<Derived>::ConstLinearType),
169  1,1>
170  operator* (const ForceDense<Derived> & f) const
171  {
172  typedef Eigen::Matrix<
173  typename EIGEN_DOT_PRODUCT_RETURN_TYPE(Vector3,typename ForceDense<Derived>::ConstLinearType),
174  1,1> ReturnType;
175 
176  ReturnType res; res[0] = ref.axis.dot(f.linear());
177  return res;
178  }
179 
180  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
181  template<typename D>
182  friend
183 #if EIGEN_VERSION_AT_LEAST(3,2,90)
184  const Eigen::Product<
185  Eigen::Transpose<const Vector3>,
186  typename Eigen::MatrixBase<const D>::template NRowsBlockXpr<3>::Type
187  >
188 #else
189  const typename Eigen::ProductReturnType<
190  Eigen::Transpose<const Vector3>,
191  typename Eigen::MatrixBase<const D>::template NRowsBlockXpr<3>::Type
192  >::Type
193 #endif
194  operator* (const TransposeConst & tc, const Eigen::MatrixBase<D> & F)
195  {
196  EIGEN_STATIC_ASSERT(D::RowsAtCompileTime==6,THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
197  /* Return ax.T * F[1:3,:] */
198  return tc.ref.axis.transpose () * F.template topRows<3> ();
199  }
200 
201  };
202  TransposeConst transpose() const { return TransposeConst(*this); }
203 
204 
205  /* CRBA joint operators
206  * - ForceSet::Block = ForceSet
207  * - ForceSet operator* (Inertia Y,Constraint S)
208  * - MatrixBase operator* (Constraint::Transpose S, ForceSet::Block)
209  * - SE3::act(ForceSet::Block)
210  */
211  DenseBase matrix_impl() const
212  {
213  DenseBase S;
214  S << axis, Vector3::Zero();
215  return S;
216  }
217 
218  template<typename MotionDerived>
219  DenseBase motionAction(const MotionDense<MotionDerived> & v) const
220  {
221  DenseBase res;
222  res << v.angular().cross(axis), Vector3::Zero();
223 
224  return res;
225  }
226 
227  // data
228  Vector3 axis;
229 
230  }; // struct ConstraintPrismaticUnaligned
231 
232 
233  template<typename MotionDerived, typename S2, int O2>
234  inline typename MotionDerived::MotionPlain
235  operator^(const MotionDense<MotionDerived> & m1,
237  {
238  typedef typename MotionDerived::MotionPlain ReturnType;
239  /* m1xm2 = [ v1xw2 + w1xv2; w1xw2 ] = [ v1xw2; w1xw2 ] */
240  const typename MotionDerived::ConstAngularType & w1 = m1.angular();
241  const typename MotionPrismaticUnaligned<S2,O2>::Vector3 & v2 = m2.axis * m2.rate;
242  return ReturnType(w1.cross(v2), ReturnType::Vector3::Zero());
243  }
244 
245  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
246  template<typename S1, int O1, typename S2, int O2>
247  inline Eigen::Matrix<S1,6,1>
248  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismaticUnaligned<S2,O2> & cpu)
249  {
250  typedef InertiaTpl<S1,O1> Inertia;
251  /* YS = [ m -mcx ; mcx I-mcxcx ] [ v ; 0 ] = [ mv ; mcxv ] */
252  const S1 & m = Y.mass();
253  const typename Inertia::Vector3 & c = Y.lever();
254 
255  Eigen::Matrix<S1,6,1> res;
256  res.template head<3>().noalias() = m*cpu.axis;
257  res.template tail<3>() = c.cross(res.template head<3>());
258  return res;
259  }
260 
261  /* [ABA] Y*S operator (Inertia Y,Constraint S) */
262  template<typename M6, typename S2, int O2>
263 #if EIGEN_VERSION_AT_LEAST(3,2,90)
264  const typename Eigen::Product<
265  Eigen::Block<const M6,6,3>,
267  Eigen::DefaultProduct>
268 #else
269  const typename Eigen::ProductReturnType<
270  Eigen::Block<const M6,6,3>,
271  const typename ConstraintPrismaticUnaligned<S2,O2>::Vector3
272  >::Type
273 #endif
274  operator*(const Eigen::MatrixBase<M6> & Y, const ConstraintPrismaticUnaligned<S2,O2> & cpu)
275  {
276  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(M6,6,6);
277  return Y.template block<6,3> (0,Inertia::LINEAR) * cpu.axis;
278  }
279 
280 
281  namespace internal
282  {
283  template<typename Scalar, int Options>
284  struct SE3GroupAction< ConstraintPrismaticUnaligned<Scalar,Options> >
285  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
286 
287  template<typename Scalar, int Options, typename MotionDerived>
288  struct MotionAlgebraAction< ConstraintPrismaticUnaligned<Scalar,Options>,MotionDerived >
289  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
290  }
291 
292  template<typename Scalar, int Options> struct JointPrismaticUnalignedTpl;
293 
294  template<typename _Scalar, int _Options>
295  struct traits< JointPrismaticUnalignedTpl<_Scalar,_Options> >
296  {
297  enum {
298  NQ = 1,
299  NV = 1
300  };
301  typedef _Scalar Scalar;
302  enum { Options = _Options };
308  typedef BiasZero Bias_t;
309  typedef Eigen::Matrix<Scalar,6,NV,Options> F_t;
310 
311  // [ABA]
312  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
313  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
314  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
315 
316  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
317  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
318  };
319 
320  template<typename Scalar, int Options>
321  struct traits< JointDataPrismaticUnalignedTpl<Scalar,Options> >
323 
324  template<typename _Scalar, int _Options>
325  struct JointDataPrismaticUnalignedTpl : public JointDataBase< JointDataPrismaticUnalignedTpl<_Scalar,_Options> >
326  {
328  SE3_JOINT_TYPEDEF_TEMPLATE;
329 
330  Transformation_t M;
331  Constraint_t S;
332  Motion_t v;
333  Bias_t c;
334 
335  F_t F;
336 
337  // [ABA] specific data
338  U_t U;
339  D_t Dinv;
340  UD_t UDinv;
341 
343  : M(1)
344  , S(Motion_t::Vector3::Constant(NAN))
345  , v(Motion_t::Vector3::Constant(NAN),NAN)
346  , U(), Dinv(), UDinv()
347  {}
348 
349  template<typename Vector3Like>
350  JointDataPrismaticUnalignedTpl(const Eigen::MatrixBase<Vector3Like> & axis)
351  : M(1)
352  , S(axis)
353  , v(axis,NAN)
354  , U(), Dinv(), UDinv()
355  {}
356 
357  }; // struct JointDataPrismaticUnalignedTpl
358 
359  template<typename Scalar, int Options>
360  struct traits< JointModelPrismaticUnalignedTpl<Scalar,Options> >
362 
363  template<typename _Scalar, int _Options>
364  struct JointModelPrismaticUnalignedTpl : public JointModelBase< JointModelPrismaticUnalignedTpl<_Scalar,_Options> >
365  {
367  SE3_JOINT_TYPEDEF_TEMPLATE;
368 
373  typedef Motion::Vector3 Vector3;
374 
375  JointModelPrismaticUnalignedTpl() : axis(Vector3::Constant(NAN)) {}
376  JointModelPrismaticUnalignedTpl(Scalar x, Scalar y, Scalar z)
377  {
378  axis << x, y, z ;
379  axis.normalize();
380  assert(axis.isUnitary() && "Translation axis is not unitary");
381  }
382 
383  template<typename Vector3Like>
384  JointModelPrismaticUnalignedTpl(const Eigen::MatrixBase<Vector3Like> & axis) : axis(axis)
385  {
386  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Vector3Like);
387  assert(axis.isUnitary() && "Translation axis is not unitary");
388  }
389 
390  JointDataDerived createData() const { return JointDataDerived(axis); }
391 
392  template<typename ConfigVector>
393  void calc(JointDataDerived & data,
394  const typename Eigen::MatrixBase<ConfigVector> & qs) const
395  {
396  EIGEN_STATIC_ASSERT_VECTOR_ONLY(ConfigVector);
397  typedef typename ConfigVector::Scalar Scalar;
398  const Scalar & q = qs[idx_q()];
399 
400  data.M.translation().noalias() = axis * q;
401  }
402 
403  template<typename ConfigVector, typename TangentVector>
404  void calc(JointDataDerived & data,
405  const typename Eigen::MatrixBase<ConfigVector> & qs,
406  const typename Eigen::MatrixBase<TangentVector> & vs) const
407  {
408  EIGEN_STATIC_ASSERT_VECTOR_ONLY(TangentVector);
409  calc(data,qs.derived());
410 
411  typedef typename TangentVector::Scalar S2;
412  const S2 & v = vs[idx_v()];
413  data.v.rate = v;
414  }
415 
416  template<typename S2, int O2>
417  void calc_aba(JointDataDerived & data, Eigen::Matrix<S2,6,6,O2> & I, const bool update_I) const
418  {
419  data.U.noalias() = I.template block<6,3> (0,Inertia::LINEAR) * axis;
420  data.Dinv[0] = Scalar(1)/axis.dot(data.U.template segment <3> (Inertia::LINEAR));
421  data.UDinv.noalias() = data.U * data.Dinv;
422 
423  if (update_I)
424  I -= data.UDinv * data.U.transpose();
425  }
426 
427  Scalar finiteDifferenceIncrement() const
428  {
429  using std::sqrt;
430  return sqrt(Eigen::NumTraits<Scalar>::epsilon());
431  }
432 
433  static std::string classname() { return std::string("JointModelPrismaticUnalignedTpl"); }
434  std::string shortname() const { return classname(); }
435 
436  Vector3 axis;
437  }; // struct JointModelPrismaticUnalignedTpl
438 
440 } //namespace se3
441 
442 
443 #endif // ifndef __se3_joint_prismatic_unaligned_hpp__
JointDataVariant createData(const JointModelVariant &jmodel)
Visit a JointModelVariant through CreateData visitor to create a JointDataVariant.
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...
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...