Crocoddyl
multiple-contacts.hpp
1 // 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.
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>
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  typedef _Scalar Scalar;
30 
31  ContactItemTpl() {}
32  ContactItemTpl(const std::string& name,
33  boost::shared_ptr<ContactModelAbstract> contact,
34  const bool active = true)
35  : name(name), contact(contact), active(active) {}
36 
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  boost::shared_ptr<ContactModelAbstract> contact;
48  bool active;
49 };
50 
59 template <typename _Scalar>
61  public:
62  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63 
64  typedef _Scalar Scalar;
70 
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, boost::shared_ptr<ContactItem> >
79  ContactModelContainer;
80  typedef std::map<std::string, boost::shared_ptr<ContactDataAbstract> >
81  ContactDataContainer;
82  typedef typename pinocchio::container::aligned_vector<
83  pinocchio::ForceTpl<Scalar> >::iterator ForceIterator;
84 
91  ContactModelMultipleTpl(boost::shared_ptr<StateMultibody> state,
92  const std::size_t nu);
93 
99  ContactModelMultipleTpl(boost::shared_ptr<StateMultibody> state);
101 
111  void addContact(const std::string& name,
112  boost::shared_ptr<ContactModelAbstract> contact,
113  const bool active = true);
114 
120  void removeContact(const std::string& name);
121 
128  void changeContactStatus(const std::string& name, const bool active);
129 
136  void calc(const boost::shared_ptr<ContactDataMultiple>& data,
137  const Eigen::Ref<const VectorXs>& x);
138 
145  void calcDiff(const boost::shared_ptr<ContactDataMultiple>& data,
146  const Eigen::Ref<const VectorXs>& x);
147 
155  void updateAcceleration(const boost::shared_ptr<ContactDataMultiple>& data,
156  const VectorXs& dv) const;
157 
165  void updateForce(const boost::shared_ptr<ContactDataMultiple>& data,
166  const VectorXs& force);
167 
178  const boost::shared_ptr<ContactDataMultiple>& data,
179  const MatrixXs& ddv_dx) const;
180 
192  void updateForceDiff(const boost::shared_ptr<ContactDataMultiple>& data,
193  const MatrixXs& df_dx, const MatrixXs& df_du) const;
194 
205  void updateRneaDiff(const boost::shared_ptr<ContactDataMultiple>& data,
206  pinocchio::DataTpl<Scalar>& pinocchio) const;
207 
214  boost::shared_ptr<ContactDataMultiple> createData(
215  pinocchio::DataTpl<Scalar>* const data);
216 
220  const boost::shared_ptr<StateMultibody>& get_state() const;
221 
225  const ContactModelContainer& get_contacts() const;
226 
230  std::size_t get_nc() const;
231 
235  std::size_t get_nc_total() const;
236 
240  std::size_t get_nu() const;
241 
245  const std::set<std::string>& get_active_set() const;
246 
250  const std::set<std::string>& get_inactive_set() const;
251 
255  bool getContactStatus(const std::string& name) const;
256 
262  bool getComputeAllContacts() const;
263 
270  void setComputeAllContacts(const bool status);
271 
275  template <class Scalar>
276  friend std::ostream& operator<<(std::ostream& os,
277  const ContactModelMultipleTpl<Scalar>& model);
278 
279  private:
280  boost::shared_ptr<StateMultibody> state_;
281  ContactModelContainer contacts_;
282  std::size_t nc_;
283  std::size_t nc_total_;
284  std::size_t nu_;
285  std::set<std::string> active_set_;
286  std::set<std::string> inactive_set_;
287  bool compute_all_contacts_;
288 };
289 
295 template <typename _Scalar>
297  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
298 
299  typedef _Scalar Scalar;
303  typedef typename MathBase::VectorXs VectorXs;
304  typedef typename MathBase::MatrixXs MatrixXs;
305 
312  template <template <typename Scalar> class Model>
313  ContactDataMultipleTpl(Model<Scalar>* const model,
314  pinocchio::DataTpl<Scalar>* const data)
315  : Jc(model->get_nc_total(), model->get_state()->get_nv()),
316  a0(model->get_nc_total()),
317  da0_dx(model->get_nc_total(), model->get_state()->get_ndx()),
318  dv(model->get_state()->get_nv()),
319  ddv_dx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
320  fext(model->get_state()->get_pinocchio()->njoints,
321  pinocchio::ForceTpl<Scalar>::Zero()) {
322  Jc.setZero();
323  a0.setZero();
324  da0_dx.setZero();
325  dv.setZero();
326  ddv_dx.setZero();
327  for (typename ContactModelMultiple::ContactModelContainer::const_iterator
328  it = model->get_contacts().begin();
329  it != model->get_contacts().end(); ++it) {
330  const boost::shared_ptr<ContactItem>& item = it->second;
331  contacts.insert(
332  std::make_pair(item->name, item->contact->createData(data)));
333  }
334  }
335 
336  MatrixXs Jc;
339  VectorXs a0;
342  MatrixXs
347  VectorXs dv;
349  MatrixXs
354  typename ContactModelMultiple::ContactDataContainer
356  pinocchio::container::aligned_vector<pinocchio::ForceTpl<Scalar> >
358 };
359 
360 } // namespace crocoddyl
361 
362 /* --- Details -------------------------------------------------------------- */
363 /* --- Details -------------------------------------------------------------- */
364 /* --- Details -------------------------------------------------------------- */
365 #include "crocoddyl/multibody/contacts/multiple-contacts.hxx"
366 
367 #endif // CROCODDYL_MULTIBODY_CONTACTS_MULTIPLE_CONTACTS_HPP_
Define a stack of contact models.
ContactModelMultipleTpl(boost::shared_ptr< StateMultibody > state, const std::size_t nu)
Initialize the multi-contact model.
boost::shared_ptr< ContactDataMultiple > createData(pinocchio::DataTpl< Scalar > *const data)
Create the multi-contact data.
std::size_t get_nc() const
Return the dimension of active contacts.
void setComputeAllContacts(const bool status)
Set the tyoe of contact computation: all or active contacts.
bool getComputeAllContacts() const
Return the type of contact computation.
void addContact(const std::string &name, boost::shared_ptr< ContactModelAbstract > contact, const bool active=true)
Add contact item.
void updateForceDiff(const boost::shared_ptr< ContactDataMultiple > &data, const MatrixXs &df_dx, const MatrixXs &df_du) const
Update the Jacobian of the spatial force defined in frame coordinate.
void changeContactStatus(const std::string &name, const bool active)
Change the contact status.
const std::set< std::string > & get_inactive_set() const
Return the names of the set of inactive contacts.
void removeContact(const std::string &name)
Remove contact item.
bool getContactStatus(const std::string &name) const
Return the status of a given contact name.
void updateAccelerationDiff(const boost::shared_ptr< ContactDataMultiple > &data, const MatrixXs &ddv_dx) const
Update the Jacobian of the constrained system acceleration.
std::size_t get_nc_total() const
Return the dimension of all contacts.
const boost::shared_ptr< StateMultibody > & get_state() const
Return the multibody state.
void calc(const boost::shared_ptr< ContactDataMultiple > &data, const Eigen::Ref< const VectorXs > &x)
Compute the contact Jacobian and contact acceleration.
void updateRneaDiff(const boost::shared_ptr< ContactDataMultiple > &data, pinocchio::DataTpl< Scalar > &pinocchio) const
Update the RNEA derivatives dtau_dq by adding the skew term (necessary for contacts expressed in LOCA...
const std::set< std::string > & get_active_set() const
Return the names of the set of active contacts.
void updateAcceleration(const boost::shared_ptr< ContactDataMultiple > &data, const VectorXs &dv) const
Update the constrained system acceleration.
void calcDiff(const boost::shared_ptr< ContactDataMultiple > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the contact holonomic constraint.
friend std::ostream & operator<<(std::ostream &os, const ContactModelMultipleTpl< Scalar > &model)
Print information on the contact models.
ContactModelMultipleTpl(boost::shared_ptr< StateMultibody > state)
Initialize the multi-contact model.
void updateForce(const boost::shared_ptr< ContactDataMultiple > &data, const VectorXs &force)
Update the spatial force defined in frame coordinate.
const ContactModelContainer & get_contacts() const
Return the contact models.
std::size_t get_nu() const
Return the dimension of control vector.
State multibody representation.
Definition: multibody.hpp:35
Define the multi-contact data.
ContactModelMultiple::ContactDataContainer contacts
Stack of contact data.
ContactDataMultipleTpl(Model< Scalar > *const model, pinocchio::DataTpl< Scalar > *const data)
Initialized a multi-contact data.
pinocchio::container::aligned_vector< pinocchio::ForceTpl< Scalar > > fext
External spatial forces in body coordinates.
friend std::ostream & operator<<(std::ostream &os, const ContactItemTpl< Scalar > &model)
Print information on the contact item.