Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OSQP as a custom solver for Matlab MPC #35

Open
Hbbaz opened this issue Mar 25, 2021 · 3 comments
Open

Use OSQP as a custom solver for Matlab MPC #35

Hbbaz opened this issue Mar 25, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Hbbaz
Copy link

Hbbaz commented Mar 25, 2021

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.

@gbanjac gbanjac added the enhancement New feature or request label Mar 25, 2021
@gelinhe
Copy link

gelinhe commented Apr 19, 2021

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
Copy link

gelinhe commented May 6, 2021

@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
Copy link
Author

Hbbaz commented Jul 2, 2021

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

@imciner2 imciner2 self-assigned this Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants