joint-free-flyer.hpp 11.2 KB
Newer Older
1
//
2
// Copyright (c) 2015-2019 CNRS INRIA
3
// Copyright (c) 2015-2016 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4
5
//

6
7
#ifndef __pinocchio_joint_free_flyer_hpp__
#define __pinocchio_joint_free_flyer_hpp__
8

9
#include "pinocchio/macros.hpp"
10
#include "pinocchio/spatial/inertia.hpp"
11
#include "pinocchio/spatial/explog.hpp"
12
#include "pinocchio/multibody/joint/joint-base.hpp"
13
#include "pinocchio/multibody/constraint.hpp"
14
15
16
#include "pinocchio/math/fwd.hpp"
#include "pinocchio/math/quaternion.hpp"

17
namespace pinocchio
18
19
{

20
  template<typename Scalar, int Options> struct ConstraintIdentityTpl;
21

22
23
  template<typename _Scalar, int _Options>
  struct traits< ConstraintIdentityTpl<_Scalar,_Options> >
24
  {
25
26
27
    typedef _Scalar Scalar;
    enum { Options = _Options };
    typedef Eigen::Matrix<Scalar,6,6,Options> Matrix6;
28
29
30
31
    enum {
      LINEAR = 0,
      ANGULAR = 3
    };
32
    typedef MotionTpl<Scalar,Options> JointMotion;
33
34
    typedef Eigen::Matrix<Scalar,6,1,Options> JointForce;
    typedef Eigen::Matrix<Scalar,6,6,Options> DenseBase;
35
36
    typedef typename Matrix6::IdentityReturnType ConstMatrixReturnType;
    typedef typename Matrix6::IdentityReturnType MatrixReturnType;
37
  }; // traits ConstraintRevolute
38

39

40
  template<typename _Scalar, int _Options>
41
42
  struct ConstraintIdentityTpl
  : ConstraintBase< ConstraintIdentityTpl<_Scalar,_Options> >
43
  {
44
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
45
    PINOCCHIO_CONSTRAINT_TYPEDEF_TPL(ConstraintIdentityTpl)
46
    
47
    enum { NV = 6 };
48
    
49
50
51
52
53
54
55
    template<typename Vector6Like>
    JointMotion __mult__(const Eigen::MatrixBase<Vector6Like> & vj) const
    {
      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector6Like,6);
      return JointMotion(vj);
    }
    
56
    template<typename S1, int O1>
57
58
59
60
61
62
63
64
65
66
67
68
69
    typename SE3Tpl<S1,O1>::ActionMatrixType
    se3Action(const SE3Tpl<S1,O1> & m) const
    {
      return m.toActionMatrix();
      
    }
    
    template<typename S1, int O1>
    typename SE3Tpl<S1,O1>::ActionMatrixType
    se3ActionInverse(const SE3Tpl<S1,O1> & m) const
    {
      return m.toActionMatrixInverse();
    }
70
71
72
73
    
    int nv_impl() const { return NV; }
    
    struct TransposeConst
74
    {
75
76
77
78
      template<typename Derived>
      typename ForceDense<Derived>::ToVectorConstReturnType
      operator*(const ForceDense<Derived> & phi)
      {  return phi.toVector();  }
79
      
80
81
      /* [CRBA]  MatrixBase operator* (Constraint::Transpose S, ForceSet::Block) */
      template<typename MatrixDerived>
gabrielebndn's avatar
gabrielebndn committed
82
      typename PINOCCHIO_EIGEN_REF_CONST_TYPE(MatrixDerived)
83
84
85
86
87
88
89
      operator*(const Eigen::MatrixBase<MatrixDerived> & F)
      {
        return F.derived();
      }
    };
    
    TransposeConst transpose() const { return TransposeConst(); }
90
    MatrixReturnType matrix_impl() const { return DenseBase::Identity(); }
91
92
93
94
95
96
97
98
99
    
    template<typename MotionDerived>
    typename MotionDerived::ActionMatrixType
    motionAction(const MotionBase<MotionDerived> & v) const
    { return v.toActionMatrix(); }
    
  }; // struct ConstraintIdentityTpl
  
  template<typename Scalar, int Options, typename Vector6Like>
