crocoddyl  1.5.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
contact-friction-cone.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2018-2020, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_MULTIBODY_COSTS_CONTACT_FRICTION_CONE_HPP_
10 #define CROCODDYL_MULTIBODY_COSTS_CONTACT_FRICTION_CONE_HPP_
11 
12 #include "crocoddyl/multibody/fwd.hpp"
13 #include "crocoddyl/multibody/cost-base.hpp"
14 #include "crocoddyl/multibody/contact-base.hpp"
15 #include "crocoddyl/multibody/contacts/contact-3d.hpp"
16 #include "crocoddyl/multibody/contacts/contact-6d.hpp"
17 #include "crocoddyl/multibody/data/contacts.hpp"
18 #include "crocoddyl/multibody/frames.hpp"
19 #include "crocoddyl/multibody/friction-cone.hpp"
20 #include "crocoddyl/core/utils/exception.hpp"
21 #include "crocoddyl/core/utils/deprecate.hpp"
22 
23 namespace crocoddyl {
24 
25 template <typename _Scalar>
27  public:
28  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29 
30  typedef _Scalar Scalar;
42  typedef typename MathBase::Vector6s Vector6s;
43  typedef typename MathBase::VectorXs VectorXs;
44  typedef typename MathBase::MatrixXs MatrixXs;
45  typedef typename MathBase::MatrixX3s MatrixX3s;
46 
47  CostModelContactFrictionConeTpl(boost::shared_ptr<StateMultibody> state,
48  boost::shared_ptr<ActivationModelAbstract> activation, const FrameFrictionCone& fref,
49  const std::size_t& nu);
50  CostModelContactFrictionConeTpl(boost::shared_ptr<StateMultibody> state,
51  boost::shared_ptr<ActivationModelAbstract> activation,
52  const FrameFrictionCone& fref);
53  CostModelContactFrictionConeTpl(boost::shared_ptr<StateMultibody> state, const FrameFrictionCone& fref,
54  const std::size_t& nu);
55  CostModelContactFrictionConeTpl(boost::shared_ptr<StateMultibody> state, const FrameFrictionCone& fref);
57 
58  virtual void calc(const boost::shared_ptr<CostDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
59  const Eigen::Ref<const VectorXs>& u);
60  virtual void calcDiff(const boost::shared_ptr<CostDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
61  const Eigen::Ref<const VectorXs>& u);
62  virtual boost::shared_ptr<CostDataAbstract> createData(DataCollectorAbstract* const data);
63 
64  protected:
65  virtual void set_referenceImpl(const std::type_info& ti, const void* pv);
66  virtual void get_referenceImpl(const std::type_info& ti, void* pv) const;
67 
68  using Base::activation_;
69  using Base::nu_;
70  using Base::state_;
71  using Base::unone_;
72 
73  private:
74  FrameFrictionCone fref_;
75 };
76 
77 template <typename _Scalar>
79  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
80 
81  typedef _Scalar Scalar;
87  typedef typename MathBase::VectorXs VectorXs;
88  typedef typename MathBase::MatrixXs MatrixXs;
89  typedef typename MathBase::Matrix6xs Matrix6xs;
90 
91  template <template <typename Scalar> class Model>
92  CostDataContactFrictionConeTpl(Model<Scalar>* const model, DataCollectorAbstract* const data)
93  : Base(model, data),
94  Arr_Ru(model->get_activation()->get_nr(), model->get_state()->get_nv()),
95  more_than_3_constraints(false) {
96  Arr_Ru.setZero();
97 
98  // Check that proper shared data has been passed
100  if (d == NULL) {
101  throw_pretty("Invalid argument: the shared data should be derived from DataCollectorContact");
102  }
103 
104  // Avoids data casting at runtime
105  FrameFrictionCone fref = model->template get_reference<FrameFrictionCone>();
106  std::string frame_name = model->get_state()->get_pinocchio()->frames[fref.id].name;
107  bool found_contact = false;
108  for (typename ContactModelMultiple::ContactDataContainer::iterator it = d->contacts->contacts.begin();
109  it != d->contacts->contacts.end(); ++it) {
110  if (it->second->frame == fref.id) {
111  ContactData3DTpl<Scalar>* d3d = dynamic_cast<ContactData3DTpl<Scalar>*>(it->second.get());
112  if (d3d != NULL) {
113  found_contact = true;
114  contact = it->second;
115  break;
116  }
117  ContactData6DTpl<Scalar>* d6d = dynamic_cast<ContactData6DTpl<Scalar>*>(it->second.get());
118  if (d6d != NULL) {
119  more_than_3_constraints = true;
120  found_contact = true;
121  contact = it->second;
122  break;
123  }
124  throw_pretty("Domain error: there isn't defined at least a 3d contact for " + frame_name);
125  break;
126  }
127  }
128  if (!found_contact) {
129  throw_pretty("Domain error: there isn't defined contact data for " + frame_name);
130  }
131  }
132 
133  boost::shared_ptr<ContactDataAbstractTpl<Scalar> > contact;
134  MatrixXs Arr_Ru;
135  MatrixXs Arr_Rx;
136  bool more_than_3_constraints;
137  using Base::activation;
138  using Base::cost;
139  using Base::Lu;
140  using Base::Luu;
141  using Base::Lx;
142  using Base::Lxu;
143  using Base::Lxx;
144  using Base::r;
145  using Base::Ru;
146  using Base::Rx;
147  using Base::shared;
148 };
149 
150 } // namespace crocoddyl
151 
152 /* --- Details -------------------------------------------------------------- */
153 /* --- Details -------------------------------------------------------------- */
154 /* --- Details -------------------------------------------------------------- */
155 #include "crocoddyl/multibody/costs/contact-friction-cone.hxx"
156 
157 #endif // CROCODDYL_MULTIBODY_COSTS_CONTACT_FRICTION_CONE_HPP_
Define a stack of contact models.