GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/multibody/actions/free-invdyn.cpp Lines: 65 67 97.0 %
Date: 2024-02-13 11:12:33 Branches: 63 126 50.0 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2021-2023, 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/actions/free-invdyn.hpp"
10
11
#include "python/crocoddyl/core/diff-action-base.hpp"
12
#include "python/crocoddyl/multibody/multibody.hpp"
13
#include "python/crocoddyl/utils/copyable.hpp"
14
15
namespace crocoddyl {
16
namespace python {
17
18
10
void exposeDifferentialActionFreeInvDynamics() {
19

10
  bp::scope().attr("yes") = 1;
20

10
  bp::scope().attr("no") = 0;
21
  {
22
    bp::register_ptr_to_python<
23
10
        boost::shared_ptr<DifferentialActionModelFreeInvDynamics>>();
24
    bp::scope model_outer =
25
10
        bp::class_<DifferentialActionModelFreeInvDynamics,
26
                   bp::bases<DifferentialActionModelAbstract>>(
27
            "DifferentialActionModelFreeInvDynamics",
28
            "Differential action model for free inverse dynamics in multibody "
29
            "systems.\n\n"
30
            "This class implements the dynamics using Recursive Newton Euler "
31
            "Algorithm (RNEA) as an equality "
32
            "constraint.\n"
33
            "The stack of cost and constraint functions are implemented in\n"
34
            "ConstraintModelManager() and CostModelSum(), respectively.",
35
20
            bp::init<boost::shared_ptr<StateMultibody>,
36
                     boost::shared_ptr<ActuationModelAbstract>,
37
                     boost::shared_ptr<CostModelSum>,
38
                     bp::optional<boost::shared_ptr<ConstraintModelManager>>>(
39
10
                bp::args("self", "state", "actuation", "costs", "constraints"),
40
                "Initialize the free inverse-dynamics action model.\n\n"
41
                "It describes the kinematic evolution of the multibody system "
42
                "and computes the\n"
43
                "needed torques using inverse dynamics.\n"
44
                ":param state: multibody state\n"
45
                ":param actuation: abstract actuation model\n"
46
                ":param costs: stack of cost functions\n"
47
                ":param constraints: stack of constraint functions"))
48
            .def<void (DifferentialActionModelFreeInvDynamics::*)(
49
                const boost::shared_ptr<DifferentialActionDataAbstract>&,
50
                const Eigen::Ref<const Eigen::VectorXd>&,
51
                const Eigen::Ref<const Eigen::VectorXd>&)>(
52
                "calc", &DifferentialActionModelFreeInvDynamics::calc,
53
20
                bp::args("self", "data", "x", "u"),
54
                "Compute the next state and cost value.\n\n"
55
                ":param data: free inverse-dynamics data\n"
56
                ":param x: state point (dim. state.nx)\n"
57
20
                ":param u: control input (dim. nu)")
58
            .def<void (DifferentialActionModelFreeInvDynamics::*)(
59
                const boost::shared_ptr<DifferentialActionDataAbstract>&,
60
                const Eigen::Ref<const Eigen::VectorXd>&)>(
61
                "calc", &DifferentialActionModelAbstract::calc,
62

20
                bp::args("self", "data", "x"))
63
            .def<void (DifferentialActionModelFreeInvDynamics::*)(
64
                const boost::shared_ptr<DifferentialActionDataAbstract>&,
65
                const Eigen::Ref<const Eigen::VectorXd>&,
66
                const Eigen::Ref<const Eigen::VectorXd>&)>(
67
                "calcDiff", &DifferentialActionModelFreeInvDynamics::calcDiff,
68
20
                bp::args("self", "data", "x", "u"),
69
                "Compute the derivatives of the differential multibody system "
70
                "(free of contact) and\n"
71
                "its cost functions.\n\n"
72
                "It computes the partial derivatives of the differential "
73
                "multibody system and the\n"
74
                "cost function. It assumes that calc has been run first.\n"
75
                "This function builds a quadratic approximation of the\n"
76
                "action model (i.e. dynamical system and cost function).\n"
77
                ":param data: free inverse-dynamics data\n"
78
                ":param x: state point (dim. state.nx)\n"
79
10
                ":param u: control input (dim. nu)")
80
            .def<void (DifferentialActionModelFreeInvDynamics::*)(
81
                const boost::shared_ptr<DifferentialActionDataAbstract>&,
82
                const Eigen::Ref<const Eigen::VectorXd>&)>(
83
                "calcDiff", &DifferentialActionModelAbstract::calcDiff,
84

20
                bp::args("self", "data", "x"))
85
            .def("createData",
86
                 &DifferentialActionModelFreeInvDynamics::createData,
87
20
                 bp::args("self"),
88

20
                 "Create the free inverse-dynamics differential action data.")
89
            .add_property(
90
                "actuation",
91
10
                bp::make_function(
92
                    &DifferentialActionModelFreeInvDynamics::get_actuation,
93
10
                    bp::return_value_policy<bp::return_by_value>()),
94
10
                "actuation model")
95
            .add_property(
96
                "costs",
97
10
                bp::make_function(
98
                    &DifferentialActionModelFreeInvDynamics::get_costs,
99
10
                    bp::return_value_policy<bp::return_by_value>()),
100
10
                "total cost model")
101
            .add_property(
102
                "constraints",
103
10
                bp::make_function(
104
                    &DifferentialActionModelFreeInvDynamics::get_constraints,
105
10
                    bp::return_value_policy<bp::return_by_value>()),
106
10
                "constraint model manager")
107

10
            .def(CopyableVisitor<DifferentialActionModelFreeInvDynamics>());
108
109
    bp::register_ptr_to_python<boost::shared_ptr<
110
10
        DifferentialActionModelFreeInvDynamics::ResidualModelActuation>>();
111
112
10
    bp::class_<DifferentialActionModelFreeInvDynamics::ResidualModelActuation,
113
               bp::bases<ResidualModelAbstract>>(
114
        "ResidualModelActuation",
115
        "This residual function enforces the torques of under-actuated joints "
116
        "(e.g., floating-base\n"
117
        "joints) to be zero. We compute these torques and their derivatives "
118
        "using RNEA inside \n"
119
        "DifferentialActionModelFreeInvDynamics.",
120
10
        bp::init<boost::shared_ptr<StateMultibody>, std::size_t>(
121
20
            bp::args("self", "state", "nu"),
122
            "Initialize the actuation residual model.\n\n"
123
            ":param state: state description\n"
124
            ":param nu: dimension of the joint torques"))
125
        .def<void (
126
            DifferentialActionModelFreeInvDynamics::ResidualModelActuation::*)(
127
            const boost::shared_ptr<ResidualDataAbstract>&,
128
            const Eigen::Ref<const Eigen::VectorXd>&,
129
            const Eigen::Ref<const Eigen::VectorXd>&)>(
130
            "calc",
131
            &DifferentialActionModelFreeInvDynamics::ResidualModelActuation::
132
                calc,
133
20
            bp::args("self", "data", "x", "u"),
134
            "Compute the actuation residual.\n\n"
135
            ":param data: residual data\n"
136
            ":param x: state point (dim. state.nx)\n"
137
20
            ":param u: control input (dim. nu)")
138
        .def<void (
139
            DifferentialActionModelFreeInvDynamics::ResidualModelActuation::*)(
140
            const boost::shared_ptr<ResidualDataAbstract>&,
141
            const Eigen::Ref<const Eigen::VectorXd>&)>(
142

20
            "calc", &ResidualModelAbstract::calc, bp::args("self", "data", "x"))
143
        .def<void (
144
            DifferentialActionModelFreeInvDynamics::ResidualModelActuation::*)(
145
            const boost::shared_ptr<ResidualDataAbstract>&,
146
            const Eigen::Ref<const Eigen::VectorXd>&,
147
            const Eigen::Ref<const Eigen::VectorXd>&)>(
148
            "calcDiff",
149
            &DifferentialActionModelFreeInvDynamics::ResidualModelActuation::
150
                calcDiff,
151
20
            bp::args("self", "data", "x", "u"),
152
            "Compute the Jacobians of the actuation residual.\n\n"
153
            "It assumes that calc has been run first.\n"
154
            ":param data: action data\n"
155
            ":param x: state point (dim. state.nx)\n"
156
10
            ":param u: control input (dim. nu)\n")
157
        .def<void (
158
            DifferentialActionModelFreeInvDynamics::ResidualModelActuation::*)(
159
            const boost::shared_ptr<ResidualDataAbstract>&,
160
            const Eigen::Ref<const Eigen::VectorXd>&)>(
161
            "calcDiff", &ResidualModelAbstract::calcDiff,
162

20
            bp::args("self", "data", "x"))
163
        .def("createData",
164
             &DifferentialActionModelFreeInvDynamics::ResidualModelActuation::
165
                 createData,
166
             bp::with_custodian_and_ward_postcall<0, 2>(),
167
20
             bp::args("self", "data"),
168
             "Create the actuation residual data.\n\n"
169
             "Each residual model has its own data that needs to be allocated. "
170
             "This function\n"
171
             "returns the allocated data for the actuation residual.\n"
172
             ":param data: shared data\n"
173

20
             ":return residual data.")
174
10
        .def(CopyableVisitor<
175
10
             DifferentialActionModelFreeInvDynamics::ResidualModelActuation>());
176
  }
177
178
  bp::register_ptr_to_python<
179
10
      boost::shared_ptr<DifferentialActionDataFreeInvDynamics>>();
180
181
  bp::scope data_outer =
182
10
      bp::class_<DifferentialActionDataFreeInvDynamics,
183
                 bp::bases<DifferentialActionDataAbstract>>(
184
          "DifferentialActionDataFreeInvDynamics",
185
          "Action data for the free inverse-dynamics system.",
186
20
          bp::init<DifferentialActionModelFreeInvDynamics*>(
187
10
              bp::args("self", "model"),
188
              "Create free inverse-dynamics action data.\n\n"
189
              ":param model: free inverse-dynamics action model"))
190
          .add_property(
191
              "pinocchio",
192
10
              bp::make_getter(&DifferentialActionDataFreeInvDynamics::pinocchio,
193
10
                              bp::return_internal_reference<>()),
194
10
              "pinocchio data")
195
          .add_property(
196
              "multibody",
197
10
              bp::make_getter(&DifferentialActionDataFreeInvDynamics::multibody,
198
10
                              bp::return_internal_reference<>()),
199
10
              "multibody data")
200
          .add_property(
201
              "costs",
202
10
              bp::make_getter(&DifferentialActionDataFreeInvDynamics::costs,
203
10
                              bp::return_value_policy<bp::return_by_value>()),
204
10
              "total cost data")
205
          .add_property("constraints",
206
                        bp::make_getter(
207
10
                            &DifferentialActionDataFreeInvDynamics::constraints,
208
10
                            bp::return_value_policy<bp::return_by_value>()),
209
10
                        "constraint data")
210

10
          .def(CopyableVisitor<DifferentialActionDataFreeInvDynamics>());
211
212
  bp::register_ptr_to_python<boost::shared_ptr<
213
10
      DifferentialActionDataFreeInvDynamics::ResidualDataActuation>>();
214
215
10
  bp::class_<DifferentialActionDataFreeInvDynamics::ResidualDataActuation,
216
             bp::bases<ResidualDataAbstract>>(
217
      "ResidualDataActuation", "Data for actuation residual.\n\n",
218
10
      bp::init<DifferentialActionModelFreeInvDynamics::ResidualModelActuation*,
219
               DataCollectorAbstract*>(
220
10
          bp::args("self", "model", "data"),
221
          "Create actuation residual data.\n\n"
222
          ":param model: actuation residual model\n"
223
10
          ":param data: shared data")[bp::with_custodian_and_ward<
224
20
          1, 2, bp::with_custodian_and_ward<1, 3>>()])
225
10
      .def(CopyableVisitor<
226
10
           DifferentialActionDataFreeInvDynamics::ResidualDataActuation>());
227
10
}
228
229
}  // namespace python
230
}  // namespace crocoddyl