pinocchio  3.3.1
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
model.hpp
1 //
2 // Copyright (c) 2015-2020 CNRS INRIA
3 // Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_multibody_model_hpp__
7 #define __pinocchio_multibody_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 <map>
24 #include <iterator>
25 
26 namespace pinocchio
27 {
28  template<
29  typename NewScalar,
30  typename Scalar,
31  int Options,
32  template<typename, int> class JointCollectionTpl>
33  struct CastType<NewScalar, ModelTpl<Scalar, Options, JointCollectionTpl>>
34  {
36  };
37 
38  template<typename _Scalar, int _Options, template<typename, int> class JointCollectionTpl>
39  struct traits<ModelTpl<_Scalar, _Options, JointCollectionTpl>>
40  {
41  typedef _Scalar Scalar;
42  };
43 
44  template<typename _Scalar, int _Options, template<typename, int> class JointCollectionTpl>
45  struct ModelTpl
46  : serialization::Serializable<ModelTpl<_Scalar, _Options, JointCollectionTpl>>
47  , NumericalBase<ModelTpl<_Scalar, _Options, JointCollectionTpl>>
48  {
49  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 
51  typedef _Scalar Scalar;
52  enum
53  {
54  Options = _Options
55  };
56 
57  typedef JointCollectionTpl<Scalar, Options> JointCollection;
59 
65 
66  typedef pinocchio::Index Index;
67  typedef pinocchio::JointIndex JointIndex;
68  typedef pinocchio::GeomIndex GeomIndex;
69  typedef pinocchio::FrameIndex FrameIndex;
70  typedef std::vector<Index> IndexVector;
71 
74 
75  typedef PINOCCHIO_ALIGNED_STD_VECTOR(JointModel) JointModelVector;
76  typedef PINOCCHIO_ALIGNED_STD_VECTOR(JointData) JointDataVector;
77 
78  typedef PINOCCHIO_ALIGNED_STD_VECTOR(Frame) FrameVector;
79 
80  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
81  typedef Eigen::Matrix<Scalar, 3, 1, Options> Vector3;
82 
83  typedef PINOCCHIO_ALIGNED_STD_VECTOR(Inertia) InertiaVector;
84  typedef PINOCCHIO_ALIGNED_STD_VECTOR(SE3) SE3Vector;
85 
87  typedef VectorXs ConfigVectorType;
88 
90  typedef std::map<std::string, ConfigVectorType> ConfigVectorMap;
91 
95  typedef VectorXs TangentVectorType;
96 
98  int nq;
99 
101  int nv;
102 
104  int njoints;
105 
107  int nbodies;
108 
110  int nframes;
111 
113  InertiaVector inertias;
114 
116  SE3Vector jointPlacements;
117 
119  JointModelVector joints;
120 
122  std::vector<int> idx_qs;
123 
125  std::vector<int> nqs;
126 
128  std::vector<int> idx_vs;
129 
131  std::vector<int> nvs;
132 
135  std::vector<JointIndex> parents;
136 
139  std::vector<IndexVector> children;
140 
142  std::vector<std::string> names;
143 
146 
149  VectorXs armature;
150 
153 
156 
159 
162 
165 
168 
171 
174 
176  FrameVector frames;
177 
183  std::vector<IndexVector> supports;
184 
188  std::vector<IndexVector> subtrees;
189 
192 
194  static const Vector3 gravity981;
195 
197  std::string name;
198 
201  : nq(0)
202  , nv(0)
203  , njoints(1)
204  , nbodies(1)
205  , nframes(0)
206  , inertias(1, Inertia::Zero())
207  , jointPlacements(1, SE3::Identity())
208  , joints(1)
209  , idx_qs(1, 0)
210  , nqs(1, 0)
211  , idx_vs(1, 0)
212  , nvs(1, 0)
213  , parents(1, 0)
214  , children(1)
215  , names(1)
216  , supports(1, IndexVector(1, 0))
217  , subtrees(1)
218  , gravity(gravity981, Vector3::Zero())
219  {
220  names[0] = "universe"; // Should be "universe joint (trivial)"
221  // FIXME Should the universe joint be a FIXED_JOINT even if it is
222  // in the list of joints ? See comment in definition of
223  // Model::addJointFrame and Model::addBodyFrame
224  addFrame(Frame("universe", 0, 0, SE3::Identity(), FIXED_JOINT));
225  }
226 
232  template<typename S2, int O2>
233  explicit ModelTpl(const ModelTpl<S2, O2> & other)
234  {
235  *this = other.template cast<Scalar>();
236  }
237 
239  template<typename NewScalar>
241 
247  bool operator==(const ModelTpl & other) const;
248 
252  bool operator!=(const ModelTpl & other) const
253  {
254  return !(*this == other);
255  }
256 
276  JointIndex addJoint(
277  const JointIndex parent,
278  const JointModel & joint_model,
279  const SE3 & joint_placement,
280  const std::string & joint_name);
281 
291  JointIndex addJoint(
292  const JointIndex parent,
293  const JointModel & joint_model,
294  const SE3 & joint_placement,
295  const std::string & joint_name,
296  const VectorXs & max_effort,
297  const VectorXs & max_velocity,
298  const VectorXs & min_config,
299  const VectorXs & max_config);
300 
308  JointIndex addJoint(
309  const JointIndex parent,
310  const JointModel & joint_model,
311  const SE3 & joint_placement,
312  const std::string & joint_name,
313  const VectorXs & max_effort,
314  const VectorXs & max_velocity,
315  const VectorXs & min_config,
316  const VectorXs & max_config,
317  const VectorXs & friction,
318  const VectorXs & damping);
319 
329  FrameIndex addJointFrame(const JointIndex & joint_index, int previous_frame_index = -1);
330 
342  const JointIndex joint_index,
343  const Inertia & Y,
344  const SE3 & body_placement = SE3::Identity());
345 
358  FrameIndex addBodyFrame(
359  const std::string & body_name,
360  const JointIndex & parentJoint,
361  const SE3 & body_placement = SE3::Identity(),
362  int parentFrame = -1);
363 
375  FrameIndex getBodyId(const std::string & name) const;
376 
384  bool existBodyName(const std::string & name) const;
385 
396  JointIndex getJointId(const std::string & name) const;
397 
405  bool existJointName(const std::string & name) const;
406 
420  FrameIndex getFrameId(
421  const std::string & name,
422  const FrameType & type = (FrameType)(JOINT | FIXED_JOINT | BODY | OP_FRAME | SENSOR)) const;
423 
433  const std::string & name,
434  const FrameType & type = (FrameType)(JOINT | FIXED_JOINT | BODY | OP_FRAME | SENSOR)) const;
435 
448  FrameIndex addFrame(const Frame & frame, const bool append_inertia = true);
449 
460  template<typename D>
461  bool check(const AlgorithmCheckerBase<D> & checker = AlgorithmCheckerBase<D>()) const
462  {
463  return checker.checkModel(*this);
464  }
465 
471  std::vector<bool> hasConfigurationLimit();
472 
478  std::vector<bool> hasConfigurationLimitInTangent();
479 
481  bool check() const;
482 
490  bool check(const Data & data) const;
491 
492  protected:
498  void addJointIndexToParentSubtrees(const JointIndex joint_id);
499  };
500 
501 } // namespace pinocchio
502 
503 /* --- Details -------------------------------------------------------------- */
504 /* --- Details -------------------------------------------------------------- */
505 /* --- Details -------------------------------------------------------------- */
506 #include "pinocchio/multibody/model.hxx"
507 
508 #if PINOCCHIO_ENABLE_TEMPLATE_INSTANTIATION
509  #include "pinocchio/multibody/model.txx"
510 #endif // PINOCCHIO_ENABLE_TEMPLATE_INSTANTIATION
511 
512 #endif // ifndef __pinocchio_multibody_model_hpp__
Main pinocchio namespace.
Definition: treeview.dox:11
FrameType
Enum on the possible types of frames.
Definition: frame.hpp:32
@ BODY
body frame: attached to the collision, inertial or visual properties of a link
Definition: frame.hpp:36
@ SENSOR
sensor frame: defined in a sensor element
Definition: frame.hpp:38
@ OP_FRAME
operational frame: user-defined frames that are defined at runtime
Definition: frame.hpp:33
@ JOINT
joint frame: attached to the child body of a joint (a.k.a. child frame)
Definition: frame.hpp:34
@ FIXED_JOINT
fixed joint frame: joint frame but for a fixed joint
Definition: frame.hpp:35
CRTP class describing the API of the checkers.
Definition: check.hpp:23
Type of the cast of a class C templated by Scalar and Options, to a new NewScalar type....
Definition: fwd.hpp:99
A Plucker coordinate frame attached to a parent joint inside a kinematic tree.
Definition: frame.hpp:56
JointIndex addJoint(const JointIndex parent, const JointModel &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, const VectorXs &friction, const VectorXs &damping)
TangentVectorType velocityLimit
Vector of maximal joint velocities.
Definition: model.hpp:167
TangentVectorType friction
Vector of joint friction parameters.
Definition: model.hpp:158
std::vector< int > idx_vs
Starting index of the *i*th joint in the tangent configuration space.
Definition: model.hpp:128
FrameIndex addJointFrame(const JointIndex &joint_index, int previous_frame_index=-1)
Add a joint to the frame tree.
std::vector< IndexVector > subtrees
Vector of joint subtrees. subtree[j] corresponds to the subtree supported by the joint j....
Definition: model.hpp:188
std::vector< IndexVector > supports
Vector of joint supports. supports[j] corresponds to the vector of indices of the joints located on t...
Definition: model.hpp:183
std::vector< bool > hasConfigurationLimit()
Check if joints have configuration limits.
Motion gravity
Spatial gravity of the model.
Definition: model.hpp:191
FrameIndex getBodyId(const std::string &name) const
Return the index of a body given by its name.
bool operator==(const ModelTpl &other) const
Equality comparison operator.
ConfigVectorType upperPositionLimit
Upper joint configuration limit.
Definition: model.hpp:173
TangentVectorType rotorGearRatio
Vector of rotor gear ratio parameters.
Definition: model.hpp:155
ModelTpl()
Default constructor. Builds an empty model with no joints.
Definition: model.hpp:200
bool operator!=(const ModelTpl &other) const
Definition: model.hpp:252
InertiaVector inertias
Vector of spatial inertias supported by each joint.
Definition: model.hpp:113
FrameIndex addBodyFrame(const std::string &body_name, const JointIndex &parentJoint, const SE3 &body_placement=SE3::Identity(), int parentFrame=-1)
Add a body to the frame tree.
std::vector< int > idx_qs
Vector of starting index of the *i*th joint in the configuration space.
Definition: model.hpp:122
SE3Vector jointPlacements
Vector of joint placements: placement of a joint i wrt its parent joint frame.
Definition: model.hpp:116
int nframes
Number of operational frames.
Definition: model.hpp:110
VectorXs TangentVectorType
Dense vectorized version of a joint tangent vector (e.g. velocity, acceleration, etc)....
Definition: model.hpp:95
JointModelVector joints
Vector of joint models.
Definition: model.hpp:119
JointIndex addJoint(const JointIndex parent, const JointModel &joint_model, const SE3 &joint_placement, const std::string &joint_name)
Add a joint to the kinematic tree with infinite bounds.
bool check() const
Run check(fusion::list) with DEFAULT_CHECKERS as argument.
std::vector< int > nqs
Vector of dimension of the joint configuration subspace.
Definition: model.hpp:125
std::vector< IndexVector > children
Vector of children index. Chidren of the i*th joint, denoted *mu(i) corresponds to the set (i==parent...
Definition: model.hpp:139
ModelTpl(const ModelTpl< S2, O2 > &other)
Copy constructor by casting.
Definition: model.hpp:233
TangentVectorType rotorInertia
Vector of rotor inertia parameters.
Definition: model.hpp:152
TangentVectorType effortLimit
Vector of maximal joint torques.
Definition: model.hpp:164
int nq
Dimension of the configuration vector representation.
Definition: model.hpp:98
std::vector< JointIndex > parents
Vector of parent joint indexes. The parent of joint i, denoted li, corresponds to li==parents[i].
Definition: model.hpp:135
int nbodies
Number of bodies.
Definition: model.hpp:107
CastType< NewScalar, ModelTpl >::type cast() const
std::string name
Model name.
Definition: model.hpp:197
std::vector< std::string > names
Name of the joints.
Definition: model.hpp:142
JointIndex getJointId(const std::string &name) const
Return the index of a joint given by its name.
int nv
Dimension of the velocity vector space.
Definition: model.hpp:101
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.
bool check(const Data &data) const
Run checkData on data and current model.
ConfigVectorMap referenceConfigurations
Map of reference configurations, indexed by user given names.
Definition: model.hpp:145
FrameVector frames
Vector of operational frames registered on the model.
Definition: model.hpp:176
void addJointIndexToParentSubtrees(const JointIndex joint_id)
Add the joint_id to its parent subtrees.
bool existBodyName(const std::string &name) const
Check if a body given by its name exists.
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.
std::vector< int > nvs
Dimension of the *i*th joint tangent subspace.
Definition: model.hpp:131
VectorXs armature
Vector of armature values expressed at the joint level This vector may contain the contribution of ro...
Definition: model.hpp:149
bool check(const AlgorithmCheckerBase< D > &checker=AlgorithmCheckerBase< D >()) const
Check the validity of the attributes of Model with respect to the specification of some algorithms.
Definition: model.hpp:461
static const Vector3 gravity981
Default 3D gravity vector (=(0,0,-9.81)).
Definition: model.hpp:194
JointIndex addJoint(const JointIndex parent, const JointModel &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)
int njoints
Number of joints.
Definition: model.hpp:104
VectorXs ConfigVectorType
Dense vectorized version of a joint configuration vector.
Definition: model.hpp:87
std::map< std::string, ConfigVectorType > ConfigVectorMap
Map between a string (key) and a configuration vector.
Definition: model.hpp:90
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.
bool existJointName(const std::string &name) const
Check if a joint given by its name exists.
TangentVectorType damping
Vector of joint damping parameters.
Definition: model.hpp:161
ConfigVectorType lowerPositionLimit
Lower joint configuration limit.
Definition: model.hpp:170
FrameIndex addFrame(const Frame &frame, const bool append_inertia=true)
Adds a frame to the kinematic tree. The inertia stored within the frame will be happened to the inert...
std::vector< bool > hasConfigurationLimitInTangent()
Check if joints have configuration limits.
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:72