Crocoddyl
contact.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, University of Edinburgh, LAAS-CNRS,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
9 
10 #ifndef CROCODDYL_MULTIBODY_NUMDIFF_CONTACT_HPP_
11 #define CROCODDYL_MULTIBODY_NUMDIFF_CONTACT_HPP_
12 
13 #include <boost/function.hpp>
14 
15 #include "crocoddyl/multibody/contact-base.hpp"
16 #include "crocoddyl/multibody/fwd.hpp"
17 
18 namespace crocoddyl {
19 
20 template <typename _Scalar>
22  public:
23  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
24 
25  typedef _Scalar Scalar;
30  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
31  typedef boost::function<void(const VectorXs&, const VectorXs&)>
32  ReevaluationFunction;
33 
40  explicit ContactModelNumDiffTpl(const boost::shared_ptr<Base>& model);
41 
46 
50  void calc(const boost::shared_ptr<ContactDataAbstract>& data,
51  const Eigen::Ref<const VectorXs>& x);
52 
56  void calcDiff(const boost::shared_ptr<ContactDataAbstract>& data,
57  const Eigen::Ref<const VectorXs>& x);
58 
62  void updateForce(const boost::shared_ptr<ContactDataAbstract>& data,
63  const VectorXs& force);
64 
71  boost::shared_ptr<ContactDataAbstract> createData(
72  pinocchio::DataTpl<Scalar>* const data);
73 
77  const boost::shared_ptr<Base>& get_model() const;
78 
83  const Scalar get_disturbance() const;
84 
89  void set_disturbance(const Scalar disturbance);
90 
98  void set_reevals(const std::vector<ReevaluationFunction>& reevals);
99 
100  protected:
101  using Base::nc_;
102  using Base::nu_;
103  using Base::state_;
104 
105  boost::shared_ptr<Base> model_;
106  Scalar e_jac_;
108  std::vector<ReevaluationFunction>
110 
111  private:
123  void assertStableStateFD(const Eigen::Ref<const VectorXs>& /*x*/);
124 };
125 
126 template <typename _Scalar>
128  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
129 
130  typedef _Scalar Scalar;
133  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
134 
135  template <template <typename Scalar> class Model>
136  explicit ContactDataNumDiffTpl(Model<Scalar>* const model,
137  pinocchio::DataTpl<Scalar>* const data)
138  : Base(model, data),
139  dx(model->get_state()->get_ndx()),
140  xp(model->get_state()->get_nx()) {
141  dx.setZero();
142  xp.setZero();
143 
144  const std::size_t ndx = model->get_model()->get_state()->get_ndx();
145  data_0 = model->get_model()->createData(data);
146  for (std::size_t i = 0; i < ndx; ++i) {
147  data_x.push_back(model->get_model()->createData(data));
148  }
149  }
150 
151  virtual ~ContactDataNumDiffTpl() {}
152 
153  using Base::a0;
154  using Base::da0_dx;
155  using Base::f;
156  using Base::pinocchio;
157 
158  Scalar x_norm;
159  Scalar
161  VectorXs dx;
162  VectorXs xp;
164  boost::shared_ptr<Base> data_0;
165  std::vector<boost::shared_ptr<Base> >
167 };
168 
169 } // namespace crocoddyl
170 
171 /* --- Details -------------------------------------------------------------- */
172 /* --- Details -------------------------------------------------------------- */
173 /* --- Details -------------------------------------------------------------- */
174 #include "crocoddyl/multibody/numdiff/contact.hxx"
175 
176 #endif // CROCODDYL_MULTIBODY_NUMDIFF_CONTACT_HPP_
void calc(const boost::shared_ptr< ContactDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the contact Jacobian and acceleration drift.
void updateForce(const boost::shared_ptr< ContactDataAbstract > &data, const VectorXs &force)
Convert the force into a stack of spatial forces.
const Scalar get_disturbance() const
Return the disturbance constant used in the numerical differentiation routine.
void set_reevals(const std::vector< ReevaluationFunction > &reevals)
Register functions that take a pinocchio model, a pinocchio data, a state and a control....
void calcDiff(const boost::shared_ptr< ContactDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the acceleration-based contact.
boost::shared_ptr< ContactDataAbstract > createData(pinocchio::DataTpl< Scalar > *const data)
Create a Data object.
void set_disturbance(const Scalar disturbance)
Modify the disturbance constant used in the numerical differentiation routine.
const boost::shared_ptr< Base > & get_model() const
Return the acton model that we use to numerical differentiate.
boost::shared_ptr< Base > model_
contact model to differentiate
Definition: contact.hpp:105
ContactModelNumDiffTpl(const boost::shared_ptr< Base > &model)
Construct a new ContactModelNumDiff object from a ContactModelAbstract.
std::vector< ReevaluationFunction > reevals_
functions that need execution before calc or calcDiff
Definition: contact.hpp:109
virtual ~ContactModelNumDiffTpl()
Default destructor of the ContactModelNumDiff object.
PinocchioData * pinocchio
Pinocchio data.
Definition: force-base.hpp:49
Force f
Contact force expressed in the coordinate defined by type.
Definition: force-base.hpp:54
Scalar x_norm
Norm of the state vector.
Definition: contact.hpp:158
boost::shared_ptr< Base > data_0
The data at the approximation point.
Definition: contact.hpp:164
Scalar xh_jac
Disturbance value used for computing .
Definition: contact.hpp:160
std::vector< boost::shared_ptr< Base > > data_x
The temporary data associated with the state variation.
Definition: contact.hpp:166
VectorXs dx
State disturbance.
Definition: contact.hpp:161