Crocoddyl
contact-fwddyn.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2022, LAAS-CNRS, University of Edinburgh, CTU, INRIA,
5 // University of Oxford, Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
9 
10 #ifndef CROCODDYL_MULTIBODY_ACTIONS_CONTACT_FWDDYN_HPP_
11 #define CROCODDYL_MULTIBODY_ACTIONS_CONTACT_FWDDYN_HPP_
12 
13 #include <stdexcept>
14 
15 #include "crocoddyl/core/actuation-base.hpp"
16 #include "crocoddyl/core/constraints/constraint-manager.hpp"
17 #include "crocoddyl/core/costs/cost-sum.hpp"
18 #include "crocoddyl/core/diff-action-base.hpp"
19 #include "crocoddyl/multibody/contacts/multiple-contacts.hpp"
20 #include "crocoddyl/multibody/data/contacts.hpp"
21 #include "crocoddyl/multibody/fwd.hpp"
22 #include "crocoddyl/multibody/states/multibody.hpp"
23 
24 namespace crocoddyl {
25 
73 template <typename _Scalar>
75  : public DifferentialActionModelAbstractTpl<_Scalar> {
76  public:
77  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
78 
79  typedef _Scalar Scalar;
90  typedef typename MathBase::VectorXs VectorXs;
91  typedef typename MathBase::MatrixXs MatrixXs;
92 
110  boost::shared_ptr<StateMultibody> state,
111  boost::shared_ptr<ActuationModelAbstract> actuation,
112  boost::shared_ptr<ContactModelMultiple> contacts,
113  boost::shared_ptr<CostModelSum> costs,
114  const Scalar JMinvJt_damping = Scalar(0.),
115  const bool enable_force = false);
116 
135  boost::shared_ptr<StateMultibody> state,
136  boost::shared_ptr<ActuationModelAbstract> actuation,
137  boost::shared_ptr<ContactModelMultiple> contacts,
138  boost::shared_ptr<CostModelSum> costs,
139  boost::shared_ptr<ConstraintModelManager> constraints,
140  const Scalar JMinvJt_damping = Scalar(0.),
141  const bool enable_force = false);
143 
153  virtual void calc(
154  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
155  const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
156 
169  virtual void calc(
170  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
171  const Eigen::Ref<const VectorXs>& x);
172 
180  virtual void calcDiff(
181  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
182  const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
183 
195  virtual void calcDiff(
196  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
197  const Eigen::Ref<const VectorXs>& x);
198 
204  virtual boost::shared_ptr<DifferentialActionDataAbstract> createData();
205 
210  virtual bool checkData(
211  const boost::shared_ptr<DifferentialActionDataAbstract>& data);
212 
216  virtual void quasiStatic(
217  const boost::shared_ptr<DifferentialActionDataAbstract>& data,
218  Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x,
219  const std::size_t maxiter = 100, const Scalar tol = Scalar(1e-9));
220 
224  virtual std::size_t get_ng() const;
225 
229  virtual std::size_t get_nh() const;
230 
234  virtual const VectorXs& get_g_lb() const;
235 
239  virtual const VectorXs& get_g_ub() const;
240 
244  const boost::shared_ptr<ActuationModelAbstract>& get_actuation() const;
245 
249  const boost::shared_ptr<ContactModelMultiple>& get_contacts() const;
250 
254  const boost::shared_ptr<CostModelSum>& get_costs() const;
255 
259  const boost::shared_ptr<ConstraintModelManager>& get_constraints() const;
260 
264  pinocchio::ModelTpl<Scalar>& get_pinocchio() const;
265 
269  const VectorXs& get_armature() const;
270 
274  const Scalar get_damping_factor() const;
275 
279  void set_armature(const VectorXs& armature);
280 
284  void set_damping_factor(const Scalar damping);
285 
291  virtual void print(std::ostream& os) const;
292 
293  protected:
294  using Base::g_lb_;
295  using Base::g_ub_;
296  using Base::nu_;
297  using Base::state_;
298 
299  private:
300  void init();
301  boost::shared_ptr<ActuationModelAbstract> actuation_;
302  boost::shared_ptr<ContactModelMultiple> contacts_;
303  boost::shared_ptr<CostModelSum> costs_;
304  boost::shared_ptr<ConstraintModelManager> constraints_;
305  pinocchio::ModelTpl<Scalar>& pinocchio_;
306  bool with_armature_;
307  VectorXs armature_;
308  Scalar JMinvJt_damping_;
310  bool enable_force_;
312 };
313 
314 template <typename _Scalar>
316  : public DifferentialActionDataAbstractTpl<_Scalar> {
317  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
318  typedef _Scalar Scalar;
324  typedef typename MathBase::VectorXs VectorXs;
325  typedef typename MathBase::MatrixXs MatrixXs;
326 
327  template <template <typename Scalar> class Model>
329  Model<Scalar>* const model)
330  : Base(model),
331  pinocchio(pinocchio::DataTpl<Scalar>(model->get_pinocchio())),
332  multibody(
333  &pinocchio, model->get_actuation()->createData(),
334  boost::make_shared<JointDataAbstract>(
335  model->get_state(), model->get_actuation(), model->get_nu()),
336  model->get_contacts()->createData(&pinocchio)),
337  costs(model->get_costs()->createData(&multibody)),
338  Kinv(model->get_state()->get_nv() +
339  model->get_contacts()->get_nc_total(),
340  model->get_state()->get_nv() +
341  model->get_contacts()->get_nc_total()),
342  df_dx(model->get_contacts()->get_nc_total(),
343  model->get_state()->get_ndx()),
344  df_du(model->get_contacts()->get_nc_total(), model->get_nu()),
345  tmp_xstatic(model->get_state()->get_nx()),
346  tmp_Jstatic(model->get_state()->get_nv(),
347  model->get_nu() + model->get_contacts()->get_nc_total()) {
348  multibody.joint->dtau_du.diagonal().setOnes();
349  costs->shareMemory(this);
350  if (model->get_constraints() != nullptr) {
351  constraints = model->get_constraints()->createData(&multibody);
352  constraints->shareMemory(this);
353  }
354  Kinv.setZero();
355  df_dx.setZero();
356  df_du.setZero();
357  tmp_xstatic.setZero();
358  tmp_Jstatic.setZero();
359  pinocchio.lambda_c.resize(model->get_contacts()->get_nc_total());
360  pinocchio.lambda_c.setZero();
361  }
362 
363  pinocchio::DataTpl<Scalar> pinocchio;
365  boost::shared_ptr<CostDataSumTpl<Scalar> > costs;
366  boost::shared_ptr<ConstraintDataManagerTpl<Scalar> > constraints;
367  MatrixXs Kinv;
368  MatrixXs df_dx;
369  MatrixXs df_du;
370  VectorXs tmp_xstatic;
371  MatrixXs tmp_Jstatic;
372 
373  using Base::cost;
374  using Base::Fu;
375  using Base::Fx;
376  using Base::Lu;
377  using Base::Luu;
378  using Base::Lx;
379  using Base::Lxu;
380  using Base::Lxx;
381  using Base::r;
382  using Base::xout;
383 };
384 
385 } // namespace crocoddyl
386 
387 /* --- Details -------------------------------------------------------------- */
388 /* --- Details -------------------------------------------------------------- */
389 /* --- Details -------------------------------------------------------------- */
390 #include <crocoddyl/multibody/actions/contact-fwddyn.hxx>
391 
392 #endif // CROCODDYL_MULTIBODY_ACTIONS_CONTACT_FWDDYN_HPP_
Abstract class for the actuation-mapping model.
Define a stack of contact models.
Abstract class for differential action model.
VectorXs g_ub_
Lower bound of the inequality constraints.
boost::shared_ptr< StateAbstract > state_
Model of the state.
VectorXs g_lb_
Lower bound of the inequality constraints.
Differential action model for contact forward dynamics in multibody systems.
pinocchio::ModelTpl< Scalar > & get_pinocchio() const
Return the Pinocchio model.
const boost::shared_ptr< ContactModelMultiple > & get_contacts() const
Return the contact model.
virtual void calc(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
virtual std::size_t get_ng() const
Return the number of inequality constraints.
virtual void calcDiff(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the derivatives of the contact dynamics, and cost function.
const VectorXs & get_armature() const
Return the armature vector.
virtual std::size_t get_nh() const
Return the number of equality constraints.
virtual boost::shared_ptr< DifferentialActionDataAbstract > createData()
Create the contact forward-dynamics data.
virtual void print(std::ostream &os) const
Print relevant information of the contact forward-dynamics model.
DifferentialActionModelContactFwdDynamicsTpl(boost::shared_ptr< StateMultibody > state, boost::shared_ptr< ActuationModelAbstract > actuation, boost::shared_ptr< ContactModelMultiple > contacts, boost::shared_ptr< CostModelSum > costs, boost::shared_ptr< ConstraintModelManager > constraints, const Scalar JMinvJt_damping=Scalar(0.), const bool enable_force=false)
Initialize the contact forward-dynamics action model.
virtual const VectorXs & get_g_lb() const
Return the lower bound of the inequality constraints.
const boost::shared_ptr< CostModelSum > & get_costs() const
Return the cost model.
void set_damping_factor(const Scalar damping)
Modify the damping factor used in operational space inertia matrix.
void set_armature(const VectorXs &armature)
Modify the armature vector.
virtual void calc(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the system acceleration, and cost value.
const Scalar get_damping_factor() const
Return the damping factor used in operational space inertia matrix.
DifferentialActionModelContactFwdDynamicsTpl(boost::shared_ptr< StateMultibody > state, boost::shared_ptr< ActuationModelAbstract > actuation, boost::shared_ptr< ContactModelMultiple > contacts, boost::shared_ptr< CostModelSum > costs, const Scalar JMinvJt_damping=Scalar(0.), const bool enable_force=false)
Initialize the contact forward-dynamics action model.
const boost::shared_ptr< ConstraintModelManager > & get_constraints() const
Return the constraint model manager.
virtual void quasiStatic(const boost::shared_ptr< DifferentialActionDataAbstract > &data, Eigen::Ref< VectorXs > u, const Eigen::Ref< const VectorXs > &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9))
Computes the quasic static commands.
const boost::shared_ptr< ActuationModelAbstract > & get_actuation() const
Return the actuation model.
virtual bool checkData(const boost::shared_ptr< DifferentialActionDataAbstract > &data)
Check that the given data belongs to the contact forward-dynamics data.
virtual const VectorXs & get_g_ub() const
Return the upper bound of the inequality constraints.
virtual void calcDiff(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the cost functions with respect to the state only.
State multibody representation.
Definition: multibody.hpp:35
MatrixXs Fx
Jacobian of the dynamics w.r.t. the state .
MatrixXs Fu
Jacobian of the dynamics w.r.t. the control .
MatrixXs Luu
Hessian of the cost w.r.t. the control .
VectorXs Lx
Jacobian of the cost w.r.t. the state .
MatrixXs Lxx
Hessian of the cost w.r.t. the state .
VectorXs Lu
Jacobian of the cost w.r.t. the control .