pinocchio  2.1.3
multibody/model.hpp
1 //
2 // Copyright (c) 2015-2019 CNRS INRIA
3 // Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_model_hpp__
7 #define __pinocchio_model_hpp__
8 
9 #include "pinocchio/spatial/fwd.hpp"
10 #include "pinocchio/spatial/se3.hpp"
11 #include "pinocchio/spatial/force.hpp"
12 #include "pinocchio/spatial/motion.hpp"
13 #include "pinocchio/spatial/inertia.hpp"
14 
15 #include "pinocchio/multibody/fwd.hpp"
16 #include "pinocchio/multibody/frame.hpp"
17 #include "pinocchio/multibody/joint/joint-generic.hpp"
18 
19 #include "pinocchio/container/aligned-vector.hpp"
20 
21 #include "pinocchio/serialization/serializable.hpp"
22 
23 #include <iostream>
24 #include <map>
25 
26 namespace pinocchio
27 {
28 
29  template<typename _Scalar, int _Options, template<typename,int> class JointCollectionTpl>
30  struct ModelTpl
31  : serialization::Serializable< ModelTpl<_Scalar,_Options,JointCollectionTpl> >
32  {
33  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 
35  typedef _Scalar Scalar;
36  enum { Options = _Options };
37 
38  typedef JointCollectionTpl<Scalar,Options> JointCollection;
39  typedef DataTpl<Scalar,Options,JointCollectionTpl> Data;
40 
41  typedef SE3Tpl<Scalar,Options> SE3;
42  typedef MotionTpl<Scalar,Options> Motion;
43  typedef ForceTpl<Scalar,Options> Force;
44  typedef InertiaTpl<Scalar,Options> Inertia;
45  typedef FrameTpl<Scalar,Options> Frame;
46 
47  typedef pinocchio::Index Index;
48  typedef pinocchio::JointIndex JointIndex;
49  typedef pinocchio::GeomIndex GeomIndex;
50  typedef pinocchio::FrameIndex FrameIndex;
51  typedef std::vector<Index> IndexVector;
52 
53  typedef JointModelTpl<Scalar,Options,JointCollectionTpl> JointModel;
54  typedef JointDataTpl<Scalar,Options,JointCollectionTpl> JointData;
55 
56  typedef container::aligned_vector<JointModel> JointModelVector;
57  typedef container::aligned_vector<JointData> JointDataVector;
58 
59  typedef container::aligned_vector<Frame> FrameVector;
60 
61  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1,Options> VectorXs;
62  typedef Eigen::Matrix<Scalar,3,1,Options> Vector3;
63 
65  typedef VectorXs ConfigVectorType;
66 
67  typedef std::map<std::string, ConfigVectorType> ConfigVectorMap;
68 
71  typedef VectorXs TangentVectorType;
72 
74  int nq;
75 
77  int nv;
78 
80  int njoints;
81 
83  int nbodies;
84 
86  int nframes;
87 
90 
93 
95  JointModelVector joints;
96 
98  std::vector<JointIndex> parents;
99 
101  std::vector<std::string> names;
102 
104  PINOCCHIO_DEPRECATED
105  ConfigVectorType neutralConfiguration;
106 
108  ConfigVectorMap referenceConfigurations;
109 
111  VectorXs rotorInertia;
112 
114  VectorXs rotorGearRatio;
115 
117  TangentVectorType effortLimit;
118 
120  TangentVectorType velocityLimit;
121 
124 
127 
129  FrameVector frames;
130 
134  std::vector<IndexVector> subtrees;
135 
137  Motion gravity;
138 
140  static const Vector3 gravity981;
141 
143  std::string name;
144 
147  : nq(0)
148  , nv(0)
149  , njoints(1)
150  , nbodies(1)
151  , nframes(0)
152  , inertias(1, Inertia::Zero())
153  , jointPlacements(1, SE3::Identity())
154  , joints(1)
155  , parents(1, 0)
156  , names(1)
157  , subtrees(1)
158  , gravity(gravity981,Vector3::Zero())
159  {
160  names[0] = "universe"; // Should be "universe joint (trivial)"
161  // FIXME Should the universe joint be a FIXED_JOINT even if it is
162  // in the list of joints ? See comment in definition of
163  // Model::addJointFrame and Model::addBodyFrame
164  addFrame(Frame("universe", 0, 0, SE3::Identity(), FIXED_JOINT));
165  }
166  ~ModelTpl() {} // std::cout << "Destroy model" << std::endl; }
167 
169  template<typename NewScalar>
171  {
173  ReturnType res;
174  res.nq = nq; res.nv = nv;
175  res.njoints = njoints;
176  res.nbodies = nbodies;
177  res.nframes = nframes;
178  res.parents = parents;
179  res.names = names;
180  res.subtrees = subtrees;
181  res.gravity = gravity.template cast<NewScalar>();
182  res.name = name;
183 
186 #pragma GCC diagnostic push
187 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
188  res.neutralConfiguration = neutralConfiguration.template cast<NewScalar>();
189 #pragma GCC diagnostic pop
190  res.rotorInertia = rotorInertia.template cast<NewScalar>();
191  res.rotorGearRatio = rotorGearRatio.template cast<NewScalar>();
192  res.effortLimit = effortLimit.template cast<NewScalar>();
193  res.velocityLimit = velocityLimit.template cast<NewScalar>();
194  res.lowerPositionLimit = lowerPositionLimit.template cast<NewScalar>();
195  res.upperPositionLimit = upperPositionLimit.template cast<NewScalar>();
196 
197  typename ReturnType::ConfigVectorMap::iterator rit = res.referenceConfigurations.begin();
198  typename ConfigVectorMap::const_iterator it;
199  for (it = referenceConfigurations.begin();
200  it != referenceConfigurations.end(); it++ )
201  {
202  rit->second = it->second.template cast<NewScalar>();
203  rit++;
204  }
205 
206 
208  res.inertias.resize(inertias.size());
209  res.jointPlacements.resize(jointPlacements.size());
210  res.joints.resize(joints.size());
211  res.frames.resize(frames.size());
212 
214  for(size_t k = 0; k < joints.size(); ++k)
215  {
216  res.inertias[k] = inertias[k].template cast<NewScalar>();
217  res.jointPlacements[k] = jointPlacements[k].template cast<NewScalar>();
218  res.joints[k] = joints[k].template cast<NewScalar>();
219  }
220 
221  for(size_t k = 0; k < frames.size(); ++k)
222  {
223  res.frames[k] = frames[k].template cast<NewScalar>();
224  }
225 
226  return res;
227  }
228 
234  bool operator==(const ModelTpl & other) const
235  {
236  bool res =
237  other.nq == nq
238  && other.nv == nv
239  && other.njoints == njoints
240  && other.nbodies == nbodies
241  && other.nframes == nframes
242  && other.parents == parents
243  && other.names == names
244  && other.subtrees == subtrees
245  && other.gravity == gravity
246  && other.name == name;
247 
249 #pragma GCC diagnostic push
250 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
251  if(other.neutralConfiguration.size() != neutralConfiguration.size())
252  return false;
253  res &= other.neutralConfiguration == neutralConfiguration;
254  if(!res) return res;
255 #pragma GCC diagnostic pop
256 
257  if(other.referenceConfigurations.size() != referenceConfigurations.size())
258  return false;
259  res &= other.referenceConfigurations == referenceConfigurations;
260  if(!res) return res;
261 
262  if(other.rotorInertia.size() != rotorInertia.size())
263  return false;
264  res &= other.rotorInertia == rotorInertia;
265  if(!res) return res;
266 
267  if(other.rotorGearRatio.size() != rotorGearRatio.size())
268  return false;
269  res &= other.rotorGearRatio == rotorGearRatio;
270  if(!res) return res;
271 
272  if(other.effortLimit.size() != effortLimit.size())
273  return false;
274  res &= other.effortLimit == effortLimit;
275  if(!res) return res;
276 
277  if(other.velocityLimit.size() != velocityLimit.size())
278  return false;
279  res &= other.velocityLimit == velocityLimit;
280  if(!res) return res;
281 
282  if(other.lowerPositionLimit.size() != lowerPositionLimit.size())
283  return false;
284  res &= other.lowerPositionLimit == lowerPositionLimit;
285  if(!res) return res;
286 
287  if(other.upperPositionLimit.size() != upperPositionLimit.size())
288  return false;
289  res &= other.upperPositionLimit == upperPositionLimit;
290  if(!res) return res;
291 
292  for(size_t k = 1; k < inertias.size(); ++k)
293  {
294  res &= other.inertias[k] == inertias[k];
295  if(!res) return res;
296  }
297 
298  for(size_t k = 1; k < other.jointPlacements.size(); ++k)
299  {
300  res &= other.jointPlacements[k] == jointPlacements[k];
301  if(!res) return res;
302  }
303 
304  res &=
305  other.joints == joints
306  && other.frames == frames;
307 
308  return res;
309  }
310 
311  //
314  bool operator!=(const ModelTpl & other) const
315  { return !(*this == other); }
316 
339  template<typename JointModelDerived>
340  JointIndex addJoint(const JointIndex parent,
341  const JointModelBase<JointModelDerived> & joint_model,
342  const SE3 & joint_placement,
343  const std::string & joint_name,
344  const VectorXs & max_effort,
345  const VectorXs & max_velocity,
346  const VectorXs & min_config,
347  const VectorXs & max_config
348  );
349 
367  template<typename JointModelDerived>
368  JointIndex addJoint(const JointIndex parent,
369  const JointModelBase<JointModelDerived> & joint_model,
370  const SE3 & joint_placement,
371  const std::string & joint_name
372  );
373 
383  int addJointFrame(const JointIndex& jointIndex,
384  int frameIndex = -1);
385 
395  void appendBodyToJoint(const JointIndex joint_index, const Inertia & Y,
396  const SE3 & body_placement = SE3::Identity());
397 
409  int addBodyFrame (const std::string & body_name,
410  const JointIndex & parentJoint,
411  const SE3 & body_placement = SE3::Identity(),
412  int previousFrame = -1);
413 
425  JointIndex getBodyId(const std::string & name) const;
426 
434  bool existBodyName(const std::string & name) const;
435 
436 
447  JointIndex getJointId(const std::string & name) const;
448 
456  bool existJointName(const std::string & name) const;
457 
465  PINOCCHIO_DEPRECATED const std::string & getJointName(const JointIndex index) const;
466 
480  FrameIndex getFrameId(const std::string & name,
481  const FrameType & type = (FrameType) (JOINT | FIXED_JOINT | BODY | OP_FRAME | SENSOR )) const;
482 
491  bool existFrame(const std::string & name,
492  const FrameType& type = (FrameType) (JOINT | FIXED_JOINT | BODY | OP_FRAME | SENSOR )) const;
493 
494 
503  int addFrame(const Frame & frame);
504 
512  template<typename D>
513  inline bool check(const AlgorithmCheckerBase<D> & checker = AlgorithmCheckerBase<D>()) const
514  { return checker.checkModel(*this); }
515 
517  inline bool check() const;
518 
524  inline bool check(const Data & data) const;
525 
526  protected:
527 
532  void addJointIndexToParentSubtrees(const JointIndex joint_id);
533  };
534 
535 } // namespace pinocchio
536 
537 /* --- Details -------------------------------------------------------------- */
538 /* --- Details -------------------------------------------------------------- */
539 /* --- Details -------------------------------------------------------------- */
540 #include "pinocchio/multibody/model.hxx"
541 
542 #endif // ifndef __pinocchio_model_hpp__
JointModelVector joints
Model of joint i, encapsulated in a JointModelAccessor.
PINOCCHIO_DEPRECATED ConfigVectorType neutralConfiguration
Vector of joint&#39;s neutral configurations.
bool check() const
Run check(fusion::list) with DEFAULT_CHECKERS as argument.
void appendBodyToJoint(const JointIndex joint_index, const Inertia &Y, const SE3 &body_placement=SE3::Identity())
Append a body to a given joint of the kinematic tree.
ModelTpl()
Default constructor. Builds an empty model with no joints.
PINOCCHIO_DEPRECATED const std::string & getJointName(const JointIndex index) const
Get the name of a joint given by its index.
VectorXs ConfigVectorType
Dense vectorized version of a joint configuration vector.
JointIndex getBodyId(const std::string &name) const
Return the index of a body given by its name.
int njoints
Number of joints.
container::aligned_vector< SE3 > jointPlacements
Placement (SE3) of the input of joint i regarding to the parent joint output li.
ConfigVectorMap referenceConfigurations
Map of reference configurations, indexed by user given names.
int nv
Dimension of the velocity vector space.
ModelTpl< NewScalar, Options, JointCollectionTpl > cast() const
int addBodyFrame(const std::string &body_name, const JointIndex &parentJoint, const SE3 &body_placement=SE3::Identity(), int previousFrame=-1)
Add a body to the frame tree.
bool existBodyName(const std::string &name) const
Check if a body given by its name exists.
Motion gravity
Spatial gravity of the model.
std::vector< std::string > names
Name of joint i
void addJointIndexToParentSubtrees(const JointIndex joint_id)
Add the joint_id to its parent subtrees.
JointIndex getJointId(const std::string &name) const
Return the index of a joint given by its name.
VectorXs upperPositionLimit
Upper joint configuration limit.
FrameVector frames
Vector of operational frames registered on the model.
VectorXs rotorInertia
Vector of rotor inertia parameters.
std::vector< IndexVector > subtrees
Vector of subtrees. subtree[j] corresponds to the subtree supported by the joint j. The first element of subtree[j] is the index of the joint j itself.
TangentVectorType effortLimit
Vector of maximal joint torques.
Specialization of an std::vector with an aligned allocator. This specialization might be used when th...
VectorXs lowerPositionLimit
Lower joint configuration limit.
int nframes
Number of operational frames.
bool existJointName(const std::string &name) const
Check if a joint given by its name exists.
bool check(const AlgorithmCheckerBase< D > &checker=AlgorithmCheckerBase< D >()) const
int addJointFrame(const JointIndex &jointIndex, int frameIndex=-1)
Add a joint to the frame tree.
VectorXs rotorGearRatio
Vector of rotor gear ratio parameters.
bool existFrame(const std::string &name, const FrameType &type=(FrameType)(JOINT|FIXED_JOINT|BODY|OP_FRAME|SENSOR)) const
Checks if a frame given by its name exists.
FrameType
Enum on the possible types of frame.
FrameIndex getFrameId(const std::string &name, const FrameType &type=(FrameType)(JOINT|FIXED_JOINT|BODY|OP_FRAME|SENSOR)) const
Returns the index of a frame given by its name.
CRTP class describing the API of the checkers.
Definition: check.hpp:22
Main pinocchio namespace.
Definition: treeview.dox:24
std::string name
Model name;.
int addFrame(const Frame &frame)
Adds a frame to the kinematic tree.
std::vector< JointIndex > parents
Joint parent of joint i, denoted li (li==parents[i]).
container::aligned_vector< Inertia > inertias
Spatial inertias of the body i expressed in the supporting joint frame i.
static const Vector3 gravity981
Default 3D gravity vector (=(0,0,-9.81)).
VectorXs TangentVectorType
Dense vectorized version of a joint tangent vector (e.g. velocity, acceleration, etc). It also handles the notion of co-tangent vector (e.g. torque, etc).
JointIndex addJoint(const JointIndex parent, const JointModelBase< JointModelDerived > &joint_model, const SE3 &joint_placement, const std::string &joint_name, const VectorXs &max_effort, const VectorXs &max_velocity, const VectorXs &min_config, const VectorXs &max_config)
Add a joint to the kinematic tree with given bounds.
TangentVectorType velocityLimit
Vector of maximal joint velocities.
int nbodies
Number of bodies.
bool operator!=(const ModelTpl &other) const
int nq
Dimension of the configuration vector representation.
bool operator==(const ModelTpl &other) const
Equality comparison operator.