GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/multibody/residuals/control-gravity.hpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 9 10 90.0%
Branches: 5 18 27.8%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2020-2021, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
7 ///////////////////////////////////////////////////////////////////////////////
8
9 #ifndef CROCODDYL_MULTIBODY_RESIDUALS_CONTROL_GRAVITY_HPP_
10 #define CROCODDYL_MULTIBODY_RESIDUALS_CONTROL_GRAVITY_HPP_
11
12 #include "crocoddyl/core/residual-base.hpp"
13 #include "crocoddyl/core/utils/exception.hpp"
14 #include "crocoddyl/multibody/data/multibody.hpp"
15 #include "crocoddyl/multibody/states/multibody.hpp"
16
17 namespace crocoddyl {
18
19 /**
20 * @brief Control gravity residual
21 *
22 * This residual function is defined as
23 * \f$\mathbf{r}=\mathbf{u}-\mathbf{g}(\mathbf{q})\f$, where
24 * \f$\mathbf{u}\in~\mathbb{R}^{nu}\f$ is the current control input,
25 * \f$\mathbf{g}(\mathbf{q})\f$ is the gravity torque corresponding to the
26 * current configuration, \f$\mathbf{q}\in~\mathbb{R}^{nq}\f$ the current
27 * position joints input. Note that the dimension of the residual vector is
28 * obtained from `StateAbstractTpl::get_nv()`.
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 ResidualModelControlGravTpl : public ResidualModelAbstractTpl<_Scalar> {
37 public:
38 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
39
40 typedef _Scalar Scalar;
41 typedef MathBaseTpl<Scalar> MathBase;
42 typedef ResidualModelAbstractTpl<Scalar> Base;
43 typedef ResidualDataControlGravTpl<Scalar> Data;
44 typedef ResidualDataAbstractTpl<Scalar> ResidualDataAbstract;
45 typedef StateMultibodyTpl<Scalar> StateMultibody;
46 typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract;
47 typedef typename MathBase::VectorXs VectorXs;
48 typedef typename MathBase::MatrixXs MatrixXs;
49
50 /**
51 * @brief Initialize the control gravity residual model
52 *
53 * @param[in] state State of the multibody system
54 * @param[in] nu Dimension of control vector
55 */
56 ResidualModelControlGravTpl(boost::shared_ptr<StateMultibody> state,
57 const std::size_t nu);
58
59 /**
60 * @brief Initialize the control gravity residual model
61 *
62 * The default `nu` value is obtained from `StateAbstractTpl::get_nv()`.
63 *
64 * @param[in] state State of the multibody system
65 */
66 ResidualModelControlGravTpl(boost::shared_ptr<StateMultibody> state);
67 virtual ~ResidualModelControlGravTpl();
68
69 /**
70 * @brief Compute the control gravity residual
71 *
72 * @param[in] data Control residual data
73 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
74 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
75 */
76 virtual void calc(const boost::shared_ptr<ResidualDataAbstract> &data,
77 const Eigen::Ref<const VectorXs> &x,
78 const Eigen::Ref<const VectorXs> &u);
79
80 /**
81 * @brief @copydoc Base::calc(const boost::shared_ptr<ResidualDataAbstract>&
82 * data, const Eigen::Ref<const VectorXs>& x)
83 */
84 virtual void calc(const boost::shared_ptr<ResidualDataAbstract> &data,
85 const Eigen::Ref<const VectorXs> &x);
86
87 /**
88 * @brief Compute the Jacobians of the control gravity residual
89 *
90 * @param[in] data Control residual data
91 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
92 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
93 */
94 virtual void calcDiff(const boost::shared_ptr<ResidualDataAbstract> &data,
95 const Eigen::Ref<const VectorXs> &x,
96 const Eigen::Ref<const VectorXs> &u);
97
98 /**
99 * @brief @copydoc Base::calcDiff(const
100 * boost::shared_ptr<ResidualDataAbstract>& data, const Eigen::Ref<const
101 * VectorXs>& x)
102 */
103 virtual void calcDiff(const boost::shared_ptr<ResidualDataAbstract> &data,
104 const Eigen::Ref<const VectorXs> &x);
105
106 virtual boost::shared_ptr<ResidualDataAbstract> createData(
107 DataCollectorAbstract *const data);
108
109 /**
110 * @brief Print relevant information of the control-grav residual
111 *
112 * @param[out] os Output stream object
113 */
114 virtual void print(std::ostream &os) const;
115
116 protected:
117 using Base::nu_;
118 using Base::state_;
119 using Base::v_dependent_;
120
121 private:
122 typename StateMultibody::PinocchioModel
123 pin_model_; //!< Pinocchio model used for internal computations
124 };
125
126 template <typename _Scalar>
127 struct ResidualDataControlGravTpl : public ResidualDataAbstractTpl<_Scalar> {
128 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
129
130 typedef _Scalar Scalar;
131 typedef MathBaseTpl<Scalar> MathBase;
132 typedef ResidualDataAbstractTpl<Scalar> Base;
133 typedef StateMultibodyTpl<Scalar> StateMultibody;
134 typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract;
135 typedef pinocchio::DataTpl<Scalar> PinocchioData;
136
137 template <template <typename Scalar> class Model>
138 2845 ResidualDataControlGravTpl(Model<Scalar> *const model,
139 DataCollectorAbstract *const data)
140
1/2
✓ Branch 2 taken 2845 times.
✗ Branch 3 not taken.
2845 : Base(model, data) {
141 // Check that proper shared data has been passed
142 2845 DataCollectorActMultibodyTpl<Scalar> *d =
143
1/2
✓ Branch 0 taken 2845 times.
✗ Branch 1 not taken.
2845 dynamic_cast<DataCollectorActMultibodyTpl<Scalar> *>(shared);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2845 times.
2845 if (d == NULL) {
145 throw_pretty(
146 "Invalid argument: the shared data should be derived from "
147 "DataCollectorActMultibodyTpl");
148 }
149 // Avoids data casting at runtime
150 StateMultibody *sm =
151 2845 static_cast<StateMultibody *>(model->get_state().get());
152
2/4
✓ Branch 3 taken 2845 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2845 times.
✗ Branch 7 not taken.
2845 pinocchio = PinocchioData(*(sm->get_pinocchio().get()));
153 2845 actuation = d->actuation;
154 2845 }
155
156 PinocchioData pinocchio; //!< Pinocchio data
157 boost::shared_ptr<ActuationDataAbstractTpl<Scalar> >
158 actuation; //!< Actuation data
159 using Base::r;
160 using Base::Ru;
161 using Base::Rx;
162 using Base::shared;
163 };
164
165 } // namespace crocoddyl
166
167 /* --- Details -------------------------------------------------------------- */
168 /* --- Details -------------------------------------------------------------- */
169 /* --- Details -------------------------------------------------------------- */
170 #include "crocoddyl/multibody/residuals/control-gravity.hxx"
171
172 #endif // CROCODDYL_MULTIBODY_RESIDUALS_CONTROL_GRAVITY_HPP_
173