| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/multibody/contacts/contact-3d.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 39 | 40 | 97.5% |
| Branches: | 73 | 146 | 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-3d.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
| 13 | |||
| 14 | namespace crocoddyl { | ||
| 15 | namespace python { | ||
| 16 | |||
| 17 | template <typename Model> | ||
| 18 | struct ContactModel3DVisitor | ||
| 19 | : public bp::def_visitor<ContactModel3DVisitor<Model>> { | ||
| 20 | typedef typename Model::StateMultibody State; | ||
| 21 | typedef typename Model::Vector3s Vector3s; | ||
| 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, Vector3s, |
| 26 | pinocchio::ReferenceFrame, bp::optional<Vector2s>>( | ||
| 27 | bp::args("self", "state", "id", "xref", "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 xref: contact position 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 3d 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 3d 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 force into a stack of spatial forces.\n\n" | ||
| 53 | ":param data: cost data\n" | ||
| 54 | ":param force: force vector (dimension 3)") | ||
| 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 3D 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 translation") | ||
| 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 ContactData3DVisitor | ||
| 77 | : public bp::def_visitor<ContactData3DVisitor<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 | "v", | ||
| 82 | bp::make_getter(&Data::v, | ||
| 83 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 84 | "spatial velocity of the contact body") | ||
| 85 |
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( |
| 86 | "a0_local", | ||
| 87 | 40 | bp::make_getter(&Data::a0_local, bp::return_internal_reference<>()), | |
| 88 | bp::make_setter(&Data::a0_local), | ||
| 89 | "desired local contact acceleration") | ||
| 90 |
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( |
| 91 | 40 | "dp", bp::make_getter(&Data::dp, bp::return_internal_reference<>()), | |
| 92 | bp::make_setter(&Data::dp), | ||
| 93 | "Translation error computed for the Baumgarte regularization term") | ||
| 94 |
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( |
| 95 | "dp_local", | ||
| 96 | 40 | bp::make_getter(&Data::dp_local, bp::return_internal_reference<>()), | |
| 97 | bp::make_setter(&Data::dp_local), | ||
| 98 | "local translation error computed for the Baumgarte " | ||
| 99 | "regularization term") | ||
| 100 |
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( |
| 101 | "f_local", | ||
| 102 | 40 | bp::make_getter(&Data::f_local, bp::return_internal_reference<>()), | |
| 103 | bp::make_setter(&Data::f_local), | ||
| 104 | "spatial contact force in local coordinates") | ||
| 105 |
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("da0_local_dx", |
| 106 | bp::make_getter(&Data::da0_local_dx, | ||
| 107 | 40 | bp::return_internal_reference<>()), | |
| 108 | bp::make_setter(&Data::da0_local_dx), | ||
| 109 | "Jacobian of the desired local contact acceleration") | ||
| 110 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 111 | "fJf", | ||
| 112 | 40 | bp::make_getter(&Data::fJf, bp::return_internal_reference<>()), | |
| 113 | "local Jacobian of the contact frame") | ||
| 114 |
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", |
| 115 | bp::make_getter(&Data::v_partial_dq, | ||
| 116 | 40 | bp::return_internal_reference<>()), | |
| 117 | "Jacobian of the spatial body velocity") | ||
| 118 |
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", |
| 119 | bp::make_getter(&Data::a_partial_dq, | ||
| 120 | 40 | bp::return_internal_reference<>()), | |
| 121 | "Jacobian of the spatial body acceleration") | ||
| 122 |
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", |
| 123 | bp::make_getter(&Data::a_partial_dv, | ||
| 124 | 40 | bp::return_internal_reference<>()), | |
| 125 | "Jacobian of the spatial body acceleration") | ||
| 126 |
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", |
| 127 | bp::make_getter(&Data::a_partial_da, | ||
| 128 | 40 | bp::return_internal_reference<>()), | |
| 129 | "Jacobian of the spatial body acceleration"); | ||
| 130 | 40 | } | |
| 131 | }; | ||
| 132 | |||
| 133 | #define CROCODDYL_CONTACT_MODEL_3D_PYTHON_BINDINGS(Scalar) \ | ||
| 134 | typedef ContactModel3DTpl<Scalar> Model; \ | ||
| 135 | typedef ContactModelAbstractTpl<Scalar> ModelBase; \ | ||
| 136 | typedef Model::StateMultibody State; \ | ||
| 137 | typedef Model::Vector3s Vector3s; \ | ||
| 138 | typedef Model::Vector2s Vector2s; \ | ||
| 139 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 140 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 141 | "ContactModel3D", \ | ||
| 142 | "Rigid 3D contact model.\n\n" \ | ||
| 143 | "It defines a rigid 3D contact models (point contact) based on " \ | ||
| 144 | "acceleration-based holonomic constraints. The calc and calcDiff " \ | ||
| 145 | "functions compute the contact Jacobian and drift (holonomic " \ | ||
| 146 | "constraint) or the derivatives of the holonomic constraint, " \ | ||
| 147 | "respectively.", \ | ||
| 148 | bp::init<std::shared_ptr<State>, pinocchio::FrameIndex, Vector3s, \ | ||
| 149 | pinocchio::ReferenceFrame, \ | ||
| 150 | bp::optional<std::size_t, Vector2s>>( \ | ||
| 151 | bp::args("self", "state", "id", "xref", "type", "nu", "gains"), \ | ||
| 152 | "Initialize the contact model.\n\n" \ | ||
| 153 | ":param state: state of the multibody system\n" \ | ||
| 154 | ":param id: reference frame id of the contact\n" \ | ||
| 155 | ":param xref: contact position used for the Baumgarte " \ | ||
| 156 | "stabilization\n" \ | ||
| 157 | ":param type: type of contact\n" \ | ||
| 158 | ":param nu: dimension of control vector (default state.nv)\n" \ | ||
| 159 | ":param gains: gains of the contact model (default " \ | ||
| 160 | "np.array([0.,0.]))")) \ | ||
| 161 | .def(ContactModel3DVisitor<Model>()) \ | ||
| 162 | .def(CastVisitor<Model>()) \ | ||
| 163 | .def(PrintableVisitor<Model>()) \ | ||
| 164 | .def(CopyableVisitor<Model>()); | ||
| 165 | |||
| 166 | #define CROCODDYL_CONTACT_DATA_3D_PYTHON_BINDINGS(Scalar) \ | ||
| 167 | typedef ContactData3DTpl<Scalar> Data; \ | ||
| 168 | typedef ContactDataAbstractTpl<Scalar> DataBase; \ | ||
| 169 | typedef ContactModel3DTpl<Scalar> Model; \ | ||
| 170 | typedef pinocchio::DataTpl<Scalar> PinocchioData; \ | ||
| 171 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 172 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 173 | "ContactData3D", "Data for 3D contact.\n\n", \ | ||
| 174 | bp::init<Model*, PinocchioData*>( \ | ||
| 175 | bp::args("self", "model", "data"), \ | ||
| 176 | "Create 3D contact data.\n\n" \ | ||
| 177 | ":param model: 3D contact model\n" \ | ||
| 178 | ":param data: Pinocchio data")[bp::with_custodian_and_ward< \ | ||
| 179 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 180 | .def(ContactData3DVisitor<Data>()) \ | ||
| 181 | .def(CopyableVisitor<Data>()); | ||
| 182 | |||
| 183 | 10 | void exposeContact3D() { | |
| 184 | #pragma GCC diagnostic push // TODO: Remove once the deprecated FrameXX has | ||
| 185 | // been removed in a future release | ||
| 186 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
| 187 | |||
| 188 |
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_3D_PYTHON_BINDINGS) |
| 189 | |||
| 190 | #pragma GCC diagnostic pop | ||
| 191 | |||
| 192 |
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_3D_PYTHON_BINDINGS) |
| 193 | 10 | } | |
| 194 | |||
| 195 | } // namespace python | ||
| 196 | } // namespace crocoddyl | ||
| 197 |