| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/actuation-base.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 30 | 30 | 100.0% |
| Branches: | 66 | 132 | 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 "python/crocoddyl/core/actuation-base.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/core/core.hpp" | ||
| 13 | |||
| 14 | namespace crocoddyl { | ||
| 15 | namespace python { | ||
| 16 | |||
| 17 | template <typename Model> | ||
| 18 | struct ActuationModelAbstractVisitor | ||
| 19 | : public bp::def_visitor<ActuationModelAbstractVisitor<Model>> { | ||
| 20 | typedef typename Model::Scalar Scalar; | ||
| 21 | typedef typename Model::ActuationModel ActuationModel; | ||
| 22 | typedef typename Model::ActuationData ActuationData; | ||
| 23 | typedef typename Model::VectorXs VectorXs; | ||
| 24 | template <class PyClass> | ||
| 25 | 40 | void visit(PyClass& cl) const { | |
| 26 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
40 | cl.def("calc", pure_virtual(&Model::calc), |
| 27 | bp::args("self", "data", "x", "u"), | ||
| 28 | "Compute the actuation signal and actuation set from the " | ||
| 29 | "joint-torque input u.\n\n" | ||
| 30 | "It describes the time-continuos evolution of the actuation model.\n" | ||
| 31 | ":param data: actuation data\n" | ||
| 32 | ":param x: state point (dim. state.nx)\n" | ||
| 33 | ":param u: joint-torque input (dim. nu)") | ||
| 34 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calc", |
| 35 | static_cast<void (ActuationModel::*)( | ||
| 36 | const std::shared_ptr<ActuationData>&, | ||
| 37 | const Eigen::Ref<const VectorXs>&)>(&ActuationModel::calc), | ||
| 38 | bp::args("self", "data", "x"), | ||
| 39 | "Ignore the computation of the actuation signal and actuation " | ||
| 40 | "set.\n\n" | ||
| 41 | "It does not update the actuation signal as this function is used " | ||
| 42 | "in " | ||
| 43 | "the\n" | ||
| 44 | "terminal nodes of an optimal control problem.\n" | ||
| 45 | ":param data: actuation data\n" | ||
| 46 | ":param x: state point (dim. state.nx)") | ||
| 47 |
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("calcDiff", pure_virtual(&Model::calcDiff), |
| 48 | bp::args("self", "data", "x", "u"), | ||
| 49 | "Compute the Jacobians of the actuation model.\n\n" | ||
| 50 | "It computes the partial derivatives of the actuation model which " | ||
| 51 | "is\n" | ||
| 52 | "describes in continouos time.\n" | ||
| 53 | ":param data: actuation data\n" | ||
| 54 | ":param x: state point (dim. state.nx)\n" | ||
| 55 | ":param u: joint-torque input (dim. nu)") | ||
| 56 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calcDiff", |
| 57 | static_cast<void (ActuationModel::*)( | ||
| 58 | const std::shared_ptr<ActuationData>&, | ||
| 59 | const Eigen::Ref<const VectorXs>&)>(&ActuationModel::calc), | ||
| 60 | bp::args("self", "data", "x"), | ||
| 61 | "Ignore the computation of the Jacobians of the actuation " | ||
| 62 | "function.\n\n" | ||
| 63 | "It does not update the Jacobians of the actuation function as " | ||
| 64 | "this " | ||
| 65 | "function\n" | ||
| 66 | "is used in the terminal nodes of an optimal control problem.\n" | ||
| 67 | ":param data: actuation data\n" | ||
| 68 | ":param x: state point (dim. state.nx)") | ||
| 69 |
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("commands", pure_virtual(&Model::commands), |
| 70 | bp::args("self", "data", "x", "tau"), | ||
| 71 | "Compute the joint-torque commands from the generalized " | ||
| 72 | "torques.\n\n" | ||
| 73 | "It stores the results in data.u.\n" | ||
| 74 | ":param data: actuation data\n" | ||
| 75 | ":param x: state point (dim. state.nx)\n" | ||
| 76 | ":param tau: generalized torques (dim state.nv)") | ||
| 77 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("torqueTransform", &Model::torqueTransform, |
| 78 | &Model::default_torqueTransform, | ||
| 79 | bp::args("self", "data", "x", "u"), | ||
| 80 | "Compute the torque transform from generalized torques to " | ||
| 81 | "joint-torque inputs.\n\n" | ||
| 82 | "It stores the results in data.Mtau.\n" | ||
| 83 | ":param data: actuation data\n" | ||
| 84 | ":param x: state point (dim. state.nx)\n" | ||
| 85 | ":param u: joint-torque input (dim nu)") | ||
| 86 |
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, &Model::default_createData, |
| 87 | bp::args("self"), | ||
| 88 | "Create the actuation data.\n\n" | ||
| 89 | "Each actuation model (AM) has its own data that needs to be " | ||
| 90 | "allocated.\n" | ||
| 91 | "This function returns the allocated data for a predefined AM.\n" | ||
| 92 | ":return AM data.") | ||
| 93 |
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("nu", bp::make_function(&Model::get_nu), |
| 94 | "dimension of joint-torque vector") | ||
| 95 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 96 | "state", | ||
| 97 | bp::make_function(&Model::get_state, | ||
| 98 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 99 | "state"); | ||
| 100 | 40 | } | |
| 101 | }; | ||
| 102 | |||
| 103 | template <typename Data> | ||
| 104 | struct ActuationDataAbstractVisitor | ||
| 105 | : public bp::def_visitor<ActuationDataAbstractVisitor<Data>> { | ||
| 106 | typedef typename Data::Scalar Scalar; | ||
| 107 | template <class PyClass> | ||
| 108 | 40 | void visit(PyClass& cl) const { | |
| 109 |
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.
|
40 | cl.add_property( |
| 110 | 40 | "tau", bp::make_getter(&Data::tau, bp::return_internal_reference<>()), | |
| 111 | bp::make_setter(&Data::tau), "generalized torques") | ||
| 112 |
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( |
| 113 | 40 | "u", bp::make_getter(&Data::u, bp::return_internal_reference<>()), | |
| 114 | bp::make_setter(&Data::u), "joint-torque inputs") | ||
| 115 |
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( |
| 116 | "dtau_dx", | ||
| 117 | 40 | bp::make_getter(&Data::dtau_dx, bp::return_internal_reference<>()), | |
| 118 | bp::make_setter(&Data::dtau_dx), | ||
| 119 | "partial derivatives of the actuation model w.r.t. the state point") | ||
| 120 |
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( |
| 121 | "dtau_du", | ||
| 122 | 40 | bp::make_getter(&Data::dtau_du, bp::return_internal_reference<>()), | |
| 123 | bp::make_setter(&Data::dtau_du), | ||
| 124 | "partial derivatives of the actuation model w.r.t. the " | ||
| 125 | "joint-torque input") | ||
| 126 |
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( |
| 127 | "Mtau", | ||
| 128 | 40 | bp::make_getter(&Data::Mtau, bp::return_internal_reference<>()), | |
| 129 | bp::make_setter(&Data::Mtau), | ||
| 130 | "torque transform from generalized torques to joint-torque input") | ||
| 131 |
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( |
| 132 | "tau_set", | ||
| 133 | bp::make_getter(&Data::tau_set, | ||
| 134 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 135 | bp::make_setter(&Data::tau_set), "actuation set"); | ||
| 136 | 40 | } | |
| 137 | }; | ||
| 138 | |||
| 139 | #define CROCODDYL_ACTUATION_MODEL_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
| 140 | typedef ActuationModelAbstractTpl<Scalar> Model; \ | ||
| 141 | typedef ActuationModelAbstractTpl_wrap<Scalar> Model_wrap; \ | ||
| 142 | typedef StateAbstractTpl<Scalar> State; \ | ||
| 143 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 144 | bp::class_<Model_wrap, boost::noncopyable>( \ | ||
| 145 | "ActuationModelAbstract", \ | ||
| 146 | "Abstract class for actuation-mapping models.\n\n" \ | ||
| 147 | "An actuation model is a function that maps state x and joint-torque " \ | ||
| 148 | "inputs u into generalized torques tau, where tau is also named as the " \ | ||
| 149 | "actuation signal of our system. The computation of the actuation " \ | ||
| 150 | "signal and its partial derivatives are mainly carried out inside " \ | ||
| 151 | "calc() and calcDiff(), respectively.", \ | ||
| 152 | bp::init<std::shared_ptr<State>, std::size_t>( \ | ||
| 153 | bp::args("self", "state", "nu"), \ | ||
| 154 | "Initialize the actuation model.\n\n" \ | ||
| 155 | ":param state: state description,\n" \ | ||
| 156 | ":param nu: dimension of the joint-torque input")) \ | ||
| 157 | .def(ActuationModelAbstractVisitor<Model_wrap>()) \ | ||
| 158 | .def(PrintableVisitor<Model_wrap>()) \ | ||
| 159 | .def(CopyableVisitor<Model_wrap>()); | ||
| 160 | |||
| 161 | #define CROCODDYL_ACTUATION_DATA_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
| 162 | typedef ActuationDataAbstractTpl<Scalar> Data; \ | ||
| 163 | typedef ActuationModelAbstractTpl<Scalar> Model; \ | ||
| 164 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 165 | bp::class_<Data>( \ | ||
| 166 | "ActuationDataAbstract", \ | ||
| 167 | "Abstract class for actuation datas.\n\n" \ | ||
| 168 | "An actuation data contains all the required information for " \ | ||
| 169 | "processing an user-defined actuation model. The actuation data " \ | ||
| 170 | "typically is allocated onces by running model.createData().", \ | ||
| 171 | bp::init<Model*>( \ | ||
| 172 | bp::args("self", "model"), \ | ||
| 173 | "Create common data shared between actuation models.\n\n" \ | ||
| 174 | "The actuation data uses the model in order to first process it.\n" \ | ||
| 175 | ":param model: actuation model")) \ | ||
| 176 | .def(ActuationDataAbstractVisitor<Data>()) \ | ||
| 177 | .def(CopyableVisitor<Data>()); | ||
| 178 | |||
| 179 | 10 | void exposeActuationAbstract() { | |
| 180 |
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_ACTUATION_MODEL_ABSTRACT_PYTHON_BINDINGS) |
| 181 |
13/26✓ 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 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 29 taken 10 times.
✗ Branch 30 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.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_ACTUATION_DATA_ABSTRACT_PYTHON_BINDINGS) |
| 182 | 10 | } | |
| 183 | |||
| 184 | } // namespace python | ||
| 185 | } // namespace crocoddyl | ||
| 186 |