Can I use interface for simulink that enables to call OSQP as a S function?

Hello,

Is it possible for OSQP solver to define a Simulink block diagram such as S-function or Matlab function in Simulink?

DD

Yes, there is a development version here GitHub - imciner2/osqp-matlab at im/simulink_cg (it is a work in progress still and hasn’t been included in any official releases). You can use it by downloading the files, then running make_osqp('simulink').

Oh, lovely. It will be very helpful.
Thanks.

DD

Hi,

I have installed your development version.
However, I can’t run the command make_osqp(‘simulink’) and got the hint ‘No rule to make target “simulink”, exiting.’. So I just run make_osqp.

In order to avoid conflict happening between standard osqp version and your verison, I uninstalled the previous version, and only installed your version.

And then I wrote a matlab function in simulink as below:

function uN1 = MPC(mpc_f, mpc_H, mpc_Ain, mpc_b1, mpc_b2)

P = sparse(mpc_H);
q = mpc_f;
A = sparse(mpc_Ain);
l = mpc_b1;
u = mpc_b2;

% Create the solver
solver = osqp;
solver.setup(P, q, A, l, u, ‘verbose’, true)

% % Solve the problem using the generated code
% [x, y, status_val, iter, run_time] = emosqp(‘solve’);
res = solver.solve();
uN1 = res.x(1,1);

Next, I received many errors when I run my simulation.
Something like:
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component:MATLAB Function | Category:Coder error
Function call resolved to MEX file ‘C:\WECCC\osqp-matlab\osqp_mex.mexw64’. Only MATLAB files are supported for code generation. Consider using coder.extrinsic to declare MEX function as extrinsic.

Function ‘osqp.m’ (#1116.2055.2083), line 59, column 33:
“osqp_mex(‘new’, varargin{:})”
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function call failed.

Function ‘Controller/Rotary/WECCCOMP Controller/MPC_controller/controller/MATLAB Function’ (#645.167.171), line 12, column 10:
“osqp”
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable ‘solver’. The first assignment to a local variable determines its class.

Function ‘Controller/Rotary/WECCCOMP Controller/MPC_controller/controller/MATLAB Function’ (#645.173.179), line 13, column 1:
“solver”
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable ‘solver’. The first assignment to a local variable determines its class.

Function ‘Controller/Rotary/WECCCOMP Controller/MPC_controller/controller/MATLAB Function’ (#645.328.334), line 17, column 7:
“solver”
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable ‘res’. The first assignment to a local variable determines its class.

Function ‘Controller/Rotary/WECCCOMP Controller/MPC_controller/controller/MATLAB Function’ (#645.350.353), line 18, column 7:
“res”
Launch diagnostic report.

I guess it means I can’t call osap function in simulink.
Is there anything which I didn’t do in a right way?

Could you help me to understand this problem?

DD

Are you sure you downloaded the branch of the repository I linked and had it on the MATLAB path? If your existing instal of OSQP was on the path, then that could make this fail because it would not have the installation script for the Simulink version.

Once you do this, you will get a new block and a new library in Simulink called OSQP. You can use that solver block to then solve the optimization problem with the matrices and vectors you provide to it.

Hello,

I tried to only download the ZIP file from your link at this time. I could run the command make_osqp(‘simulink’) successfully in my matlab 2020a.
Export successful: ‘C:\WECCC\osqp-matlab-im-simulink_cg\simulink\block\osqp_library.slx’ created for use in Simulink R2015b.

And suddenly I found that the clone repository by using HTTPS from your git link in my first trying doesn’t include a simulink folder. That’s reason why I can’t run that simulink version command.

And then, I tried to run the run_osqp_tests.m file, and then I got many many errors.
Maybe I don’t have to take care of this, but just ignore.

Next, I opened the osqp_library.slx file and saw an OSQP solver block there.
I will try to use it after my lunch.
Thank you for you help

DD

Have a look at MATLAB Implementation of the OSQP Algorithm.

OK, I will look at your link right away.
thank you

BR
DD