A sparse MPC solver for walking motion generation.
smpc_solver.h
Go to the documentation of this file.
1 
10 #ifndef SMPC_SOLVER_H
11 #define SMPC_SOLVER_H
12 
13 #include <vector>
14 
15 
16 class qp_as;
17 class qp_ip;
18 
19 
22 
24 #define SMPC_NUM_STATE_VAR 6
25 #define SMPC_NUM_CONTROL_VAR 2
27 #define SMPC_NUM_VAR 8
29 
30 namespace smpc
31 {
32  // -------------------------------
33 
34 
41  void enable_fexceptions();
42 
43 
44  // -------------------------------
45 
46 
50  class state
51  {
52  public:
64 
65 
69  state();
70 
71 
72  // -------------------------------
73 
74 
77  double &x () {return state_vector[0];};
78  double &y () {return state_vector[3];};
79  const double &x () const {return state_vector[0];};
80  const double &y () const {return state_vector[3];};
82 
85  double &vx () {return state_vector[1];};
86  double &vy () {return state_vector[4];};
87  const double &vx () const {return state_vector[1];};
88  const double &vy () const {return state_vector[4];};
90 
93  double &ax () {return state_vector[2];};
94  double &ay () {return state_vector[5];};
95  const double &ax () const {return state_vector[2];};
96  const double &ay () const {return state_vector[5];};
98 
99 
100 
101  // -------------------------------
102 
103 
111  void set (double x_, double y_);
112 
113 
124  void set (double x_, double vx_, double ax_,
125  double y_, double vy_, double ay_);
126  };
127 
128 
140  class state_com : public state
141  {
142  public:
143  state_com () : state () {};
144  };
145 
146 
159  class state_zmp : public state
160  {
161  public:
162  state_zmp () : state () {};
163  };
164 
165 
169  class control
170  {
171  public:
179 
180 
181  control();
182 
183  // -------------------------------
184 
185 
189  double &jx () {return control_vector[0];};
193  double &jy () {return control_vector[1];};
194  };
195 
196 
197 
201  class solver
202  {
203  public:
207  virtual ~solver() = 0;
208 
220  virtual void set_parameters (
221  const double* T,
222  const double* h,
223  const double h_initial,
224  const double* angle,
225  const double* zref_x,
226  const double* zref_y,
227  const double* lb,
228  const double* ub) = 0;
229 
230 
232 
239  virtual void form_init_fp (
240  const double *x_coord,
241  const double *y_coord,
242  const state_com &init_state,
243  double* X) = 0;
244 
245  virtual void form_init_fp (
246  const double *x_coord,
247  const double *y_coord,
248  const state_zmp &init_state,
249  double* X) = 0;
251 
252 
256  virtual void solve () = 0;
257 
258 
259  // -------------------------------
260 
261 
263 
268  virtual void get_next_state (state_com &s) const = 0;
269  virtual void get_next_state (state_zmp &s) const = 0;
271 
273 
279  virtual void get_state (state_com &s, const int ind) const = 0;
280  virtual void get_state (state_zmp &s, const int ind) const = 0;
282 
283 
284  // -------------------------------
285 
286 
293  virtual void get_first_controls (control &c) const = 0;
294 
295 
304  virtual void get_controls (control &c, const int ind) const = 0;
305  };
306 
307 
308 
309 
313  class solver_as : public solver
314  {
315  public:
316 
335  solver_as (
336  const int N,
337  const double gain_position = 2000.0,
338  const double gain_velocity = 150.0,
339  const double gain_acceleration = 0.02,
340  const double gain_jerk = 1.0,
341  const double tol = 1e-7,
342  const unsigned int max_added_constraints_num = 0,
343  const bool constraint_removal_on = true,
344  const bool obj_computation_on = false);
345 
346 
347  ~solver_as();
348 
349 
350  // -------------------------------
351 
352 
356  void set_parameters (
357  const double*, const double*, const double, const double*,
358  const double*, const double*, const double*, const double*);
359  void form_init_fp (const double *, const double *, const state_com &, double*);
360  void form_init_fp (const double *, const double *, const state_zmp &, double*);
361  void solve ();
362  void get_next_state (state_com &) const;
363  void get_next_state (state_zmp &) const;
364  void get_state (state_com &, const int) const;
365  void get_state (state_zmp &, const int) const;
366  void get_first_controls (control &) const;
367  void get_controls (control &, const int) const;
369 
370 
371  // -------------------------------
372 
373 
380  unsigned int added_constraints_num;
381 
388 
394  unsigned int active_set_size;
395 
396 
404  std::vector<double> objective_log;
405 
406 
407  // -------------------------------
408 
409 
414  };
415 
416 
421  {
428  };
429 
430 
434  class solver_ip : public solver
435  {
436  public:
437 
458  solver_ip (
459  const int N,
460  const double gain_position = 2000.0,
461  const double gain_velocity = 150.0,
462  const double gain_acceleration = 0.01,
463  const double gain_jerk = 1.0,
464  const double tol = 1e-3,
465  const double tol_out = 1e-2,
466  const double t = 100,
467  const double mu = 15,
468  const double bs_alpha = 0.01,
469  const double bs_beta = 0.5,
470  const int unsigned max_iter = 0,
472  const bool obj_computation_on = false);
473 
474  ~solver_ip();
475 
476 
477  // -------------------------------
478 
479 
483  void set_parameters (
484  const double*, const double*, const double, const double*,
485  const double*, const double*, const double*, const double*);
486  void form_init_fp (const double *, const double *, const state_com &, double*);
487  void form_init_fp (const double *, const double *, const state_zmp &, double*);
488  void solve ();
489  void get_next_state (state_com &) const;
490  void get_next_state (state_zmp &) const;
491  void get_state (state_com &, const int) const;
492  void get_state (state_zmp &, const int) const;
493  void get_first_controls (control &) const;
494  void get_controls (control &, const int) const;
496 
497 
498  // -------------------------------
499 
500 
506  unsigned int ext_loop_iterations;
507 
513  unsigned int int_loop_iterations;
514 
520  unsigned int bt_search_iterations;
521 
522 
530  std::vector<double> objective_log;
531 
532 
533  // -------------------------------
534 
535 
540  };
541 }
543 
544 #endif /*SMPC_SOLVER_H*/
state()
Default constructor (everything set to 0).
double state_vector[SMPC_NUM_STATE_VAR]
Definition: smpc_solver.h:63
qp_ip * qp_sol
Internal representation.
Definition: smpc_solver.h:539
#define SMPC_NUM_STATE_VAR
Number of state variables.
Definition: smpc_solver.h:24
double & x()
X/Y coordinate.
Definition: smpc_solver.h:77
unsigned int bt_search_iterations
The total number of iterations of backtracking search.
Definition: smpc_solver.h:520
Solve a quadratic program with a specific structure. qp_ip = Quadratic Programming / Interior-point m...
Definition: qp_ip.h:38
double & jx()
Jerk along Y axis.
Definition: smpc_solver.h:189
const double & x() const
X/Y coordinate.
Definition: smpc_solver.h:79
unsigned int int_loop_iterations
The total number of iterations of the internal loop.
Definition: smpc_solver.h:513
void get_first_controls(control &) const
These functions are documented in the definition of the base abstract class smpc::solver.
void get_next_state(state_com &) const
These functions are documented in the definition of the base abstract class smpc::solver.
virtual void get_controls(control &c, const int ind) const =0
The same as get_first_controls, but takes an additional parameter - the index of the control inputs....
unsigned int ext_loop_iterations
The number of iterations of the external loop.
Definition: smpc_solver.h:506
unsigned int removed_constraints_num
Number of removed constraints.
Definition: smpc_solver.h:387
virtual ~solver()=0
Virtual destructor.
Definition: smpc_solver.cpp:43
std::vector< double > objective_log
Contains values of objective function after each iteration, the initial value is also included.
Definition: smpc_solver.h:404
Use objective function, which includes logarithmic barrier term.
Definition: smpc_solver.h:425
double control_vector[SMPC_NUM_CONTROL_VAR]
Definition: smpc_solver.h:178
double & jy()
Jerk along Y axis.
Definition: smpc_solver.h:193
Use original objective function (no logarithmic barrier term).
Definition: smpc_solver.h:427
A container for a state in the original form:
Definition: smpc_solver.h:140
virtual void form_init_fp(const double *x_coord, const double *y_coord, const state_com &init_state, double *X)=0
Generates an initial feasible point.
double & ay()
Acceleration along X/Y axis.
Definition: smpc_solver.h:94
An abstract class representing state.
Definition: smpc_solver.h:50
A container for a state in tilde form (after variable substitution):
Definition: smpc_solver.h:159
void form_init_fp(const double *, const double *, const state_com &, double *)
These functions are documented in the definition of the base abstract class smpc::solver.
double & vy()
Velocity along X/Y axis.
Definition: smpc_solver.h:86
Abstract class providing common interface functions.
Definition: smpc_solver.h:201
double & y()
X/Y coordinate.
Definition: smpc_solver.h:78
Solve a quadratic program with a specific structure. qp_as = Quadratic Programming / Active Set.
Definition: qp_as.h:37
void set_parameters(const double *, const double *, const double, const double *, const double *, const double *, const double *, const double *)
These functions are documented in the definition of the base abstract class smpc::solver.
Definition: smpc_solver.cpp:80
const double & vy() const
Velocity along X/Y axis.
Definition: smpc_solver.h:88
const double & vx() const
Velocity along X/Y axis.
Definition: smpc_solver.h:87
const double & ay() const
Acceleration along X/Y axis.
Definition: smpc_solver.h:96
void set(double x_, double y_)
Set coordinates, velocities and accelerations are assumed to be zero.
virtual void solve()=0
Solve QP problem.
backtrackingSearchType
Type of the backtracking search.
Definition: smpc_solver.h:420
void solve()
These functions are documented in the definition of the base abstract class smpc::solver.
#define SMPC_NUM_CONTROL_VAR
Number of control variables.
Definition: smpc_solver.h:26
void get_controls(control &, const int) const
These functions are documented in the definition of the base abstract class smpc::solver.
void set_parameters(const double *, const double *, const double, const double *, const double *, const double *, const double *, const double *)
These functions are documented in the definition of the base abstract class smpc::solver.
qp_as * qp_sol
Internal representation.
Definition: smpc_solver.h:413
virtual void get_next_state(state_com &s) const =0
Returns the next state.
virtual void get_state(state_com &s, const int ind) const =0
Returns a state with given index.
API of the sparse MPC solver.
Definition: smpc_solver.h:313
void get_next_state(state_com &) const
These functions are documented in the definition of the base abstract class smpc::solver.
double & ax()
Acceleration along X/Y axis.
Definition: smpc_solver.h:93
virtual void set_parameters(const double *T, const double *h, const double h_initial, const double *angle, const double *zref_x, const double *zref_y, const double *lb, const double *ub)=0
Initializes quadratic problem.
const double & y() const
X/Y coordinate.
Definition: smpc_solver.h:80
API of the sparse MPC solver.
Definition: smpc_solver.h:434
void solve()
These functions are documented in the definition of the base abstract class smpc::solver.
void get_state(state_com &, const int) const
These functions are documented in the definition of the base abstract class smpc::solver.
void get_state(state_com &, const int) const
These functions are documented in the definition of the base abstract class smpc::solver.
void form_init_fp(const double *, const double *, const state_com &, double *)
These functions are documented in the definition of the base abstract class smpc::solver.
Definition: smpc_solver.cpp:93
void enable_fexceptions()
Enable floating point exceptions: die rather than process incorrect data. SIGFPE is sent to the progr...
Definition: smpc_solver.cpp:32
virtual void get_first_controls(control &c) const =0
Returns the controls that must be applied to reach the next state.
const double & ax() const
Acceleration along X/Y axis.
Definition: smpc_solver.h:95
void get_first_controls(control &) const
These functions are documented in the definition of the base abstract class smpc::solver.
unsigned int added_constraints_num
Number of added constraints (the constraints, that were removed are also counted).
Definition: smpc_solver.h:380
A container for a control vector.
Definition: smpc_solver.h:169
solver_as(const int N, const double gain_position=2000.0, const double gain_velocity=150.0, const double gain_acceleration=0.02, const double gain_jerk=1.0, const double tol=1e-7, const unsigned int max_added_constraints_num=0, const bool constraint_removal_on=true, const bool obj_computation_on=false)
Constructor: initialize an active set method solver.
Definition: smpc_solver.cpp:46
double & vx()
Velocity along X/Y axis.
Definition: smpc_solver.h:85
unsigned int active_set_size
The final size of the active set.
Definition: smpc_solver.h:394
std::vector< double > objective_log
Contains values of objective function after each iteration, the initial value is also included.
Definition: smpc_solver.h:530
void get_controls(control &, const int) const
These functions are documented in the definition of the base abstract class smpc::solver.
solver_ip(const int N, const double gain_position=2000.0, const double gain_velocity=150.0, const double gain_acceleration=0.01, const double gain_jerk=1.0, const double tol=1e-3, const double tol_out=1e-2, const double t=100, const double mu=15, const double bs_alpha=0.01, const double bs_beta=0.5, const int unsigned max_iter=0, const backtrackingSearchType bs_type=SMPC_IP_BS_LOGBAR, const bool obj_computation_on=false)
Constructor: initialize an interior-point method solver.
Disable backtracking search.
Definition: smpc_solver.h:423