GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/multibody/contacts/multiple-contacts.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 22 25 88.0%
Branches: 17 32 53.1%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, 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_CONTACTS_MULTIPLE_CONTACTS_HPP_
11 #define CROCODDYL_MULTIBODY_CONTACTS_MULTIPLE_CONTACTS_HPP_
12
13 #include <map>
14 #include <set>
15 #include <string>
16 #include <utility>
17
18 #include "crocoddyl/core/utils/exception.hpp"
19 #include "crocoddyl/multibody/contact-base.hpp"
20 #include "crocoddyl/multibody/fwd.hpp"
21
22 namespace crocoddyl {
23
24 template <typename _Scalar>
25 struct ContactItemTpl {
26 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27
28 typedef _Scalar Scalar;
29 typedef ContactModelAbstractTpl<Scalar> ContactModelAbstract;
30
31 ContactItemTpl() {}
32 1717 ContactItemTpl(const std::string& name,
33 std::shared_ptr<ContactModelAbstract> contact,
34 const bool active = true)
35 1717 : name(name), contact(contact), active(active) {}
36
37 /**
38 * @brief Print information on the contact item
39 */
40 friend std::ostream& operator<<(std::ostream& os,
41 const ContactItemTpl<Scalar>& model) {
42 os << "{" << *model.contact << "}";
43 return os;
44 }
45
46 std::string name;
47 std::shared_ptr<ContactModelAbstract> contact;
48 bool active;
49 };
50
51 /**
52 * @brief Define a stack of contact models
53 *
54 * The contact models can be defined with active and inactive status. The idea
55 * behind this design choice is to be able to create a mechanism that allocates
56 * the entire data needed for the computations. Then, there are designed
57 * routines that update the only active contacts.
58 */
59 template <typename _Scalar>
60 class ContactModelMultipleTpl {
61 public:
62 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63
64 typedef _Scalar Scalar;
65 typedef MathBaseTpl<Scalar> MathBase;
66 typedef StateMultibodyTpl<Scalar> StateMultibody;
67 typedef ContactDataAbstractTpl<Scalar> ContactDataAbstract;
68 typedef ContactDataMultipleTpl<Scalar> ContactDataMultiple;
69 typedef ContactModelAbstractTpl<Scalar> ContactModelAbstract;
70
71 typedef ContactItemTpl<Scalar> ContactItem;
72
73 typedef typename MathBase::Vector2s Vector2s;
74 typedef typename MathBase::Vector3s Vector3s;
75 typedef typename MathBase::VectorXs VectorXs;
76 typedef typename MathBase::MatrixXs MatrixXs;
77
78 typedef std::map<std::string, std::shared_ptr<ContactItem> >
79 ContactModelContainer;
80 typedef std::map<std::string, std::shared_ptr<ContactDataAbstract> >
81 ContactDataContainer;
82 typedef typename pinocchio::container::aligned_vector<
83 pinocchio::ForceTpl<Scalar> >::iterator ForceIterator;
84
85 /**
86 * @brief Initialize the multi-contact model
87 *
88 * @param[in] state Multibody state
89 * @param[in] nu Dimension of control vector
90 */
91 ContactModelMultipleTpl(std::shared_ptr<StateMultibody> state,
92 const std::size_t nu);
93
94 /**
95 * @brief Initialize the multi-contact model
96 *
97 * @param[in] state Multibody state
98 */
99 ContactModelMultipleTpl(std::shared_ptr<StateMultibody> state);
100 ~ContactModelMultipleTpl();
101
102 /**
103 * @brief Add contact item
104 *
105 * Note that the memory is allocated for inactive contacts as well.
106 *
107 * @param[in] name Contact name
108 * @param[in] contact Contact model
109 * @param[in] active Contact status (active by default)
110 */
111 void addContact(const std::string& name,
112 std::shared_ptr<ContactModelAbstract> contact,
113 const bool active = true);
114
115 /**
116 * @brief Remove contact item
117 *
118 * @param[in] name Contact name
119 */
120 void removeContact(const std::string& name);
121
122 /**
123 * @brief Change the contact status
124 *
125 * @param[in] name Contact name
126 * @param[in] active Contact status (True for active)
127 */
128 void changeContactStatus(const std::string& name, const bool active);
129
130 /**
131 * @brief Compute the contact Jacobian and contact acceleration
132 *
133 * @param[in] data Multi-contact data
134 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
135 */
136 void calc(const std::shared_ptr<ContactDataMultiple>& data,
137 const Eigen::Ref<const VectorXs>& x);
138
139 /**
140 * @brief Compute the derivatives of the contact holonomic constraint
141 *
142 * @param[in] data Multi-contact data
143 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
144 */
145 void calcDiff(const std::shared_ptr<ContactDataMultiple>& data,
146 const Eigen::Ref<const VectorXs>& x);
147
148 /**
149 * @brief Update the constrained system acceleration
150 *
151 * @param[in] data Multi-contact data
152 * @param[in] dv Constrained system acceleration
153 * \f$\dot{\mathbf{v}}\in\mathbb{R}^{nv}\f$
154 */
155 void updateAcceleration(const std::shared_ptr<ContactDataMultiple>& data,
156 const VectorXs& dv) const;
157
158 /**
159 * @brief Update the spatial force defined in frame coordinate
160 *
161 * @param[in] data Multi-contact data
162 * @param[in] force Spatial force defined in frame coordinate
163 * \f${}^o\underline{\boldsymbol{\lambda}}_c\in\mathbb{R}^{nc}\f$
164 */
165 void updateForce(const std::shared_ptr<ContactDataMultiple>& data,
166 const VectorXs& force);
167
168 /**
169 * @brief Update the Jacobian of the constrained system acceleration
170 *
171 * @param[in] data Multi-contact data
172 * @param[in] ddv_dx Jacobian of the system acceleration in generalized
173 * coordinates
174 * \f$\frac{\partial\dot{\mathbf{v}}}{\partial\mathbf{x}}\in\mathbb{R}^{nv\times
175 * ndx}\f$
176 */
177 void updateAccelerationDiff(const std::shared_ptr<ContactDataMultiple>& data,
178 const MatrixXs& ddv_dx) const;
179
180 /**
181 * @brief Update the Jacobian of the spatial force defined in frame coordinate
182 *
183 * @param[in] data Multi-contact data
184 * @param[in] df_dx Jacobian of the spatial impulse defined in frame
185 * coordinate
186 * \f$\frac{\partial{}^o\underline{\boldsymbol{\lambda}}_c}{\partial\mathbf{x}}\in\mathbb{R}^{nc\times{ndx}}\f$
187 * @param[in] df_du Jacobian of the spatial impulse defined in frame
188 * coordinate
189 * \f$\frac{\partial{}^o\underline{\boldsymbol{\lambda}}_c}{\partial\mathbf{u}}\in\mathbb{R}^{nc\times{nu}}\f$
190 */
191 void updateForceDiff(const std::shared_ptr<ContactDataMultiple>& data,
192 const MatrixXs& df_dx, const MatrixXs& df_du) const;
193
194 /**
195 * @brief Update the RNEA derivatives dtau_dq by adding the skew term
196 * (necessary for contacts expressed in LOCAL_WORLD_ALIGNED / WORLD)
197 *
198 * To learn more about the computation of the contact derivatives in different
199 * frames see https://hal.science/hal-03758989/document.
200 *
201 * @param[in] data Multi-contact data
202 * @param[in] pinocchio Pinocchio data
203 */
204 void updateRneaDiff(const std::shared_ptr<ContactDataMultiple>& data,
205 pinocchio::DataTpl<Scalar>& pinocchio) const;
206
207 /**
208 * @brief Create the multi-contact data
209 *
210 * @param[in] data Pinocchio data
211 * @return the multi-contact data.
212 */
213 std::shared_ptr<ContactDataMultiple> createData(
214 pinocchio::DataTpl<Scalar>* const data);
215
216 /**
217 * @brief Return the multibody state
218 */
219 const std::shared_ptr<StateMultibody>& get_state() const;
220
221 /**
222 * @brief Return the contact models
223 */
224 const ContactModelContainer& get_contacts() const;
225
226 /**
227 * @brief Return the dimension of active contacts
228 */
229 std::size_t get_nc() const;
230
231 /**
232 * @brief Return the dimension of all contacts
233 */
234 std::size_t get_nc_total() const;
235
236 /**
237 * @brief Return the dimension of control vector
238 */
239 std::size_t get_nu() const;
240
241 /**
242 * @brief Return the names of the set of active contacts
243 */
244 const std::set<std::string>& get_active_set() const;
245
246 /**
247 * @brief Return the names of the set of inactive contacts
248 */
249 const std::set<std::string>& get_inactive_set() const;
250
251 /**
252 * @brief Return the status of a given contact name
253 */
254 bool getContactStatus(const std::string& name) const;
255
256 /**
257 * @brief Return the type of contact computation
258 *
259 * True for all contacts, otherwise false for active contacts
260 */
261 bool getComputeAllContacts() const;
262
263 /**
264 * @brief Set the tyoe of contact computation: all or active contacts
265 *
266 * @param status Type of contact computation (true for all contacts and false
267 * for active contacts)
268 */
269 void setComputeAllContacts(const bool status);
270
271 /**
272 * @brief Print information on the contact models
273 */
274 template <class Scalar>
275 friend std::ostream& operator<<(std::ostream& os,
276 const ContactModelMultipleTpl<Scalar>& model);
277
278 private:
279 std::shared_ptr<StateMultibody> state_;
280 ContactModelContainer contacts_;
281 std::size_t nc_;
282 std::size_t nc_total_;
283 std::size_t nu_;
284 std::set<std::string> active_set_;
285 std::set<std::string> inactive_set_;
286 bool compute_all_contacts_;
287 };
288
289 /**
290 * @brief Define the multi-contact data
291 *
292 * \sa ContactModelMultipleTpl
293 */
294 template <typename _Scalar>
295 struct ContactDataMultipleTpl {
296 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
297
298 typedef _Scalar Scalar;
299 typedef MathBaseTpl<Scalar> MathBase;
300 typedef ContactModelMultipleTpl<Scalar> ContactModelMultiple;
301 typedef ContactItemTpl<Scalar> ContactItem;
302 typedef typename MathBase::VectorXs VectorXs;
303 typedef typename MathBase::MatrixXs MatrixXs;
304
305 /**
306 * @brief Initialized a multi-contact data
307 *
308 * @param[in] model Multi-contact model
309 * @param[in] data Pinocchio data
310 */
311 template <template <typename Scalar> class Model>
312 70999 ContactDataMultipleTpl(Model<Scalar>* const model,
313 pinocchio::DataTpl<Scalar>* const data)
314
1/2
✓ Branch 5 taken 70999 times.
✗ Branch 6 not taken.
70999 : Jc(model->get_nc_total(), model->get_state()->get_nv()),
315
1/2
✓ Branch 2 taken 70999 times.
✗ Branch 3 not taken.
70999 a0(model->get_nc_total()),
316
1/2
✓ Branch 5 taken 70999 times.
✗ Branch 6 not taken.
70999 da0_dx(model->get_nc_total(), model->get_state()->get_ndx()),
317
1/2
✓ Branch 4 taken 70999 times.
✗ Branch 5 not taken.
70999 dv(model->get_state()->get_nv()),
318
1/2
✓ Branch 7 taken 70999 times.
✗ Branch 8 not taken.
70999 ddv_dx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
319
2/4
✓ Branch 2 taken 70999 times.
✗ Branch 3 not taken.
✓ Branch 9 taken 70999 times.
✗ Branch 10 not taken.
70999 fext(model->get_state()->get_pinocchio()->njoints,
320 70999 pinocchio::ForceTpl<Scalar>::Zero()) {
321
1/2
✓ Branch 1 taken 70999 times.
✗ Branch 2 not taken.
70999 Jc.setZero();
322
1/2
✓ Branch 1 taken 70999 times.
✗ Branch 2 not taken.
70999 a0.setZero();
323
1/2
✓ Branch 1 taken 70999 times.
✗ Branch 2 not taken.
70999 da0_dx.setZero();
324
1/2
✓ Branch 1 taken 70999 times.
✗ Branch 2 not taken.
70999 dv.setZero();
325
1/2
✓ Branch 1 taken 70999 times.
✗ Branch 2 not taken.
70999 ddv_dx.setZero();
326 70999 for (typename ContactModelMultiple::ContactModelContainer::const_iterator
327 70999 it = model->get_contacts().begin();
328
2/2
✓ Branch 3 taken 160814 times.
✓ Branch 4 taken 70999 times.
231813 it != model->get_contacts().end(); ++it) {
329 160814 const std::shared_ptr<ContactItem>& item = it->second;
330
1/2
✓ Branch 1 taken 160814 times.
✗ Branch 2 not taken.
160814 contacts.insert(
331
2/4
✓ Branch 3 taken 160814 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 160814 times.
✗ Branch 8 not taken.
160814 std::make_pair(item->name, item->contact->createData(data)));
332 }
333 70999 }
334
335 MatrixXs Jc; //!< Contact Jacobian in frame coordinate
336 //!< \f$\mathbf{J}_c\in\mathbb{R}^{nc_{total}\times{nv}}\f$
337 //!< (memory defined for active and inactive contacts)
338 VectorXs a0; //!< Desired spatial contact acceleration in frame coordinate
339 //!< \f$\underline{\mathbf{a}}_0\in\mathbb{R}^{nc_{total}}\f$
340 //!< (memory defined for active and inactive contacts)
341 MatrixXs
342 da0_dx; //!< Jacobian of the desired spatial contact acceleration in
343 //!< frame coordinate
344 //!< \f$\frac{\partial\underline{\mathbf{a}}_0}{\partial\mathbf{x}}\in\mathbb{R}^{nc_{total}\times{ndx}}\f$
345 //!< (memory defined for active and inactive contacts)
346 VectorXs dv; //!< Constrained system acceleration in generalized coordinates
347 //!< \f$\dot{\mathbf{v}}\in\mathbb{R}^{nv}\f$
348 MatrixXs
349 ddv_dx; //!< Jacobian of the system acceleration in generalized
350 //!< coordinates
351 //!< \f$\frac{\partial\dot{\mathbf{v}}}{\partial\mathbf{x}}\in\mathbb{R}^{nv\times
352 //!< ndx}\f$
353 typename ContactModelMultiple::ContactDataContainer
354 contacts; //!< Stack of contact data
355 pinocchio::container::aligned_vector<pinocchio::ForceTpl<Scalar> >
356 fext; //!< External spatial forces in body coordinates
357 };
358
359 } // namespace crocoddyl
360
361 /* --- Details -------------------------------------------------------------- */
362 /* --- Details -------------------------------------------------------------- */
363 /* --- Details -------------------------------------------------------------- */
364 #include "crocoddyl/multibody/contacts/multiple-contacts.hxx"
365
366 #endif // CROCODDYL_MULTIBODY_CONTACTS_MULTIPLE_CONTACTS_HPP_
367