| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/multibody/residuals/control-gravity.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 18 | 18 | 100.0% |
| Branches: | 48 | 96 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2020-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/residuals/control-gravity.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
| 13 | #include "python/crocoddyl/utils/copyable.hpp" | ||
| 14 | |||
| 15 | namespace crocoddyl { | ||
| 16 | namespace python { | ||
| 17 | |||
| 18 | template <typename Model> | ||
| 19 | struct ResidualModelControlGravVisitor | ||
| 20 | : public bp::def_visitor<ResidualModelControlGravVisitor<Model>> { | ||
| 21 | typedef typename Model::ResidualDataAbstract Data; | ||
| 22 | typedef typename Model::Base ModelBase; | ||
| 23 | typedef typename Model::StateMultibody State; | ||
| 24 | typedef typename Model::VectorXs VectorXs; | ||
| 25 | template <class PyClass> | ||
| 26 | 40 | void visit(PyClass& cl) const { | |
| 27 |
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>>( |
| 28 | bp::args("self", "state"), | ||
| 29 | "Initialize the control-gravity residual model.\n\n" | ||
| 30 | "The default nu is obtained from state.nv.\n" | ||
| 31 | ":param state: state description")) | ||
| 32 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
| 33 | "calc", | ||
| 34 | static_cast<void (Model::*)( | ||
| 35 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 36 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
| 37 | bp::args("self", "data", "x", "u"), | ||
| 38 | "Compute the control residual.\n\n" | ||
| 39 | ":param data: residual data\n" | ||
| 40 | ":param x: state point (dim. state.nx)\n" | ||
| 41 | ":param u: control input (dim. nu)") | ||
| 42 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calc", |
| 43 | static_cast<void (Model::*)(const std::shared_ptr<Data>&, | ||
| 44 | const Eigen::Ref<const VectorXs>&)>( | ||
| 45 | &Model::calc), | ||
| 46 | bp::args("self", "data", "x")) | ||
| 47 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def( |
| 48 | "calcDiff", | ||
| 49 | static_cast<void (Model::*)( | ||
| 50 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 51 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
| 52 | bp::args("self", "data", "x", "u"), | ||
| 53 | "Compute the derivatives of the control residual.\n\n" | ||
| 54 | ":param data: action data\n" | ||
| 55 | ":param x: state point (dim. state.nx)\n" | ||
| 56 | ":param u: control input (dim. nu)") | ||
| 57 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calcDiff", |
| 58 | static_cast<void (Model::*)(const std::shared_ptr<Data>&, | ||
| 59 | const Eigen::Ref<const VectorXs>&)>( | ||
| 60 | &Model::calcDiff), | ||
| 61 | bp::args("self", "data", "x")) | ||
| 62 |
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 | .def("createData", &Model::createData, |
| 63 | 40 | bp::with_custodian_and_ward_postcall<0, 2>(), | |
| 64 | bp::args("self", "data"), | ||
| 65 | "Create the control residual data.\n\n" | ||
| 66 | "Each residual model has its own data that needs to be allocated. " | ||
| 67 | "This " | ||
| 68 | "function\n" | ||
| 69 | "returns the allocated data for the control gravity residual.\n" | ||
| 70 | ":param data: shared data\n" | ||
| 71 | ":return residual data."); | ||
| 72 | 40 | } | |
| 73 | }; | ||
| 74 | |||
| 75 | template <typename Data> | ||
| 76 | struct ResidualDataControlGravVisitor | ||
| 77 | : public bp::def_visitor<ResidualDataControlGravVisitor<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("pinocchio", bp::make_getter(&Data::pinocchio), |
| 81 | "Pinocchio data used for internal computations") | ||
| 82 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("actuation", |
| 83 | bp::make_getter(&Data::actuation, | ||
| 84 | 40 | bp::return_internal_reference<>()), | |
| 85 | "actuation model"); | ||
| 86 | 40 | } | |
| 87 | }; | ||
| 88 | |||
| 89 | #define CROCODDYL_RESIDUAL_MODEL_CONTROL_GRAV_PYTHON_BINDINGS(Scalar) \ | ||
| 90 | typedef ResidualModelControlGravTpl<Scalar> Model; \ | ||
| 91 | typedef ResidualModelAbstractTpl<Scalar> ModelBase; \ | ||
| 92 | typedef typename Model::StateMultibody State; \ | ||
| 93 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 94 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 95 | "ResidualModelControlGrav", \ | ||
| 96 | "This residual function is defined as r = a(u) - g(q), where a(u) is " \ | ||
| 97 | "the actuated torque; and q, g(q) are the generalized position and " \ | ||
| 98 | "gravity vector, respectively.", \ | ||
| 99 | bp::init<std::shared_ptr<State>, std::size_t>( \ | ||
| 100 | bp::args("self", "state", "nu"), \ | ||
| 101 | "Initialize the control-gravity residual model.\n\n" \ | ||
| 102 | ":param state: state description\n" \ | ||
| 103 | ":param nu: dimension of the control vector")) \ | ||
| 104 | .def(ResidualModelControlGravVisitor<Model>()) \ | ||
| 105 | .def(CastVisitor<Model>()) \ | ||
| 106 | .def(PrintableVisitor<Model>()) \ | ||
| 107 | .def(CopyableVisitor<Model>()); | ||
| 108 | |||
| 109 | #define CROCODDYL_RESIDUAL_DATA_CONTROL_GRAV_PYTHON_BINDINGS(Scalar) \ | ||
| 110 | typedef ResidualDataControlGravTpl<Scalar> Data; \ | ||
| 111 | typedef ResidualDataAbstractTpl<Scalar> DataBase; \ | ||
| 112 | typedef ResidualModelControlGravTpl<Scalar> Model; \ | ||
| 113 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
| 114 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 115 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 116 | "ResidualDataControlGrav", "Data for control gravity residual.\n\n", \ | ||
| 117 | bp::init<Model*, DataCollector*>( \ | ||
| 118 | bp::args("self", "model", "data"), \ | ||
| 119 | "Create control gravity residual data.\n\n" \ | ||
| 120 | ":param model: control gravity residual model\n" \ | ||
| 121 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
| 122 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 123 | .def(ResidualDataControlGravVisitor<Data>()) \ | ||
| 124 | .def(CopyableVisitor<Data>()); | ||
| 125 | |||
| 126 | 10 | void exposeResidualControlGrav() { | |
| 127 |
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( |
| 128 | CROCODDYL_RESIDUAL_MODEL_CONTROL_GRAV_PYTHON_BINDINGS) | ||
| 129 |
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_RESIDUAL_DATA_CONTROL_GRAV_PYTHON_BINDINGS) |
| 130 | 10 | } | |
| 131 | |||
| 132 | } // namespace python | ||
| 133 | } // namespace crocoddyl | ||
| 134 |