centroidal_control_collection
DdpCentroidal.h
Go to the documentation of this file.
1 /* Author: Masaki Murooka */
2 
3 #pragma once
4 
5 #include <nmpc_ddp/DDPSolver.h>
6 
7 namespace ForceColl
8 {
9 class Contact;
10 }
11 
12 namespace CCC
13 {
16 {
17 public:
19  struct MotionParam
20  {
21  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
22 
24  std::vector<std::shared_ptr<ForceColl::Contact>> contact_list;
25  };
26 
28  struct RefData
29  {
30  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
31 
33  Eigen::Vector3d pos = Eigen::Vector3d::Zero();
34  };
35 
37  struct WeightParam
38  {
40  Eigen::Vector3d running_pos;
41 
43  Eigen::Vector3d running_linear_momentum;
44 
46  Eigen::Vector3d running_angular_momentum;
47 
49  double running_force;
50 
52  Eigen::Vector3d terminal_pos;
53 
55  Eigen::Vector3d terminal_linear_momentum;
56 
58  Eigen::Vector3d terminal_angular_momentum;
59 
69  WeightParam(const Eigen::Vector3d & _running_pos = Eigen::Vector3d::Constant(1.0),
70  const Eigen::Vector3d & _running_linear_momentum = Eigen::Vector3d::Constant(0.0),
71  const Eigen::Vector3d & _running_angular_momentum = Eigen::Vector3d::Constant(1.0),
72  double _running_force = 1e-6,
73  const Eigen::Vector3d & _terminal_pos = Eigen::Vector3d::Constant(1.0),
74  const Eigen::Vector3d & _terminal_linear_momentum = Eigen::Vector3d::Constant(0.0),
75  const Eigen::Vector3d & _terminal_angular_momentum = Eigen::Vector3d::Constant(1.0))
76  : running_pos(_running_pos), running_linear_momentum(_running_linear_momentum),
77  running_angular_momentum(_running_angular_momentum), running_force(_running_force), terminal_pos(_terminal_pos),
78  terminal_linear_momentum(_terminal_linear_momentum), terminal_angular_momentum(_terminal_angular_momentum)
79  {
80  }
81  };
82 
87  class DdpProblem : public nmpc_ddp::DDPProblem<9, Eigen::Dynamic>
88  {
89  public:
90  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
91 
97  DdpProblem(double horizon_dt, double mass, const WeightParam & weight_param)
98  : nmpc_ddp::DDPProblem<9, Eigen::Dynamic>(horizon_dt), mass_(mass), weight_param_(weight_param)
99  {
100  }
101 
105  inline void setMotionParamFunc(const std::function<MotionParam(double)> & motion_param_func)
106  {
107  motion_param_func_ = motion_param_func;
108  }
109 
113  inline void setRefDataFunc(const std::function<RefData(double)> & ref_data_func)
114  {
115  ref_data_func_ = ref_data_func;
116  }
117 
118  using DDPProblem::inputDim;
119 
123  virtual int inputDim(double t) const override;
124 
161  virtual StateDimVector stateEq(double t, const StateDimVector & x, const InputDimVector & u) const override;
162 
169  virtual double runningCost(double t, const StateDimVector & x, const InputDimVector & u) const override;
170 
176  virtual double terminalCost(double t, const StateDimVector & x) const override;
177 
201  virtual void calcStateEqDeriv(double t,
202  const StateDimVector & x,
203  const InputDimVector & u,
204  Eigen::Ref<StateStateDimMatrix> state_eq_deriv_x,
205  Eigen::Ref<StateInputDimMatrix> state_eq_deriv_u) const override;
206 
217  inline virtual void calcStateEqDeriv(double, // t
218  const StateDimVector &, // x
219  const InputDimVector &, // u
220  Eigen::Ref<StateStateDimMatrix>, // state_eq_deriv_x
221  Eigen::Ref<StateInputDimMatrix>, // state_eq_deriv_u
222  std::vector<StateStateDimMatrix> &, // state_eq_deriv_xx
223  std::vector<InputInputDimMatrix> &, // state_eq_deriv_uu
224  std::vector<StateInputDimMatrix> & // state_eq_deriv_xu
225  ) const override
226  {
227  throw std::runtime_error("Second-order derivatives of state equation are not implemented.");
228  }
229 
237  virtual void calcRunningCostDeriv(double t,
238  const StateDimVector & x,
239  const InputDimVector & u,
240  Eigen::Ref<StateDimVector> running_cost_deriv_x,
241  Eigen::Ref<InputDimVector> running_cost_deriv_u) const override;
242 
253  virtual void calcRunningCostDeriv(double t,
254  const StateDimVector & x,
255  const InputDimVector & u,
256  Eigen::Ref<StateDimVector> running_cost_deriv_x,
257  Eigen::Ref<InputDimVector> running_cost_deriv_u,
258  Eigen::Ref<StateStateDimMatrix> running_cost_deriv_xx,
259  Eigen::Ref<InputInputDimMatrix> running_cost_deriv_uu,
260  Eigen::Ref<StateInputDimMatrix> running_cost_deriv_xu) const override;
261 
267  virtual void calcTerminalCostDeriv(double t,
268  const StateDimVector & x,
269  Eigen::Ref<StateDimVector> terminal_cost_deriv_x) const override;
270 
277  virtual void calcTerminalCostDeriv(double t,
278  const StateDimVector & x,
279  Eigen::Ref<StateDimVector> terminal_cost_deriv_x,
280  Eigen::Ref<StateStateDimMatrix> terminal_cost_deriv_xx) const override;
281 
282  public:
284  double mass_ = 0;
285 
286  protected:
289 
291  std::function<MotionParam(double)> motion_param_func_;
292 
294  std::function<RefData(double)> ref_data_func_;
295  };
296 
299  {
300  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
301 
303  Eigen::Vector3d pos = Eigen::Vector3d::Zero();
304 
306  Eigen::Vector3d vel = Eigen::Vector3d::Zero();
307 
309  Eigen::Vector3d angular_momentum = Eigen::Vector3d::Zero();
310 
316  std::vector<DdpProblem::InputDimVector> u_list = {};
317 
320 
325  InitialParam(const DdpProblem::StateDimVector & state, double mass);
326 
330  DdpProblem::StateDimVector toState(double mass) const;
331  };
332 
333 public:
334  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
335 
342  DdpCentroidal(double mass, double horizon_dt, int horizon_steps, const WeightParam & weight_param = WeightParam());
343 
351  Eigen::VectorXd planOnce(const std::function<MotionParam(double)> & motion_param_func,
352  const std::function<RefData(double)> & ref_data_func,
353  const InitialParam & initial_param,
354  double current_time);
355 
356 public:
358  std::shared_ptr<DdpProblem> ddp_problem_;
359 
361  std::shared_ptr<nmpc_ddp::DDPSolver<9, Eigen::Dynamic>> ddp_solver_;
362 
364  std::array<double, 2> force_scale_limits_ = {0.0, 1e6};
365 };
366 } // namespace CCC
CCC::DdpCentroidal::InitialParam::angular_momentum
Eigen::Vector3d angular_momentum
Angular momentum [kg m^2/s].
Definition: DdpCentroidal.h:309
CCC::DdpCentroidal::WeightParam::WeightParam
WeightParam(const Eigen::Vector3d &_running_pos=Eigen::Vector3d::Constant(1.0), const Eigen::Vector3d &_running_linear_momentum=Eigen::Vector3d::Constant(0.0), const Eigen::Vector3d &_running_angular_momentum=Eigen::Vector3d::Constant(1.0), double _running_force=1e-6, const Eigen::Vector3d &_terminal_pos=Eigen::Vector3d::Constant(1.0), const Eigen::Vector3d &_terminal_linear_momentum=Eigen::Vector3d::Constant(0.0), const Eigen::Vector3d &_terminal_angular_momentum=Eigen::Vector3d::Constant(1.0))
Constructor.
Definition: DdpCentroidal.h:69
CCC::DdpCentroidal::ddp_problem_
std::shared_ptr< DdpProblem > ddp_problem_
DDP problem.
Definition: DdpCentroidal.h:358
CCC::DdpCentroidal::DdpProblem::runningCost
virtual double runningCost(double t, const StateDimVector &x, const InputDimVector &u) const override
Calculate running cost.
CCC::DdpCentroidal::RefData::pos
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::Vector3d pos
CoM position [m].
Definition: DdpCentroidal.h:33
CCC::DdpCentroidal::DdpProblem::stateEq
virtual StateDimVector stateEq(double t, const StateDimVector &x, const InputDimVector &u) const override
Calculate discrete state equation.
CCC::DdpCentroidal::DdpProblem::setRefDataFunc
void setRefDataFunc(const std::function< RefData(double)> &ref_data_func)
Set function of reference data.
Definition: DdpCentroidal.h:113
CCC::DdpCentroidal::DdpProblem::calcStateEqDeriv
virtual void calcStateEqDeriv(double t, const StateDimVector &x, const InputDimVector &u, Eigen::Ref< StateStateDimMatrix > state_eq_deriv_x, Eigen::Ref< StateInputDimMatrix > state_eq_deriv_u) const override
Calculate first-order derivatives of discrete state equation.
CCC::DdpCentroidal::WeightParam::terminal_pos
Eigen::Vector3d terminal_pos
CoM position weight in terminal cost.
Definition: DdpCentroidal.h:52
CCC::DdpCentroidal::WeightParam::running_angular_momentum
Eigen::Vector3d running_angular_momentum
Angular momentum weight in running cost.
Definition: DdpCentroidal.h:46
CCC::DdpCentroidal::WeightParam::running_linear_momentum
Eigen::Vector3d running_linear_momentum
Linear momentum weight in running cost.
Definition: DdpCentroidal.h:43
CCC::DdpCentroidal::DdpProblem::setMotionParamFunc
void setMotionParamFunc(const std::function< MotionParam(double)> &motion_param_func)
Set function of motion parameter.
Definition: DdpCentroidal.h:105
CCC::DdpCentroidal::DdpProblem::calcStateEqDeriv
virtual void calcStateEqDeriv(double, const StateDimVector &, const InputDimVector &, Eigen::Ref< StateStateDimMatrix >, Eigen::Ref< StateInputDimMatrix >, std::vector< StateStateDimMatrix > &, std::vector< InputInputDimMatrix > &, std::vector< StateInputDimMatrix > &) const override
Calculate first-order and second-order derivatives of discrete state equation.
Definition: DdpCentroidal.h:217
CCC::DdpCentroidal::WeightParam::running_pos
Eigen::Vector3d running_pos
CoM position weight in running cost.
Definition: DdpCentroidal.h:40
CCC::DdpCentroidal::DdpProblem::terminalCost
virtual double terminalCost(double t, const StateDimVector &x) const override
Calculate terminal cost.
CCC::DdpCentroidal::RefData
Reference data.
Definition: DdpCentroidal.h:28
CCC::DdpCentroidal::WeightParam::terminal_angular_momentum
Eigen::Vector3d terminal_angular_momentum
Angular momentum weight in terminal cost.
Definition: DdpCentroidal.h:58
CCC::DdpCentroidal::MotionParam::contact_list
EIGEN_MAKE_ALIGNED_OPERATOR_NEW std::vector< std::shared_ptr< ForceColl::Contact > > contact_list
Contact list.
Definition: DdpCentroidal.h:24
CCC::DdpCentroidal::WeightParam
Weight parameter.
Definition: DdpCentroidal.h:37
CCC::DdpCentroidal::DdpProblem::DdpProblem
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DdpProblem(double horizon_dt, double mass, const WeightParam &weight_param)
Constructor.
Definition: DdpCentroidal.h:97
CCC::DdpCentroidal::DdpProblem::mass_
double mass_
Robot mass [Kg].
Definition: DdpCentroidal.h:284
CCC::DdpCentroidal::InitialParam::InitialParam
InitialParam()
Constructor.
Definition: DdpCentroidal.h:319
CCC::DdpCentroidal::DdpProblem::motion_param_func_
std::function< MotionParam(double)> motion_param_func_
Function of motion parameter.
Definition: DdpCentroidal.h:291
CCC::DdpCentroidal::DdpProblem
DDP problem of centroidal model.
Definition: DdpCentroidal.h:87
CCC::DdpCentroidal::DdpCentroidal
EIGEN_MAKE_ALIGNED_OPERATOR_NEW DdpCentroidal(double mass, double horizon_dt, int horizon_steps, const WeightParam &weight_param=WeightParam())
Constructor.
CCC::DdpCentroidal::InitialParam::vel
Eigen::Vector3d vel
CoM velocity [m/s].
Definition: DdpCentroidal.h:306
CCC
Definition: CommonModels.h:7
CCC::DdpCentroidal::InitialParam::pos
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::Vector3d pos
CoM position [m].
Definition: DdpCentroidal.h:303
CCC::DdpCentroidal::DdpProblem::calcRunningCostDeriv
virtual void calcRunningCostDeriv(double t, const StateDimVector &x, const InputDimVector &u, Eigen::Ref< StateDimVector > running_cost_deriv_x, Eigen::Ref< InputDimVector > running_cost_deriv_u) const override
Calculate first-order derivatives of running cost.
CCC::DdpCentroidal
Differential dynamic programming (DDP) for centroidal model.
Definition: DdpCentroidal.h:15
CCC::DdpCentroidal::WeightParam::running_force
double running_force
Force weight in running cost.
Definition: DdpCentroidal.h:49
CCC::DdpCentroidal::force_scale_limits_
std::array< double, 2 > force_scale_limits_
Force scale limits (i.e., limits of in the order of lower, upper)
Definition: DdpCentroidal.h:364
ForceColl
Definition: DdpCentroidal.h:7
CCC::DdpCentroidal::ddp_solver_
std::shared_ptr< nmpc_ddp::DDPSolver< 9, Eigen::Dynamic > > ddp_solver_
DDP solver.
Definition: DdpCentroidal.h:361
CCC::DdpCentroidal::DdpProblem::inputDim
virtual int inputDim(double t) const override
Gets the input dimension.
CCC::DdpCentroidal::DdpProblem::calcTerminalCostDeriv
virtual void calcTerminalCostDeriv(double t, const StateDimVector &x, Eigen::Ref< StateDimVector > terminal_cost_deriv_x) const override
Calculate first-order derivatives of terminal cost.
CCC::DdpCentroidal::WeightParam::terminal_linear_momentum
Eigen::Vector3d terminal_linear_momentum
Linear momentum weight in terminal cost.
Definition: DdpCentroidal.h:55
CCC::DdpCentroidal::InitialParam::u_list
std::vector< DdpProblem::InputDimVector > u_list
Initial guess of input sequence.
Definition: DdpCentroidal.h:316
CCC::DdpCentroidal::InitialParam::toState
DdpProblem::StateDimVector toState(double mass) const
Get state of DDP problem.
CCC::DdpCentroidal::InitialParam
Initial parameter.
Definition: DdpCentroidal.h:298
CCC::DdpCentroidal::planOnce
Eigen::VectorXd planOnce(const std::function< MotionParam(double)> &motion_param_func, const std::function< RefData(double)> &ref_data_func, const InitialParam &initial_param, double current_time)
Plan one step.
CCC::DdpCentroidal::MotionParam
Motion parameter.
Definition: DdpCentroidal.h:19
CCC::DdpCentroidal::DdpProblem::ref_data_func_
std::function< RefData(double)> ref_data_func_
Function of reference data.
Definition: DdpCentroidal.h:294
CCC::DdpCentroidal::DdpProblem::weight_param_
WeightParam weight_param_
Weight parameter.
Definition: DdpCentroidal.h:288