6#ifndef __pinocchio_python_multibody_model_hpp__
7#define __pinocchio_python_multibody_model_hpp__
9#include <eigenpy/eigen-to-python.hpp>
11#include "pinocchio/multibody/model.hpp"
12#include "pinocchio/serialization/model.hpp"
14#include <boost/python/overloads.hpp>
15#include <eigenpy/memory.hpp>
16#include <eigenpy/exception.hpp>
18#include "pinocchio/algorithm/check.hpp"
20#include "pinocchio/bindings/python/utils/cast.hpp"
21#include "pinocchio/bindings/python/utils/macros.hpp"
22#include "pinocchio/bindings/python/utils/printable.hpp"
23#include "pinocchio/bindings/python/utils/copyable.hpp"
24#include "pinocchio/bindings/python/utils/std-map.hpp"
25#include "pinocchio/bindings/python/utils/pickle.hpp"
26#include "pinocchio/bindings/python/utils/pickle-map.hpp"
27#include "pinocchio/bindings/python/utils/std-vector.hpp"
28#include "pinocchio/bindings/python/serialization/serializable.hpp"
30#if EIGENPY_VERSION_AT_MOST(2, 8, 1)
38 namespace bp = boost::python;
40 template<
typename Model>
41 struct ModelPythonVisitor :
public bp::def_visitor<ModelPythonVisitor<Model>>
44 typedef typename Model::Scalar Scalar;
46 typedef typename Model::Index Index;
47 typedef typename Model::JointIndex JointIndex;
48 typedef typename Model::JointModel JointModel;
49 typedef typename JointModel::JointModelVariant JointModelVariant;
50 typedef typename Model::FrameIndex FrameIndex;
51 typedef typename Model::IndexVector IndexVector;
53 typedef typename Model::SE3 SE3;
54 typedef typename Model::Motion Motion;
55 typedef typename Model::Force Force;
56 typedef typename Model::Frame Frame;
57 typedef typename Model::Inertia Inertia;
59 typedef typename Model::Data Data;
61 typedef typename Model::VectorXs VectorXs;
65 template<
class PyClass>
66 void visit(PyClass & cl)
const
68 cl.def(bp::init<>(bp::arg(
"self"),
"Default constructor. Constructs an empty model."))
69 .def(bp::init<const Model &>((bp::arg(
"self"), bp::arg(
"clone")),
"Copy constructor"))
72 .add_property(
"nq", &
Model::nq,
"Dimension of the configuration vector representation.")
73 .add_property(
"nv", &
Model::nv,
"Dimension of the velocity vector space.")
74 .add_property(
"nvExtended", &
Model::nvExtended,
"Dimension of the jacobian matrix space.")
79 "inertias", &
Model::inertias,
"Vector of spatial inertias supported by each joint.")
82 "Vector of joint placements: placement of a joint *i* wrt its parent joint frame.")
83 .add_property(
"joints", &
Model::joints,
"Vector of joint models.")
86 "Vector of starting index of the *i*th joint in the configuration space.")
88 "nqs", &
Model::nqs,
"Vector of dimension of the joint configuration subspace.")
91 "Starting index of the *i*th joint in the tangent configuration space.")
92 .add_property(
"nvs", &
Model::nvs,
"Dimension of the *i*th joint tangent subspace.")
95 "Starting index of the *i*th joint in the jacobian space.")
100 "Vector of parent joint indexes. The parent of joint *i*, denoted *li*, "
101 "corresponds to li==parents[i].")
104 "Vector of children index. Chidren of the *i*th joint, denoted *mu(i)* "
105 "corresponds to the set (i==parents[k] for k in mu(i)).")
106 .add_property(
"names", &
Model::names,
"Name of the joints.")
107 .def_readwrite(
"name", &
Model::name,
"Name of the model.")
110 "Map of reference configurations, indexed by user given names.")
117 .def_readwrite(
"friction", &
Model::friction,
"Vector of joint friction parameters.")
118 .def_readwrite(
"damping", &
Model::damping,
"Vector of joint damping parameters.")
126 .def_readwrite(
"frames", &
Model::frames,
"Vector of frames contained in the model.")
130 "Vector of supports. supports[j] corresponds to the list of joints on the "
132 "the current *j* to the root of the kinematic tree.")
136 "Vector of mimic supports joints. "
137 "mimic_joint_supports[j] corresponds to the vector of mimic joints indices located on "
139 "between joint *j* and \"universe\". The first element of mimic_joint_supports[j] is "
141 "If *j* is a mimic, the last element is the index of joint *j* itself")
145 "Vector of subtrees. subtree[j] corresponds to the subtree supported by the joint j.")
149 "Vector of mimicking joints in the tree (with type MimicTpl)")
153 "Vector of mimicked joints in the tree (can be any joint type)")
157 "Motion vector corresponding to the gravity field expressed in the world Frame.")
161 "addJoint", &ModelPythonVisitor::addJoint0,
162 bp::args(
"self",
"parent_id",
"joint_model",
"joint_placement",
"joint_name"),
163 "Adds a joint to the kinematic tree. The joint is defined by its placement relative "
164 "to its parent joint and its name.")
166 "addJoint", &ModelPythonVisitor::addJoint1,
168 "self",
"parent_id",
"joint_model",
"joint_placement",
"joint_name",
"max_effort",
169 "max_velocity",
"min_config",
"max_config"),
170 "Adds a joint to the kinematic tree with given bounds. The joint is defined by its "
171 "placement relative to its parent joint and its name."
172 "This signature also takes as input effort, velocity limits as well as the bounds "
173 "on the joint configuration.")
175 "addJoint", &ModelPythonVisitor::addJoint2,
177 "self",
"parent_id",
"joint_model",
"joint_placement",
"joint_name",
"max_effort",
178 "max_velocity",
"min_config",
"max_config",
"friction",
"damping"),
179 "Adds a joint to the kinematic tree with given bounds. The joint is defined by its "
180 "placement relative to its parent joint and its name.\n"
181 "This signature also takes as input effort, velocity limits as well as the bounds "
182 "on the joint configuration.\n"
183 "The user should also provide the friction and damping related to the joint.")
186 (bp::arg(
"self"), bp::arg(
"joint_id"), bp::arg(
"frame_id") = 0),
187 "Add the joint provided by its joint_id as a frame to the frame tree.\n"
188 "The frame_id may be optionally provided.")
191 bp::args(
"self",
"joint_id",
"body_inertia",
"body_placement"),
192 "Appends a body to the joint given by its index. The body is defined by its "
193 "inertia, its relative placement regarding to the joint and its name.")
197 bp::args(
"self",
"body_name",
"parentJoint",
"body_placement",
"previous_frame"),
198 "add a body to the frame tree")
201 "Return the index of a frame of type BODY given by its name")
204 "Check if a frame of type BODY exists, given its name")
207 "Return the index of a joint given by its name")
210 "Check if a joint given by its name exists")
214 (bp::arg(
"self"), bp::arg(
"name"),
216 "Returns the index of the frame given by its name and its type."
217 "If the frame is not in the frames vector, it returns the current size of the "
222 (bp::arg(
"self"), bp::arg(
"name"),
224 "Returns true if the frame given by its name exists inside the Model with the given "
229 (bp::arg(
"self"), bp::arg(
"frame"), bp::arg(
"append_inertia") =
true),
230 "Add a frame to the vector of frames. If append_inertia set to True, "
231 "the inertia value contained in frame will be added to the inertia supported by the "
235 "createData", &ModelPythonVisitor::createData, bp::arg(
"self"),
236 "Create a Data object for the given model.")
239 "check", (
bool(Model::*)(const Data &) const) & Model::check, bp::args(
"self",
"data"),
240 "Check consistency of data wrt model.")
244 "Returns list of boolean if joints have configuration limit.")
248 "Returns list of boolean if joints have configuration limit in tangent space .")
250#ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
252 .def(bp::self == bp::self)
253 .def(bp::self != bp::self)
256 .PINOCCHIO_ADD_STATIC_PROPERTY_READONLY_BYVALUE(
257 Model, gravity981,
"Default gravity field value on the Earth.");
259 bp::register_ptr_to_python<std::shared_ptr<Model>>();
262 static JointIndex addJoint0(
264 JointIndex parent_id,
265 const JointModel & jmodel,
266 const SE3 & joint_placement,
267 const std::string & joint_name)
269 return model.addJoint(parent_id, jmodel, joint_placement, joint_name);
272 static JointIndex addJoint1(
274 JointIndex parent_id,
275 const JointModel & jmodel,
276 const SE3 & joint_placement,
277 const std::string & joint_name,
278 const VectorXs & max_effort,
279 const VectorXs & max_velocity,
280 const VectorXs & min_config,
281 const VectorXs & max_config)
283 return model.addJoint(
284 parent_id, jmodel, joint_placement, joint_name, max_effort, max_velocity, min_config,
288 static JointIndex addJoint2(
290 JointIndex parent_id,
291 const JointModel & jmodel,
292 const SE3 & joint_placement,
293 const std::string & joint_name,
294 const VectorXs & max_effort,
295 const VectorXs & max_velocity,
296 const VectorXs & min_config,
297 const VectorXs & max_config,
298 const VectorXs & friction,
299 const VectorXs & damping)
301 return model.addJoint(
302 parent_id, jmodel, joint_placement, joint_name, max_effort, max_velocity, min_config,
303 max_config, friction, damping);
306 static Data createData(
const Model & model)
322 static Index index(std::vector<T>
const & x,
typename std::vector<T>::value_type
const & v)
325 for (
typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); ++it, ++i)
339 typedef bp::map_indexing_suite<ConfigVectorMap, false> map_indexing_suite;
340 StdVectorPythonVisitor<std::vector<Index>,
true>::expose(
"StdVec_Index");
341 serialize<std::vector<Index>>();
342 StdVectorPythonVisitor<std::vector<IndexVector>>::expose(
"StdVec_IndexVector");
343 serialize<std::vector<IndexVector>>();
344 StdVectorPythonVisitor<std::vector<std::string>,
true>::expose(
"StdVec_StdString");
345 StdVectorPythonVisitor<std::vector<bool>,
true>::expose(
"StdVec_Bool");
346 StdVectorPythonVisitor<std::vector<Scalar>,
true>::expose(
"StdVec_Scalar");
348#if defined(PINOCCHIO_PYTHON_INTERFACE_MAIN_MODULE)
349 bp::scope().attr(
"StdVec_Double") = bp::scope().attr(
"StdVec_Scalar");
352 serialize<std::vector<std::string>>();
353 serialize<std::vector<bool>>();
354#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
355 serialize<std::vector<Scalar>>();
357 bp::class_<typename Model::ConfigVectorMap>(
"StdMap_String_VectorXd")
358 .def(map_indexing_suite())
359 .def_pickle(PickleMap<typename Model::ConfigVectorMap>())
360 .def(details::overload_base_get_item_for_std_map<typename Model::ConfigVectorMap>());
362 bp::class_<Model>(
"Model",
"Articulated Rigid Body model", bp::no_init)
363 .def(ModelPythonVisitor())
364 .def(CastVisitor<Model>())
365 .def(ExposeConstructorByCastVisitor<Model, ::pinocchio::Model>())
366 .def(SerializableVisitor<Model>())
367 .def(PrintableVisitor<Model>())
368 .def(CopyableVisitor<Model>())
369#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
370 .def_pickle(PickleFromStringSerialization<Model>())
Main pinocchio namespace.
const std::vector< bool > hasConfigurationLimitInTangent(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointConfigurationLimitInTangentVisitor to get the configurations limit...
const std::vector< bool > hasConfigurationLimit(const JointModelTpl< Scalar, Options, JointCollectionTpl > &jmodel)
Visit a JointModelTpl through JointConfigurationLimitVisitor to get the configurations limits.
FrameType
Enum on the possible types of frames.
@ BODY
body frame: attached to the collision, inertial or visual properties of a link
@ SENSOR
sensor frame: defined in a sensor element
@ OP_FRAME
operational frame: user-defined frames that are defined at runtime
@ JOINT
joint frame: attached to the child body of a joint (a.k.a. child frame)
@ FIXED_JOINT
fixed joint frame: joint frame but for a fixed joint
TangentVectorType velocityLimit
Vector of maximal joint velocities.
TangentVectorType friction
Vector of joint friction parameters.
std::vector< int > idx_vs
Starting index of the *i*th joint in the tangent configuration space.
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....
std::vector< IndexVector > supports
Vector of joint supports. supports[j] corresponds to the vector of indices of the joints located on t...
int nvExtended
Dimension of the jacobian space.
Motion gravity
Spatial gravity of the model.
std::vector< JointIndex > mimicking_joints
Vector of mimicking joints in the tree (with type MimicTpl)
FrameIndex getBodyId(const std::string &name) const
Return the index of a body given by its name.
ConfigVectorType upperPositionLimit
Upper joint configuration limit.
TangentVectorType rotorGearRatio
Vector of rotor gear ratio parameters.
InertiaVector inertias
Vector of spatial inertias supported by each joint.
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.
SE3Vector jointPlacements
Vector of joint placements: placement of a joint i wrt its parent joint frame.
int nframes
Number of operational frames.
JointModelVector joints
Vector of joint models.
std::vector< int > nqs
Vector of dimension of the joint configuration subspace.
std::vector< IndexVector > children
Vector of children index. Chidren of the i*th joint, denoted *mu(i) corresponds to the set (i==parent...
TangentVectorType rotorInertia
Vector of rotor inertia parameters.
TangentVectorType effortLimit
Vector of maximal joint torques.
int nq
Dimension of the configuration vector representation.
std::vector< JointIndex > parents
Vector of parent joint indexes. The parent of joint i, denoted li, corresponds to li==parents[i].
int nbodies
Number of bodies.
std::vector< int > idx_vExtendeds
Starting index of the *i*th joint in the jacobian space.
std::string name
Model name.
std::vector< std::string > names
Name of the joints.
JointIndex getJointId(const std::string &name) const
Return the index of a joint given by its name.
std::vector< int > nvExtendeds
Dimension of the *i*th joint jacobian subspace.
int nv
Dimension of the velocity vector space.
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.
ConfigVectorMap referenceConfigurations
Map of reference configurations, indexed by user given names.
FrameVector frames
Vector of operational frames registered on the model.
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< JointIndex > mimicked_joints
Vector of mimicked joints in the tree (can be any joint type) The i-th element of this vector corresp...
std::vector< int > nvs
Dimension of the *i*th joint tangent subspace.
VectorXs armature
Vector of armature values expressed at the joint level This vector may contain the contribution of ro...
std::vector< IndexVector > mimic_joint_supports
Vector of mimic supports joints. mimic_joint_supports[j] corresponds to the vector of mimic joints in...
int njoints
Number of joints.
std::map< std::string, ConfigVectorType > ConfigVectorMap
Map between a string (key) and a configuration vector.
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.
ConfigVectorType lowerPositionLimit
Lower joint configuration limit.
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...