A sparse MPC solver for walking motion generation.
smpc_solver.cpp
Go to the documentation of this file.
1 
11 /****************************************
12  * INCLUDES
13  ****************************************/
14 
15 #include "solver_config.h"
16 
17 #ifdef HAVE_FEENABLEEXCEPT
18 #include <fenv.h> // feenableexcept()
19 #endif
20 
21 #include "qp_as.h"
22 #include "qp_ip.h"
23 #include "smpc_solver.h"
24 #include "state_handling.h"
25 
26 
27 /****************************************
28  * FUNCTIONS
29  ****************************************/
30 namespace smpc
31 {
33  {
34 #ifdef HAVE_FEENABLEEXCEPT
35  feenableexcept(
36  FE_ALL_EXCEPT &
37  // ignore precision loss due to rounding
38  ~FE_INEXACT);
39 #endif
40  }
41 
42 
43  solver::~solver() {} // virtual destructor
44 
45 
47  const int N,
48  const double gain_position,
49  const double gain_velocity,
50  const double gain_acceleration,
51  const double gain_jerk,
52  const double tol,
53  const unsigned int max_added_constraints_num,
54  const bool constraint_removal_on,
55  const bool obj_computation_on)
56  {
57  qp_sol = new qp_as (
58  N,
59  gain_position, gain_velocity, gain_acceleration, gain_jerk,
60  tol,
61  obj_computation_on,
62  max_added_constraints_num, constraint_removal_on);
65  active_set_size = 0;
66  }
67 
68 
70  {
71  if (qp_sol != NULL)
72  {
73  delete qp_sol;
74  }
75  }
76 
77 
78 
79 
81  const double* T, const double* h, const double h_initial,
82  const double* angle,
83  const double* zref_x, const double* zref_y,
84  const double* lb, const double* ub)
85  {
86  if (qp_sol != NULL)
87  {
88  qp_sol->set_parameters(T, h, h_initial, angle, zref_x, zref_y, lb, ub);
89  }
90  }
91 
92 
94  const double *x_coord,
95  const double *y_coord,
96  const state_com &init_state,
97  double* X)
98  {
99  if (qp_sol != NULL)
100  {
101  qp_sol->form_init_fp (x_coord, y_coord, init_state.state_vector, false, X);
102  }
103  }
104 
105 
107  const double *x_coord,
108  const double *y_coord,
109  const state_zmp &init_state,
110  double* X)
111  {
112  if (qp_sol != NULL)
113  {
114  qp_sol->form_init_fp (x_coord, y_coord, init_state.state_vector, true, X);
115  }
116  }
117 
118 
120  {
121  if (qp_sol != NULL)
122  {
124 
128  }
129  }
130 
131 
132  //************************************************************
133 
134 
136  {
137  get_state (s, 0);
138  }
139 
140 
141  void solver_as::get_state (state_zmp &s, const int ind) const
142  {
143  if (qp_sol != NULL)
144  {
145  int index;
146  if (ind >= qp_sol->N)
147  {
148  index = qp_sol->N - 1;
149  }
150  else
151  {
152  index = ind;
153  }
154 
155  for (int i = 0; i < SMPC_NUM_STATE_VAR; i++)
156  {
157  s.state_vector[i] = qp_sol->X[index*SMPC_NUM_STATE_VAR + i];
158  }
159  }
160  }
161 
162 
163  //************************************************************
164 
165 
167  {
168  get_state (s, 0);
169  }
170 
171 
172  void solver_as::get_state (state_com &s, const int ind) const
173  {
174  if (qp_sol != NULL)
175  {
176  int index;
177  if (ind >= qp_sol->N)
178  {
179  index = qp_sol->N - 1;
180  }
181  else
182  {
183  index = ind;
184  }
185 
186  for (int i = 0; i < SMPC_NUM_STATE_VAR; i++)
187  {
188  s.state_vector[i] = qp_sol->X[index*SMPC_NUM_STATE_VAR + i];
189  }
191  }
192  }
193 
194 
195  //************************************************************
196 
197 
199  {
200  get_controls (c, 0);
201  }
202 
203 
204  void solver_as::get_controls (control &c, const int ind) const
205  {
206  if (qp_sol != NULL)
207  {
209  qp_sol->N,
210  qp_sol->X,
211  ind,
212  c.control_vector);
213  }
214  }
215 
216 
217 //************************************************************
218 //************************************************************
219 //************************************************************
220 
221 
223  const int N,
224  const double gain_position, const double gain_velocity, const double gain_acceleration,
225  const double gain_jerk,
226  const double tol, const double tol_out,
227  const double t,
228  const double mu,
229  const double bs_alpha, const double bs_beta,
230  const unsigned int max_iter,
231  const backtrackingSearchType bs_type,
232  const bool obj_computation_on)
233  {
234  qp_sol = new qp_ip (
235  N,
236  gain_position, gain_velocity, gain_acceleration, gain_jerk,
237  tol,
238  obj_computation_on, bs_type);
239  qp_sol->set_ip_parameters (t, mu, bs_alpha, bs_beta, max_iter, tol_out);
240 
244  }
245 
246 
248  {
249  if (qp_sol != NULL)
250  {
251  delete qp_sol;
252  }
253  }
254 
255 
257  const double* T, const double* h, const double h_initial,
258  const double* angle,
259  const double* zref_x, const double* zref_y,
260  const double* lb, const double* ub)
261  {
262  if (qp_sol != NULL)
263  {
264  qp_sol->set_parameters(T, h, h_initial, angle, zref_x, zref_y, lb, ub);
265  }
266  }
267 
268 
269 
271  const double *x_coord,
272  const double *y_coord,
273  const state_com &init_state,
274  double* X)
275  {
276  if (qp_sol != NULL)
277  {
278  qp_sol->form_init_fp (x_coord, y_coord, init_state.state_vector, false, X);
279  }
280  }
281 
283  const double *x_coord,
284  const double *y_coord,
285  const state_zmp &init_state,
286  double* X)
287  {
288  if (qp_sol != NULL)
289  {
290  qp_sol->form_init_fp (x_coord, y_coord, init_state.state_vector, true, X);
291  }
292  }
293 
294 
295 
297  {
298  if (qp_sol != NULL)
299  {
301 
305  }
306  }
307 
308 
309  //************************************************************
310 
311 
313  {
314  get_state (s, 0);
315  }
316 
317 
318  void solver_ip::get_state (state_zmp &s, const int ind) const
319  {
320  if (qp_sol != NULL)
321  {
322  int index;
323  if (ind >= qp_sol->N)
324  {
325  index = qp_sol->N - 1;
326  }
327  else
328  {
329  index = ind;
330  }
331 
332  for (int i = 0; i < SMPC_NUM_STATE_VAR; i++)
333  {
334  s.state_vector[i] = qp_sol->X[index*SMPC_NUM_STATE_VAR + i];
335  }
337  qp_sol->spar[index].sin,
338  qp_sol->spar[index].cos,
339  s.state_vector);
340  }
341  }
342 
343 
344  //************************************************************
345 
346 
348  {
349  get_state (s, 0);
350  }
351 
352 
353  void solver_ip::get_state (state_com &s, const int ind) const
354  {
355  if (qp_sol != NULL)
356  {
357  int index;
358  if (ind >= qp_sol->N)
359  {
360  index = qp_sol->N - 1;
361  }
362  else
363  {
364  index = ind;
365  }
366 
367  for (int i = 0; i < SMPC_NUM_STATE_VAR; i++)
368  {
369  s.state_vector[i] = qp_sol->X[index*SMPC_NUM_STATE_VAR + i];
370  }
372  qp_sol->spar[index].sin,
373  qp_sol->spar[index].cos,
374  s.state_vector);
376  }
377  }
378 
379 
380  //************************************************************
381 
382 
384  {
385  get_controls (c, 0);
386  }
387 
388 
389  void solver_ip::get_controls (control &c, const int ind) const
390  {
391  if (qp_sol != NULL)
392  {
394  qp_sol->N,
395  qp_sol->X,
396  ind,
397  c.control_vector);
398  }
399  }
400 
401 
402 //************************************************************
403 //************************************************************
404 //************************************************************
405 
406 
408  {
409  control_vector[0] = 0.0;
410  control_vector[1] = 0.0;
411  }
412 
413 
415  {
416  set (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
417  }
418 
419 
420  void state::set (double x_, double y_)
421  {
422  set (x_, 0.0, 0.0, y_, 0.0, 0.0);
423  }
424 
425 
426  void state::set (
427  double x_, double vx_, double ax_,
428  double y_, double vy_, double ay_)
429  {
430  state_vector[0] = x_;
431  state_vector[1] = vx_;
432  state_vector[2] = ax_;
433  state_vector[3] = y_;
434  state_vector[4] = vy_;
435  state_vector[5] = ay_;
436  }
437 }
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
unsigned int ext_loop_counter
Definition: qp_ip.h:88
#define SMPC_NUM_STATE_VAR
Number of state variables.
Definition: smpc_solver.h:24
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
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 bar_to_tilde(const double sinA, const double cosA, double *state)
Converts state from X_tilde to X_bar.
void get_next_state(state_com &) const
These functions are documented in the definition of the base abstract class smpc::solver.
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
unsigned int int_loop_counter
Definition: qp_ip.h:87
double control_vector[SMPC_NUM_CONTROL_VAR]
Definition: smpc_solver.h:178
void set_ip_parameters(const double, const double, const double, const double, const unsigned int, const double)
Set parameters of interior-point method.
Definition: qp_ip.cpp:402
void set_parameters(const double *, const double *, const double, const double *, const double *, const double *, const double *, const double *)
Initializes quadratic problem.
Definition: qp_ip.cpp:97
A container for a state in the original form:
Definition: smpc_solver.h:140
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 * X
Definition: qp_as.h:76
unsigned int active_set_size
Definition: qp_as.h:82
Interface of the library.
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
void solve(vector< double > &)
Solve QP using interior-point method.
Definition: qp_ip.cpp:427
void form_init_fp(const double *, const double *, const double *, const bool, double *)
Generates an initial feasible point.
Definition: qp_as.cpp:141
void set(double x_, double y_)
Set coordinates, velocities and accelerations are assumed to be zero.
backtrackingSearchType
Type of the backtracking search.
Definition: smpc_solver.h:420
state_parameters * spar
void solve()
These functions are documented in the definition of the base abstract class smpc::solver.
unsigned int removed_constraints_num
Definition: qp_as.h:81
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
double * X
Definition: qp_ip.h:84
state_parameters * spar
void get_next_state(state_com &) const
These functions are documented in the definition of the base abstract class smpc::solver.
unsigned int bs_counter
Definition: qp_ip.h:89
void solve()
These functions are documented in the definition of the base abstract class smpc::solver.
unsigned int added_constraints_num
Definition: qp_as.h:80
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
void get_controls(const int preview_window_size, const double *X, const int ind, double *controls)
Returns the controls,that must be applied to reach the next state.
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
void solve(vector< double > &)
Solve QP problem.
Definition: qp_as.cpp:258
void tilde_to_orig(const double h, double *state)
Converts state from X_tilde to original variables.
A container for a control vector.
Definition: smpc_solver.h:169
void form_init_fp(const double *, const double *, const double *, const bool, double *)
Generates an initial feasible point. First we perform a change of variable to X_tilde generate a feas...
Definition: qp_ip.cpp:603
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
void set_parameters(const double *, const double *, const double, const double *, const double *, const double *, const double *, const double *)
Initializes quadratic problem.
Definition: qp_as.cpp:84
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.