Crocoddyl
multiple-impulses.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_IMPULSES_MULTIPLE_IMPULSES_HPP_
11 #define CROCODDYL_MULTIBODY_IMPULSES_MULTIPLE_IMPULSES_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/fwd.hpp"
20 #include "crocoddyl/multibody/impulse-base.hpp"
21 
22 namespace crocoddyl {
23 
24 template <typename _Scalar>
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  typedef _Scalar Scalar;
30 
31  ImpulseItemTpl() {}
32  ImpulseItemTpl(const std::string& name,
33  boost::shared_ptr<ImpulseModelAbstract> impulse,
34  const bool active = true)
35  : name(name), impulse(impulse), active(active) {}
36 
40  friend std::ostream& operator<<(std::ostream& os,
41  const ImpulseItemTpl<Scalar>& model) {
42  os << "{" << *model.impulse << "}";
43  return os;
44  }
45 
46  std::string name;
47  boost::shared_ptr<ImpulseModelAbstract> impulse;
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<ImpulseItem> >
79  ImpulseModelContainer;
80  typedef std::map<std::string, boost::shared_ptr<ImpulseDataAbstract> >
81  ImpulseDataContainer;
82  typedef typename pinocchio::container::aligned_vector<
83  pinocchio::ForceTpl<Scalar> >::iterator ForceIterator;
84 
90  explicit ImpulseModelMultipleTpl(boost::shared_ptr<StateMultibody> state);
92 
102  void addImpulse(const std::string& name,
103  boost::shared_ptr<ImpulseModelAbstract> impulse,
104  const bool active = true);
105 
111  void removeImpulse(const std::string& name);
112 
119  void changeImpulseStatus(const std::string& name, const bool active);
120 
127  void calc(const boost::shared_ptr<ImpulseDataMultiple>& data,
128  const Eigen::Ref<const VectorXs>& x);
129 
136  void calcDiff(const boost::shared_ptr<ImpulseDataMultiple>& data,
137  const Eigen::Ref<const VectorXs>& x);
138 
146  void updateVelocity(const boost::shared_ptr<ImpulseDataMultiple>& data,
147  const VectorXs& vnext) const;
148 
156  void updateForce(const boost::shared_ptr<ImpulseDataMultiple>& data,
157  const VectorXs& impulse);
158 
167  void updateVelocityDiff(const boost::shared_ptr<ImpulseDataMultiple>& data,
168  const MatrixXs& dvnext_dx) const;
169 
179  void updateForceDiff(const boost::shared_ptr<ImpulseDataMultiple>& data,
180  const MatrixXs& df_dx) const;
181 
191  void updateRneaDiff(const boost::shared_ptr<ImpulseDataMultiple>& data,
192  pinocchio::DataTpl<Scalar>& pinocchio) const;
193 
200  boost::shared_ptr<ImpulseDataMultiple> createData(
201  pinocchio::DataTpl<Scalar>* const data);
202 
206  const boost::shared_ptr<StateMultibody>& get_state() const;
207 
211  const ImpulseModelContainer& get_impulses() const;
212 
216  std::size_t get_nc() const;
217 
221  std::size_t get_nc_total() const;
222 
226  const std::set<std::string>& get_active_set() const;
227 
231  const std::set<std::string>& get_inactive_set() const;
232 
236  bool getImpulseStatus(const std::string& name) const;
237 
241  template <class Scalar>
242  friend std::ostream& operator<<(std::ostream& os,
243  const ImpulseModelMultipleTpl<Scalar>& model);
244 
245  private:
246  boost::shared_ptr<StateMultibody> state_;
247  ImpulseModelContainer impulses_;
248  std::size_t nc_;
249  std::size_t nc_total_;
250  std::set<std::string> active_set_;
251  std::set<std::string> inactive_set_;
252 };
253 
259 template <typename _Scalar>
261  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
262 
263  typedef _Scalar Scalar;
267  typedef typename MathBase::VectorXs VectorXs;
268  typedef typename MathBase::MatrixXs MatrixXs;
269 
276  template <template <typename Scalar> class Model>
277  ImpulseDataMultipleTpl(Model<Scalar>* const model,
278  pinocchio::DataTpl<Scalar>* const data)
279  : Jc(model->get_nc_total(), model->get_state()->get_nv()),
280  dv0_dq(model->get_nc_total(), model->get_state()->get_nv()),
281  vnext(model->get_state()->get_nv()),
282  dvnext_dx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
283  fext(model->get_state()->get_pinocchio()->njoints,
284  pinocchio::ForceTpl<Scalar>::Zero()) {
285  Jc.setZero();
286  dv0_dq.setZero();
287  vnext.setZero();
288  dvnext_dx.setZero();
289  for (typename ImpulseModelMultiple::ImpulseModelContainer::const_iterator
290  it = model->get_impulses().begin();
291  it != model->get_impulses().end(); ++it) {
292  const boost::shared_ptr<ImpulseItem>& item = it->second;
293  impulses.insert(
294  std::make_pair(item->name, item->impulse->createData(data)));
295  }
296  }
297 
298  MatrixXs Jc;
301  MatrixXs
306  VectorXs vnext;
308  MatrixXs
313  typename ImpulseModelMultiple::ImpulseDataContainer
315  pinocchio::container::aligned_vector<pinocchio::ForceTpl<Scalar> >
317 };
318 
319 } // namespace crocoddyl
320 
321 /* --- Details -------------------------------------------------------------- */
322 /* --- Details -------------------------------------------------------------- */
323 /* --- Details -------------------------------------------------------------- */
324 #include "crocoddyl/multibody/impulses/multiple-impulses.hxx"
325 
326 #endif // CROCODDYL_MULTIBODY_IMPULSES_MULTIPLE_IMPULSES_HPP_
Define a stack of impulse models.
void updateRneaDiff(const boost::shared_ptr< ImpulseDataMultiple > &data, pinocchio::DataTpl< Scalar > &pinocchio) const
Update the RNEA derivatives dtau_dq by adding the skew term (necessary for impulses expressed in LOCA...
void updateForceDiff(const boost::shared_ptr< ImpulseDataMultiple > &data, const MatrixXs &df_dx) const
Update the Jacobian of the spatial impulse defined in frame coordinate.
std::size_t get_nc() const
Return the dimension of active impulses.
const ImpulseModelContainer & get_impulses() const
Return the impulse models.
void addImpulse(const std::string &name, boost::shared_ptr< ImpulseModelAbstract > impulse, const bool active=true)
Add impulse item.
void calcDiff(const boost::shared_ptr< ImpulseDataMultiple > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the impulse holonomic constraint.
void updateVelocity(const boost::shared_ptr< ImpulseDataMultiple > &data, const VectorXs &vnext) const
Update the system velocity after impulse.
void updateForce(const boost::shared_ptr< ImpulseDataMultiple > &data, const VectorXs &impulse)
Update the spatial impulse defined in frame coordinate.
void calc(const boost::shared_ptr< ImpulseDataMultiple > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total impulse Jacobian and impulse velocity.
const std::set< std::string > & get_inactive_set() const
Return the names of the set of inactive impulses.
void updateVelocityDiff(const boost::shared_ptr< ImpulseDataMultiple > &data, const MatrixXs &dvnext_dx) const
Update the Jacobian of the system velocity after impulse.
std::size_t get_nc_total() const
Return the dimension of all impulses.
const boost::shared_ptr< StateMultibody > & get_state() const
Return the multibody state.
const std::set< std::string > & get_active_set() const
Return the names of the set of active impulses.
friend std::ostream & operator<<(std::ostream &os, const ImpulseModelMultipleTpl< Scalar > &model)
Print information on the impulse models.
ImpulseModelMultipleTpl(boost::shared_ptr< StateMultibody > state)
Initialize the multi-impulse model.
bool getImpulseStatus(const std::string &name) const
Return the status of a given impulse name.
void changeImpulseStatus(const std::string &name, const bool active)
Change the impulse status.
boost::shared_ptr< ImpulseDataMultiple > createData(pinocchio::DataTpl< Scalar > *const data)
Create the multi-impulse data.
void removeImpulse(const std::string &name)
Remove impulse item.
State multibody representation.
Definition: multibody.hpp:35
Define the multi-impulse data.
ImpulseDataMultipleTpl(Model< Scalar > *const model, pinocchio::DataTpl< Scalar > *const data)
Initialized a multi-impulse data.
pinocchio::container::aligned_vector< pinocchio::ForceTpl< Scalar > > fext
External spatial forces in body coordinates.
ImpulseModelMultiple::ImpulseDataContainer impulses
Stack of impulse data.
friend std::ostream & operator<<(std::ostream &os, const ImpulseItemTpl< Scalar > &model)
Print information on the impulse item.