| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/actuation/actuation-squashing.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 19 | 19 | 100.0% |
| Branches: | 45 | 90 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2019-2025, University of Edinburgh, IRI: CSIC-UPC | ||
| 5 | // Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | #include "crocoddyl/core/actuation/actuation-squashing.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/core/core.hpp" | ||
| 13 | |||
| 14 | namespace crocoddyl { | ||
| 15 | namespace python { | ||
| 16 | |||
| 17 | template <typename Model> | ||
| 18 | struct ActuationModelSquashingVisitor | ||
| 19 | : public bp::def_visitor<ActuationModelSquashingVisitor<Model>> { | ||
| 20 | typedef typename Model::Scalar Scalar; | ||
| 21 | typedef typename Model::ActuationDataAbstract Data; | ||
| 22 | typedef typename Model::VectorXs VectorXs; | ||
| 23 | template <class PyClass> | ||
| 24 | 40 | void visit(PyClass& cl) const { | |
| 25 | 40 | cl.def("calc", | |
| 26 | static_cast<void (Model::*)( | ||
| 27 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 28 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
| 29 | bp::args("self", "data", "x", "u"), | ||
| 30 | "Compute the actuation signal from the squashing input u.\n\n" | ||
| 31 | "It describes the time-continuos evolution of the actuation model.\n" | ||
| 32 | ":param data: actuation data\n" | ||
| 33 | ":param x: state point (dim. state.nx)\n" | ||
| 34 | ":param u: squashing function input") | ||
| 35 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def( |
| 36 | "calcDiff", | ||
| 37 | static_cast<void (Model::*)( | ||
| 38 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 39 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
| 40 | bp::args("self", "data", "x", "u"), | ||
| 41 | "Compute the derivatives of the actuation model.\n\n" | ||
| 42 | "It computes the partial derivatives of the actuation model which " | ||
| 43 | "is\n" | ||
| 44 | "describes in continouos time. It assumes that calc has been run " | ||
| 45 | "first.\n" | ||
| 46 | ":param data: actuation data\n" | ||
| 47 | ":param x: state point (dim. state.nx)\n" | ||
| 48 | ":param u: control input (dim. nu).") | ||
| 49 |
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, bp::args("self"), |
| 50 | "Create the actuation squashing data.\n\n" | ||
| 51 | "Each actuation model (AM) has its own data that needs to be " | ||
| 52 | "allocated.\n" | ||
| 53 | "This function returns the allocated data for a predefined AM.\n" | ||
| 54 | ":return AM data.") | ||
| 55 |
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 | .add_property( |
| 56 | "squashing", | ||
| 57 | bp::make_function(&Model::get_squashing, | ||
| 58 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 59 | "squashing") | ||
| 60 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 61 | "actuation", | ||
| 62 | bp::make_function(&Model::get_actuation, | ||
| 63 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 64 | "actuation"); | ||
| 65 | 40 | } | |
| 66 | }; | ||
| 67 | |||
| 68 | template <typename Data> | ||
| 69 | struct ActuationDataSquashingVisitor | ||
| 70 | : public bp::def_visitor<ActuationDataSquashingVisitor<Data>> { | ||
| 71 | template <class PyClass> | ||
| 72 | 40 | void visit(PyClass& cl) const { | |
| 73 |
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( |
| 74 | "squashing", | ||
| 75 | bp::make_getter(&Data::squashing, | ||
| 76 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 77 | bp::make_setter(&Data::squashing), | ||
| 78 | "Data of the associated squashing model") | ||
| 79 |
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( |
| 80 | "actuation", | ||
| 81 | bp::make_getter(&Data::actuation, | ||
| 82 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
| 83 | bp::make_setter(&Data::actuation), | ||
| 84 | "Data of the associated actuation model"); | ||
| 85 | 40 | } | |
| 86 | }; | ||
| 87 | |||
| 88 | #define CROCODDYL_ACTUATION_MODEL_SQUASHING_PYTHON_BINDINGS(Scalar) \ | ||
| 89 | typedef ActuationSquashingModelTpl<Scalar> Model; \ | ||
| 90 | typedef ActuationModelAbstractTpl<Scalar> ModelBase; \ | ||
| 91 | typedef SquashingModelAbstractTpl<Scalar> Squashing; \ | ||
| 92 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 93 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 94 | "ActuationSquashingModel", \ | ||
| 95 | "Class for squashing an actuation model.\n\n", \ | ||
| 96 | bp::init<std::shared_ptr<ModelBase>, std::shared_ptr<Squashing>, \ | ||
| 97 | std::size_t>( \ | ||
| 98 | bp::args("self", "actuation", "squashing", "nu"), \ | ||
| 99 | "Initialize the actuation model with squashing function.\n\n" \ | ||
| 100 | ":param actuation: actuation model to be squashed.\n" \ | ||
| 101 | ":param squashing: squashing function.\n" \ | ||
| 102 | ":param nu: number of controls")) \ | ||
| 103 | .def(ActuationModelSquashingVisitor<Model>()) \ | ||
| 104 | .def(CastVisitor<Model>()) \ | ||
| 105 | .def(PrintableVisitor<Model>()) \ | ||
| 106 | .def(CopyableVisitor<Model>()); | ||
| 107 | |||
| 108 | #define CROCODDYL_ACTUATION_DATA_SQUASHING_PYTHON_BINDINGS(Scalar) \ | ||
| 109 | typedef ActuationSquashingDataTpl<Scalar> Data; \ | ||
| 110 | typedef ActuationDataAbstractTpl<Scalar> DataBase; \ | ||
| 111 | typedef ActuationSquashingModelTpl<Scalar> Model; \ | ||
| 112 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 113 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 114 | "ActuationSquashingData", \ | ||
| 115 | "Class for actuation datas using squashing functions.\n\n" \ | ||
| 116 | "In crocoddyl, an actuation data contains all the required information " \ | ||
| 117 | "for processing an user-defined actuation model. The actuation data " \ | ||
| 118 | "typically is allocated onces by running via model.createData().", \ | ||
| 119 | bp::init<Model*>( \ | ||
| 120 | bp::args("self", "model"), \ | ||
| 121 | "Create common data shared between actuation models.\n\n" \ | ||
| 122 | "The actuation data uses the model in order to first process it.\n" \ | ||
| 123 | ":param model: actuation model")) \ | ||
| 124 | .def(ActuationDataSquashingVisitor<Data>()) \ | ||
| 125 | .def(CopyableVisitor<Data>()); | ||
| 126 | |||
| 127 | 10 | void exposeActuationSquashing() { | |
| 128 |
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_ACTUATION_MODEL_SQUASHING_PYTHON_BINDINGS) |
| 129 |
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_SQUASHING_PYTHON_BINDINGS) |
| 130 | 10 | } | |
| 131 | |||
| 132 | } // namespace python | ||
| 133 | } // namespace crocoddyl | ||
| 134 |