A sparse MPC solver for walking motion generation (old version).
solver/smpc_solver.cpp
Go to the documentation of this file.
00001 
00011 /****************************************
00012  * INCLUDES 
00013  ****************************************/
00014 
00015 #include "solver_config.h"
00016 
00017 #ifdef HAVE_FEENABLEEXCEPT
00018 #include <fenv.h> // feenableexcept()
00019 #endif
00020 
00021 #include "qp_solver.h"
00022 #include "qp_as.h"
00023 #include "qp_ip.h"
00024 #include "smpc_solver.h"
00025 #include "state_handling.h"
00026 
00027 
00028 /****************************************
00029  * FUNCTIONS 
00030  ****************************************/
00031 
00032 
00033 
00034 smpc::solver::solver (
00035                 const int N,
00036                 const double Alpha, const double Beta, const double Gamma,
00037                 const double regularization, const double tol)
00038 {
00039     qp_sol = new qp_as (N, Alpha, Beta, Gamma, regularization, tol);
00040 }
00041 
00042 
00043 smpc::solver::solver (
00044                 const int N,
00045                 const int max_iter,
00046                 const double Alpha, const double Beta, const double Gamma,
00047                 const double regularization, 
00048                 const double tol, const double tol_out,
00049                 const double t,
00050                 const double mu,
00051                 const double bs_alpha, const double bs_beta)
00052 {
00053     qp_ip * qpip_solver = new qp_ip (N, Alpha, Beta, Gamma, regularization, tol);
00054     qpip_solver->set_ip_parameters (t, mu, bs_alpha, bs_beta, max_iter, tol_out);
00055     qp_sol = qpip_solver;
00056 }
00057 
00058 
00059 smpc::solver::~solver()
00060 {
00061     if (qp_sol != NULL)
00062     {
00063         delete qp_sol;
00064     }
00065 }
00066 
00067 
00068 void smpc::solver::enable_fexceptions()
00069 {
00070 #ifdef HAVE_FEENABLEEXCEPT
00071     feenableexcept(
00072             FE_ALL_EXCEPT &
00073             // ignore precision loss due to rounding
00074             !FE_INEXACT);
00075 #endif
00076 }
00077 
00078 
00079 void smpc::solver::set_parameters(
00080         const double* T, const double* h, const double h_initial,
00081         const double* angle,
00082         const double* zref_x, const double* zref_y,
00083         const double* lb, const double* ub)
00084 {
00085     if (qp_sol != NULL)
00086     {
00087         qp_sol->set_parameters(T, h, h_initial, angle, zref_x, zref_y, lb, ub);
00088     }
00089 }
00090 
00091 
00092 
00093 void smpc::solver::form_init_fp (
00094         const double *x_coord,
00095         const double *y_coord,
00096         const state_orig &init_state,
00097         double* X)
00098 {
00099     if (qp_sol != NULL)
00100     {
00101         qp_sol->form_init_fp (x_coord, y_coord, init_state.state_vector, X);
00102     }
00103 }
00104 
00105 
00106 
00107 int smpc::solver::solve()
00108 {
00109     if (qp_sol != NULL)
00110     {
00111         return (qp_sol->solve ());
00112     }
00113     return (-1);
00114 }
00115 
00116 
00117 //************************************************************
00118 
00119 
00120 smpc::state::state()
00121 {
00122     state_vector[0] = 0.0;
00123     state_vector[1] = 0.0;
00124     state_vector[2] = 0.0;
00125     state_vector[3] = 0.0;
00126     state_vector[4] = 0.0;
00127     state_vector[5] = 0.0;
00128 }
00129 
00130 
00131 void smpc::state::set (double x_, double y_)
00132 {
00133     set (x_, 0.0, 0.0, y_, 0.0, 0.0);
00134 }
00135 
00136 
00137 void smpc::state::set (
00138         double x_, double vx_, double ax_,
00139         double y_, double vy_, double ay_)
00140 {
00141     state_vector[0] = x_;
00142     state_vector[1] = vx_;
00143     state_vector[2] = ax_;
00144     state_vector[3] = y_;
00145     state_vector[4] = vy_;
00146     state_vector[5] = ay_;
00147 }
00148 
00149 
00150 //************************************************************
00151 
00152 
00153 void smpc::state_tilde::get_next_state (const smpc::solver &smpc_solver)
00154 {
00155     get_state (smpc_solver, 0);
00156 }
00157 
00158 
00159 void smpc::state_tilde::get_state (const smpc::solver &smpc_solver, const int ind)
00160 {
00161     if (smpc_solver.qp_sol != NULL)
00162     {
00163         state_handling::get_state_tilde (
00164                 *smpc_solver.qp_sol, 
00165                 smpc_solver.qp_sol->X, 
00166                 ind, 
00167                 state_vector);
00168     }
00169 }
00170 
00171 
00172 //************************************************************
00173 
00174 
00175 void smpc::state_orig::get_next_state (const smpc::solver &smpc_solver)
00176 {
00177     get_state (smpc_solver, 0);
00178 }
00179 
00180 
00181 void smpc::state_orig::get_state (const smpc::solver &smpc_solver, const int ind)
00182 {
00183     if (smpc_solver.qp_sol != NULL)
00184     {
00185         state_handling::get_state (
00186                 *smpc_solver.qp_sol, 
00187                 smpc_solver.qp_sol->X, 
00188                 ind, 
00189                 state_vector);
00190     }
00191 }
00192 
00193 
00194 //************************************************************
00195 
00196 smpc::control::control()
00197 {
00198     control_vector[0] = 0.0;
00199     control_vector[1] = 0.0;
00200 }
00201 
00202 
00203 void smpc::control::get_first_controls (const smpc::solver &smpc_solver)
00204 {
00205     get_controls (smpc_solver, 0);
00206 }
00207 
00208 
00209 void smpc::control::get_controls (const smpc::solver &smpc_solver, const int ind)
00210 {
00211     if (smpc_solver.qp_sol != NULL)
00212     {
00213         state_handling::get_controls (
00214                 smpc_solver.qp_sol->N, 
00215                 smpc_solver.qp_sol->X, 
00216                 ind, 
00217                 control_vector);
00218     }
00219 }