crocoddyl  1.4.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
action.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2018-2020, LAAS-CNRS, New York University, Max Planck Gesellschaft,
5 // University of Edinburgh
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
9 
10 #ifndef CROCODDYL_CORE_NUMDIFF_ACTION_HPP_
11 #define CROCODDYL_CORE_NUMDIFF_ACTION_HPP_
12 
13 #include <vector>
14 
15 #include "crocoddyl/core/fwd.hpp"
16 #include "crocoddyl/core/action-base.hpp"
17 
18 namespace crocoddyl {
19 
51 template <typename _Scalar>
52 class ActionModelNumDiffTpl : public ActionModelAbstractTpl<_Scalar> {
53  public:
54  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
55 
56  typedef _Scalar Scalar;
57  typedef ActionDataAbstractTpl<Scalar> ActionDataAbstract;
58  typedef ActionModelAbstractTpl<Scalar> Base;
59  typedef ActionDataNumDiffTpl<Scalar> Data;
60  typedef MathBaseTpl<Scalar> MathBase;
61  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
62  typedef typename MathBaseTpl<Scalar>::MatrixXs MatrixXs;
63 
69  explicit ActionModelNumDiffTpl(boost::shared_ptr<Base> model);
70 
74  virtual ~ActionModelNumDiffTpl();
75 
79  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
80  const Eigen::Ref<const VectorXs>& u);
81 
85  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
86  const Eigen::Ref<const VectorXs>& u);
87 
93  virtual boost::shared_ptr<ActionDataAbstract> createData();
94 
100  const boost::shared_ptr<Base>& get_model() const;
101 
107  const Scalar& get_disturbance() const;
108 
114  void set_disturbance(const Scalar& disturbance);
115 
122  bool get_with_gauss_approx();
123 
124  protected:
126  using Base::nr_;
127  using Base::nu_;
128  using Base::state_;
129  using Base::u_lb_;
130  using Base::u_ub_;
131  using Base::unone_;
132 
133  private:
145  void assertStableStateFD(const Eigen::Ref<const VectorXs>& x);
146 
151  boost::shared_ptr<Base> model_;
152 
157  Scalar disturbance_;
158 };
159 
160 template <typename _Scalar>
161 struct ActionDataNumDiffTpl : public ActionDataAbstractTpl<_Scalar> {
162  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
163 
164  typedef _Scalar Scalar;
165  typedef MathBaseTpl<Scalar> MathBase;
166  typedef ActionDataAbstractTpl<Scalar> Base;
167  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
168  typedef typename MathBaseTpl<Scalar>::MatrixXs MatrixXs;
169 
176  template <template <typename Scalar> class Model>
177  explicit ActionDataNumDiffTpl(Model<Scalar>* const model)
178  : Base(model),
179  Rx(model->get_model()->get_nr(), model->get_model()->get_state()->get_ndx()),
180  Ru(model->get_model()->get_nr(), model->get_model()->get_nu()),
181  dx(model->get_model()->get_state()->get_ndx()),
182  du(model->get_model()->get_nu()),
183  xp(model->get_model()->get_state()->get_nx()) {
184  Rx.setZero();
185  Ru.setZero();
186  dx.setZero();
187  du.setZero();
188  xp.setZero();
189 
190  const std::size_t& ndx = model->get_model()->get_state()->get_ndx();
191  const std::size_t& nu = model->get_model()->get_nu();
192  data_0 = model->get_model()->createData();
193  for (std::size_t i = 0; i < ndx; ++i) {
194  data_x.push_back(model->get_model()->createData());
195  }
196  for (std::size_t i = 0; i < nu; ++i) {
197  data_u.push_back(model->get_model()->createData());
198  }
199  }
200 
201  using Base::cost;
202  using Base::Fu;
203  using Base::Fx;
204  using Base::Lu;
205  using Base::Luu;
206  using Base::Lx;
207  using Base::Lxu;
208  using Base::Lxx;
209  using Base::r;
210  using Base::xnext;
211 
212  MatrixXs Rx;
213  MatrixXs Ru;
214  VectorXs dx;
215  VectorXs du;
216  VectorXs xp;
217  boost::shared_ptr<Base> data_0;
218  std::vector<boost::shared_ptr<Base> > data_x;
219  std::vector<boost::shared_ptr<Base> > data_u;
220 };
221 
222 } // namespace crocoddyl
223 
224 /* --- Details -------------------------------------------------------------- */
225 /* --- Details -------------------------------------------------------------- */
226 /* --- Details -------------------------------------------------------------- */
227 #include "crocoddyl/core/numdiff/action.hxx"
228 
229 #endif // CROCODDYL_CORE_NUMDIFF_ACTION_HPP_
virtual boost::shared_ptr< ActionDataAbstract > createData()
Create a Data object from the given model.
bool has_control_limits_
Indicates whether any of the control limits is finite.
Definition: action-base.hpp:71
MatrixXs Ru
Cost residual jacobian: .
Definition: action.hpp:213
VectorXs dx
State disturbance.
Definition: action.hpp:214
const boost::shared_ptr< Base > & get_model() const
Get the model_ object.
std::size_t nu_
Control dimension.
Definition: action-base.hpp:65
virtual void calc(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
ActionDataNumDiffTpl(Model< Scalar > *const model)
Construct a new ActionDataNumDiff object.
Definition: action.hpp:177
void set_disturbance(const Scalar &disturbance)
Set the disturbance_ object.
virtual ~ActionModelNumDiffTpl()
Destroy the ActionModelNumDiff object.
VectorXs xp
The integrated state from the disturbance on one DoF "\f$ \int x dx_i \f$".
Definition: action.hpp:216
boost::shared_ptr< Base > data_0
The data that contains the final results.
Definition: action.hpp:217
virtual void calcDiff(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
ActionModelNumDiffTpl(boost::shared_ptr< Base > model)
Construct a new ActionModelNumDiff object.
std::vector< boost::shared_ptr< Base > > data_u
The temporary data associated with the control variation.
Definition: action.hpp:219
MatrixXs Rx
Cost residual jacobian: .
Definition: action.hpp:212
VectorXs unone_
Neutral state.
Definition: action-base.hpp:68
VectorXs u_ub_
Upper control limits.
Definition: action-base.hpp:70
std::vector< boost::shared_ptr< Base > > data_x
The temporary data associated with the state variation.
Definition: action.hpp:218
VectorXs u_lb_
Lower control limits.
Definition: action-base.hpp:69
boost::shared_ptr< StateAbstract > state_
Model of the state.
Definition: action-base.hpp:67
const Scalar & get_disturbance() const
Get the disturbance_ object.
bool get_with_gauss_approx()
Identify if the Gauss approximation is going to be used or not.
VectorXs du
Control disturbance.
Definition: action.hpp:215
std::size_t nr_
Dimension of the cost residual.
Definition: action-base.hpp:66