action.hpp
Go to the documentation of this file.
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 SOBEC_LPF_HPP_
10 #define SOBEC_LPF_HPP_
11 
12 #include <crocoddyl/core/action-base.hpp>
13 #include <crocoddyl/core/activations/quadratic-barrier.hpp>
14 #include <crocoddyl/core/diff-action-base.hpp>
15 #include <crocoddyl/core/fwd.hpp>
16 #include <crocoddyl/multibody/states/multibody.hpp>
17 #include <pinocchio/multibody/model.hpp>
18 
19 #include "state.hpp"
20 
21 namespace sobec {
22 using namespace crocoddyl;
23 
24 template <typename _Scalar>
25 class IntegratedActionModelLPFTpl : public ActionModelAbstractTpl<_Scalar> {
26  public:
27  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
28 
29  typedef _Scalar Scalar;
30  typedef MathBaseTpl<Scalar> MathBase;
31  typedef ActionModelAbstractTpl<Scalar> Base;
33  typedef ActionDataAbstractTpl<Scalar> ActionDataAbstract;
34  typedef DifferentialActionModelAbstractTpl<Scalar>
36  typedef typename MathBase::VectorXs VectorXs;
37  typedef typename MathBase::MatrixXs MatrixXs;
39  typedef StateMultibodyTpl<Scalar> StateMultibody;
40  typedef pinocchio::ModelTpl<Scalar> PinocchioModel;
41  typedef ActivationModelQuadraticBarrierTpl<Scalar>
43  typedef ActivationBoundsTpl<Scalar> ActivationBounds;
44 
46  boost::shared_ptr<DifferentialActionModelAbstract> model,
47  std::vector<std::string> lpf_joint_names = {},
48  const Scalar& time_step = Scalar(1e-3),
49  const bool& with_cost_residual = true, const Scalar& fc = 0,
50  const bool& tau_plus_integration = true, const int& filter = 0);
51  virtual ~IntegratedActionModelLPFTpl();
52 
53  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
54  const Eigen::Ref<const VectorXs>& y,
55  const Eigen::Ref<const VectorXs>& w);
56 
57  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
58  const Eigen::Ref<const VectorXs>& y);
59 
60  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
61  const Eigen::Ref<const VectorXs>& y,
62  const Eigen::Ref<const VectorXs>& w);
63 
64  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
65  const Eigen::Ref<const VectorXs>& y);
66 
67  virtual boost::shared_ptr<ActionDataAbstract> createData();
68  virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
69 
70  virtual void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
71  Eigen::Ref<VectorXs> u,
72  const Eigen::Ref<const VectorXs>& x,
73  const std::size_t maxiter = 100,
74  const Scalar tol = Scalar(1e-9));
75 
76  const boost::shared_ptr<DifferentialActionModelAbstract>& get_differential()
77  const;
78  const Scalar& get_dt() const;
79  const Scalar& get_fc() const;
80  const Scalar& get_alpha() const { return alpha_; };
81 
82  const std::size_t& get_nw() const { return nw_; };
83  const std::size_t& get_ntau() const { return ntau_; };
84  const std::size_t& get_ny() const { return ny_; };
85 
86  const std::vector<std::string>& get_lpf_joint_names() const {
87  return lpf_joint_names_;
88  };
89 
90  const std::vector<int>& get_lpf_torque_ids() const {
91  return lpf_torque_ids_;
92  };
93  const std::vector<int>& get_non_lpf_torque_ids() const {
94  return non_lpf_torque_ids_;
95  };
96 
97  void set_dt(const Scalar& dt);
98  void set_fc(const Scalar& fc);
99  void set_alpha(const Scalar& alpha);
100  void set_differential(
101  boost::shared_ptr<DifferentialActionModelAbstract> model);
102 
103  // hard-coded costs
104  void set_control_reg_cost(const Scalar& cost_weight_w_reg,
105  const VectorXs& cost_ref_w_reg);
106  void set_control_lim_cost(const Scalar& cost_weight_w_lim);
107 
108  void compute_alpha(const Scalar& fc);
109 
110  protected:
111  using Base::has_control_limits_;
112  using Base::nr_;
114  using Base::nu_;
115  using Base::u_lb_;
116  using Base::u_ub_;
117  using Base::unone_;
118  std::size_t nw_;
119  std::size_t ntau_;
120  std::size_t ny_;
121  using Base::state_;
122 
123  public:
124  boost::shared_ptr<ActivationModelQuadraticBarrier>
126 
127  private:
128  boost::shared_ptr<DifferentialActionModelAbstract> differential_;
129  Scalar time_step_;
130  Scalar time_step2_;
131  Scalar alpha_;
132  bool with_cost_residual_;
133  Scalar fc_;
134  // bool enable_integration_;
135  Scalar tauReg_weight_;
136  VectorXs tauReg_reference_;
137  VectorXs tauReg_residual_,
139  tauLim_residual_;
140  // bool gravity_reg_; //!< Use gravity torque for
141  // unfiltered torque reg, or user-provided reference?
142  Scalar tauLim_weight_;
143  bool tau_plus_integration_;
144  int filter_;
146  boost::shared_ptr<PinocchioModel> pin_model_;
147  bool is_terminal_;
148  std::vector<std::string>
150  lpf_joint_names_;
151  std::vector<int>
152  lpf_joint_ids_;
153  std::vector<int>
154  lpf_torque_ids_;
155 
156  // std::vector<std::string> non_lpf_joint_names_; //!< Vector of joint
157  // names that are NOT low-pass filtered
158  std::vector<int> non_lpf_joint_ids_;
159  std::vector<int> non_lpf_torque_ids_;
161 };
163 
164 template <typename _Scalar>
165 class IntegratedActionDataLPFTpl : public ActionDataAbstractTpl<_Scalar> {
166  public:
167  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
168 
169  typedef _Scalar Scalar;
170  typedef MathBaseTpl<Scalar> MathBase;
171  typedef ActionDataAbstractTpl<Scalar> Base;
172  typedef typename MathBase::VectorXs VectorXs;
173  typedef typename MathBase::MatrixXs MatrixXs;
174  typedef pinocchio::DataTpl<Scalar> PinocchioData;
175  typedef DifferentialActionDataAbstractTpl<Scalar>
177  typedef ActivationDataQuadraticBarrierTpl<Scalar>
179 
180  template <template <typename Scalar> class Model>
181  explicit IntegratedActionDataLPFTpl(Model<Scalar>* const model)
182  : Base(model), tau_tmp(model->get_nu()) {
183  tau_tmp.setZero();
184  differential = model->get_differential()->createData();
185  const std::size_t& ndy = model->get_state()->get_ndx();
186  dy = VectorXs::Zero(ndy);
187  // for wlim cost
188  activation = boost::static_pointer_cast<ActivationDataQuadraticBarrier>(
189  model->activation_model_tauLim_->createData());
190  }
192 
193  boost::shared_ptr<DifferentialActionDataAbstractTpl<Scalar> > differential;
195 
196  // PinocchioData pinocchio; // for reg
197  // cost
198  boost::shared_ptr<ActivationDataQuadraticBarrier> activation; // for lim cost
199 
200  using Base::cost;
201  using Base::r;
203  // use refs to "alias" base class member names
204  VectorXs& ynext = Base::xnext;
205  MatrixXs& Fy = Base::Fx;
206  MatrixXs& Fw = Base::Fu;
207  VectorXs& Ly = Base::Lx;
208  VectorXs& Lw = Base::Lu;
209  MatrixXs& Lyy = Base::Lxx;
210  MatrixXs& Lyw = Base::Lxu;
211  MatrixXs& Lww = Base::Luu;
212 };
213 
214 } // namespace sobec
215 
216 /* --- Details -------------------------------------------------------------- */
217 /* --- Details -------------------------------------------------------------- */
218 /* --- Details -------------------------------------------------------------- */
220 
221 #endif // SOBEC_LPF_HPP_
sobec::IntegratedActionModelLPFTpl::ny_
std::size_t ny_
Augmented state dimension : nq+nv+ntau.
Definition: action.hpp:120
sobec::newcontacts::y
@ y
Definition: contact1d.hpp:26
sobec::IntegratedActionModelLPFTpl::get_non_lpf_torque_ids
const std::vector< int > & get_non_lpf_torque_ids() const
Definition: action.hpp:93
sobec::IntegratedActionModelLPFTpl::MathBase
MathBaseTpl< Scalar > MathBase
Definition: action.hpp:30
sobec::IntegratedActionModelLPFTpl::get_alpha
const Scalar & get_alpha() const
Definition: action.hpp:80
sobec::IntegratedActionModelLPFTpl::get_nw
const std::size_t & get_nw() const
Definition: action.hpp:82
sobec::IntegratedActionModelLPFTpl::ActionDataAbstract
ActionDataAbstractTpl< Scalar > ActionDataAbstract
Definition: action.hpp:33
sobec::IntegratedActionDataLPFTpl::DifferentialActionDataAbstract
DifferentialActionDataAbstractTpl< Scalar > DifferentialActionDataAbstract
Definition: action.hpp:176
sobec::IntegratedActionModelLPFTpl::get_ny
const std::size_t & get_ny() const
Definition: action.hpp:84
sobec::IntegratedActionModelLPFTpl::VectorXs
MathBase::VectorXs VectorXs
Definition: action.hpp:36
sobec::IntegratedActionModelLPFTpl::Base
ActionModelAbstractTpl< Scalar > Base
Definition: action.hpp:31
sobec::StateLPFTpl
Definition: state.hpp:19
sobec::IntegratedActionDataLPFTpl::Scalar
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef _Scalar Scalar
Definition: action.hpp:169
sobec::IntegratedActionModelLPFTpl::ActivationBounds
ActivationBoundsTpl< Scalar > ActivationBounds
Definition: action.hpp:43
sobec::IntegratedActionModelLPFTpl::get_ntau
const std::size_t & get_ntau() const
Definition: action.hpp:83
sobec::IntegratedActionModelLPFTpl::DifferentialActionModelAbstract
DifferentialActionModelAbstractTpl< Scalar > DifferentialActionModelAbstract
Definition: action.hpp:35
sobec::IntegratedActionModelLPFTpl::nw_
std::size_t nw_
< Neutral state
Definition: action.hpp:118
sobec::IntegratedActionDataLPFTpl::PinocchioData
pinocchio::DataTpl< Scalar > PinocchioData
Definition: action.hpp:174
sobec::IntegratedActionModelLPFTpl::Data
IntegratedActionDataLPFTpl< Scalar > Data
Definition: action.hpp:32
sobec::IntegratedActionModelLPFTpl::activation_model_tauLim_
boost::shared_ptr< ActivationModelQuadraticBarrier > activation_model_tauLim_
< Model of the state
Definition: action.hpp:125
sobec::IntegratedActionDataLPFTpl::tau_tmp
VectorXs tau_tmp
Definition: action.hpp:202
sobec::IntegratedActionModelLPFTpl::ntau_
std::size_t ntau_
Filtered torque dimension ("lpf" dimension)
Definition: action.hpp:119
sobec::IntegratedActionModelLPFTpl::Scalar
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef _Scalar Scalar
Definition: action.hpp:29
sobec
Definition: activation-quad-ref.hpp:19
sobec::IntegratedActionDataLPFTpl::VectorXs
MathBase::VectorXs VectorXs
Definition: action.hpp:172
sobec::IntegratedActionModelLPFTpl::StateMultibody
StateMultibodyTpl< Scalar > StateMultibody
Definition: action.hpp:39
action.hxx
sobec::IntegratedActionDataLPFTpl::activation
boost::shared_ptr< ActivationDataQuadraticBarrier > activation
Definition: action.hpp:198
sobec::IntegratedActionModelLPFTpl::get_lpf_joint_names
const std::vector< std::string > & get_lpf_joint_names() const
Definition: action.hpp:86
sobec::newcontacts::x
@ x
Definition: contact1d.hpp:26
sobec::IntegratedActionModelLPFTpl::get_lpf_torque_ids
const std::vector< int > & get_lpf_torque_ids() const
Definition: action.hpp:90
sobec::IntegratedActionModelLPFTpl::PinocchioModel
pinocchio::ModelTpl< Scalar > PinocchioModel
Definition: action.hpp:40
sobec::IntegratedActionModelLPFTpl
Definition: action.hpp:25
sobec::IntegratedActionDataLPFTpl::MatrixXs
MathBase::MatrixXs MatrixXs
Definition: action.hpp:173
sobec::IntegratedActionDataLPFTpl::IntegratedActionDataLPFTpl
IntegratedActionDataLPFTpl(Model< Scalar > *const model)
Definition: action.hpp:181
sobec::IntegratedActionDataLPFTpl::dy
VectorXs dy
Definition: action.hpp:194
sobec::IntegratedActionModelLPFTpl::ActivationModelQuadraticBarrier
ActivationModelQuadraticBarrierTpl< Scalar > ActivationModelQuadraticBarrier
Definition: action.hpp:42
sobec::IntegratedActionModelLPFTpl::MatrixXs
MathBase::MatrixXs MatrixXs
Definition: action.hpp:37
state.hpp
sobec::IntegratedActionDataLPFTpl::ActivationDataQuadraticBarrier
ActivationDataQuadraticBarrierTpl< Scalar > ActivationDataQuadraticBarrier
Definition: action.hpp:178
sobec::IntegratedActionDataLPFTpl::differential
boost::shared_ptr< DifferentialActionDataAbstractTpl< Scalar > > differential
Definition: action.hpp:193
sobec::IntegratedActionModelLPFTpl::StateLPF
StateLPFTpl< Scalar > StateLPF
Definition: action.hpp:38
sobec::IntegratedActionDataLPFTpl::MathBase
MathBaseTpl< Scalar > MathBase
Definition: action.hpp:170
sobec::IntegratedActionDataLPFTpl::~IntegratedActionDataLPFTpl
virtual ~IntegratedActionDataLPFTpl()
Definition: action.hpp:191
sobec::IntegratedActionDataLPFTpl::Base
ActionDataAbstractTpl< Scalar > Base
Definition: action.hpp:171
sobec::IntegratedActionDataLPFTpl
Definition: action.hpp:165