100
101
102
  MotionRef<const Vector6Like>
  operator*(const ConstraintIdentityTpl<Scalar,Options> &,
            const Eigen::MatrixBase<Vector6Like> & v)
103
104
  {
    EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector6Like,6);
105
//    typedef typename ConstraintIdentityTpl<Scalar,Options>::Motion Motion;
106
107
    typedef MotionRef<const Vector6Like> Motion;
    return Motion(v.derived());
108
  }
109

110
  /* [CRBA] ForceSet operator* (Inertia Y,Constraint S) */
111
112
113
  template<typename S1, int O1, typename S2, int O2>
  inline typename InertiaTpl<S1,O1>::Matrix6
  operator*(const InertiaTpl<S1,O1> & Y, const ConstraintIdentityTpl<S2,O2> &)
114
115
116
117
118
  {
    return Y.matrix();
  }
  
  /* [ABA] Y*S operator*/
119
  template<typename Matrix6Like, typename S2, int O2>
gabrielebndn's avatar
gabrielebndn committed
120
  inline typename PINOCCHIO_EIGEN_REF_CONST_TYPE(Matrix6Like)
121
  operator*(const Eigen::MatrixBase<Matrix6Like> & Y, const ConstraintIdentityTpl<S2,O2> &)
122
  {
123
    return Y.derived();
124
125
  }
  
126
127
128
129
130
131
132
  template<typename S1, int O1>
  struct SE3GroupAction< ConstraintIdentityTpl<S1,O1> >
  { typedef typename SE3Tpl<S1,O1>::ActionMatrixType ReturnType; };
  
  template<typename S1, int O1, typename MotionDerived>
  struct MotionAlgebraAction< ConstraintIdentityTpl<S1,O1>,MotionDerived >
  { typedef typename SE3Tpl<S1,O1>::ActionMatrixType ReturnType; };
133

134
  template<typename Scalar, int Options> struct JointFreeFlyerTpl;
135

136
137
  template<typename _Scalar, int _Options>
  struct traits< JointFreeFlyerTpl<_Scalar,_Options> >
138
  {
139
140
141
142
    enum {
      NQ = 7,
      NV = 6
    };
143
    typedef _Scalar Scalar;
144
145
146
    enum { Options = _Options };
    typedef JointDataFreeFlyerTpl<Scalar,Options> JointDataDerived;
    typedef JointModelFreeFlyerTpl<Scalar,Options> JointModelDerived;
147
    typedef ConstraintIdentityTpl<Scalar,Options> Constraint_t;
148
149
    typedef SE3Tpl<Scalar,Options> Transformation_t;
    typedef MotionTpl<Scalar,Options> Motion_t;
150
    typedef MotionZeroTpl<Scalar,Options> Bias_t;
Justin Carpentier's avatar
Justin Carpentier committed
151

152
    // [ABA]
153
154
155
    typedef Eigen::Matrix<Scalar,6,NV,Options> U_t;
    typedef Eigen::Matrix<Scalar,NV,NV,Options> D_t;
    typedef Eigen::Matrix<Scalar,6,NV,Options> UD_t;
156
    
Gabriele Buondonno's avatar
Gabriele Buondonno committed
157
    PINOCCHIO_JOINT_DATA_BASE_ACCESSOR_DEFAULT_RETURN_TYPE
158

159
160
    typedef Eigen::Matrix<Scalar,NQ,1,Options> ConfigVector_t;
    typedef Eigen::Matrix<Scalar,NV,1,Options> TangentVector_t;
161
  };
162
163
164
165
166
167
168
169
  
  template<typename Scalar, int Options>
  struct traits< JointDataFreeFlyerTpl<Scalar,Options> >
  { typedef JointFreeFlyerTpl<Scalar,Options> JointDerived; };
  
  template<typename Scalar, int Options>
  struct traits< JointModelFreeFlyerTpl<Scalar,Options> >
  { typedef JointFreeFlyerTpl<Scalar,Options> JointDerived; };
