Directory: | ./ |
---|---|
File: | include/crocoddyl/multibody/residuals/contact-control-gravity.hpp |
Date: | 2025-01-30 11:01:55 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 10 | 11 | 90.9% |
Branches: | 7 | 22 | 31.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2020-2024, 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_CONTACT_CONTROL_GRAVITY_HPP_ | ||
11 | #define CROCODDYL_MULTIBODY_RESIDUALS_CONTACT_CONTROL_GRAVITY_HPP_ | ||
12 | |||
13 | #include "crocoddyl/core/residual-base.hpp" | ||
14 | #include "crocoddyl/core/utils/exception.hpp" | ||
15 | #include "crocoddyl/multibody/data/contacts.hpp" | ||
16 | #include "crocoddyl/multibody/states/multibody.hpp" | ||
17 | |||
18 | namespace crocoddyl { | ||
19 | |||
20 | /** | ||
21 | * @brief Control gravity residual under contact | ||
22 | * | ||
23 | * This residual function is defined as | ||
24 | * \f$\mathbf{r}=\mathbf{u}-(\mathbf{g}(\mathbf{q}) - \sum | ||
25 | * \mathbf{J}_c(\mathbf{q})^{\top} \mathbf{f}_c)\f$, where | ||
26 | * \f$\mathbf{u}\in~\mathbb{R}^{nu}\f$ is the current control input, | ||
27 | * \f$\mathbf{J}_c(\mathbf{q})\f$ is the contact Jacobians, \f$\mathbf{f}_c\f$ | ||
28 | * contains the contact forces, \f$\mathbf{g}(\mathbf{q})\f$ is the gravity | ||
29 | * torque corresponding to the current configuration, | ||
30 | * \f$\mathbf{q}\in~\mathbb{R}^{nq}\f$ is the current position joints input. | ||
31 | * Note that the dimension of the residual vector is obtained from | ||
32 | * `state->get_nv()`. | ||
33 | * | ||
34 | * As described in `ResidualModelAbstractTpl()`, the residual value and its | ||
35 | * Jacobians are calculated by `calc()` and `calcDiff()`, respectively. | ||
36 | * | ||
37 | * \sa `ResidualModelAbstractTpl`, `calc()`, `calcDiff()`, `createData()` | ||
38 | */ | ||
39 | template <typename _Scalar> | ||
40 | class ResidualModelContactControlGravTpl | ||
41 | : public ResidualModelAbstractTpl<_Scalar> { | ||
42 | public: | ||
43 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
44 | |||
45 | typedef _Scalar Scalar; | ||
46 | typedef MathBaseTpl<Scalar> MathBase; | ||
47 | typedef ResidualModelAbstractTpl<Scalar> Base; | ||
48 | typedef ResidualDataContactControlGravTpl<Scalar> Data; | ||
49 | typedef ResidualDataAbstractTpl<Scalar> ResidualDataAbstract; | ||
50 | typedef StateMultibodyTpl<Scalar> StateMultibody; | ||
51 | typedef ActuationModelAbstractTpl<Scalar> ActuationModelAbstract; | ||
52 | typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract; | ||
53 | typedef typename MathBase::VectorXs VectorXs; | ||
54 | typedef typename MathBase::MatrixXs MatrixXs; | ||
55 | |||
56 | /** | ||
57 | * @brief Initialize the contact control gravity contact residual model | ||
58 | * | ||
59 | * @param[in] state State of the multibody system | ||
60 | * @param[in] nu Dimension of the control vector | ||
61 | */ | ||
62 | ResidualModelContactControlGravTpl(std::shared_ptr<StateMultibody> state, | ||
63 | const std::size_t nu); | ||
64 | |||
65 | /** | ||
66 | * @brief Initialize the contact control gravity contact residual model | ||
67 | * | ||
68 | * The default `nu` value is obtained from `StateAbstractTpl::get_nv()`. | ||
69 | * | ||
70 | * @param[in] state State of the multibody system | ||
71 | */ | ||
72 | explicit ResidualModelContactControlGravTpl( | ||
73 | std::shared_ptr<StateMultibody> state); | ||
74 | virtual ~ResidualModelContactControlGravTpl(); | ||
75 | |||
76 | /** | ||
77 | * @brief Compute the contact control gravity contact residual | ||
78 | * | ||
79 | * @param[in] data Contact control gravity residual data | ||
80 | * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ | ||
81 | * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ | ||
82 | */ | ||
83 | virtual void calc(const std::shared_ptr<ResidualDataAbstract>& data, | ||
84 | const Eigen::Ref<const VectorXs>& x, | ||
85 | const Eigen::Ref<const VectorXs>& u); | ||
86 | |||
87 | /** | ||
88 | * @brief Compute the residual vector for nodes that depends only on the state | ||
89 | * | ||
90 | * It updates the residual vector based on the state only (i.e., it ignores | ||
91 | * the contact forces). This function is used in the terminal nodes of an | ||
92 | * optimal control problem. | ||
93 | * | ||
94 | * @param[in] data Residual data | ||
95 | * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ | ||
96 | */ | ||
97 | virtual void calc(const std::shared_ptr<ResidualDataAbstract>& data, | ||
98 | const Eigen::Ref<const VectorXs>& x); | ||
99 | |||
100 | /** | ||
101 | * @brief Compute the Jacobians of the contact control gravity contact | ||
102 | * residual | ||
103 | * | ||
104 | * @param[in] data Contact control gravity residual data | ||
105 | * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ | ||
106 | * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ | ||
107 | */ | ||
108 | virtual void calcDiff(const std::shared_ptr<ResidualDataAbstract>& data, | ||
109 | const Eigen::Ref<const VectorXs>& x, | ||
110 | const Eigen::Ref<const VectorXs>& u); | ||
111 | |||
112 | /** | ||
113 | * @brief Compute the Jacobian of the residual functions with respect to the | ||
114 | * state only | ||
115 | * | ||
116 | * It updates the Jacobian of the residual function based on the state only | ||
117 | * (i.e., it ignores the contact forces). This function is used in the | ||
118 | * terminal nodes of an optimal control problem. | ||
119 | * | ||
120 | * @param[in] data Residual data | ||
121 | * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ | ||
122 | */ | ||
123 | virtual void calcDiff(const std::shared_ptr<ResidualDataAbstract>& data, | ||
124 | const Eigen::Ref<const VectorXs>& x); | ||
125 | |||
126 | /** | ||
127 | * @brief Create the contact-control-gravity residual data | ||
128 | */ | ||
129 | virtual std::shared_ptr<ResidualDataAbstract> createData( | ||
130 | DataCollectorAbstract* const data); | ||
131 | |||
132 | /** | ||
133 | * @brief Print relevant information of the contact-control-grav residual | ||
134 | * | ||
135 | * @param[out] os Output stream object | ||
136 | */ | ||
137 | virtual void print(std::ostream& os) const; | ||
138 | |||
139 | protected: | ||
140 | using Base::nu_; | ||
141 | using Base::state_; | ||
142 | using Base::v_dependent_; | ||
143 | |||
144 | private: | ||
145 | typename StateMultibody::PinocchioModel pin_model_; | ||
146 | }; | ||
147 | |||
148 | template <typename _Scalar> | ||
149 | struct ResidualDataContactControlGravTpl | ||
150 | : public ResidualDataAbstractTpl<_Scalar> { | ||
151 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
152 | |||
153 | typedef _Scalar Scalar; | ||
154 | typedef MathBaseTpl<Scalar> MathBase; | ||
155 | typedef ResidualDataAbstractTpl<Scalar> Base; | ||
156 | typedef StateMultibodyTpl<Scalar> StateMultibody; | ||
157 | typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract; | ||
158 | typedef pinocchio::DataTpl<Scalar> PinocchioData; | ||
159 | |||
160 | template <template <typename Scalar> class Model> | ||
161 | 2774 | ResidualDataContactControlGravTpl(Model<Scalar>* const model, | |
162 | DataCollectorAbstract* const data) | ||
163 |
2/4✓ Branch 2 taken 2774 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2774 times.
✗ Branch 7 not taken.
|
2774 | : Base(model, data) { |
164 | 2774 | StateMultibody* sm = static_cast<StateMultibody*>(model->get_state().get()); | |
165 |
2/4✓ Branch 3 taken 2774 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2774 times.
✗ Branch 7 not taken.
|
2774 | pinocchio = PinocchioData(*(sm->get_pinocchio().get())); |
166 | |||
167 | // Check that proper shared data has been passed | ||
168 | 2774 | DataCollectorActMultibodyInContactTpl<Scalar>* d = | |
169 |
1/2✓ Branch 0 taken 2774 times.
✗ Branch 1 not taken.
|
2774 | dynamic_cast<DataCollectorActMultibodyInContactTpl<Scalar>*>(shared); |
170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2774 times.
|
2774 | if (d == NULL) { |
171 | ✗ | throw_pretty( | |
172 | "Invalid argument: the shared data should be derived from " | ||
173 | "DataCollectorActMultibodyInContactTpl"); | ||
174 | } | ||
175 | // Avoids data casting at runtime | ||
176 | // pinocchio = d->pinocchio; | ||
177 |
1/2✓ Branch 2 taken 2774 times.
✗ Branch 3 not taken.
|
2774 | fext = d->contacts->fext; |
178 | 2774 | actuation = d->actuation; | |
179 | 2774 | } | |
180 | |||
181 | PinocchioData pinocchio; //!< Pinocchio data | ||
182 | std::shared_ptr<ActuationDataAbstractTpl<Scalar> > | ||
183 | actuation; //!< Actuation data | ||
184 | pinocchio::container::aligned_vector<pinocchio::ForceTpl<Scalar> > | ||
185 | fext; //!< External spatial forces | ||
186 | using Base::r; | ||
187 | using Base::Ru; | ||
188 | using Base::Rx; | ||
189 | using Base::shared; | ||
190 | }; | ||
191 | |||
192 | } // namespace crocoddyl | ||
193 | |||
194 | /* --- Details -------------------------------------------------------------- */ | ||
195 | /* --- Details -------------------------------------------------------------- */ | ||
196 | /* --- Details -------------------------------------------------------------- */ | ||
197 | #include "crocoddyl/multibody/residuals/contact-control-gravity.hxx" | ||
198 | |||
199 | #endif // CROCODDYL_MULTIBODY_RESIDUALS_CONTACT_CONTROL_GRAVITY_HPP_ | ||
200 |