| Directory: | ./ | 
|---|---|
| File: | bindings/python/crocoddyl/multibody/residuals/contact-control-gravity.cpp | 
| Date: | 2025-03-26 19:23:43 | 
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 20 | 20 | 100.0% | 
| Branches: | 50 | 100 | 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/contact-control-gravity.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
| 13 | |||
| 14 | namespace crocoddyl { | ||
| 15 | namespace python { | ||
| 16 | |||
| 17 | template <typename Model> | ||
| 18 | struct ResidualModelContactControlGravVisitor | ||
| 19 | : public bp::def_visitor<ResidualModelContactControlGravVisitor<Model>> { | ||
| 20 | typedef typename Model::ResidualDataAbstract Data; | ||
| 21 | typedef typename Model::Base ModelBase; | ||
| 22 | typedef typename Model::StateMultibody State; | ||
| 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(bp::init<std::shared_ptr<State>>( | 
| 27 | bp::args("self", "state"), | ||
| 28 | "Initialize the contact control-gravity residual model.\n\n" | ||
| 29 | "The default nu is obtained from state.nv.\n" | ||
| 30 | ":param state: state description")) | ||
| 31 | 
        1/2✓ Branch 1 taken 20 times. 
          ✗ Branch 2 not taken. 
         | 
      80 | .def( | 
| 32 | "calc", | ||
| 33 | static_cast<void (Model::*)( | ||
| 34 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 35 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
| 36 | bp::args("self", "data", "x", "u"), | ||
| 37 | "Compute the contact control-gravity residual.\n\n" | ||
| 38 | ":param data: residual data\n" | ||
| 39 | ":param x: state point (dim. state.nx)\n" | ||
| 40 | ":param u: control input (dim. nu)") | ||
| 41 | 
        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( | 
| 42 | "calc", | ||
| 43 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
| 44 | const Eigen::Ref<const VectorXs>&)>( | ||
| 45 | &ModelBase::calc), | ||
| 46 | bp::args("self", "data", "x")) | ||
| 47 | 
        1/2✓ Branch 1 taken 20 times. 
          ✗ Branch 2 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 Jacobias of the contact control-gravity 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 | 
        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( | 
| 58 | "calcDiff", | ||
| 59 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
| 60 | const Eigen::Ref<const VectorXs>&)>( | ||
| 61 | &ModelBase::calcDiff), | ||
| 62 | bp::args("self", "data", "x")) | ||
| 63 | 
        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, | 
| 64 | 40 | bp::with_custodian_and_ward_postcall<0, 2>(), | |
| 65 | bp::args("self", "data"), | ||
| 66 | "Create the contact control-gravity residual data.\n\n" | ||
| 67 | "Each residual model has its own data that needs to be allocated. " | ||
| 68 | "This function returns the allocated data for a predefined " | ||
| 69 | "residual.\n" | ||
| 70 | ":param data: shared data\n" | ||
| 71 | ":return residual data."); | ||
| 72 | 40 | } | |
| 73 | }; | ||
| 74 | |||
| 75 | template <typename Data> | ||
| 76 | struct ResidualDataContactControlGravVisitor | ||
| 77 | : public bp::def_visitor<ResidualDataContactControlGravVisitor<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 | 
        2/4✓ Branch 1 taken 20 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 4 taken 20 times. 
          ✗ Branch 5 not taken. 
         | 
      40 | .add_property( | 
| 87 | "fext", | ||
| 88 | 40 | bp::make_getter(&Data::fext, bp::return_internal_reference<>()), | |
| 89 | "external spatial forces"); | ||
| 90 | 40 | } | |
| 91 | }; | ||
| 92 | |||
| 93 | #define CROCODDYL_RESIDUAL_MODEL_CONTACT_CONTROL_GRAV_PYTHON_BINDINGS(Scalar) \ | ||
| 94 | typedef ResidualModelContactControlGravTpl<Scalar> Model; \ | ||
| 95 | typedef ResidualModelAbstractTpl<Scalar> ModelBase; \ | ||
| 96 | typedef typename Model::StateMultibody State; \ | ||
| 97 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 98 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 99 | "ResidualModelContactControlGrav", \ | ||
| 100 | "This residual function defines a residual vector as r = u - " \ | ||
| 101 | "g(q,fext), with u as the control, q as the position, fext as the " \ | ||
| 102 | "external forces and g as the gravity vector in contact", \ | ||
| 103 | bp::init<std::shared_ptr<State>, std::size_t>( \ | ||
| 104 | bp::args("self", "state", "nu"), \ | ||
| 105 | "Initialize the contact control-gravity residual model.\n\n" \ | ||
| 106 | ":param state: state description\n" \ | ||
| 107 | ":param nu: dimension of the control vector")) \ | ||
| 108 | .def(ResidualModelContactControlGravVisitor<Model>()) \ | ||
| 109 | .def(CastVisitor<Model>()) \ | ||
| 110 | .def(PrintableVisitor<Model>()) \ | ||
| 111 | .def(CopyableVisitor<Model>()); | ||
| 112 | |||
| 113 | #define CROCODDYL_RESIDUAL_DATA_CONTACT_CONTROL_GRAV_PYTHON_BINDINGS(Scalar) \ | ||
| 114 | typedef ResidualDataContactControlGravTpl<Scalar> Data; \ | ||
| 115 | typedef ResidualDataAbstractTpl<Scalar> DataBase; \ | ||
| 116 | typedef ResidualModelContactControlGravTpl<Scalar> Model; \ | ||
| 117 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
| 118 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 119 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 120 | "ResidualDataContactControlGrav", \ | ||
| 121 | "Data for control gravity residual in contact.\n\n", \ | ||
| 122 | bp::init<Model*, DataCollector*>( \ | ||
| 123 | bp::args("self", "model", "data"), \ | ||
| 124 | "Create contact control-gravity gravity contact residual data.\n\n" \ | ||
| 125 | ":param model: control gravity residual model in contact\n" \ | ||
| 126 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
| 127 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 128 | .def(ResidualDataContactControlGravVisitor<Data>()) \ | ||
| 129 | .def(CopyableVisitor<Data>()); | ||
| 130 | |||
| 131 | 10 | void exposeResidualContactControlGrav() { | |
| 132 | 
        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( | 
| 133 | CROCODDYL_RESIDUAL_MODEL_CONTACT_CONTROL_GRAV_PYTHON_BINDINGS) | ||
| 134 | 
        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( | 
| 135 | CROCODDYL_RESIDUAL_DATA_CONTACT_CONTROL_GRAV_PYTHON_BINDINGS) | ||
| 136 | 10 | } | |
| 137 | |||
| 138 | } // namespace python | ||
| 139 | } // namespace crocoddyl | ||
| 140 |