pinocchio  UNKNOWN
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 // 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_hpp__
20 #define __se3_joint_prismatic_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/spatial-axis.hpp"
27 #include "pinocchio/utils/axis-label.hpp"
28 
29 namespace se3
30 {
31 
32  template<typename Scalar, int Options, int _axis> struct MotionPrismatic;
33 
34  template<typename _Scalar, int _Options, int _axis>
35  struct traits < MotionPrismatic<_Scalar,_Options,_axis> >
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 EIGEN_REF_CONSTTYPE(Vector6) ToVectorConstReturnType;
43  typedef typename 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  enum {
51  LINEAR = 0,
52  ANGULAR = 3
53  };
54  }; // struct traits MotionPrismatic
55 
56  template<typename _Scalar, int _Options, int _axis>
57  struct MotionPrismatic : MotionBase < MotionPrismatic<_Scalar,_Options,_axis> >
58  {
59  MOTION_TYPEDEF_TPL(MotionPrismatic);
60  typedef SpatialAxis<_axis+LINEAR> Axis;
61 
62  MotionPrismatic() : v(NAN) {}
63  MotionPrismatic( const Scalar & v ) : v(v) {}
64  Scalar v;
65 
66  inline operator MotionPlain() const { return Axis() * v; }
67 
68  template<typename Derived>
69  void addTo(MotionDense<Derived> & v_) const
70  {
71  typedef typename MotionDense<Derived>::Scalar OtherScalar;
72  v_.linear()[_axis] += (OtherScalar) v;
73  }
74  }; // struct MotionPrismatic
75 
76  template<typename Scalar, int Options, int axis, typename MotionDerived>
77  typename MotionDerived::MotionPlain
78  operator+(const MotionPrismatic<Scalar,Options,axis> & m1,
79  const MotionDense<MotionDerived> & m2)
80  {
81  typename MotionDerived::MotionPlain res(m2);
82  res += m1;
83  return res;
84  }
85 
86  template<typename Scalar, int Options, int axis> struct ConstraintPrismatic;
87 
88  template<typename _Scalar, int _Options, int axis>
89  struct traits< ConstraintPrismatic<_Scalar,_Options,axis> >
90  {
91  typedef _Scalar Scalar;
92  enum { Options = _Options };
93  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
94  typedef Eigen::Matrix<Scalar,4,1,Options> Vector4;
95  typedef Eigen::Matrix<Scalar,6,1,Options> Vector6;
96  typedef Eigen::Matrix<Scalar,3,3,Options> Matrix3;
97  typedef Eigen::Matrix<Scalar,4,4,Options> Matrix4;
98  typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
99  typedef Matrix3 Angular_t;
100  typedef Vector3 Linear_t;
101  typedef const Matrix3 ConstAngular_t;
102  typedef const Vector3 ConstLinear_t;
103  typedef Matrix6 ActionMatrix_t;
104  typedef Eigen::Quaternion<Scalar,Options> Quaternion_t;
105  typedef SE3Tpl<Scalar,Options> SE3;
109  enum {
110  LINEAR = 0,
111  ANGULAR = 3
112  };
113  typedef Eigen::Matrix<Scalar,1,1,Options> JointMotion;
114  typedef Eigen::Matrix<Scalar,1,1,Options> JointForce;
115  typedef Eigen::Matrix<Scalar,6,1,Options> DenseBase;
116  typedef DenseBase MatrixReturnType;
117  typedef const DenseBase ConstMatrixReturnType;
118  }; // traits ConstraintRevolute
119 
120  template<typename _Scalar, int _Options, int axis>
121  struct ConstraintPrismatic : ConstraintBase < ConstraintPrismatic <_Scalar,_Options,axis> >
122  {
123  SPATIAL_TYPEDEF_TEMPLATE(ConstraintPrismatic);
124  enum { NV = 1, Options = _Options };
125  typedef typename traits<ConstraintPrismatic>::JointMotion JointMotion;
126  typedef typename traits<ConstraintPrismatic>::JointForce JointForce;
127  typedef typename traits<ConstraintPrismatic>::DenseBase DenseBase;
128  typedef SpatialAxis<LINEAR+axis> Axis;
129 
130  template<typename D>
131  MotionPrismatic<Scalar,Options,axis> operator*(const Eigen::MatrixBase<D> & v) const
132  {
133 // EIGEN_STATIC_ASSERT_SIZE_1x1(D); // There is actually a bug in Eigen with such a macro
134  assert(v.cols() == 1 && v.rows() == 1);
136  }
137 
138  template<typename S2, int O2>
139  DenseBase se3Action(const SE3Tpl<S2,O2> & m) const
140  {
141  DenseBase res;
142  MotionRef<DenseBase> v(res);
143  v.linear() = m.rotation().col(axis);
144  v.angular().setZero();
145  return res;
146  }
147 
148  int nv_impl() const { return NV; }
149 
151  {
152  const ConstraintPrismatic & ref;
153  TransposeConst(const ConstraintPrismatic & ref) : ref(ref) {}
154 
155  template<typename Derived>
156  typename ForceDense<Derived>::ConstLinearType::template ConstFixedSegmentReturnType<1>::Type
157  operator* (const ForceDense<Derived> & f) const
158  { return f.linear().template segment<1>(axis); }
159 
160  /* [CRBA] MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
161  template<typename D>
162  friend typename Eigen::MatrixBase<D>::ConstRowXpr
163  operator*( const TransposeConst &, const Eigen::MatrixBase<D> & F )
164  {
165  assert(F.rows()==6);
166  return F.row(axis);
167  }
168 
169  }; // struct TransposeConst
170  TransposeConst transpose() const { return TransposeConst(*this); }
171 
172  /* CRBA joint operators
173  * - ForceSet::Block = ForceSet
174  * - ForceSet operator* (Inertia Y,Constraint S)
175  * - MatrixBase operator* (Constraint::Transpose S, ForceSet::Block)
176  * - SE3::act(ForceSet::Block)
177  */
178  DenseBase matrix_impl() const
179  {
180  DenseBase S;
182  v << Axis();
183  return S;
184  }
185 
186  template<typename MotionDerived>
187  DenseBase motionAction(const MotionDense<MotionDerived> & m) const
188  {
189  DenseBase res;
190  MotionRef<DenseBase> v(res);
191  v = m.cross(Axis());
192  return res;
193  }
194 
195  }; // struct ConstraintPrismatic
196 
197  template<typename MotionDerived, typename S1, int O1>
198  inline typename MotionDerived::MotionPlain
199  operator^(const MotionDense<MotionDerived> & m1,
200  const MotionPrismatic<S1,O1,0>& m2)
201  {
202  /* nu1^nu2 = ( v1^w2+w1^v2, w1^w2 )
203  * nu1^(v2,0) = ( w1^v2 , 0 )
204  * (x,y,z)^(v,0,0) = ( 0,zv,-yv )
205  * nu1^(0,vx) = ( 0,wz1 vx,-wy1 vx, 0, 0, 0)
206  */
207  typedef typename MotionDerived::MotionPlain MotionPlain;
208  const typename MotionDerived::ConstAngularType & w = m1.angular();
209  const S1 & vx = m2.v;
210  return MotionPlain(typename MotionPlain::Vector3(0,w[2]*vx,-w[1]*vx),
211  MotionPlain::Vector3::Zero());
212  }
213 
214  template<typename MotionDerived, typename S1, int O1>
215  inline typename MotionDerived::MotionPlain
216  operator^(const MotionDense<MotionDerived> & m1,
217  const MotionPrismatic<S1,O1,1>& m2)
218  {
219  /* nu1^nu2 = ( v1^w2+w1^v2, w1^w2 )
220  * nu1^(v2,0) = ( w1^v2 , 0 )
221  * (x,y,z)^(0,v,0) = ( -zv,0,xv )
222  * nu1^(0,vx) = ( -vz1 vx,0,vx1 vx, 0, 0, 0)
223  */
224  typedef typename MotionDerived::MotionPlain MotionPlain;
225  const typename MotionDerived::ConstAngularType & w = m1.angular();
226  const S1 & vy = m2.v;
227  return MotionPlain(typename MotionPlain::Vector3(-w[2]*vy,0,w[0]*vy),
228  MotionPlain::Vector3::Zero());
229  }
230 
231  template<typename MotionDerived, typename S1, int O1>
232  inline typename MotionDerived::MotionPlain
233  operator^(const MotionDense<MotionDerived> & m1,
234  const MotionPrismatic<S1,O1,2>& m2)
235  {
236  /* nu1^nu2 = ( v1^w2+w1^v2, w1^w2 )
237  * nu1^(v2,0) = ( w1^v2 , 0 )
238  * (x,y,z)^(0,0,v) = ( yv,-xv,0 )
239  * nu1^(0,vx) = ( vy1 vx,-vx1 vx, 0, 0, 0, 0 )
240  */
241  typedef typename MotionDerived::MotionPlain MotionPlain;
242  const typename MotionDerived::ConstAngularType & w = m1.angular();
243  const S1 & vz = m2.v;
244  return MotionPlain(typename Motion::Vector3(w[1]*vz,-w[0]*vz,0),
245  MotionPlain::Vector3::Zero());
246  }
247 
248  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
249  template<typename S1, int O1, typename S2, int O2>
250  inline Eigen::Matrix<S1,6,1,O1>
251  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismatic<S2,O2,0> &)
252  {
253  /* Y(:,0) = ( 1,0, 0, 0 , z , -y ) */
254  const S1
255  &m = Y.mass(),
256  &y = Y.lever()[1],
257  &z = Y.lever()[2];
258  Eigen::Matrix<S1,6,1,O1> res;
259  res << m, S1(0), S1(0), S1(0), m*z, -m*y;
260  return res;
261  }
262  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
263  template<typename S1, int O1, typename S2, int O2>
264  inline Eigen::Matrix<S1,6,1,O1>
265  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismatic<S2,O2,1> & )
266  {
267  /* Y(:,1) = ( 0,1, 0, -z , 0 , x) */
268  const S1
269  &m = Y.mass(),
270  &x = Y.lever()[0],
271  &z = Y.lever()[2];
272  Eigen::Matrix<S1,6,1,O1> res;
273  res << S1(0), m, S1(0), -m*z, S1(0), m*x;
274  return res;
275  }
276  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
277  template<typename S1, int O1, typename S2, int O2>
278  inline Eigen::Matrix<S1,6,1,O1>
279  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintPrismatic<S2,O2,2> & )
280  {
281  /* Y(:,2) = ( 0,0, 1, y , -x , 0) */
282  const S1
283  &m = Y.mass(),
284  &x = Y.lever()[0],
285  &y = Y.lever()[1];
286  Eigen::Matrix<S1,6,1,O1> res;
287  res << S1(0), S1(0), m, m*y, -m*x, S1(0);
288  return res;
289  }
290 
291  /* [ABA] operator* (Inertia Y,Constraint S) */
292  template<typename M6Like, typename S2, int O2, int axis>
293  inline const typename M6Like::ConstColXpr
294  operator*(const Eigen::MatrixBase<M6Like> & Y, const ConstraintPrismatic<S2,O2,axis> &)
295  {
296  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(M6Like,6,6);
297  return Y.derived().col(Inertia::LINEAR + axis);
298  }
299 
300  namespace internal
301  {
302  template<typename Scalar, int Options, int axis>
303  struct SE3GroupAction< ConstraintPrismatic<Scalar,Options,axis> >
304  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
305 
306  template<typename Scalar, int Options, int axis, typename MotionDerived>
307  struct MotionAlgebraAction< ConstraintPrismatic<Scalar,Options,axis>, MotionDerived >
308  { typedef Eigen::Matrix<Scalar,6,1,Options> ReturnType; };
309  }
310 
311  template<typename Scalar, int Options, int axis> struct JointPrismatic {};
312 
313 
314  template<typename Scalar, int Options>
315  struct JointPrismatic<Scalar,Options,0>
316  {
317  template<typename S1, typename S2, int O2>
318  static void cartesianTranslation(const S1 & shift, SE3Tpl<S2,O2> & m)
319  {
320  m.translation() << (S2)(shift), S2(0), S2(0);
321  }
322  };
323 
324  template<typename Scalar, int Options>
325  struct JointPrismatic<Scalar,Options,1>
326  {
327  template<typename S1, typename S2, int O2>
328  static void cartesianTranslation(const S1 & shift, SE3Tpl<S2,O2> & m)
329  {
330  m.translation() << S2(0), (S2)(shift), S2(0);
331  }
332  };
333 
334  template<typename Scalar, int Options>
335  struct JointPrismatic<Scalar,Options,2>
336  {
337  template<typename S1, typename S2, int O2>
338  static void cartesianTranslation(const S1 & shift, SE3Tpl<S2,O2> & m)
339  {
340  m.translation() << S2(0), S2(0), (S2)(shift);
341  }
342  };
343 
344  template<typename _Scalar, int _Options, int axis>
345  struct traits< JointPrismatic<_Scalar,_Options,axis> >
346  {
347  enum {
348  NQ = 1,
349  NV = 1
350  };
351  typedef _Scalar Scalar;
352  enum { Options = _Options };
358  typedef BiasZero Bias_t;
359  typedef Eigen::Matrix<Scalar,6,NV,Options> F_t;
360 
361  // [ABA]
362  typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
363  typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
364  typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
365 
366  typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
367  typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
368  };
369 
370  template<typename Scalar, int Options, int axis>
371  struct traits< JointDataPrismatic<Scalar,Options,axis> >
373 
374  template<typename Scalar, int Options, int axis>
375  struct traits< JointModelPrismatic<Scalar,Options,axis> >
377 
378  template<typename _Scalar, int _Options, int axis>
379  struct JointDataPrismatic : public JointDataBase< JointDataPrismatic<_Scalar,_Options,axis> >
380  {
382  SE3_JOINT_TYPEDEF_TEMPLATE;
383 
384  Constraint_t S;
385  Transformation_t M;
386  Motion_t v;
387  Bias_t c;
388 
389  F_t F;
390 
391  // [ABA] specific data
392  U_t U;
393  D_t Dinv;
394  UD_t UDinv;
395 
396  JointDataPrismatic() : M(1), U(), Dinv(), UDinv()
397  {}
398 
399  }; // struct JointDataPrismatic
400 
401  template<typename _Scalar, int _Options, int axis>
402  struct JointModelPrismatic : public JointModelBase< JointModelPrismatic<_Scalar,_Options,axis> >
403  {
405  SE3_JOINT_TYPEDEF_TEMPLATE;
406 
411 
412  JointDataDerived createData() const { return JointDataDerived(); }
413 
414  template<typename ConfigVector>
415  void calc(JointDataDerived & data,
416  const typename Eigen::MatrixBase<ConfigVector> & qs) const
417  {
418  EIGEN_STATIC_ASSERT_VECTOR_ONLY(ConfigVector);
419 
420  typedef typename ConfigVector::Scalar Scalar;
421  const Scalar & q = qs[idx_q()];
422  JointDerived::cartesianTranslation(q,data.M);
423  }
424 
425  template<typename ConfigVector, typename TangentVector>
426  void calc(JointDataDerived & data,
427  const typename Eigen::MatrixBase<ConfigVector> & qs,
428  const typename Eigen::MatrixBase<TangentVector> & vs) const
429  {
430  EIGEN_STATIC_ASSERT_VECTOR_ONLY(TangentVector);
431  calc(data,qs.derived());
432 
433  typedef typename TangentVector::Scalar S2;
434  const S2 & v = vs[idx_v()];
435  data.v.v = v;
436  }
437 
438  template<typename S2, int O2>
439  void calc_aba(JointDataDerived & data, Eigen::Matrix<S2,6,6,O2> & I, const bool update_I) const
440  {
441  data.U = I.col(Inertia::LINEAR + axis);
442  data.Dinv[0] = 1./I(Inertia::LINEAR + axis, Inertia::LINEAR + axis);
443  data.UDinv.noalias() = data.U * data.Dinv[0];
444 
445  if (update_I)
446  I -= data.UDinv * data.U.transpose();
447  }
448 
449  Scalar finiteDifferenceIncrement() const
450  {
451  using std::sqrt;
452  return sqrt(Eigen::NumTraits<Scalar>::epsilon());
453  }
454 
455  static std::string classname()
456  {
457  return std::string("JointModelP") + axisLabel<axis>();
458  }
459  std::string shortname() const { return classname(); }
460 
461  }; // struct JointModelPrismatic
462 
466 
470 
474 
475 } //namespace se3
476 
477 #endif // ifndef __se3_joint_prismatic_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...