pinocchio  3.9.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
data.hpp
1//
2// Copyright (c) 2015-2018 CNRS
3// Copyright (c) 2018-2025 INRIA
4//
5
6#ifndef __pinocchio_python_multibody_data_hpp__
7#define __pinocchio_python_multibody_data_hpp__
8
9#include "pinocchio/multibody/data.hpp"
10#include "pinocchio/serialization/data.hpp"
11
12#include <eigenpy/memory.hpp>
13#include <eigenpy/eigen-to-python.hpp>
14#include <eigenpy/exception.hpp>
15
16#include "pinocchio/bindings/python/fwd.hpp"
17#include "pinocchio/bindings/python/utils/macros.hpp"
18#include "pinocchio/bindings/python/serialization/serializable.hpp"
19#include "pinocchio/bindings/python/utils/std-vector.hpp"
20#include "pinocchio/bindings/python/utils/std-aligned-vector.hpp"
21#include "pinocchio/bindings/python/utils/copyable.hpp"
22
23#if EIGENPY_VERSION_AT_MOST(2, 8, 1)
24EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(pinocchio::Data)
25#endif
26
27namespace pinocchio
28{
29 namespace python
30 {
31 namespace bp = boost::python;
32
33 template<typename Data>
34 struct PickleData : bp::pickle_suite
35 {
36 static bp::tuple getinitargs(const Data &)
37 {
38 return bp::make_tuple();
39 }
40
41 static bp::tuple getstate(const Data & data)
42 {
43 const std::string str(data.saveToString());
44 return bp::make_tuple(bp::str(str));
45 }
46
47 static void setstate(Data & data, bp::tuple tup)
48 {
49 if (bp::len(tup) == 0 || bp::len(tup) > 1)
50 {
51 throw eigenpy::Exception(
52 "Pickle was not able to reconstruct the model from the loaded data.\n"
53 "The pickle data structure contains too many elements.");
54 }
55
56 bp::object py_obj = tup[0];
57 boost::python::extract<std::string> obj_as_string(py_obj.ptr());
58 if (obj_as_string.check())
59 {
60 const std::string str = obj_as_string;
61 data.loadFromString(str);
62 }
63 else
64 {
65 throw eigenpy::Exception(
66 "Pickle was not able to reconstruct the model from the loaded data.\n"
67 "The entry is not a string.");
68 }
69 }
70
71 static bool getstate_manages_dict()
72 {
73 return true;
74 }
75 };
76
77 template<typename Data>
78 struct DataPythonVisitor : public boost::python::def_visitor<DataPythonVisitor<Data>>
79 {
80 typedef typename Data::Matrix6 Matrix6;
81 typedef typename Data::Matrix6x Matrix6x;
82 typedef typename Data::Matrix3x Matrix3x;
83 typedef typename Data::Vector3 Vector3;
84
85 public:
86#define ADD_DATA_PROPERTY(NAME, DOC) PINOCCHIO_ADD_PROPERTY(Data, NAME, DOC)
87
88#define ADD_DATA_PROPERTY_READONLY(NAME, DOC) PINOCCHIO_ADD_PROPERTY_READONLY(Data, NAME, DOC)
89
90#define ADD_DATA_PROPERTY_READONLY_BYVALUE(NAME, DOC) \
91 PINOCCHIO_ADD_PROPERTY_READONLY_BYVALUE(Data, NAME, DOC)
92
93 /* --- Exposing C++ API to python through the handler ----------------- */
94 template<class PyClass>
95 void visit(PyClass & cl) const
96 {
97 cl.def(bp::init<>(bp::arg("self"), "Default constructor."))
98 .def(bp::init<const context::Model &>(
99 bp::args("self", "model"), "Constructs a data structure from a given model."))
100
101 .ADD_DATA_PROPERTY(
102 joints,
103 "Vector of JointData associated to each JointModel stored in the related model.")
104 .ADD_DATA_PROPERTY(
105 a, "Vector of joint accelerations expressed in the local frame of the joint.")
106 .ADD_DATA_PROPERTY(
107 oa, "Joint spatial acceleration expressed at the origin of the world frame.")
108 .ADD_DATA_PROPERTY(
109 a_gf, "Joint spatial acceleration containing also the contribution of "
110 "the gravity acceleration")
111 .ADD_DATA_PROPERTY(
112 oa_gf, "Joint spatial acceleration containing also the contribution of the gravity "
113 "acceleration, but expressed at the origin of the world frame.")
114
115 .ADD_DATA_PROPERTY(
116 v, "Vector of joint velocities expressed in the local frame of the joint.")
117 .ADD_DATA_PROPERTY(ov, "Vector of joint velocities expressed at the origin of the world.")
118
119 .ADD_DATA_PROPERTY(f, "Vector of body forces expressed in the local frame of the joint.")
120 .ADD_DATA_PROPERTY(of, "Vector of body forces expressed at the origin of the world.")
121 .ADD_DATA_PROPERTY(
122 of_augmented, "Vector of body forces expressed at the origin of the "
123 "world, in the context of lagrangian formulation")
124 .ADD_DATA_PROPERTY(
125 h, "Vector of spatial momenta expressed in the local frame of the joint.")
126 .ADD_DATA_PROPERTY(oh, "Vector of spatial momenta expressed at the origin of the world.")
127 .ADD_DATA_PROPERTY(oMi, "Body absolute placement (wrt world)")
128 .ADD_DATA_PROPERTY(oMf, "frames absolute placement (wrt world)")
129 .ADD_DATA_PROPERTY(liMi, "Body relative placement (wrt parent)")
130 .ADD_DATA_PROPERTY(tau, "Joint torques (output of RNEA)")
131 .ADD_DATA_PROPERTY(nle, "Non Linear Effects (output of nle algorithm)")
132 .ADD_DATA_PROPERTY(ddq, "Joint accelerations (output of ABA)")
133 .ADD_DATA_PROPERTY(Ycrb, "Inertia of the sub-tree composit rigid body")
134 .ADD_DATA_PROPERTY(
135 oYcrb, "Composite Rigid Body Inertia of the sub-tree expressed in the "
136 "WORLD coordinate system.")
137 .ADD_DATA_PROPERTY(Yaba, "Articulated Body Inertia of the sub-tree")
138 .ADD_DATA_PROPERTY(
139 oYaba,
140 "Articulated Body Inertia of the sub-tree expressed in the WORLD coordinate system.")
141 .ADD_DATA_PROPERTY(oL, "Acceleration propagator.")
142 .ADD_DATA_PROPERTY(oK, "Inverse articulated inertia.")
143 .ADD_DATA_PROPERTY(M, "The joint space inertia matrix")
144 .ADD_DATA_PROPERTY(Minv, "The inverse of the joint space inertia matrix")
145 .ADD_DATA_PROPERTY(
146 C, "The Coriolis C(q,v) matrix such that the Coriolis effects are "
147 "given by c(q,v) = C(q,v)v")
148 .ADD_DATA_PROPERTY(g, "Vector of generalized gravity (dim model.nv).")
149 .ADD_DATA_PROPERTY(Fcrb, "Spatial forces set, used in CRBA")
150 .ADD_DATA_PROPERTY(lastChild, "Index of the last child (for CRBA)")
151 .ADD_DATA_PROPERTY(nvSubtree, "Dimension of the subtree motion space (for CRBA)")
152 .ADD_DATA_PROPERTY(U, "Joint Inertia square root (upper triangle)")
153 .ADD_DATA_PROPERTY(D, "Diagonal of UDUT inertia decomposition")
154 .ADD_DATA_PROPERTY(
155 supports_fromRow,
156 "Each element of this vector corresponds to the ordered list of indexes "
157 "belonging to the supporting tree of the given index at the row level. "
158 "It may be helpful to retrieve the sparsity pattern through it.")
159 .ADD_DATA_PROPERTY(start_idx_v_fromRow, "Starting index of the Joint motion subspace")
160 .ADD_DATA_PROPERTY(end_idx_v_fromRow, "End index of the Joint motion subspace")
161 .ADD_DATA_PROPERTY(parents_fromRow, "First previous non-zero row in M (used in Cholesky)")
162 .ADD_DATA_PROPERTY(
163 mimic_parents_fromRow,
164 "First previous non-zero row belonging to a mimic joint in M (used in Jacobian).")
165 .ADD_DATA_PROPERTY(
166 non_mimic_parents_fromRow,
167 "First previous non-zero row belonging to a non mimic joint in M (used in Jacobian).")
168 .ADD_DATA_PROPERTY(
169 idx_vExtended_to_idx_v_fromRow,
170 "Extended model mapping of the joint rows "
171 "(idx_vExtended_to_idx_v_fromRow[idx_vExtended] = idx_v)")
172 .ADD_DATA_PROPERTY(
173 nvSubtree_fromRow, "Subtree of the current row index (used in Cholesky)")
174 .ADD_DATA_PROPERTY(
175 mimic_subtree_joint, "When mimic joints are in the tree, this will store the index of "
176 "the first non mimic child of each mimic joint. (useful in crba)")
177 .ADD_DATA_PROPERTY(J, "Jacobian of joint placement")
178 .ADD_DATA_PROPERTY(dJ, "Time variation of the Jacobian of joint placement (data.J).")
179 .ADD_DATA_PROPERTY(iMf, "Body placement wrt to algorithm end effector.")
180
181 .ADD_DATA_PROPERTY(Ivx, "Right variation of the inertia matrix.")
182 .ADD_DATA_PROPERTY(vxI, "Left variation of the inertia matrix.")
183 .ADD_DATA_PROPERTY(
184 B, "Combined variations of the inertia matrix consistent with Christoffel symbols.")
185
186 .ADD_DATA_PROPERTY(
187 Ag, "Centroidal matrix which maps from joint velocity to the centroidal momentum.")
188 .ADD_DATA_PROPERTY(dAg, "Time derivative of the centroidal momentum matrix Ag.")
189 .ADD_DATA_PROPERTY(
190 hg, "Centroidal momentum (expressed in the frame centered at the CoM "
191 "and aligned with the world frame).")
192 .ADD_DATA_PROPERTY(
193 dhg, "Centroidal momentum time derivative (expressed in the frame "
194 "centered at the CoM and aligned with the world frame).")
195 .ADD_DATA_PROPERTY(Ig, "Centroidal Composite Rigid Body Inertia.")
196
197 .ADD_DATA_PROPERTY(com, "CoM position of the subtree starting at joint index i.")
198 .ADD_DATA_PROPERTY(vcom, "CoM velocity of the subtree starting at joint index i.")
199 .ADD_DATA_PROPERTY(acom, "CoM acceleration of the subtree starting at joint index i.")
200 .ADD_DATA_PROPERTY(mass, "Mass of the subtree starting at joint index i.")
201 .ADD_DATA_PROPERTY(Jcom, "Jacobian of center of mass.")
202
203 .ADD_DATA_PROPERTY(
204 dAdq,
205 "Variation of the spatial acceleration set with respect to the joint configuration.")
206 .ADD_DATA_PROPERTY(
207 dAdv, "Variation of the spatial acceleration set with respect to the joint velocity.")
208 .ADD_DATA_PROPERTY(
209 dHdq, "Variation of the spatial momenta set with respect to the joint configuration.")
210 .ADD_DATA_PROPERTY(
211 dFdq, "Variation of the force set with respect to the joint configuration.")
212 .ADD_DATA_PROPERTY(dFdv, "Variation of the force set with respect to the joint velocity.")
213 .ADD_DATA_PROPERTY(
214 dFda, "Variation of the force set with respect to the joint acceleration.")
215
216 .ADD_DATA_PROPERTY(
217 dtau_dq, "Partial derivative of the joint torque vector with respect "
218 "to the joint configuration.")
219 .ADD_DATA_PROPERTY(
220 dtau_dv,
221 "Partial derivative of the joint torque vector with respect to the joint velocity.")
222 .ADD_DATA_PROPERTY(
223 ddq_dq, "Partial derivative of the joint acceleration vector with "
224 "respect to the joint configuration.")
225 .ADD_DATA_PROPERTY(
226 ddq_dv, "Partial derivative of the joint acceleration vector with "
227 "respect to the joint velocity.")
228 .ADD_DATA_PROPERTY(
229 ddq_dtau,
230 "Partial derivative of the joint acceleration vector with respect to the joint torque.")
231 .ADD_DATA_PROPERTY(
232 dvc_dq, "Partial derivative of the constraint velocity vector with "
233 "respect to the joint configuration.")
234
235 .ADD_DATA_PROPERTY(
236 dac_dq, "Partial derivative of the contact acceleration vector with "
237 "respect to the joint configuration.")
238 .ADD_DATA_PROPERTY(
239 dac_dv, "Partial derivative of the contact acceleration vector vector "
240 "with respect to the joint velocity.")
241 .ADD_DATA_PROPERTY(
242 dac_da, "Partial derivative of the contact acceleration vector vector "
243 "with respect to the joint acceleration.")
244
245 .ADD_DATA_PROPERTY(osim, "Operational space inertia matrix.")
246
247 .ADD_DATA_PROPERTY_READONLY_BYVALUE(
248 dlambda_dq, "Partial derivative of the contact force vector with "
249 "respect to the joint configuration.")
250 .ADD_DATA_PROPERTY_READONLY_BYVALUE(
251 dlambda_dv,
252 "Partial derivative of the contact force vector with respect to the joint velocity.")
253 .ADD_DATA_PROPERTY_READONLY_BYVALUE(
254 dlambda_dtau,
255 "Partial derivative of the contact force vector with respect to the torque.")
256 .ADD_DATA_PROPERTY(
257 kinetic_energy, "Kinetic energy in [J] computed by computeKineticEnergy")
258 .ADD_DATA_PROPERTY(
259 potential_energy, "Potential energy in [J] computed by computePotentialEnergy")
260 .ADD_DATA_PROPERTY(
261 mechanical_energy,
262 "Mechanical energy in [J] of the system computed by computeMechanicalEnergy")
263
264 .ADD_DATA_PROPERTY(lambda_c, "Lagrange Multipliers linked to contact forces")
265 .ADD_DATA_PROPERTY(impulse_c, "Lagrange Multipliers linked to contact impulses")
266 .ADD_DATA_PROPERTY(contact_chol, "Contact Cholesky decomposition.")
267 .ADD_DATA_PROPERTY(
268 primal_dual_contact_solution,
269 "Right hand side vector when solving the contact dynamics KKT problem.")
270 .ADD_DATA_PROPERTY(
271 lambda_c_prox, "Proximal Lagrange Multipliers used in the computation "
272 "of the Forward Dynamics computations.")
273 .ADD_DATA_PROPERTY(primal_rhs_contact, "Primal RHS in contact dynamic equations.")
274
275 .ADD_DATA_PROPERTY(dq_after, "Generalized velocity after the impact.")
276 .ADD_DATA_PROPERTY(staticRegressor, "Static regressor.")
277 .ADD_DATA_PROPERTY(jointTorqueRegressor, "Joint torque regressor.")
278 .ADD_DATA_PROPERTY(kineticEnergyRegressor, "Kinetic energy regressor.")
279 .ADD_DATA_PROPERTY(potentialEnergyRegressor, "Potential energy regressor.")
280
281#ifndef PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
282 .def(bp::self == bp::self)
283 .def(bp::self != bp::self)
284#endif
285 ;
286
287 bp::register_ptr_to_python<std::shared_ptr<Data>>();
288 }
289
290 /* --- Expose --------------------------------------------------------- */
291 static void expose()
292 {
293 bp::class_<Data>(
294 "Data",
295 "Articulated rigid body data related to a Model.\n"
296 "It contains all the data that can be modified by the Pinocchio algorithms.",
297 bp::no_init)
298 .def(DataPythonVisitor())
299 .def(CopyableVisitor<Data>())
300#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
301 .def(SerializableVisitor<Data>())
302 .def_pickle(PickleData<Data>())
303#endif
304 ;
305
306 typedef PINOCCHIO_ALIGNED_STD_VECTOR(Vector3) StdVec_Vector3;
307 typedef PINOCCHIO_ALIGNED_STD_VECTOR(Matrix6x) StdVec_Matrix6x;
308 typedef PINOCCHIO_ALIGNED_STD_VECTOR(Matrix6) StdVec_Matrix6;
309
310 StdVectorPythonVisitor<std::vector<std::vector<int>>>::expose("StdVec_StdVec_Int");
311 StdAlignedVectorPythonVisitor<Vector3, false>::expose(
312 "StdVec_Vector3",
313 eigenpy::details::overload_base_get_item_for_std_vector<StdVec_Vector3>());
314
315 StdAlignedVectorPythonVisitor<Matrix6x, false>::expose(
316 "StdVec_Matrix6x",
317 eigenpy::details::overload_base_get_item_for_std_vector<StdVec_Matrix6x>());
318 StdAlignedVectorPythonVisitor<Matrix6, false>::expose(
319 "StdVec_Matrix6",
320 eigenpy::details::overload_base_get_item_for_std_vector<StdVec_Matrix6>());
321 StdVectorPythonVisitor<std::vector<int>, true>::expose("StdVec_int");
322#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
323 serialize<typename StdAlignedVectorPythonVisitor<Vector3, false>::vector_type>();
324 serialize<typename StdAlignedVectorPythonVisitor<Matrix6x, false>::vector_type>();
325#endif
326 serialize<std::vector<int>>();
327 }
328 };
329
330 } // namespace python
331} // namespace pinocchio
332
333#undef ADD_DATA_PROPERTY
334#undef ADD_DATA_PROPERTY_READONLY
335#undef ADD_DATA_PROPERTY_READONLY_BYVALUE
336
337#endif // ifndef __pinocchio_python_multibody_data_hpp__
Main pinocchio namespace.
Definition treeview.dox:11
Eigen::Matrix< Scalar, 6, Eigen::Dynamic, Options > Matrix6x
Definition data.hpp:92
Eigen::Matrix< Scalar, 3, Eigen::Dynamic, Options > Matrix3x
Definition data.hpp:94
std::string saveToString() const
Saves a Derived object to a string.
void loadFromString(const std::string &str)
Loads a Derived object from a string.