bindings/python/multibody/model.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015-2021 CNRS INRIA
3 // Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
4 //
5 
6 #ifndef __pinocchio_python_multibody_model_hpp__
7 #define __pinocchio_python_multibody_model_hpp__
8 
10 
11 #include "pinocchio/multibody/model.hpp"
12 #include "pinocchio/serialization/model.hpp"
13 
14 #include <boost/python/overloads.hpp>
15 #include <eigenpy/memory.hpp>
16 #include <eigenpy/exception.hpp>
17 
18 #include "pinocchio/algorithm/check.hpp"
25 
27 
28 namespace pinocchio
29 {
30  namespace python
31  {
32  namespace bp = boost::python;
33 
34  BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFrameId_overload,Model::getFrameId,1,2)
35  BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(existFrame_overload,Model::existFrame,1,2)
36  BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(addJointFrame_overload,Model::addJointFrame,1,2)
37 
38  template<typename Model>
39  struct PickleModel : bp::pickle_suite
40  {
41  static bp::tuple getinitargs(const Model &)
42  {
43  return bp::make_tuple();
44  }
45 
46  static bp::tuple getstate(const Model & model)
47  {
48  const std::string str(model.saveToString());
49  return bp::make_tuple(bp::str(str));
50  }
51 
52  static void setstate(Model & model, bp::tuple tup)
53  {
54  if(bp::len(tup) == 0 || bp::len(tup) > 1)
55  {
56  throw eigenpy::Exception("Pickle was not able to reconstruct the model from the loaded data.\n"
57  "The pickle data structure contains too many elements.");
58  }
59 
60  bp::object py_obj = tup[0];
61  boost::python::extract<std::string> obj_as_string(py_obj.ptr());
62  if(obj_as_string.check())
63  {
64  const std::string str = obj_as_string;
65  model.loadFromString(str);
66  }
67  else
68  {
69  throw eigenpy::Exception("Pickle was not able to reconstruct the model from the loaded data.\n"
70  "The entry is not a string.");
71  }
72 
73  }
74 
75  static bool getstate_manages_dict() { return true; }
76 
77  };
78 
79  template<typename Model>
81  : public bp::def_visitor< ModelPythonVisitor<Model> >
82  {
83  public:
84  typedef typename Model::Scalar Scalar;
85 
86  typedef typename Model::Index Index;
87  typedef typename Model::JointIndex JointIndex;
88  typedef typename Model::FrameIndex FrameIndex;
89  typedef typename Model::IndexVector IndexVector;
90 
91  typedef typename Model::SE3 SE3;
92  typedef typename Model::Motion Motion;
93  typedef typename Model::Force Force;
94  typedef typename Model::Frame Frame;
95  typedef typename Model::Inertia Inertia;
96 
97  typedef typename Model::Data Data;
98 
99  typedef typename Model::VectorXs VectorXs;
100 
101  BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(addFrame_overload,Model::addFrame,1,2)
102 
103  public:
104 
105  /* --- Exposing C++ API to python through the handler ----------------- */
106  template<class PyClass>
107  void visit(PyClass& cl) const
108  {
109  cl
110  .def(bp::init<>(bp::arg("self"),
111  "Default constructor. Constructs an empty model."))
112 
113  // Class Members
114  .add_property("nq", &Model::nq)
115  .add_property("nv", &Model::nv)
116  .add_property("njoints", &Model::njoints)
117  .add_property("nbodies", &Model::nbodies)
118  .add_property("nframes", &Model::nframes)
119  .add_property("inertias",&Model::inertias)
120  .add_property("jointPlacements",&Model::jointPlacements)
121  .add_property("joints",&Model::joints)
122  .add_property("idx_qs",&Model::idx_qs)
123  .add_property("nqs",&Model::nqs)
124  .add_property("idx_vs",&Model::idx_vs)
125  .add_property("nvs",&Model::nvs)
126  .add_property("parents",&Model::parents)
127  .add_property("names",&Model::names)
128  .def_readwrite("name",&Model::name)
129  .def_readwrite("referenceConfigurations", &Model::referenceConfigurations)
130 
131  .def_readwrite("rotorInertia",&Model::rotorInertia,
132  "Vector of rotor inertia parameters.")
133  .def_readwrite("rotorGearRatio",&Model::rotorGearRatio,
134  "Vector of rotor gear ratio parameters.")
135  .def_readwrite("friction",&Model::friction,
136  "Vector of joint friction parameters.")
137  .def_readwrite("damping",&Model::damping,
138  "Vector of joint damping parameters.")
139  .def_readwrite("effortLimit",&Model::effortLimit,
140  "Joint max effort.")
141  .def_readwrite("velocityLimit",&Model::velocityLimit,
142  "Joint max velocity.")
143  .def_readwrite("lowerPositionLimit",&Model::lowerPositionLimit,
144  "Limit for joint lower position.")
145  .def_readwrite("upperPositionLimit",&Model::upperPositionLimit,
146  "Limit for joint upper position.")
147 
148  .def_readwrite("frames",&Model::frames,
149  "Vector of frames contained in the model.")
150 
151  .def_readwrite("supports",
153  "Vector of supports. supports[j] corresponds to the list of joints on the path between\n"
154  "the current *j* to the root of the kinematic tree.")
155 
156  .def_readwrite("subtrees",
158  "Vector of subtrees. subtree[j] corresponds to the subtree supported by the joint j.")
159 
160  .def_readwrite("gravity",&Model::gravity,
161  "Motion vector corresponding to the gravity field expressed in the world Frame.")
162 
163  // Class Methods
164  .def("addJoint",&ModelPythonVisitor::addJoint0,
165  bp::args("self","parent_id","joint_model","joint_placement","joint_name"),
166  "Adds a joint to the kinematic tree. The joint is defined by its placement relative to its parent joint and its name.")
167  .def("addJoint",&ModelPythonVisitor::addJoint1,
168  bp::args("self","parent_id","joint_model","joint_placement","joint_name",
169  "max_effort","max_velocity","min_config","max_config"),
170  "Adds a joint to the kinematic tree with given bounds. The joint is defined by its placement relative to its parent joint and its name."
171  "This signature also takes as input effort, velocity limits as well as the bounds on the joint configuration.")
172  .def("addJoint",&ModelPythonVisitor::addJoint2,
173  bp::args("self","parent_id","joint_model","joint_placement","joint_name",
174  "max_effort","max_velocity","min_config","max_config",
175  "friction","damping"),
176  "Adds a joint to the kinematic tree with given bounds. The joint is defined by its placement relative to its parent joint and its name.\n"
177  "This signature also takes as input effort, velocity limits as well as the bounds on the joint configuration.\n"
178  "The user should also provide the friction and damping related to the joint.")
179  .def("addJointFrame", &Model::addJointFrame,
180  addJointFrame_overload(bp::args("self","joint_id", "frame_id"),
181  "Add the joint provided by its joint_id as a frame to the frame tree.\n"
182  "The frame_id may be optionally provided."))
183  .def("appendBodyToJoint",&Model::appendBodyToJoint,
184  bp::args("self","joint_id","body_inertia","body_placement"),
185  "Appends a body to the joint given by its index. The body is defined by its inertia, its relative placement regarding to the joint and its name.")
186 
187  .def("addBodyFrame", &Model::addBodyFrame, bp::args("self","body_name", "parentJoint", "body_placement", "previous_frame"), "add a body to the frame tree")
188  .def("getBodyId",&Model::getBodyId, bp::args("self","name"), "Return the index of a frame of type BODY given by its name")
189  .def("existBodyName", &Model::existBodyName, bp::args("self","name"), "Check if a frame of type BODY exists, given its name")
190  .def("getJointId",&Model::getJointId, bp::args("self","name"), "Return the index of a joint given by its name")
191  .def("existJointName", &Model::existJointName, bp::args("self","name"), "Check if a joint given by its name exists")
192 
193  .def("getFrameId",&Model::getFrameId,getFrameId_overload(bp::args("self","name","type"),"Returns the index of the frame given by its name and its type. If the frame is not in the frames vector, it returns the current size of the frames vector."))
194 
195  .def("existFrame",&Model::existFrame,existFrame_overload(bp::args("self","name","type"),"Returns true if the frame given by its name exists inside the Model with the given type."))
196 
197  .def("addFrame",&Model::addFrame,
198  addFrame_overload((bp::arg("self"), bp::arg("frame"), bp::arg("append_inertia") = true),
199  "Add a frame to the vector of frames. If append_inertia set to True, "
200  "the inertia value contained in frame will be added to the inertia supported by the parent joint."))
201 
202  .def("createData",
203  &ModelPythonVisitor::createData,bp::arg("self"),
204  "Create a Data object for the given model.")
205 
206  .def("check",(bool (Model::*)(const Data &) const) &Model::check,bp::args("self","data"),
207  "Check consistency of data wrt model.")
208  .def("hasConfigurationLimit",&Model::hasConfigurationLimit, bp::args("self"), "Returns list of boolean if joints have configuration limit.")
209  .def("hasConfigurationLimitInTangent",&Model::hasConfigurationLimitInTangent, bp::args("self"), "Returns list of boolean if joints have configuration limit in tangent space .")
210 
211  .def(bp::self == bp::self)
212  .def(bp::self != bp::self)
213  ;
214  }
215 
216  static JointIndex addJoint0(Model & model,
217  JointIndex parent_id,
218  const JointModel & jmodel,
219  const SE3 & joint_placement,
220  const std::string & joint_name)
221  {
222  return model.addJoint(parent_id,jmodel,joint_placement,joint_name);
223  }
224 
225  static JointIndex addJoint1(Model & model,
226  JointIndex parent_id,
227  const JointModel & jmodel,
228  const SE3 & joint_placement,
229  const std::string & joint_name,
230  const VectorXs & max_effort,
231  const VectorXs & max_velocity,
232  const VectorXs & min_config,
233  const VectorXs & max_config)
234  {
235  return model.addJoint(parent_id,jmodel,joint_placement,joint_name,
236  max_effort,max_velocity,min_config,max_config);
237  }
238 
239  static JointIndex addJoint2(Model & model,
240  JointIndex parent_id,
241  const JointModel & jmodel,
242  const SE3 & joint_placement,
243  const std::string & joint_name,
244  const VectorXs & max_effort,
245  const VectorXs & max_velocity,
246  const VectorXs & min_config,
247  const VectorXs & max_config,
248  const VectorXs & friction,
249  const VectorXs & damping)
250  {
251  return model.addJoint(parent_id,jmodel,joint_placement,joint_name,
252  max_effort,max_velocity,min_config,max_config,
253  friction,damping);
254  }
255 
256  static Data createData(const Model & model) { return Data(model); }
257 
268  template<typename T>
269  static Index index(std::vector<T> const& x,
270  typename std::vector<T>::value_type const& v)
271  {
272  Index i = 0;
273  for(typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); ++it, ++i)
274  {
275  if(*it == v)
276  {
277  return i;
278  }
279  }
280  return x.size();
281  }
282 
283  /* --- Expose --------------------------------------------------------- */
284  static void expose()
285  {
286  typedef typename Model::ConfigVectorMap ConfigVectorMap;
287  typedef bp::map_indexing_suite<ConfigVectorMap,false> map_indexing_suite;
289  serialize< std::vector<Index> >();
290  StdVectorPythonVisitor<IndexVector>::expose("StdVec_IndexVector");
291  serialize< std::vector<IndexVector> >();
293  serialize< std::vector<std::string> >();
295  serialize< std::vector<bool> >();
297  serialize< std::vector<Scalar> >();
298  bp::class_<typename Model::ConfigVectorMap>("StdMap_String_VectorXd")
299  .def(map_indexing_suite())
302 
303  bp::class_<Model>("Model",
304  "Articulated Rigid Body model",
305  bp::no_init)
310  .def_pickle(PickleModel<Model>())
311  ;
312  }
313  };
314 
315  }} // namespace pinocchio::python
316 
317 #endif // ifndef __pinocchio_python_multibody_model_hpp__
bool existBodyName(const std::string &name) const
Check if a body given by its name exists.
JointModelVector joints
Model of joint i, encapsulated in a JointModelAccessor.
FrameIndex addJointFrame(const JointIndex &joint_index, int previous_frame_index=-1)
Add a joint to the frame tree.
ModelTpl< double > Model
static void setstate(Model &model, bp::tuple tup)
static JointIndex addJoint2(Model &model, JointIndex parent_id, const JointModel &jmodel, 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)
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.
A Plucker coordinate frame attached to a parent joint inside a kinematic tree.
int njoints
Number of joints.
std::vector< std::string > names
Name of joint i
ConfigVectorType lowerPositionLimit
Lower joint configuration limit.
static bp::tuple getstate(const Model &model)
Set the Python method str and repr to use the overloading operator<<.
Definition: printable.hpp:21
FrameVector frames
Vector of operational frames registered on the model.
void def(const char *name, Func func)
std::vector< bool > hasConfigurationLimit()
Check if joints have configuration limits.
std::vector< int > nvs
Dimension of the joint i tangent subspace.
TangentVectorType rotorInertia
Vector of rotor inertia parameters.
bool existJointName(const std::string &name) const
Check if a joint given by its name exists.
static JointIndex addJoint1(Model &model, JointIndex parent_id, const JointModel &jmodel, 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)
std::vector< IndexVector > subtrees
Vector of joint subtrees. subtree[j] corresponds to the subtree supported by the joint j...
Scalar value_type
Definition: eigen_plugin.hpp:2
std::string saveToString() const
Saves a Derived object to a string.
ConfigVectorMap referenceConfigurations
Map of reference configurations, indexed by user given names.
int nframes
Number of operational frames.
std::string name
Model name;.
TangentVectorType rotorGearRatio
Vector of rotor gear ratio parameters.
pinocchio::FrameIndex FrameIndex
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.
std::vector< int > nqs
Dimension of the joint i configuration subspace.
TangentVectorType friction
Vector of joint friction parameters.
std::vector< int > idx_qs
Starting index of the joint i in the configuration space.
std::vector< Index > IndexVector
int nbodies
Number of bodies.
pinocchio::JointIndex JointIndex
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.
static ::boost::python::class_< vector_type > expose(const std::string &class_name, const std::string &doc_string="")
Definition: std-vector.hpp:177
Motion gravity
Spatial gravity of the model.
std::map< std::string, ConfigVectorType > ConfigVectorMap
Map between a string (key) and a configuration vector.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef _Scalar Scalar
Add the Python method copy to allow a copy of this by calling the copy constructor.
Definition: copyable.hpp:21
static bp::tuple getinitargs(const Model &)
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...
Main pinocchio namespace.
Definition: timings.cpp:28
std::vector< int > idx_vs
Starting index of the joint i in the tangent configuration space.
std::vector< bool > hasConfigurationLimitInTangent()
Check if joints have configuration limits.
TangentVectorType effortLimit
Vector of maximal joint torques.
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, Options > VectorXs
JointIndex getJointId(const std::string &name) const
Return the index of a joint given by its name.
void loadFromString(const std::string &str)
Loads a Derived object from a string.
TangentVectorType damping
Vector of joint damping parameters.
std::vector< JointIndex > parents
Joint parent of joint i, denoted li (li==parents[i]).
int nv
Dimension of the velocity vector space.
TangentVectorType velocityLimit
Vector of maximal joint velocities.
FrameIndex getBodyId(const std::string &name) const
Return the index of a body given by its name.
ConfigVectorType upperPositionLimit
Upper joint configuration limit.
static Index index(std::vector< T > const &x, typename std::vector< T >::value_type const &v)
Provide equivalent to python list index function for vectors.
#define EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(...)
static JointIndex addJoint0(Model &model, JointIndex parent_id, const JointModel &jmodel, const SE3 &joint_placement, const std::string &joint_name)
std::vector< IndexVector > supports
Vector of joint supports. supports[j] corresponds to the collection of all joints located on the path...
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.
Create a pickle interface for the std::map and aligned map.
Definition: pickle-map.hpp:24
JointCollectionTpl & model
int nq
Dimension of the configuration vector representation.
bool check() const
Run check(fusion::list) with DEFAULT_CHECKERS as argument.
str
FrameIndex 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.


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:32