Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/multibody/residuals/frame-velocity.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 20 | 21 | 95.2% |
Branches: | 49 | 98 | 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/frame-velocity.hpp" | ||
10 | |||
11 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
12 | |||
13 | namespace crocoddyl { | ||
14 | namespace python { | ||
15 | |||
16 | template <typename Model> | ||
17 | struct ResidualModelFrameVelocityVisitor | ||
18 | : public bp::def_visitor<ResidualModelFrameVelocityVisitor<Model>> { | ||
19 | typedef typename Model::ResidualDataAbstract Data; | ||
20 | typedef typename Model::Base ModelBase; | ||
21 | typedef typename Model::StateMultibody State; | ||
22 | typedef typename Model::Motion Motion; | ||
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>, pinocchio::FrameIndex, Motion, |
27 | pinocchio::ReferenceFrame>( | ||
28 | bp::args("self", "state", "id", "velocity", "type"), | ||
29 | "Initialize the frame velocity residual model.\n\n" | ||
30 | ":param state: state of the multibody system\n" | ||
31 | ":param residual: residual model\n" | ||
32 | ":param id: reference frame id\n" | ||
33 | ":param velocity: reference velocity\n" | ||
34 | ":param type: reference type of velocity")) | ||
35 | 80 | .def( | |
36 | "calc", | ||
37 | static_cast<void (Model::*)( | ||
38 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
39 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
40 | "Compute the frame velocity residual.\n\n" | ||
41 | ":param data: residual data\n" | ||
42 | ":param x: state point (dim. state.nx)\n" | ||
43 | ":param u: control input (dim. nu)") | ||
44 |
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( |
45 | "calc", | ||
46 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
47 | const Eigen::Ref<const VectorXs>&)>( | ||
48 | &ModelBase::calc), | ||
49 | bp::args("self", "data", "x")) | ||
50 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
51 | "calcDiff", | ||
52 | static_cast<void (Model::*)( | ||
53 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
54 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
55 | bp::args("self", "data", "x", "u"), | ||
56 | "Compute the Jacobians of the frame velocity residual.\n\n" | ||
57 | "It assumes that calc has been run first.\n" | ||
58 | ":param data: action data\n" | ||
59 | ":param x: state point (dim. state.nx)\n" | ||
60 | ":param u: control input (dim. nu)") | ||
61 |
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( |
62 | "calcDiff", | ||
63 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
64 | const Eigen::Ref<const VectorXs>&)>( | ||
65 | &ModelBase::calcDiff), | ||
66 | bp::args("self", "data", "x")) | ||
67 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, |
68 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
69 | bp::args("self", "data"), | ||
70 | "Create the frame velocity residual data.\n\n" | ||
71 | "Each residual model has its own data that needs to be allocated. " | ||
72 | "This function\n" | ||
73 | "returns the allocated data for the frame velocity residual.\n" | ||
74 | ":param data: shared data\n" | ||
75 | ":return residual data.") | ||
76 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
40 | .add_property("id", &Model::get_id, &Model::set_id, |
77 | "reference frame id") | ||
78 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
120 | .add_property("reference", |
79 | bp::make_function(&Model::get_reference, | ||
80 | 40 | bp::return_internal_reference<>()), | |
81 | &Model::set_reference, "reference velocity") | ||
82 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("type", &Model::get_type, &Model::set_type, |
83 | "reference type of velocity"); | ||
84 | 40 | } | |
85 | }; | ||
86 | |||
87 | template <typename Data> | ||
88 | struct ResidualDataFrameVelocityVisitor | ||
89 | : public bp::def_visitor<ResidualDataFrameVelocityVisitor<Data>> { | ||
90 | template <class PyClass> | ||
91 | 40 | void visit(PyClass& cl) const { | |
92 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
93 | "pinocchio", | ||
94 | 40 | bp::make_getter(&Data::pinocchio, bp::return_internal_reference<>()), | |
95 | "pinocchio data"); | ||
96 | 40 | } | |
97 | }; | ||
98 | |||
99 | #define CROCODDYL_RESIDUAL_MODEL_FRAME_VELOCITY_PYTHON_BINDINGS(Scalar) \ | ||
100 | typedef ResidualModelFrameVelocityTpl<Scalar> Model; \ | ||
101 | typedef ResidualModelAbstractTpl<Scalar> ModelBase; \ | ||
102 | typedef typename Model::StateMultibody State; \ | ||
103 | typedef typename Model::Motion Motion; \ | ||
104 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
105 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
106 | "ResidualModelFrameVelocity", \ | ||
107 | "This residual function defines r = v - vref, with v and vref as the " \ | ||
108 | "current and reference frame velocities, respectively.", \ | ||
109 | bp::init<std::shared_ptr<State>, std::size_t, Motion, \ | ||
110 | pinocchio::ReferenceFrame, std::size_t>( \ | ||
111 | bp::args("self", "state", "id", "velocity", "type", "nu"), \ | ||
112 | "Initialize the frame velocity residual model.\n\n" \ | ||
113 | ":param state: state of the multibody system\n" \ | ||
114 | ":param residual: residual model\n" \ | ||
115 | ":param id: reference frame id\n" \ | ||
116 | ":param velocity: reference velocity\n" \ | ||
117 | ":param type: reference type of velocity\n" \ | ||
118 | ":param nu: dimension of control vector")) \ | ||
119 | .def(ResidualModelFrameVelocityVisitor<Model>()) \ | ||
120 | .def(CastVisitor<Model>()) \ | ||
121 | .def(PrintableVisitor<Model>()) \ | ||
122 | .def(CopyableVisitor<Model>()); | ||
123 | |||
124 | #define CROCODDYL_RESIDUAL_DATA_FRAME_VELOCITY_PYTHON_BINDINGS(Scalar) \ | ||
125 | typedef ResidualDataFrameVelocityTpl<Scalar> Data; \ | ||
126 | typedef ResidualDataAbstractTpl<Scalar> DataBase; \ | ||
127 | typedef ResidualModelFrameVelocityTpl<Scalar> Model; \ | ||
128 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
129 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
130 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
131 | "ResidualDataFrameVelocity", "Data for frame velocity residual.\n\n", \ | ||
132 | bp::init<Model*, DataCollector*>( \ | ||
133 | bp::args("self", "model", "data"), \ | ||
134 | "Create frame velocity residual data.\n\n" \ | ||
135 | ":param model: frame Velocity residual model\n" \ | ||
136 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
137 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
138 | .def(ResidualDataFrameVelocityVisitor<Data>()) \ | ||
139 | .def(CopyableVisitor<Data>()); | ||
140 | |||
141 | 10 | void exposeResidualFrameVelocity() { | |
142 |
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( |
143 | CROCODDYL_RESIDUAL_MODEL_FRAME_VELOCITY_PYTHON_BINDINGS) | ||
144 |
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( |
145 | CROCODDYL_RESIDUAL_DATA_FRAME_VELOCITY_PYTHON_BINDINGS) | ||
146 | 10 | } | |
147 | |||
148 | } // namespace python | ||
149 | } // namespace crocoddyl | ||
150 |