pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
contact-info.hpp
1//
2// Copyright (c) 2019-2022 INRIA
3//
4
5#pragma once
6
7#include <eigenpy/memory.hpp>
8
9#include "pinocchio/constraints.hpp"
10
11#include "pinocchio/bindings/python/utils/cast.hpp"
12#include "pinocchio/bindings/python/utils/macros.hpp"
13#include "pinocchio/bindings/python/utils/comparable.hpp"
14#include "pinocchio/bindings/python/utils/std-vector.hpp"
15#include "pinocchio/bindings/python/algorithm/constraints/baumgarte-corrector-parameters.hpp"
16#include "pinocchio/bindings/python/utils/eigen.hpp"
17
18#include "pinocchio/bindings/python/utils/model-checker.hpp"
19
20namespace pinocchio
21{
22 namespace python
23 {
24 namespace bp = boost::python;
25
26 template<typename RigidConstraintModel>
28 : public boost::python::def_visitor<RigidConstraintModelPythonVisitor<RigidConstraintModel>>
29 {
30 typedef typename RigidConstraintModel::Scalar Scalar;
31 typedef typename RigidConstraintModel::SE3 SE3;
32 typedef RigidConstraintModel Self;
33 typedef typename RigidConstraintModel::ContactData ContactData;
34 typedef
35 typename RigidConstraintModel::BaumgarteCorrectorParameters BaumgarteCorrectorParameters;
36
37 typedef ModelTpl<Scalar, RigidConstraintModel::Options, JointCollectionDefaultTpl> Model;
38 typedef DataTpl<Scalar, RigidConstraintModel::Options, JointCollectionDefaultTpl> Data;
39
40 public:
41 template<class PyClass>
42 void visit(PyClass & cl) const
43 {
44 cl
45 // .def(bp::init<>(bp::arg("self"),
46 // "Default constructor."))
47 .def(
48 bp::init<
49 ContactType, const Model &, JointIndex, const SE3 &, JointIndex, const SE3 &,
50 bp::optional<ReferenceFrame>>(
51 (bp::arg("self"), bp::arg("contact_type"), bp::arg("model"), bp::arg("joint1_id"),
52 bp::arg("joint1_placement"), bp::arg("joint2_id"), bp::arg("joint2_placement"),
53 bp::arg("reference_frame")),
54 "Constructor from a given ContactType, joint index and placement for the two joints "
55 "implied in the constraint."))
56 .def(
57 bp::init<
58 ContactType, const Model &, JointIndex, const SE3 &, bp::optional<ReferenceFrame>>(
59 (bp::arg("self"), bp::arg("contact_type"), bp::arg("model"), bp::arg("joint1_id"),
60 bp::arg("joint1_placement"), bp::arg("reference_frame")),
61 "Constructor from a given ContactType, joint index and placement only for the first "
62 "joint implied in the constraint."))
63 .def(
64 bp::init<ContactType, const Model &, JointIndex, bp::optional<ReferenceFrame>>(
65 (bp::arg("self"), bp::arg("contact_type"), bp::arg("model"), bp::arg("joint1_id"),
66 bp::arg("reference_frame")),
67 "Constructor from a given ContactType and joint index. The base joint is taken as 0 "
68 "in "
69 "the constraint."))
70 .PINOCCHIO_ADD_PROPERTY(Self, name, "Name of the contact.")
71 .PINOCCHIO_ADD_PROPERTY(Self, type, "Type of the contact.")
72 .PINOCCHIO_ADD_PROPERTY(Self, joint1_id, "Index of first parent joint in the model tree.")
73 .PINOCCHIO_ADD_PROPERTY(
74 Self, joint2_id, "Index of second parent joint in the model tree.")
75 .PINOCCHIO_ADD_PROPERTY(
76 Self, joint1_placement, "Relative placement with respect to the frame of joint1.")
77 .PINOCCHIO_ADD_PROPERTY(
78 Self, joint2_placement, "Relative placement with respect to the frame of joint2.")
79 .PINOCCHIO_ADD_PROPERTY(
80 Self, reference_frame,
81 "Reference frame where the constraint is expressed (WORLD, "
82 "LOCAL_WORLD_ALIGNED or LOCAL).")
83 .PINOCCHIO_ADD_PROPERTY(Self, desired_contact_placement, "Desired contact placement.")
84 .PINOCCHIO_ADD_PROPERTY(
85 Self, desired_contact_velocity, "Desired contact spatial velocity.")
86 .PINOCCHIO_ADD_PROPERTY(
87 Self, desired_contact_acceleration, "Desired contact spatial acceleration.")
88 .def(
89 "createData", &RigidConstraintModelPythonVisitor::createData,
90 "Create a Data object for the given model.")
91 .def(ComparableVisitor<Self, pinocchio::is_floating_point<Scalar>::value>())
92 .def(
93 "calc", (void (Self::*)(const Model &, const Data &, ContactData &) const) & Self::calc,
94 bp::args("self", "model", "data", "constraint_data"))
95 .def("jacobian", &jacobian, bp::args("self", "model", "data", "constraint_data"))
96 .def(
97 "residualSize", &residualSize,
98 (bp::arg("self"), bp::arg("sel") = ConstraintSelectionType::CURRENT),
99 "Constraint size for the selection.")
100 .def(
101 "setCompliance", &setCompliance,
102 (bp::arg("self"), bp::arg("vec"), bp::arg("sel") = ConstraintSelectionType::CURRENT),
103 "Set the compliance value for the selected constraint.")
104 .def(
105 "retrieveCompliance", &retrieveCompliance,
106 (bp::arg("self"), bp::arg("sel") = ConstraintSelectionType::CURRENT),
107 "Retrieve the compliance value for the selected constraint.")
108 .def(
109 "setBaumgarteCorrectorParameters", &setBaumgarteCorrectorParameters,
110 (bp::arg("self"), bp::arg("bp"), bp::arg("sel") = ConstraintSelectionType::CURRENT),
111 "Set the Baumgarte parameters.");
112 cl.add_property(
113 "m_baumgarte_parameters",
114 bp::make_function( //
115 +[](Self & self) -> BaumgarteCorrectorParameters & {
116 return self.baumgarte_corrector_parameters();
117 },
118 bp::return_internal_reference<>()),
119 bp::make_function( //
120 +[](Self & self, const BaumgarteCorrectorParameters & copy) {
121 self.baumgarte_corrector_parameters() = copy;
122 },
123 bp::return_internal_reference<>()),
124 "Baumgarte parameters associated with the constraint.");
125 }
126
127 static context::RigidConstraintModel convertToRigidConstraintModel_point(
128 const context::Model & model,
129 const context::PointAnchorConstraintModel & constraint,
130 const ReferenceFrame reference_frame)
131 {
132 return convertToRigidConstraintModel(model, constraint, reference_frame);
133 }
134
135 static context::RigidConstraintModel convertToRigidConstraintModel_frame(
136 const context::Model & model,
137 const context::FrameAnchorConstraintModel & constraint,
138 const ReferenceFrame reference_frame)
139 {
140 return convertToRigidConstraintModel(model, constraint, reference_frame);
141 }
142
143 static void expose()
144 {
145 bp::class_<RigidConstraintModel>(
146 "RigidConstraintModel", "Rigid contact model for contact dynamic algorithms.",
147 bp::no_init)
150 .def(
152 RigidConstraintModel, ::pinocchio::RigidConstraintModel>());
153
154 bp::def(
155 "convertToRigidConstraintModel", convertToRigidConstraintModel_point,
156 (bp::arg("model"), bp::arg("constraint"),
157 bp::arg("reference_frame") = ReferenceFrame::LOCAL),
158 "Convert a PointAnchorConstraintModel to a RigidConstraintModel with contact type "
159 "CONTACT_3D.\n\n"
160 "The kinematic structure (joint IDs and placements) is preserved. The 3D desired "
161 "offset is placed in the translation of desired_contact_placement, and the 3D desired "
162 "velocity/acceleration are placed in the linear parts of the corresponding Motion "
163 "fields.",
165
166 bp::def(
167 "convertToRigidConstraintModel", convertToRigidConstraintModel_frame,
168 (bp::arg("model"), bp::arg("constraint"),
169 bp::arg("reference_frame") = ReferenceFrame::LOCAL),
170 "Convert a FrameAnchorConstraintModel to a RigidConstraintModel with contact type "
171 "CONTACT_6D.\n\n"
172 "The kinematic structure (joint IDs and placements) is preserved. The 6D desired "
173 "offset (ordered [linear; angular]) is mapped to desired_contact_placement via exp6, "
174 "and the 6D desired velocity/acceleration vectors are mapped directly to Motion fields.",
176 }
177
178 static ContactData createData(const Self & self)
179 {
180 return ContactData(self);
181 }
182
183 static context::MatrixXs jacobian(
184 const Self & self, const Model & model, const Data & data, ContactData & constraint_data)
185 {
186 context::MatrixXs res(self.residualSize(), model.nv);
187 self.jacobian(model, data, constraint_data, res);
188 return res;
189 }
190
191 static int residualSize(const Self & self, ConstraintSelectionType sel)
192 {
193 switch (sel)
194 {
195 case ConstraintSelectionType::CURRENT:
196 return self.residualSize(CurrentSelection());
197 case ConstraintSelectionType::MAXIMAL:
198 return self.residualSize(MaximalSelection());
199 default:
200 PINOCCHIO_UNREACHABLE();
201 }
202 }
203
204 static void
205 setCompliance(Self & self, context::VectorXs & vector, ConstraintSelectionType sel)
206 {
207 switch (sel)
208 {
209 case ConstraintSelectionType::CURRENT:
210 return self.setCompliance(vector, CurrentSelection());
211 case ConstraintSelectionType::MAXIMAL:
212 return self.setCompliance(vector, MaximalSelection());
213 }
214 }
215
216 static context::VectorXs retrieveCompliance(const Self & self, ConstraintSelectionType sel)
217 {
218 switch (sel)
219 {
220 case ConstraintSelectionType::CURRENT: {
221 context::VectorXs res(self.residualSize(CurrentSelection()));
222 self.retrieveCompliance(res, CurrentSelection());
223 return res;
224 }
225 case ConstraintSelectionType::MAXIMAL: {
226 context::VectorXs res(self.residualSize(MaximalSelection()));
227 self.retrieveCompliance(res, MaximalSelection());
228 return res;
229 }
230 default:
231 PINOCCHIO_UNREACHABLE();
232 }
233 }
234
235 static void setBaumgarteCorrectorParameters(
236 Self & self, const BaumgarteCorrectorParameters & bp, ConstraintSelectionType sel)
237 {
238 switch (sel)
239 {
240 case ConstraintSelectionType::CURRENT:
241 return self.setBaumgarteCorrectorParameters(bp, CurrentSelection());
242 case ConstraintSelectionType::MAXIMAL:
243 return self.setBaumgarteCorrectorParameters(bp, MaximalSelection());
244 }
245 }
246 };
247
248 template<typename RigidConstraintData>
250 : public boost::python::def_visitor<RigidConstraintDataPythonVisitor<RigidConstraintData>>
251 {
252 typedef typename RigidConstraintData::Scalar Scalar;
253 typedef RigidConstraintData Self;
254 typedef typename RigidConstraintData::ContactModel ContactModel;
255
256 public:
257 template<class PyClass>
258 void visit(PyClass & cl) const
259 {
260 cl.def(
261 bp::init<const ContactModel &>(
262 bp::args("self", "contact_model"), "Default constructor."))
263
264 .PINOCCHIO_ADD_PROPERTY(Self, contact_force, "Constraint force.")
265 .PINOCCHIO_ADD_PROPERTY(
266 Self, oMc1, "Placement of the constraint frame 1 with respect to the WORLD frame.")
267 .PINOCCHIO_ADD_PROPERTY(
268 Self, oMc2, "Placement of the constraint frame 2 with respect to the WORLD frame.")
269 .PINOCCHIO_ADD_PROPERTY(Self, c1Mc2, "Relative displacement between the two frames.")
270 .PINOCCHIO_ADD_PROPERTY(
271 Self, contact_placement_error,
272 "Current contact placement error between the two contact Frames.\n"
273 "This corresponds to the relative placement between the two contact Frames seen as a "
274 "Motion error.")
275 .PINOCCHIO_ADD_PROPERTY(
276 Self, contact1_velocity, "Current contact Spatial velocity of the constraint 1.")
277 .PINOCCHIO_ADD_PROPERTY(
278 Self, contact2_velocity, "Current contact Spatial velocity of the constraint 2.")
279 .PINOCCHIO_ADD_PROPERTY(
280 Self, contact_velocity_error,
281 "Current contact Spatial velocity error between the two contact Frames.\n"
282 "This corresponds to the relative velocity between the two contact Frames.")
283 .PINOCCHIO_ADD_PROPERTY(
284 Self, contact_acceleration, "Current contact Spatial acceleration.")
285 .PINOCCHIO_ADD_PROPERTY(
286 Self, contact_acceleration_desired, "Desired contact acceleration.")
287 .PINOCCHIO_ADD_PROPERTY(
288 Self, contact_acceleration_error,
289 "Current contact spatial error (due to the integration step).")
290 .PINOCCHIO_ADD_PROPERTY(
291 Self, contact1_acceleration_drift,
292 "Current contact drift acceleration (acceleration only due to the Coriolis and "
293 "centrifugal effects) of the contact frame 1.")
294 .PINOCCHIO_ADD_PROPERTY(
295 Self, contact2_acceleration_drift,
296 "Current contact drift acceleration (acceleration only due to the Coriolis and "
297 "centrifugal effects) of the contact frame 2.")
298 .PINOCCHIO_ADD_PROPERTY(
299 Self, contact_acceleration_deviation,
300 "Contact deviation from the reference acceleration (a.k.a the error).")
301
302 .PINOCCHIO_ADD_PROPERTY(
303 Self, extended_motion_propagators_joint1,
304 "Extended force/motion propagators for joint 1.")
305 .PINOCCHIO_ADD_PROPERTY(
306 Self, lambdas_joint1, "Extended force/motion propagators for joint 1.")
307 .PINOCCHIO_ADD_PROPERTY(
308 Self, extended_motion_propagators_joint2,
309 "Extended force/motion propagators for joint 2.")
310
311 .def(ComparableVisitor<Self, pinocchio::is_floating_point<Scalar>::value>());
312 }
313
314 static void expose()
315 {
316 bp::class_<RigidConstraintData>(
317 "RigidConstraintData",
318 "Rigid constraint data associated to a "
319 "RigidConstraintModel for contact dynamic algorithms.",
320 bp::no_init)
322
323 typedef typename RigidConstraintData::VectorOfMatrix6 VectorOfMatrix6;
324 StdVectorPythonVisitor<VectorOfMatrix6, true>::expose("StdVec_Matrix6_");
325 }
326 };
327
328 } // namespace python
329} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11
void copy(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const DataTpl< Scalar, Options, JointCollectionTpl > &origin, DataTpl< Scalar, Options, JointCollectionTpl > &dest, KinematicLevel kinematic_level)
Copy part of the data from origin to dest. Template parameter can be used to select at which differen...
Add the Python method cast.
Definition cast.hpp:23
Add the Python method == and != to allow a comparison of this.