Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/multibody/residuals/impulse-com.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 24 | 24 | 100.0% |
Branches: | 52 | 104 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2025, University of Edinburgh, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #include "crocoddyl/multibody/residuals/impulse-com.hpp" | ||
10 | |||
11 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
12 | |||
13 | namespace crocoddyl { | ||
14 | namespace python { | ||
15 | |||
16 | template <typename Model> | ||
17 | struct ResidualModelImpulseCoMVisitor | ||
18 | : public bp::def_visitor<ResidualModelImpulseCoMVisitor<Model>> { | ||
19 | typedef typename Model::ResidualDataAbstract Data; | ||
20 | typedef typename Model::Base ModelBase; | ||
21 | typedef typename Model::VectorXs VectorXs; | ||
22 | template <class PyClass> | ||
23 | 40 | void visit(PyClass& cl) const { | |
24 | 40 | cl.def("calc", | |
25 | static_cast<void (Model::*)( | ||
26 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
27 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
28 | bp::args("self", "data", "x", "u"), | ||
29 | "Compute the CoM position residual.\n\n" | ||
30 | ":param data: residual data\n" | ||
31 | ":param x: state point (dim. state.nx)") | ||
32 |
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( |
33 | "calc", | ||
34 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
35 | const Eigen::Ref<const VectorXs>&)>( | ||
36 | &ModelBase::calc), | ||
37 | bp::args("self", "data", "x")) | ||
38 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
39 | "calcDiff", | ||
40 | static_cast<void (Model::*)( | ||
41 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
42 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
43 | bp::args("self", "data", "x", "u"), | ||
44 | "Compute the derivatives of the CoM position residual for impulse " | ||
45 | "dynamics.\n\n" | ||
46 | "It assumes that calc has been run first.\n" | ||
47 | ":param data: action data\n" | ||
48 | ":param x: state point (dim. state.nx)") | ||
49 |
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( |
50 | "calcDiff", | ||
51 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
52 | const Eigen::Ref<const VectorXs>&)>( | ||
53 | &ModelBase::calcDiff), | ||
54 | bp::args("self", "data", "x")) | ||
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 | 40 | bp::with_custodian_and_ward_postcall<0, 2>(), | |
57 | bp::args("self", "data"), | ||
58 | "Create the CoM position residual data.\n\n" | ||
59 | "Each residual model has its own data that needs to be allocated. " | ||
60 | "This function returns the allocated data for the impulse CoM " | ||
61 | "residual.\n" | ||
62 | ":param data: shared data\n" | ||
63 | ":return residual data."); | ||
64 | 40 | } | |
65 | }; | ||
66 | |||
67 | template <typename Data> | ||
68 | struct ResidualDataImpulseCoMVisitor | ||
69 | : public bp::def_visitor<ResidualDataImpulseCoMVisitor<Data>> { | ||
70 | template <class PyClass> | ||
71 | 40 | void visit(PyClass& cl) const { | |
72 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
73 | "pinocchio", | ||
74 | 40 | bp::make_getter(&Data::pinocchio, bp::return_internal_reference<>()), | |
75 | "pinocchio data") | ||
76 |
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( |
77 | "impulses", | ||
78 | bp::make_getter(&Data::impulses, | ||
79 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
80 | bp::make_setter(&Data::impulses), | ||
81 | "impulses data associated with the current residual") | ||
82 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
83 | "dvc_dq", | ||
84 | 40 | bp::make_getter(&Data::dvc_dq, bp::return_internal_reference<>()), | |
85 | "Jacobian of the CoM velocity") | ||
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 | "ddv_dv", | ||
88 | 40 | bp::make_getter(&Data::ddv_dv, bp::return_internal_reference<>()), | |
89 | "Jacobian of the impulse velocity") | ||
90 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("pinocchio_internal", |
91 | bp::make_getter(&Data::pinocchio_internal, | ||
92 | 40 | bp::return_internal_reference<>()), | |
93 | "internal pinocchio data used for extra computations"); | ||
94 | 40 | } | |
95 | }; | ||
96 | |||
97 | #define CROCODDYL_RESIDUAL_MODEL_IMPULSE_COM_PYTHON_BINDINGS(Scalar) \ | ||
98 | typedef ResidualModelImpulseCoMTpl<Scalar> Model; \ | ||
99 | typedef ResidualModelAbstractTpl<Scalar> ModelBase; \ | ||
100 | typedef typename Model::StateMultibody State; \ | ||
101 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
102 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
103 | "ResidualModelImpulseCoM", \ | ||
104 | "This residual function defines a residual vector as r = Jcom * " \ | ||
105 | "(vnext-v), with Jcom as the CoM Jacobian, and vnext the velocity " \ | ||
106 | "after impact and v the velocity before impact, respectively.", \ | ||
107 | bp::init<std::shared_ptr<State>>( \ | ||
108 | bp::args("self", "state"), \ | ||
109 | "Initialize the CoM position cost model for impulse dynamics.\n\n" \ | ||
110 | "The default nu is obtained from state.nv.\n" \ | ||
111 | ":param state: state of the multibody system")) \ | ||
112 | .def(ResidualModelImpulseCoMVisitor<Model>()) \ | ||
113 | .def(CastVisitor<Model>()) \ | ||
114 | .def(PrintableVisitor<Model>()) \ | ||
115 | .def(CopyableVisitor<Model>()); | ||
116 | |||
117 | #define CROCODDYL_RESIDUAL_DATA_IMPULSE_COM_PYTHON_BINDINGS(Scalar) \ | ||
118 | typedef ResidualDataImpulseCoMTpl<Scalar> Data; \ | ||
119 | typedef ResidualDataAbstractTpl<Scalar> DataBase; \ | ||
120 | typedef ResidualModelImpulseCoMTpl<Scalar> Model; \ | ||
121 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
122 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
123 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
124 | "ResidualDataImpulseCoM", "Data for impulse CoM residual.\n\n", \ | ||
125 | bp::init<Model*, DataCollector*>( \ | ||
126 | bp::args("self", "model", "data"), \ | ||
127 | "Create contact force residual data.\n\n" \ | ||
128 | ":param model: impulse CoM residual model\n" \ | ||
129 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
130 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
131 | .def(ResidualDataImpulseCoMVisitor<Data>()) \ | ||
132 | .def(CopyableVisitor<Data>()); | ||
133 | |||
134 | 10 | void exposeResidualImpulseCoM() { | |
135 |
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_RESIDUAL_MODEL_IMPULSE_COM_PYTHON_BINDINGS) |
136 |
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_IMPULSE_COM_PYTHON_BINDINGS) |
137 | 10 | } | |
138 | |||
139 | } // namespace python | ||
140 | } // namespace crocoddyl | ||
141 |