Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/multibody/contacts/contact-6d.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 31 | 32 | 96.9% |
Branches: | 60 | 120 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh | ||
5 | // Heriot-Watt University | ||
6 | // Copyright note valid unless otherwise stated in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #include "crocoddyl/multibody/contacts/contact-6d.hpp" | ||
11 | |||
12 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
13 | |||
14 | namespace crocoddyl { | ||
15 | namespace python { | ||
16 | |||
17 | template <typename Model> | ||
18 | struct ContactModel6DVisitor | ||
19 | : public bp::def_visitor<ContactModel6DVisitor<Model>> { | ||
20 | typedef typename Model::StateMultibody State; | ||
21 | typedef typename Model::SE3 SE3; | ||
22 | typedef typename Model::Vector2s Vector2s; | ||
23 | template <class PyClass> | ||
24 | 40 | void visit(PyClass& cl) const { | |
25 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
40 | cl.def(bp::init<std::shared_ptr<State>, pinocchio::FrameIndex, SE3, |
26 | pinocchio::ReferenceFrame, bp::optional<Vector2s>>( | ||
27 | bp::args("self", "state", "id", "pref", "type", "gains"), | ||
28 | "Initialize the contact model.\n\n" | ||
29 | ":param state: state of the multibody system\n" | ||
30 | ":param id: reference frame id of the contact\n" | ||
31 | ":param pref: contact placement used for the Baumgarte " | ||
32 | "stabilization\n" | ||
33 | ":param type: type of contact\n" | ||
34 | ":param gains: gains of the contact model (default " | ||
35 | "np.matrix([0.,0.]))")) | ||
36 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("calc", &Model::calc, bp::args("self", "data", "x"), |
37 | "Compute the 6D contact Jacobian and drift.\n\n" | ||
38 | "The rigid contact model throught acceleration-base holonomic " | ||
39 | "constraint of the contact frame placement.\n" | ||
40 | ":param data: contact data\n" | ||
41 | ":param x: state point (dim. state.nx)") | ||
42 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calcDiff", &Model::calcDiff, bp::args("self", "data", "x"), |
43 | "Compute the derivatives of the 6D contact holonomic " | ||
44 | "constraint.\n\n" | ||
45 | "The rigid contact model throught acceleration-base holonomic " | ||
46 | "constraint of the contact frame placement. It assumes that calc " | ||
47 | "has been run first.\n" | ||
48 | ":param data: cost data\n" | ||
49 | ":param x: state point (dim. state.nx)") | ||
50 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("updateForce", &Model::updateForce, |
51 | bp::args("self", "data", "force"), | ||
52 | "Convert the Lagrangian into a stack of spatial forces.\n\n" | ||
53 | ":param data: cost data\n" | ||
54 | ":param force: force vector (dimension 6)") | ||
55 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("createData", &Model::createData, |
56 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
57 | bp::args("self", "data"), | ||
58 | "Create the 6D contact data.\n\n" | ||
59 | "Each contact model has its own data that needs to be allocated. " | ||
60 | "This function returns the allocated data for a predefined cost.\n" | ||
61 | ":param data: Pinocchio data\n" | ||
62 | ":return contact data.") | ||
63 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("reference", |
64 | bp::make_function(&Model::get_reference, | ||
65 | 40 | bp::return_internal_reference<>()), | |
66 | &Model::set_reference, "reference contact placement") | ||
67 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .add_property( |
68 | "gains", | ||
69 | bp::make_function(&Model::get_gains, | ||
70 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
71 | "contact gains"); | ||
72 | 40 | } | |
73 | }; | ||
74 | |||
75 | template <typename Data> | ||
76 | struct ContactData6DVisitor | ||
77 | : public bp::def_visitor<ContactData6DVisitor<Data>> { | ||
78 | template <class PyClass> | ||
79 | 40 | void visit(PyClass& cl) const { | |
80 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
81 | "rMf", | ||
82 | bp::make_getter(&Data::jMf, | ||
83 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
84 | "error frame placement of the contact frame") | ||
85 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
86 | "v", | ||
87 | bp::make_getter(&Data::v, | ||
88 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
89 | "spatial velocity of the contact body") | ||
90 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
91 | "a0_local", | ||
92 | bp::make_getter(&Data::a0_local, | ||
93 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
94 | "desired local contact acceleration") | ||
95 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("v_partial_dq", |
96 | bp::make_getter(&Data::v_partial_dq, | ||
97 | 40 | bp::return_internal_reference<>()), | |
98 | "Jacobian of the spatial body velocity") | ||
99 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("a_partial_dq", |
100 | bp::make_getter(&Data::a_partial_dq, | ||
101 | 40 | bp::return_internal_reference<>()), | |
102 | "Jacobian of the spatial body acceleration") | ||
103 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("a_partial_dv", |
104 | bp::make_getter(&Data::a_partial_dv, | ||
105 | 40 | bp::return_internal_reference<>()), | |
106 | "Jacobian of the spatial body acceleration") | ||
107 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("a_partial_da", |
108 | bp::make_getter(&Data::a_partial_da, | ||
109 | 40 | bp::return_internal_reference<>()), | |
110 | "Jacobian of the spatial body acceleration"); | ||
111 | 40 | } | |
112 | }; | ||
113 | |||
114 | #define CROCODDYL_CONTACT_MODEL_6D_PYTHON_BINDINGS(Scalar) \ | ||
115 | typedef ContactModel6DTpl<Scalar> Model; \ | ||
116 | typedef ContactModelAbstractTpl<Scalar> ModelBase; \ | ||
117 | typedef Model::StateMultibody State; \ | ||
118 | typedef Model::SE3 SE3; \ | ||
119 | typedef Model::Vector2s Vector2s; \ | ||
120 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
121 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
122 | "ContactModel6D", \ | ||
123 | "Rigid 6D contact model.\n\n" \ | ||
124 | "It defines a rigid 6D contact models based on acceleration-based " \ | ||
125 | "holonomic constraints. The calc and calcDiff functions compute the " \ | ||
126 | "contact Jacobian and drift (holonomic constraint) or the derivatives " \ | ||
127 | "of the holonomic constraint, respectively.", \ | ||
128 | bp::init<std::shared_ptr<State>, pinocchio::FrameIndex, SE3, \ | ||
129 | pinocchio::ReferenceFrame, std::size_t, \ | ||
130 | bp::optional<Vector2s>>( \ | ||
131 | bp::args("self", "state", "id", "pref", "type", "nu", "gains"), \ | ||
132 | "Initialize the contact model.\n\n" \ | ||
133 | ":param state: state of the multibody system\n" \ | ||
134 | ":param id: reference frame id of the contact\n" \ | ||
135 | ":param pref: contact placement used for the Baumgarte " \ | ||
136 | "stabilization\n" \ | ||
137 | ":param type: type of contact\n" \ | ||
138 | ":param nu: dimension of control vector\n" \ | ||
139 | ":param gains: gains of the contact model (default " \ | ||
140 | "np.matrix([0.,0.]))")) \ | ||
141 | .def(ContactModel6DVisitor<Model>()) \ | ||
142 | .def(CastVisitor<Model>()) \ | ||
143 | .def(PrintableVisitor<Model>()) \ | ||
144 | .def(CopyableVisitor<Model>()); | ||
145 | |||
146 | #define CROCODDYL_CONTACT_DATA_6D_PYTHON_BINDINGS(Scalar) \ | ||
147 | typedef ContactData6DTpl<Scalar> Data; \ | ||
148 | typedef ContactDataAbstractTpl<Scalar> DataBase; \ | ||
149 | typedef ContactModel6DTpl<Scalar> Model; \ | ||
150 | typedef pinocchio::DataTpl<Scalar> PinocchioData; \ | ||
151 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
152 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
153 | "ContactData6D", "Data for 6D contact.\n\n", \ | ||
154 | bp::init<Model*, PinocchioData*>( \ | ||
155 | bp::args("self", "model", "data"), \ | ||
156 | "Create 6D contact data.\n\n" \ | ||
157 | ":param model: 6D contact model\n" \ | ||
158 | ":param data: Pinocchio data")[bp::with_custodian_and_ward< \ | ||
159 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
160 | .def(ContactData6DVisitor<Data>()) \ | ||
161 | .def(CopyableVisitor<Data>()); | ||
162 | |||
163 | 10 | void exposeContact6D() { | |
164 | #pragma GCC diagnostic push // TODO: Remove once the deprecated FrameXX has | ||
165 | // been removed in a future release | ||
166 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
167 | |||
168 |
17/34✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 10 times.
✗ Branch 31 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
✓ Branch 53 taken 10 times.
✗ Branch 54 not taken.
✓ Branch 56 taken 10 times.
✗ Branch 57 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_CONTACT_MODEL_6D_PYTHON_BINDINGS) |
169 | |||
170 | #pragma GCC diagnostic pop | ||
171 | |||
172 |
15/30✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_CONTACT_DATA_6D_PYTHON_BINDINGS) |
173 | 10 | } | |
174 | |||
175 | } // namespace python | ||
176 | } // namespace crocoddyl | ||
177 |