Crocoddyl
contact-base.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_CONTACT_BASE_HPP_
11 #define CROCODDYL_MULTIBODY_CONTACT_BASE_HPP_
12 
13 #include <pinocchio/multibody/fwd.hpp>
14 
15 #include "crocoddyl/core/mathbase.hpp"
16 #include "crocoddyl/core/utils/deprecate.hpp"
17 #include "crocoddyl/multibody/force-base.hpp"
18 #include "crocoddyl/multibody/fwd.hpp"
19 #include "crocoddyl/multibody/states/multibody.hpp"
20 
21 namespace crocoddyl {
22 
23 template <typename _Scalar>
25  public:
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  typedef _Scalar Scalar;
32  typedef typename MathBase::VectorXs VectorXs;
33  typedef typename MathBase::MatrixXs MatrixXs;
34 
43  ContactModelAbstractTpl(boost::shared_ptr<StateMultibody> state,
44  const pinocchio::ReferenceFrame type,
45  const std::size_t nc, const std::size_t nu);
46  ContactModelAbstractTpl(boost::shared_ptr<StateMultibody> state,
47  const pinocchio::ReferenceFrame type,
48  const std::size_t nc);
49 
50  DEPRECATED(
51  "Use constructor that passes the type type of contact, this assumes is "
52  "pinocchio::LOCAL",
53  ContactModelAbstractTpl(boost::shared_ptr<StateMultibody> state,
54  const std::size_t nc, const std::size_t nu);)
55  DEPRECATED(
56  "Use constructor that passes the type type of contact, this assumes is "
57  "pinocchio::LOCAL",
58  ContactModelAbstractTpl(boost::shared_ptr<StateMultibody> state,
59  const std::size_t nc);)
60  virtual ~ContactModelAbstractTpl();
61 
69  virtual void calc(const boost::shared_ptr<ContactDataAbstract>& data,
70  const Eigen::Ref<const VectorXs>& x) = 0;
71 
79  virtual void calcDiff(const boost::shared_ptr<ContactDataAbstract>& data,
80  const Eigen::Ref<const VectorXs>& x) = 0;
81 
88  virtual void updateForce(const boost::shared_ptr<ContactDataAbstract>& data,
89  const VectorXs& force) = 0;
90 
97  void updateForceDiff(const boost::shared_ptr<ContactDataAbstract>& data,
98  const MatrixXs& df_dx, const MatrixXs& df_du) const;
99 
105  void setZeroForce(const boost::shared_ptr<ContactDataAbstract>& data) const;
106 
113  const boost::shared_ptr<ContactDataAbstract>& data) const;
114 
118  virtual boost::shared_ptr<ContactDataAbstract> createData(
119  pinocchio::DataTpl<Scalar>* const data);
120 
124  const boost::shared_ptr<StateMultibody>& get_state() const;
125 
129  std::size_t get_nc() const;
130 
134  std::size_t get_nu() const;
135 
139  pinocchio::FrameIndex get_id() const;
140 
144  void set_id(const pinocchio::FrameIndex id);
145 
149  void set_type(const pinocchio::ReferenceFrame type);
150 
154  pinocchio::ReferenceFrame get_type() const;
155 
159  template <class Scalar>
160  friend std::ostream& operator<<(std::ostream& os,
161  const ContactModelAbstractTpl<Scalar>& model);
162 
168  virtual void print(std::ostream& os) const;
169 
170  protected:
171  boost::shared_ptr<StateMultibody> state_;
172  std::size_t nc_;
173  std::size_t nu_;
174  pinocchio::FrameIndex id_;
175  pinocchio::ReferenceFrame type_;
176 };
177 
178 template <typename _Scalar>
180  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
181 
182  typedef _Scalar Scalar;
185  typedef typename MathBase::VectorXs VectorXs;
186  typedef typename MathBase::MatrixXs MatrixXs;
187  typedef typename pinocchio::SE3Tpl<Scalar> SE3;
188 
189  template <template <typename Scalar> class Model>
190  ContactDataAbstractTpl(Model<Scalar>* const model,
191  pinocchio::DataTpl<Scalar>* const data)
192  : Base(model, data),
193  fXj(jMf.inverse().toActionMatrix()),
194  a0(model->get_nc()),
195  da0_dx(model->get_nc(), model->get_state()->get_ndx()),
196  dtau_dq(model->get_state()->get_nv(), model->get_state()->get_nv()) {
197  a0.setZero();
198  da0_dx.setZero();
199  dtau_dq.setZero();
200  }
201  virtual ~ContactDataAbstractTpl() {}
202 
203  using Base::df_du;
204  using Base::df_dx;
205  using Base::f;
206  using Base::frame;
207  using Base::Jc;
208  using Base::jMf;
209  using Base::pinocchio;
210 
211  typename SE3::ActionMatrixType fXj;
212  VectorXs a0;
213  MatrixXs da0_dx;
214  MatrixXs dtau_dq;
215 };
216 
217 } // namespace crocoddyl
218 
219 /* --- Details -------------------------------------------------------------- */
220 /* --- Details -------------------------------------------------------------- */
221 /* --- Details -------------------------------------------------------------- */
222 #include "crocoddyl/multibody/contact-base.hxx"
223 
224 #endif // CROCODDYL_MULTIBODY_CONTACT_BASE_HPP_
std::size_t get_nc() const
Return the dimension of the contact.
virtual void calcDiff(const boost::shared_ptr< ContactDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)=0
Compute the derivatives of the acceleration-based contact.
virtual void updateForce(const boost::shared_ptr< ContactDataAbstract > &data, const VectorXs &force)=0
Convert the force into a stack of spatial forces.
void setZeroForce(const boost::shared_ptr< ContactDataAbstract > &data) const
Set the stack of spatial forces to zero.
virtual void print(std::ostream &os) const
Print relevant information of the contact model.
void updateForceDiff(const boost::shared_ptr< ContactDataAbstract > &data, const MatrixXs &df_dx, const MatrixXs &df_du) const
Convert the force into a stack of spatial forces.
void setZeroForceDiff(const boost::shared_ptr< ContactDataAbstract > &data) const
Set the stack of spatial forces Jacobians to zero.
virtual void calc(const boost::shared_ptr< ContactDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)=0
Compute the contact Jacobian and acceleration drift.
void set_id(const pinocchio::FrameIndex id)
Modify the reference frame id.
pinocchio::ReferenceFrame type_
Type of contact.
pinocchio::FrameIndex get_id() const
Return the reference frame id.
void set_type(const pinocchio::ReferenceFrame type)
Modify the type of contact.
virtual boost::shared_ptr< ContactDataAbstract > createData(pinocchio::DataTpl< Scalar > *const data)
Create the contact data.
const boost::shared_ptr< StateMultibody > & get_state() const
Return the state.
pinocchio::ReferenceFrame get_type() const
Return the type of contact.
pinocchio::FrameIndex id_
Reference frame id of the contact.
ContactModelAbstractTpl(boost::shared_ptr< StateMultibody > state, const pinocchio::ReferenceFrame type, const std::size_t nc, const std::size_t nu)
Initialize the contact abstraction.
friend std::ostream & operator<<(std::ostream &os, const ContactModelAbstractTpl< Scalar > &model)
Print information on the contact model.
std::size_t get_nu() const
Return the dimension of the control vector.
State multibody representation.
Definition: multibody.hpp:35
SE3 jMf
Local frame placement of the contact frame.
Definition: force-base.hpp:52
PinocchioData * pinocchio
Pinocchio data.
Definition: force-base.hpp:49
pinocchio::FrameIndex frame
Frame index of the contact frame.
Definition: force-base.hpp:50
SE3 jMf
Local frame placement of the contact frame.
Definition: force-base.hpp:52
MatrixXs Jc
Contact Jacobian.
Definition: force-base.hpp:53
Force f
Contact force expressed in the coordinate defined by type.
Definition: force-base.hpp:54