simple_casadi_mpc
GitHub
Loading...
Searching...
No Matches
simple_casadi_mpc::CompiledMPC Class Reference

MPC variant that loads an externally-compiled solver shared library. More...

#include <simple_casadi_mpc.hpp>

Inheritance diagram for simple_casadi_mpc::CompiledMPC:
simple_casadi_mpc::MPC

Classes

struct  CompiledLibraryConfig
 Locator for an AOT-compiled solver shared library. More...
 

Public Member Functions

template<class T >
 CompiledMPC (const CompiledLibraryConfig &lib_config, std::shared_ptr< T > prob)
 Load the prebuilt solver from lib_config and bind it to prob.
 
Eigen::VectorXd solve (Eigen::VectorXd x0, casadi::DMDict new_param_list=casadi::DMDict()) override
 Solve the NLP at the current state and return the first optimal control.
 
- Public Member Functions inherited from simple_casadi_mpc::MPC
template<class T >
 MPC (std::shared_ptr< T > prob, std::string solver_name="ipopt", casadi::Dict config=default_ipopt_config())
 Build the NLP from prob and create the underlying nlpsol.
 
casadi::MXDict casadi_prob () const
 Symbolic NLP description {x, f, g, p} constructed from the Problem.
 
const std::string & solver_name () const
 Backend name passed to CasADi nlpsol.
 
casadi::Dict solver_config () const
 Effective config forwarded to nlpsol (after consuming simple-casadi-mpc keys).
 
std::vector< casadi_int > equality_flags () const
 Per-constraint flags marking equality (true) vs inequality (false).
 

Static Public Member Functions

template<class T >
static void generate_code (const std::string &export_solver_name, const std::string &export_dir, const std::string &solver_name="ipopt", const casadi::Dict &solver_config=MPC::default_ipopt_config(), const casadi::Dict &codegen_options={})
 Emit the C source for an AOT-compiled solver. Called from a codegen executable.
 
- Static Public Member Functions inherited from simple_casadi_mpc::MPC
static casadi::Dict default_ipopt_config ()
 Reasonable defaults for the IPOPT backend (silent, warm-start enabled).
 
static casadi::Dict default_qpoases_config ()
 Reasonable defaults for SQP method with the qpOASES inner QP solver.
 
static casadi::Dict default_fatrop_config ()
 Reasonable defaults for the FATROP backend (auto structure detection).
 
static bool equality_required (const std::string &solver_name, const casadi::Dict &config)
 Whether the chosen backend needs an equality flag vector in the config.
 

Additional Inherited Members

- Protected Member Functions inherited from simple_casadi_mpc::MPC
void build_with_map (size_t nx, size_t nu, casadi_int N, bool mapsum_stage_cost=true, bool expand_inner_functions=true)
 
- Protected Attributes inherited from simple_casadi_mpc::MPC
std::shared_ptr< Problemprob_
 
std::string solver_name_
 
casadi::Dict config_
 
casadi::MXDict casadi_prob_
 
casadi::Function solver_
 
std::vector< casadi::MX > Xs = {}
 
std::vector< casadi::MX > Us = {}
 
casadi::DM lbw_
 
casadi::DM ubw_
 
casadi::DM lbg_
 
casadi::DM ubg_
 
std::vector< casadi::DM > param_vec_ = {}
 
std::vector< bool > equality_ = {}
 
casadi::DM w0_
 
casadi::DM lam_x0_
 
casadi::DM lam_g0_
 

Detailed Description

MPC variant that loads an externally-compiled solver shared library.

Use the CMake helper add_simple_casadi_mpc_codegen to generate and build the shared library at project build time, then pass the resulting CompiledLibraryConfig (provided by a generated _config.cpp) here. Trades flexibility (solver backend and its options are baked in) for the best runtime startup and steady-state solve time.

Constructor & Destructor Documentation

◆ CompiledMPC()

template<class T >
simple_casadi_mpc::CompiledMPC::CompiledMPC ( const CompiledLibraryConfig & lib_config,
std::shared_ptr< T > prob )
inline

Load the prebuilt solver from lib_config and bind it to prob.

Parameters
lib_configlocator for the compiled solver, see CompiledLibraryConfig.
probthe matching Problem instance (used for shapes and parameters).

Member Function Documentation

◆ generate_code()

template<class T >
static void simple_casadi_mpc::CompiledMPC::generate_code ( const std::string & export_solver_name,
const std::string & export_dir,
const std::string & solver_name = "ipopt",
const casadi::Dict & solver_config = MPC::default_ipopt_config(),
const casadi::Dict & codegen_options = {} )
inlinestatic

Emit the C source for an AOT-compiled solver. Called from a codegen executable.

The generated <export_solver_name>.c (and matching header) is compiled into a shared library by the add_simple_casadi_mpc_codegen CMake helper.

Template Parameters
Tthe Problem subclass to instantiate.
Parameters
export_solver_nameCasADi function name embedded in the generated source.
export_diroutput directory for the generated files.
solver_nameCasADi nlpsol backend baked into the compiled solver.
solver_confignlpsol options baked in (cannot be changed at runtime).
codegen_optionsforwarded to casadi::CodeGenerator.

◆ solve()

Eigen::VectorXd simple_casadi_mpc::CompiledMPC::solve ( Eigen::VectorXd x0,
casadi::DMDict new_param_list = casadi::DMDict() )
inlineoverridevirtual

Solve the NLP at the current state and return the first optimal control.

Warm-starts from the previous solve (x, lam_x, lam_g are cached internally), so calling this repeatedly during closed-loop simulation benefits from incremental convergence.

Parameters
x0current measured state, shape (nx,).
new_param_listupdates to parameters declared via Problem::parameter. Keys are parameter names; values are casadi::DM of matching shape.
Returns
optimal control to apply now, u_0, shape (nu,).

Reimplemented from simple_casadi_mpc::MPC.


The documentation for this class was generated from the following file: