GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/multibody/actuations/floating-base.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 40 42 95.2%
Branches: 44 138 31.9%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2022, 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 #ifndef CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_
11 #define CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_
12
13 #include "crocoddyl/core/actuation-base.hpp"
14 #include "crocoddyl/core/utils/exception.hpp"
15 #include "crocoddyl/multibody/fwd.hpp"
16 #include "crocoddyl/multibody/states/multibody.hpp"
17
18 namespace crocoddyl {
19
20 /**
21 * @brief Floating-base actuation model
22 *
23 * It considers the first joint, defined in the Pinocchio model, as the
24 * floating-base joints. Then, this joint (that might have various DoFs) is
25 * unactuated.
26 *
27 * The main computations are carrying out in `calc`, and `calcDiff`, where the
28 * former computes actuation signal \f$\mathbf{a}\f$ from a given joint-torque
29 * input \f$\mathbf{u}\f$ and state point \f$\mathbf{x}\f$, and the latter
30 * computes the Jacobians of the actuation-mapping function. Note that
31 * `calcDiff` requires to run `calc` first.
32 *
33 * \sa `ActuationModelAbstractTpl`, `calc()`, `calcDiff()`, `createData()`
34 */
35 template <typename _Scalar>
36 class ActuationModelFloatingBaseTpl
37 : public ActuationModelAbstractTpl<_Scalar> {
38 public:
39 typedef _Scalar Scalar;
40 typedef MathBaseTpl<Scalar> MathBase;
41 typedef ActuationModelAbstractTpl<Scalar> Base;
42 typedef ActuationDataAbstractTpl<Scalar> Data;
43 typedef StateMultibodyTpl<Scalar> StateMultibody;
44 typedef typename MathBase::VectorXs VectorXs;
45 typedef typename MathBase::MatrixXs MatrixXs;
46
47 /**
48 * @brief Initialize the floating-base actuation model
49 *
50 * @param[in] state State of a multibody system
51 * @param[in] nu Dimension of joint-torque vector
52 */
53 703 explicit ActuationModelFloatingBaseTpl(std::shared_ptr<StateMultibody> state)
54 : Base(state,
55 703 state->get_nv() -
56 703 state->get_pinocchio()
57 703 ->joints[(
58
4/6
✓ Branch 5 taken 703 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 703 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 581 times.
✓ Branch 11 taken 122 times.
1406 state->get_pinocchio()->existJointName("root_joint")
59
4/10
✓ Branch 5 taken 581 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 581 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 581 times.
✓ Branch 12 taken 122 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
1284 ? state->get_pinocchio()->getJointId("root_joint")
60 : 0)]
61
5/8
✓ Branch 2 taken 703 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 581 times.
✓ Branch 8 taken 122 times.
✓ Branch 10 taken 703 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 703 times.
✗ Branch 14 not taken.
2109 .nv()) {};
62 1410 virtual ~ActuationModelFloatingBaseTpl() {};
63
64 /**
65 * @brief Compute the floating-base actuation signal from the joint-torque
66 * input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
67 *
68 * @param[in] data Actuation data
69 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
70 * @param[in] u Joint-torque input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
71 */
72 54507 virtual void calc(const std::shared_ptr<Data>& data,
73 const Eigen::Ref<const VectorXs>& /*x*/,
74 const Eigen::Ref<const VectorXs>& u) {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54507 times.
54507 if (static_cast<std::size_t>(u.size()) != nu_) {
76 throw_pretty(
77 "Invalid argument: " << "u has wrong dimension (it should be " +
78 std::to_string(nu_) + ")");
79 }
80
1/2
✓ Branch 3 taken 54507 times.
✗ Branch 4 not taken.
54507 data->tau.tail(nu_) = u;
81 54507 };
82
83 /**
84 * @brief Compute the Jacobians of the floating-base actuation function
85 *
86 * @param[in] data Actuation data
87 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
88 * @param[in] u Joint-torque input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
89 */
90 #ifndef NDEBUG
91 8676 virtual void calcDiff(const std::shared_ptr<Data>& data,
92 const Eigen::Ref<const VectorXs>& /*x*/,
93 const Eigen::Ref<const VectorXs>& /*u*/) {
94 #else
95 virtual void calcDiff(const std::shared_ptr<Data>&,
96 const Eigen::Ref<const VectorXs>& /*x*/,
97 const Eigen::Ref<const VectorXs>& /*u*/) {
98 #endif
99 // The derivatives has constant values which were set in createData.
100
2/12
✓ Branch 3 taken 8676 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 8676 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
8676 assert_pretty(data->dtau_dx.isZero(), "dtau_dx has wrong value");
101
2/12
✓ Branch 4 taken 8676 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 8676 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
8676 assert_pretty(MatrixXs(data->dtau_du).isApprox(dtau_du_),
102 "dtau_du has wrong value");
103 8676 };
104
105 23375 virtual void commands(const std::shared_ptr<Data>& data,
106 const Eigen::Ref<const VectorXs>&,
107 const Eigen::Ref<const VectorXs>& tau) {
108
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 23375 times.
23375 if (static_cast<std::size_t>(tau.size()) != state_->get_nv()) {
109 throw_pretty(
110 "Invalid argument: " << "tau has wrong dimension (it should be " +
111 std::to_string(state_->get_nv()) + ")");
112 }
113
1/2
✓ Branch 3 taken 23375 times.
✗ Branch 4 not taken.
23375 data->u = tau.tail(nu_);
114 23375 }
115
116 #ifndef NDEBUG
117 3145 virtual void torqueTransform(const std::shared_ptr<Data>& data,
118 const Eigen::Ref<const VectorXs>&,
119 const Eigen::Ref<const VectorXs>&) {
120 #else
121 virtual void torqueTransform(const std::shared_ptr<Data>&,
122 const Eigen::Ref<const VectorXs>&,
123 const Eigen::Ref<const VectorXs>&) {
124 #endif
125 // The torque transform has constant values which were set in createData.
126
2/12
✓ Branch 4 taken 3145 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 3145 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
3145 assert_pretty(MatrixXs(data->Mtau).isApprox(Mtau_), "Mtau has wrong value");
127 3145 }
128
129 /**
130 * @brief Create the floating-base actuation data
131 *
132 * @return the actuation data
133 */
134 55605 virtual std::shared_ptr<Data> createData() {
135 typedef StateMultibodyTpl<Scalar> StateMultibody;
136 55605 std::shared_ptr<StateMultibody> state =
137 55605 std::static_pointer_cast<StateMultibody>(state_);
138
1/2
✓ Branch 1 taken 55605 times.
✗ Branch 2 not taken.
55605 std::shared_ptr<Data> data =
139 55605 std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
140 55605 const std::size_t root_joint_id =
141
2/4
✓ Branch 5 taken 55605 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 55605 times.
✗ Branch 9 not taken.
111210 state->get_pinocchio()->existJointName("root_joint")
142
8/14
✓ Branch 0 taken 51883 times.
✓ Branch 1 taken 3722 times.
✓ Branch 7 taken 51883 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 51883 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 51883 times.
✓ Branch 13 taken 3722 times.
✓ Branch 15 taken 51883 times.
✓ Branch 16 taken 3722 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
55605 ? state->get_pinocchio()->getJointId("root_joint")
143 : 0;
144
1/2
✓ Branch 5 taken 55605 times.
✗ Branch 6 not taken.
55605 const std::size_t nfb = state->get_pinocchio()->joints[root_joint_id].nv();
145
2/4
✓ Branch 2 taken 55605 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 55605 times.
✗ Branch 6 not taken.
55605 data->dtau_du.diagonal(-nfb).setOnes();
146
2/4
✓ Branch 2 taken 55605 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 55605 times.
✗ Branch 6 not taken.
55605 data->Mtau.diagonal(nfb).setOnes();
147
2/2
✓ Branch 0 taken 315020 times.
✓ Branch 1 taken 55605 times.
370625 for (std::size_t i = 0; i < nfb; ++i) {
148
1/2
✓ Branch 2 taken 315020 times.
✗ Branch 3 not taken.
315020 data->tau_set[i] = false;
149 }
150 #ifndef NDEBUG
151
1/2
✓ Branch 2 taken 55605 times.
✗ Branch 3 not taken.
55605 dtau_du_ = data->dtau_du;
152
1/2
✓ Branch 2 taken 55605 times.
✗ Branch 3 not taken.
55605 Mtau_ = data->Mtau;
153 #endif
154 111210 return data;
155 55605 };
156
157 protected:
158 using Base::nu_;
159 using Base::state_;
160
161 #ifndef NDEBUG
162 private:
163 MatrixXs dtau_du_;
164 MatrixXs Mtau_;
165 #endif
166 };
167
168 } // namespace crocoddyl
169
170 #endif // CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_
171