170

171
172
  template<typename _Scalar, int _Options>
  struct JointDataFreeFlyerTpl : public JointDataBase< JointDataFreeFlyerTpl<_Scalar,_Options> >
173
  {
174
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
175
    typedef JointFreeFlyerTpl<_Scalar,_Options> JointDerived;
176
    PINOCCHIO_JOINT_DATA_TYPEDEF_TEMPLATE(JointDerived);
Gabriele Buondonno's avatar
Gabriele Buondonno committed
177
    PINOCCHIO_JOINT_DATA_BASE_DEFAULT_ACCESSOR
178
179
180
181
182
183
    
    Constraint_t S;
    Transformation_t M;
    Motion_t v;
    Bias_t c;

184
185
186
187
188
    // [ABA] specific data
    U_t U;
    D_t Dinv;
    UD_t UDinv;
    
189
    JointDataFreeFlyerTpl() : M(1), U(), Dinv(), UDinv(UD_t::Identity()) {}
190

191
192
193
    static std::string classname() { return std::string("JointDataFreeFlyer"); }
    std::string shortname() const { return classname(); }
    
194
  }; // struct JointDataFreeFlyerTpl
195

Gabriele Buondonno's avatar
Gabriele Buondonno committed
196
  PINOCCHIO_JOINT_CAST_TYPE_SPECIALIZATION(JointModelFreeFlyerTpl);
197
  template<typename _Scalar, int _Options>
jcarpent's avatar
jcarpent committed
198
199
  struct JointModelFreeFlyerTpl
  : public JointModelBase< JointModelFreeFlyerTpl<_Scalar,_Options> >
