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