GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/multibody/residuals/centroidal-momentum.hpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 12 14 85.7%
Branches: 10 30 33.3%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021-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 #ifndef CROCODDYL_MULTIBODY_RESIDUALS_MOMENTUM_HPP_
11 #define CROCODDYL_MULTIBODY_RESIDUALS_MOMENTUM_HPP_
12
13 #include "crocoddyl/core/residual-base.hpp"
14 #include "crocoddyl/multibody/data/multibody.hpp"
15 #include "crocoddyl/multibody/fwd.hpp"
16 #include "crocoddyl/multibody/states/multibody.hpp"
17
18 namespace crocoddyl {
19
20 /**
21 * @brief Centroidal momentum residual
22 *
23 * This residual function defines the centroidal momentum tracking as
24 * \f$\mathbf{r}=\mathbf{h}-\mathbf{h}^*\f$, where
25 * \f$\mathbf{h},\mathbf{h}^*\in~\mathcal{X}\f$ are the current and reference
26 * centroidal momenta, respectively. Note that the dimension of the residual
27 * vector is 6. Furthermore, the Jacobians of the residual function are computed
28 * analytically.
29 *
30 * As described in `ResidualModelAbstractTpl()`, the residual value and its
31 * Jacobians are calculated by `calc` and `calcDiff`, respectively.
32 *
33 * \sa `ResidualModelAbstractTpl`, `calc()`, `calcDiff()`, `createData()`
34 */
35 template <typename _Scalar>
36 class ResidualModelCentroidalMomentumTpl
37 : public ResidualModelAbstractTpl<_Scalar> {
38 public:
39 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
40 CROCODDYL_DERIVED_CAST(ResidualModelBase, ResidualModelCentroidalMomentumTpl)
41
42 typedef _Scalar Scalar;
43 typedef MathBaseTpl<Scalar> MathBase;
44 typedef ResidualModelAbstractTpl<Scalar> Base;
45 typedef ResidualDataCentroidalMomentumTpl<Scalar> Data;
46 typedef StateMultibodyTpl<Scalar> StateMultibody;
47 typedef ResidualDataAbstractTpl<Scalar> ResidualDataAbstract;
48 typedef ActivationModelAbstractTpl<Scalar> ActivationModelAbstract;
49 typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract;
50 typedef typename MathBase::Vector6s Vector6s;
51 typedef typename MathBase::VectorXs VectorXs;
52 typedef typename MathBase::Matrix6xs Matrix6xs;
53
54 /**
55 * @brief Initialize the centroidal momentum residual model
56 *
57 * @param[in] state State of the multibody system
58 * @param[in] href Reference centroidal momentum
59 * @param[in] nu Dimension of the control vector
60 */
61 ResidualModelCentroidalMomentumTpl(std::shared_ptr<StateMultibody> state,
62 const Vector6s& href,
63 const std::size_t nu);
64
65 /**
66 * @brief Initialize the centroidal momentum residual model
67 *
68 * The default `nu` is obtained from `StateAbstractTpl::get_nv()`.
69 *
70 * @param[in] state State of the multibody system
71 * @param[in] href Reference centroidal momentum
72 */
73 ResidualModelCentroidalMomentumTpl(std::shared_ptr<StateMultibody> state,
74 const Vector6s& href);
75 116 virtual ~ResidualModelCentroidalMomentumTpl() = default;
76
77 /**
78 * @brief Compute the centroidal momentum residual
79 *
80 * @param[in] data Centroidal momentum residual data
81 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
82 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
83 */
84 virtual void calc(const std::shared_ptr<ResidualDataAbstract>& data,
85 const Eigen::Ref<const VectorXs>& x,
86 const Eigen::Ref<const VectorXs>& u) override;
87
88 /**
89 * @brief Compute the derivatives of the centroidal momentum residual
90 *
91 * @param[in] data Centroidal momentum residual data
92 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
93 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
94 */
95 virtual void calcDiff(const std::shared_ptr<ResidualDataAbstract>& data,
96 const Eigen::Ref<const VectorXs>& x,
97 const Eigen::Ref<const VectorXs>& u) override;
98
99 /**
100 * @brief Create the centroidal momentum residual data
101 */
102 virtual std::shared_ptr<ResidualDataAbstract> createData(
103 DataCollectorAbstract* const data) override;
104
105 /**
106 * @brief Cast the centroidal-momentum residual model to a different scalar
107 * type.
108 *
109 * It is useful for operations requiring different precision or scalar types.
110 *
111 * @tparam NewScalar The new scalar type to cast to.
112 * @return ResidualModelCentroidalMomentumTpl<NewScalar> A residual model with
113 * the new scalar type.
114 */
115 template <typename NewScalar>
116 ResidualModelCentroidalMomentumTpl<NewScalar> cast() const;
117
118 /**
119 * @brief Return the reference centroidal momentum
120 */
121 const Vector6s& get_reference() const;
122
123 /**
124 * @brief Modify the reference centroidal momentum
125 */
126 void set_reference(const Vector6s& href);
127
128 /**
129 * @brief Print relevant information of the centroidal-momentum residual
130 *
131 * @param[out] os Output stream object
132 */
133 virtual void print(std::ostream& os) const override;
134
135 protected:
136 using Base::nu_;
137 using Base::state_;
138 using Base::u_dependent_;
139
140 private:
141 Vector6s href_; //!< Reference centroidal momentum
142 std::shared_ptr<typename StateMultibody::PinocchioModel>
143 pin_model_; //!< Pinocchio model
144 };
145
146 template <typename _Scalar>
147 struct ResidualDataCentroidalMomentumTpl
148 : public ResidualDataAbstractTpl<_Scalar> {
149 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
150
151 typedef _Scalar Scalar;
152 typedef MathBaseTpl<Scalar> MathBase;
153 typedef ResidualDataAbstractTpl<Scalar> Base;
154 typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract;
155 typedef typename MathBase::Matrix6xs Matrix6xs;
156
157 template <template <typename Scalar> class Model>
158 2406 ResidualDataCentroidalMomentumTpl(Model<Scalar>* const model,
159 DataCollectorAbstract* const data)
160 : Base(model, data),
161
2/4
✓ Branch 2 taken 2404 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2404 times.
✗ Branch 6 not taken.
2406 dhd_dq(6, model->get_state()->get_nv()),
162
4/8
✓ Branch 2 taken 2404 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2404 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2404 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 2404 times.
✗ Branch 13 not taken.
4812 dhd_dv(6, model->get_state()->get_nv()) {
163
1/2
✓ Branch 1 taken 2404 times.
✗ Branch 2 not taken.
2406 dhd_dq.setZero();
164
1/2
✓ Branch 1 taken 2404 times.
✗ Branch 2 not taken.
2406 dhd_dv.setZero();
165
166 // Check that proper shared data has been passed
167 2406 DataCollectorMultibodyTpl<Scalar>* d =
168
1/2
✓ Branch 0 taken 2404 times.
✗ Branch 1 not taken.
2406 dynamic_cast<DataCollectorMultibodyTpl<Scalar>*>(shared);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2404 times.
2406 if (d == NULL) {
170 throw_pretty(
171 "Invalid argument: the shared data should be derived from "
172 "DataCollectorMultibody");
173 }
174
175 // Avoids data casting at runtime
176 2406 pinocchio = d->pinocchio;
177 2406 }
178 4808 virtual ~ResidualDataCentroidalMomentumTpl() = default;
179
180 pinocchio::DataTpl<Scalar>* pinocchio; //!< Pinocchio data
181 Matrix6xs dhd_dq; //!< Jacobian of the centroidal momentum
182 Matrix6xs dhd_dv; //!< Jacobian of the centroidal momentum
183 using Base::r;
184 using Base::Ru;
185 using Base::Rx;
186 using Base::shared;
187 };
188
189 } // namespace crocoddyl
190
191 /* --- Details -------------------------------------------------------------- */
192 /* --- Details -------------------------------------------------------------- */
193 /* --- Details -------------------------------------------------------------- */
194 #include "crocoddyl/multibody/residuals/centroidal-momentum.hxx"
195
196 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
197 crocoddyl::ResidualModelCentroidalMomentumTpl)
198 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(
199 crocoddyl::ResidualDataCentroidalMomentumTpl)
200
201 #endif // CROCODDYL_MULTIBODY_RESIDUALS_MOMENTUM_HPP_
202