200
  {
201
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
202
    typedef JointFreeFlyerTpl<_Scalar,_Options> JointDerived;
203
    PINOCCHIO_JOINT_TYPEDEF_TEMPLATE(JointDerived);
jcarpent's avatar
jcarpent committed
204
205
206
207
208
209
    
    typedef JointModelBase<JointModelFreeFlyerTpl> Base;
    using Base::id;
    using Base::idx_q;
    using Base::idx_v;
    using Base::setIndexes;
Valenza Florian's avatar
Valenza Florian committed
210

211
    JointDataDerived createData() const { return JointDataDerived(); }
jcarpent's avatar
jcarpent committed
212
    
213
214
    template<typename ConfigVectorLike>
    inline void forwardKinematics(Transformation_t & M, const Eigen::MatrixBase<ConfigVectorLike> & q_joint) const
jcarpent's avatar
jcarpent committed
215
    {
216
      EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(ConfigVector_t,ConfigVectorLike);
gabrielebndn's avatar
gabrielebndn committed
217
      typedef typename Eigen::Quaternion<typename ConfigVectorLike::Scalar,PINOCCHIO_EIGEN_PLAIN_TYPE(ConfigVectorLike)::Options> Quaternion;
jcarpent's avatar
jcarpent committed
218
      typedef Eigen::Map<const Quaternion> ConstQuaternionMap;
219

jcarpent's avatar
jcarpent committed
220
      ConstQuaternionMap quat(q_joint.template tail<4>().data());
221
222
      //assert(math::fabs(quat.coeffs().squaredNorm()-1.) <= sqrt(Eigen::NumTraits<typename V::Scalar>::epsilon())); TODO: check validity of the rhs precision
      assert(math::fabs(quat.coeffs().squaredNorm()-1.) <= 1e-4);
jcarpent's avatar
jcarpent committed
223
224
225
226
227
      
      M.rotation(quat.matrix());
      M.translation(q_joint.template head<3>());
    }
    
228
229
230
231
232
233
234
235
236
237
    template<typename Vector3Derived, typename QuaternionDerived>
    EIGEN_DONT_INLINE
    void calc(JointDataDerived & data,
              const typename Eigen::MatrixBase<Vector3Derived> & trans,
              const typename Eigen::QuaternionBase<QuaternionDerived> & quat) const
    {
      data.M.translation(trans);
      data.M.rotation(quat.matrix());
    }
    
238
    template<typename ConfigVector>
239
    EIGEN_DONT_INLINE
240
    void calc(JointDataDerived & data,
241
              const typename Eigen::PlainObjectBase<ConfigVector> & qs) const
242
    {
243
244
      typedef typename Eigen::Quaternion<typename ConfigVector::Scalar,ConfigVector::Options> Quaternion;
      typedef Eigen::Map<const Quaternion> ConstQuaternionMap;
245
      
246
      typename ConfigVector::template ConstFixedSegmentReturnType<NQ>::Type q = qs.template segment<NQ>(idx_q());
247
      ConstQuaternionMap quat(q.template tail<4>().data());
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
      
      calc(data,q.template head<3>(),quat);
    }
    
    template<typename ConfigVector>
    EIGEN_DONT_INLINE
    void calc(JointDataDerived & data,
              const typename Eigen::MatrixBase<ConfigVector> & qs) const
    {
      typedef typename Eigen::Quaternion<Scalar,Options> Quaternion;
      
      typename ConfigVector::template ConstFixedSegmentReturnType<NQ>::Type q = qs.template segment<NQ>(idx_q());
      const Quaternion quat(q.template tail<4>());
      
      calc(data,q.template head<3>(),quat);
263
    }
264
    
265
    template<typename ConfigVector, typename TangentVector>
266
    EIGEN_DONT_INLINE
267
    void calc(JointDataDerived & data,
268
269
              const typename Eigen::MatrixBase<ConfigVector> & qs,
              const typename Eigen::MatrixBase<TangentVector> & vs) const
270
    {
271
      calc(data,qs.derived());
272
      
273
      data.v = vs.template segment<NV>(idx_v());
274
    }
275
    
276
    template<typename Matrix6Like>
277
278
279
    void calc_aba(JointDataDerived & data,
                  const Eigen::MatrixBase<Matrix6Like> & I,
                  const bool update_I) const
280
281
    {
      data.U = I;
282
283
284
285
286
      
      // compute inverse
//      data.Dinv.setIdentity();
//      I.llt().solveInPlace(data.Dinv);
      internal::PerformStYSInversion<Scalar>::run(I,data.Dinv);
287
288
      
      if (update_I)
gabrielebndn's avatar
gabrielebndn committed
289
        PINOCCHIO_EIGEN_CONST_CAST(Matrix6Like,I).setZero();
290
    }
291

292
293
    static std::string classname() { return std::string("JointModelFreeFlyer"); }
    std::string shortname() const { return classname(); }
294
295
296
297
298
299
300
301
302
303
    
    /// \returns An expression of *this with the Scalar type casted to NewScalar.
    template<typename NewScalar>
    JointModelFreeFlyerTpl<NewScalar,Options> cast() const
    {
      typedef JointModelFreeFlyerTpl<NewScalar,Options> ReturnType;
      ReturnType res;
      res.setIndexes(id(),idx_q(),idx_v());
      return res;
    }
304

305
  }; // struct JointModelFreeFlyerTpl
306

307
} // namespace pinocchio
308

309
310
311
312
313
#include <boost/type_traits.hpp>

namespace boost
{
  template<typename Scalar, int Options>
314
  struct has_nothrow_constructor< ::pinocchio::JointModelFreeFlyerTpl<Scalar,Options> >
315
316
317
  : public integral_constant<bool,true> {};
  
  template<typename Scalar, int Options>
318
  struct has_nothrow_copy< ::pinocchio::JointModelFreeFlyerTpl<Scalar,Options> >
319
320
321
  : public integral_constant<bool,true> {};
  
  template<typename Scalar, int Options>
322
  struct has_nothrow_constructor< ::pinocchio::JointDataFreeFlyerTpl<Scalar,Options> >
323
324
325
  : public integral_constant<bool,true> {};
  
  template<typename Scalar, int Options>
326
  struct has_nothrow_copy< ::pinocchio::JointDataFreeFlyerTpl<Scalar,Options> >
327
328
329
  : public integral_constant<bool,true> {};
}

330
#endif // ifndef __pinocchio_joint_free_flyer_hpp__