osqp / osqp-matlab

Matlab interface for OSQP
https://osqp.org/
Apache License 2.0
42 stars 25 forks source link

Use OSQP as a custom solver for Matlab MPC #35

Open Hbbaz opened 3 years ago

Hbbaz commented 3 years ago

I am raising this issue to request for an interface to enable using OSQP as a custom solver with the MathWorks MPC tools for simulation and code generation. This interface will leverage the design capabilities of MPC Toolbox software and the computational performance of OSQP solver.

The following links explain the process and include some examples:

https://www.mathworks.com/help/mpc/ug/qp-solver.html?searchHighlight=QP%20solver&s_tid=srchtitle and https://www.mathworks.com/help/mpc/ug/generate-code-for-mpc-controller-with-custom-qp-solver.html https://www.mathworks.com/help/mpc/ug/simulate-mpc-controller-with-a-custom-qp-solver.html

All needed would be an EML (that can be code generated) or MEX file using the OSQP code that can be called within a function [x,status] = mpcCustomSolverCodeGen(H,f,A,b,x0) signature.

gelinhe commented 3 years ago

I have the same problem, when Simulate MPC Controller with a Custom QP Solver,here is my configuration

function [x, status] = mpcCustomSolver(H, f, A, b, x0) %#codegen persistent prob; u_dim = length(b); u = 1./zeros(u_dim,1); if isempty(prob) prob = osqp; prob.setup(H, f, A, b, u);
end prob.update('Px', nonzeros(triu(H)), 'Ax', nonzeros(A)); prob.update('q', f, 'l', b, 'u', u); res = prob.solve(); x = res.x; solve_status = res.info.status_val; switch solve_status case 1 status = 1; case 2 status = 1; otherwise status = 1; end % Always return a non-empty x of the correct size. When the solver fails, % one convenient solution is to set x to the initial guess. if status <= 0 x = x0; end

it just ok for simulation

but when code generation, here is my configuration

function [x, status] = mpcCustomSolverCodeGen(H, f, A, b, x0) %#codegen u_dim = length(b); u = 1./zeros(u_dim,1); prob = osqp; prob.setup(H, f, A, b, u);

prob.update('Px', nonzeros(triu(H)), 'Ax', nonzeros(A)); prob.update('q', f, 'l', b, 'u', u); res = prob.solve(); x = res.x; solve_status = res.info.status_val; switch solve_status case 1 status = 1; case 2 status = 1; otherwise status = 1; end % Always return a non-empty x of the correct size. When the solver fails, % one convenient solution is to set x to the initial guess. if status <= 0 x = x0; end

get errors as follows, Only MATLAB files are supported for code generation. Unsupported file extension 'mexw64' for 'C:\Users\Administrator\Desktop\VMC\osqp_mpc\osqp\osqp-0.6.2-matlab-windows64\osqp_mex.mexw64'.

Function 'osqp.m' (#621.2055.2083), line 59, column 33: "osqp_mex('new', varargin{:})" Component:MATLAB Function | Category:Coder error Function call failed.

Function 'mpcCustomSolverCodeGen.m' (#620.121.125), line 5, column 8: "osqp"

gelinhe commented 3 years ago

@gbanjac

Thank you very much for your excellent open source project.What I want to know is whether there is any solution to this problem, or whether the future upgrade will support this function.

Hbbaz commented 3 years ago

@gbanjac Is there any plan to implement this enhancement feature soon?