pinocchio  UNKNOWN
All Classes Namespaces Functions Variables Typedefs Enumerations Modules Pages
joint-composite.hpp
1 //
2 // Copyright (c) 2016 CNRS
3 //
4 // This file is part of Pinocchio
5 // Pinocchio is free software: you can redistribute it
6 // and/or modify it under the terljMj of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // Pinocchio is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // Pinocchio If not, see
16 // <http://www.gnu.org/licenses/>.
17 
18 #ifndef __se3_joint_composite_hpp__
19 #define __se3_joint_composite_hpp__
20 
21 #include "pinocchio/multibody/joint/fwd.hpp"
22 #include "pinocchio/multibody/joint/joint-variant.hpp"
23 #include "pinocchio/multibody/joint/joint-basic-visitors.hpp"
24 #include "pinocchio/container/aligned-vector.hpp"
25 #include "pinocchio/spatial/act-on-set.hpp"
26 
27 
28 namespace se3
29 {
30 
31  struct JointComposite;
32 
33  template<>
34  struct traits<JointComposite>
35  {
36 
37  enum {
38  NQ = Eigen::Dynamic,
39  NV = Eigen::Dynamic
40  };
41 
42  typedef double Scalar;
45  typedef ConstraintXd Constraint_t;
46  typedef SE3 Transformation_t;
47  typedef Motion Motion_t;
48  typedef Motion Bias_t;
49 
50  typedef Eigen::Matrix<double,6,Eigen::Dynamic> F_t;
51  // [ABA]
52  typedef Eigen::Matrix<double,6,Eigen::Dynamic> U_t;
53  typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> D_t;
54  typedef Eigen::Matrix<double,6,Eigen::Dynamic> UD_t;
55 
56  typedef Eigen::Matrix<double,Eigen::Dynamic,1> ConfigVector_t;
57  typedef Eigen::Matrix<double,Eigen::Dynamic,1> TangentVector_t;
58  };
59 
60  template<> struct traits< JointDataComposite > { typedef JointComposite JointDerived; };
61  template<> struct traits< JointModelComposite > { typedef JointComposite JointDerived; };
62 
63  struct JointDataComposite : public JointDataBase< JointDataComposite >
64  {
65  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
66 
68  typedef JointComposite Joint;
70 // typedef boost::array<JointDataVariant,njoints> JointDataVector;
71 
72  typedef Base::Transformation_t Transformation_t;
73  typedef Base::Motion_t Motion_t;
74  typedef Base::Bias_t Bias_t;
75  typedef Base::Constraint_t Constraint_t;
76  typedef Base::U_t U_t;
77  typedef Base::D_t D_t;
78  typedef Base::UD_t UD_t;
79 
80  // JointDataComposite() {} // can become necessary if we want a vector of JointDataComposite ?
81 
82  JointDataComposite(const JointDataVector & joint_data, const int /*nq*/, const int nv)
83  : joints(joint_data), iMlast(joint_data.size()), pjMi(joint_data.size())
84  , S(nv)
85  , M(), v(), c()
86  , U(6,nv), Dinv(nv,nv), UDinv(6,nv)
87  {}
88 
90  JointDataVector joints;
91 
94 
97 
98  Constraint_t S;
99  Transformation_t M;
100  Motion_t v;
101  Bias_t c;
102 
103  // // [ABA] specific data
104  U_t U;
105  D_t Dinv;
106  UD_t UDinv;
107 
108  };
109 
110  struct JointModelComposite : public JointModelBase< JointModelComposite >
111  {
112  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
113 
114  enum
115  {
118  };
119 
120  typedef traits<JointComposite>::Scalar Scalar;
124 // typedef boost::array<JointModelVariant,njoints> JointModelVector;
125  typedef traits<JointComposite>::Transformation_t Transformation_t;
126  typedef traits<JointComposite>::Constraint_t Constraint_t;
127  typedef traits<JointComposite>::ConfigVector_t ConfigVector_t;
128  typedef traits<JointComposite>::TangentVector_t TangentVector_t;
129 
130  using Base::id;
131  using Base::idx_q;
132  using Base::idx_v;
133  using Base::setIndexes;
134  using Base::nq;
135  using Base::nv;
136 
139  : joints()
140  , jointPlacements()
141  , m_nq(0)
142  , m_nv(0)
143  {}
144 
151  template<typename JointModel>
152  JointModelComposite(const JointModelBase<JointModel> & jmodel, const SE3 & placement = SE3::Identity())
153  : joints(1,jmodel.derived())
154  , jointPlacements(1,placement)
155  , m_nq(jmodel.nq())
156  , m_nv(jmodel.nv())
157  , m_idx_q(1), m_nqs(1,jmodel.nq())
158  , m_idx_v(1), m_nvs(1,jmodel.nv())
159  {}
160 
167  : Base(other)
168  , joints(other.joints)
169  , jointPlacements(other.jointPlacements)
170  , m_nq(other.m_nq)
171  , m_nv(other.m_nv)
172  , m_idx_q(other.m_idx_q), m_nqs(other.m_nqs)
173  , m_idx_v(other.m_idx_v), m_nvs(other.m_nvs)
174  {}
175 
176 
183  template<typename JointModel>
184  void addJoint(const JointModelBase<JointModel> & jmodel, const SE3 & placement = SE3::Identity())
185  {
186  joints.push_back(jmodel.derived());
187  jointPlacements.push_back(placement);
188 
189  m_nq += jmodel.nq(); m_nv += jmodel.nv();
190 
191  updateJointIndexes();
192  }
193 
194  JointData createData() const
195  {
196  JointData::JointDataVector jdata(joints.size());
197  for (int i = 0; i < (int)joints.size(); ++i)
198  jdata[(size_t)i] = ::se3::createData(joints[(size_t)i]);
199  return JointDataDerived(jdata,nq(),nv());
200  }
201 
202  friend struct JointCompositeCalcZeroOrderStep;
203  void calc(JointData & data, const Eigen::VectorXd & qs) const;
204 
205  friend struct JointCompositeCalcFirstOrderStep;
206  void calc(JointData & data, const Eigen::VectorXd & qs, const Eigen::VectorXd & vs) const;
207 
208  void calc_aba(JointData & data, Inertia::Matrix6 & I, const bool update_I) const
209  {
210  data.U.noalias() = I * data.S;
211  Eigen::MatrixXd tmp (data.S.matrix().transpose() * data.U);
212  data.Dinv = tmp.inverse();
213  data.UDinv.noalias() = data.U * data.Dinv;
214 
215  if (update_I)
216  I -= data.UDinv * data.U.transpose();
217  }
218 
219  Scalar finiteDifferenceIncrement() const
220  {
221  using std::max;
222  Scalar eps = 0;
223  for(JointModelVector::const_iterator it = joints.begin();
224  it != joints.end(); ++it)
225  eps = max((Scalar)::se3::finiteDifferenceIncrement(*it),eps);
226 
227  return eps;
228  }
229 
230  int nv_impl() const { return m_nv; }
231  int nq_impl() const { return m_nq; }
232 
233 
237  void setIndexes_impl(JointIndex id, int q, int v)
238  {
239  Base::setIndexes_impl(id, q, v);
240  updateJointIndexes();
241  }
242 
243  static std::string classname() { return std::string("JointModelComposite"); }
244  std::string shortname() const { return classname(); }
245 
246  JointModelComposite & operator=(const JointModelComposite & other)
247  {
248  Base::operator=(other);
249  m_nq = other.m_nq;
250  m_nv = other.m_nv;
251  joints = other.joints;
252  jointPlacements = other.jointPlacements;
253 
254 
255  return *this;
256  }
257 
259  JointModelVector joints;
263  int m_nq,m_nv;
264 
265  template<typename D>
266  typename SizeDepType<NQ>::template SegmentReturn<D>::ConstType
267  jointConfigSelector(const Eigen::MatrixBase<D>& a) const { return a.segment(i_q,nq()); }
268  template<typename D>
269  typename SizeDepType<NQ>::template SegmentReturn<D>::Type
270  jointConfigSelector( Eigen::MatrixBase<D>& a) const { return a.segment(i_q,nq()); }
271 
272  template<typename D>
273  typename SizeDepType<NV>::template SegmentReturn<D>::ConstType
274  jointVelocitySelector(const Eigen::MatrixBase<D>& a) const { return a.segment(i_v,nv()); }
275  template<typename D>
276  typename SizeDepType<NV>::template SegmentReturn<D>::Type
277  jointVelocitySelector( Eigen::MatrixBase<D>& a) const { return a.segment(i_v,nv()); }
278 
279  template<typename D>
280  typename SizeDepType<NV>::template ColsReturn<D>::ConstType
281  jointCols(const Eigen::MatrixBase<D>& A) const { return A.middleCols(i_v,nv()); }
282  template<typename D>
283  typename SizeDepType<NV>::template ColsReturn<D>::Type
284  jointCols(Eigen::MatrixBase<D>& A) const { return A.middleCols(i_v,nv()); }
285 
286  template<typename D>
287  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::ConstType
288  jointConfigSelector_impl(const Eigen::MatrixBase<D>& a) const { return a.segment(i_q,nq()); }
289  template<typename D>
290  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::Type
291  jointConfigSelector_impl(Eigen::MatrixBase<D>& a) const { return a.segment(i_q,nq()); }
292  template<typename D>
293  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::ConstType
294  jointVelocitySelector_impl(const Eigen::MatrixBase<D>& a) const { return a.segment(i_v,nv()); }
295  template<typename D>
296  typename SizeDepType<Eigen::Dynamic>::template SegmentReturn<D>::Type
297  jointVelocitySelector_impl(Eigen::MatrixBase<D>& a) const { return a.segment(i_v,nv()); }
298 
299  template<typename D>
300  typename SizeDepType<Eigen::Dynamic>::template ColsReturn<D>::ConstType
301  jointCols_impl(const Eigen::MatrixBase<D>& A) const { return A.middleCols(i_v,nv()); }
302  template<typename D>
303  typename SizeDepType<Eigen::Dynamic>::template ColsReturn<D>::Type
304  jointCols_impl(Eigen::MatrixBase<D>& A) const { return A.middleCols(i_v,nv()); }
305 
306 
307 
308  protected:
309 
313  {
314  int idx_q = this->idx_q();
315  int idx_v = this->idx_v();
316 
317  m_idx_q.resize(joints.size());
318  m_idx_v.resize(joints.size());
319  m_nqs.resize(joints.size());
320  m_nvs.resize(joints.size());
321 
322  for(size_t i = 0; i < joints.size(); ++i)
323  {
324  JointModelVariant & joint = joints[i];
325 
326  m_idx_q[i] = idx_q; m_idx_v[i] = idx_v;
327  ::se3::setIndexes(joint,i,idx_q,idx_v);
328  m_nqs[i] = ::se3::nq(joint);
329  m_nvs[i] = ::se3::nv(joint);
330  idx_q += m_nqs[i]; idx_v += m_nvs[i];
331  }
332  }
333 
335 
337  std::vector<int> m_idx_q;
339  std::vector<int> m_nqs;
341  std::vector<int> m_idx_v;
343  std::vector<int> m_nvs;
344  };
345 
346 
347  inline std::ostream & operator << (std::ostream & os, const JointModelComposite & jmodel)
348  {
349  typedef JointModelComposite::JointModelVector JointModelVector;
350  os << "JointModelComposite containing following models:\n" ;
351  for (JointModelVector::const_iterator it = jmodel.joints.begin();
352  it != jmodel.joints.end(); ++it)
353  os << " " << shortname(*it) << std::endl;
354  return os;
355  }
356 
357 } // namespace se3
358 
359 /* --- Details -------------------------------------------------------------- */
360 /* --- Details -------------------------------------------------------------- */
361 /* --- Details -------------------------------------------------------------- */
362 #include "pinocchio/multibody/joint/joint-composite.hxx"
363 
364 #endif // ifndef __se3_joint_composite_hpp__
void addJoint(const JointModelBase< JointModel > &jmodel, const SE3 &placement=SE3::Identity())
Add a joint to the composition of joints.
JointDataVariant createData(const JointModelVariant &jmodel)
Visit a JointModelVariant through CreateData visitor to create a JointDataVariant.
container::aligned_vector< Transformation_t > iMlast
Transforms from previous joint to last joint.
JointModelComposite()
Empty contructor.
container::aligned_vector< SE3 > jointPlacements
Vector of joint placements. Those placements correspond to the origin of the joint relatively to thei...
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 updateJointIndexes()
Update the indexes of the joints contained in the composition according to the position of the joint ...
std::vector< int > m_nvs
Dimension of the segment in the tangent vector.
int nq(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointNqVisitor to get the dimension of the joint configuration spac...
std::vector< int > m_idx_q
Keep information of both the dimension and the position of the joints in the composition.
container::aligned_vector< Transformation_t > pjMi
Transforms from previous joint to joint i.
std::vector< int > m_idx_v
Index in the tangent vector.
std::vector< int > m_nqs
Dimension of the segment in the config vector.
JointModelComposite(const JointModelBase< JointModel > &jmodel, const SE3 &placement=SE3::Identity())
Default constructor with at least one joint.
std::string shortname(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointShortnameVisitor to get the shortname of the derived joint mod...
int nv(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointNvVisitor to get the dimension of the joint tangent space...
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...
JointModelVector joints
Vector of joints contained in the joint composite.
int idx_v(const JointModelVariant &jmodel)
Visit a JointModelVariant through JointIdxVVisitor to get the index in the full model tangent space c...
JointModelComposite(const JointModelComposite &other)
Copy constructor.
int m_nq
Dimensions of the config and tangent space of the composite joint.
JointDataVector joints
Vector of joints.
void setIndexes_impl(JointIndex id, int q, int v)
Update the indexes of subjoints in the stack.