osqp icon indicating copy to clipboard operation
osqp copied to clipboard

How to use osqp in Simulink?

Open lovedheart opened this issue 7 years ago • 25 comments

I wanna use osqp for mpc-solver in simulink? Is there any Interface for that?

lovedheart avatar Jan 07 '19 20:01 lovedheart

We do not have a simulink-specific interface for OSQP. The fastest solution would be to just use the OSQP Matlab interface and make a call to the solver using one of the Matlab user-defined function blocks within Simulink, e.g. by writing a level 2 Matlab S-function wrapper. You could even use the ‘Interpreted Matlab function’ block, but it will likely be very slow unless you are careful to only initialise the solver one time at the start of the simulation.

The best (meaning fastest performance) thing to do would be to write a C language S function wrapper modelled on the existing mex file that we provide for the regular matlab interface. We can make this an enhancement request if that is of interest, but it would be some time before it is available.

On 7 Jan 2019, at 20:32, lovedheart <[email protected]mailto:[email protected]> wrote:

I wanna use osqp for mpc-solver in simulink? Is there any Interface for that?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/oxfordcontrol/osqp/issues/113, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGhaqSmzac3vbYjAh5QFwTi_cf2KYsyxks5vA67YgaJpZM4Z0RA7.

goulart-paul avatar Jan 07 '19 22:01 goulart-paul

@goulart-paul Thank you for your suggestion. I have managed to use Interpreted MATLAB function to realize a MPC controller in Simulink.

I upload a simple example, if someone has interests. Tested on Windows 10 with MATLAB Version R2018a osqp_mpc_simulink.zip

lovedheart avatar Jan 08 '19 13:01 lovedheart

This has actually been on my todo list to implement for some time now, since I have some systems I want to simulate with MPC. I will probably be able to work on it in the next few weeks though. Ideally I also want to add the ability to use the Simulink Coder with the interface so you could do code generation from the Simulink environment, but that might take more time to complete.

@lovedheart Does your design update just the vectors or does it also update the matrices?

imciner2 avatar Jan 17 '19 15:01 imciner2

Hi Ian,

Thanks for this. I am in the middle of writing this interface already, but would be happy to have some help / use you as a tester.

The initial version will be an m-file S-function wrapper that mostly uses the existing OSQP mex interface internally. I think it is also possible to make a C language S function wrapper that mostly leverages the existing code in the mex file, but that’s more work. I will try it the first way initially and see how how much overhead there is.

I will try to write the simulink OSQP block so that it allows matrix updates through some configurable parameter with an enable port. I will do the same also for warm starting.

Paul

On 17 Jan 2019, at 15:33, Ian McInerney <[email protected]mailto:[email protected]> wrote:

This has actually been on my todo list to implement for some time now, since I have some systems I want to simulate with MPC. I will probably be able to work on it in the next few weeks though. Ideally I also want to add the ability to use the Simulink Coder with the interface so you could do code generation from the Simulink environment, but that might take more time to complete.

@lovedhearthttps://github.com/lovedheart Does your design update just the vectors or does it also update the matrices?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/oxfordcontrol/osqp/issues/113#issuecomment-455214564, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGhaqWRZosgi_3EKItS8c_DQY-f2jGgcks5vEJfRgaJpZM4Z0RA7.

goulart-paul avatar Jan 17 '19 16:01 goulart-paul

Reading this topic I cannot avoid to jump in the conversation and drop here the link of blockfactory, a tool that greatly simplifies wrapping C++ code to Simulink. It also provides OOTB support to Simulink Coder.

Disclaimer: I am the maintainer of the project.

diegoferigo avatar Jan 17 '19 21:01 diegoferigo

I have added a simulink-osqp interface on this branch in the osqp-matlab repo.

I have not tested it extensively but will do so when I can and will add a few examples.

NB: this is implemented using a matlab s-function that acts as a wrapper around the existing osqp.m mex interface. It might run faster if the s-function were implemented instead as a c++ mex function directly (sharing code with osqp_mex.cpp), but that would be substantially more work.

goulart-paul avatar Mar 28 '19 11:03 goulart-paul

A quick follow up of my previous comment, https://github.com/robotology/wb-toolbox/pull/209 implements a OSQP block for Simulink

diegoferigo avatar May 24 '21 07:05 diegoferigo

@goulart-paul Thank you for your suggestion. I have managed to use Interpreted MATLAB function to realize a MPC controller in Simulink.

I upload a simple example, if someone has interests. Tested on Windows 10 with MATLAB Version R2018a osqp_mpc_simulink.zip

The code is working right! Thanks a lot!

yanghq13 avatar Sep 28 '21 07:09 yanghq13

Is there anybody has ever performed code generation of simulink with osqp? It seems that a level-2 M S-function need a .tlc file for the generation which bothers me. It would be a great help if someone had related experience

x10485 avatar Mar 08 '22 11:03 x10485

Not as far as I am aware. Maybe @imciner2 has done something though.

goulart-paul avatar Mar 08 '22 11:03 goulart-paul

Yes, I have managed to successfully get OSQP to generate code through Simulink Coder and run on an Opal-RT platform. The branch I used at the time is https://github.com/imciner2/osqp-matlab/tree/im/simulink_cg (the im/simulink_cg branch on my personal fork). I last tested it on Simulink 2019, and haven't had a chance to use it on the newer Simulink versions to see if it still works.

imciner2 avatar Mar 08 '22 11:03 imciner2

Yes, I have managed to successfully get OSQP to generate code through Simulink Coder and run on an Opal-RT platform. The branch I used at the time is https://github.com/imciner2/osqp-matlab/tree/im/simulink_cg (the im/simulink_cg branch on my personal fork). I last tested it on Simulink 2019, and haven't had a chance to use it on the newer Simulink versions to see if it still works.

Thanks a lot! I'm currently applying the OSQP in MPC structure and carrying a real time experiment on autobox or dSPACE, the MATLAB/Simulink version is 2020b, there may be an update here with words description if I succeed since I'm actually new to GitHub😀

x10485 avatar Mar 08 '22 11:03 x10485

I trying to use the OSQP simulink for code generation in simulink, in combination with some research I am doing for Robust MPC, however, I am struggling to get the code generation to work. Using the branch of @imciner2, after running "make_osqp('simulink'), nothing is working (neither simulink examples nor "run_osqp_tests.m". However, having both this branch and the main branch in path, then simulink examples and my own research works fine. That is, except for the code generation, as it will not compile. (For use on a linux embedded platform of floating testbed robots).

Is there something I am misunderstanding, or do you have any ideas of what goes wrong?

thomaaf avatar Aug 18 '22 20:08 thomaaf

Trying to compile the example, "quadcopter_example_codegen", gives the attached error message. The error message "too many input arguments" has got me stuck, as I am not sure what exactly is having too many input arguments, or in that sense, how to fix it either. Is it an error in the osqp.m file, or is this error in the .tlc or the s-function?

Generating code into build folder: /home/fss_gs/MPC_circum_Robust/2018Tests/quadcopter_example_codegen_ert_linux
OSQP v0.6.2 - Operator Splitting QP Solver
(c) Bartolomeo Stellato, Goran Banjac
University of Oxford - Stanford University 2021
problem: variables n = 172, constraints m = 304
nnz(P) + nnz(A) = 1001
settings: linear system solver = qdldl,
eps_abs = 1.0e-03, eps_rel = 1.0e-03,```

eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
rho = 1.00e-01 (adaptive),
sigma = 1.00e-06, alpha = 1.60, max_iter = 4000
check_termination: on (interval 25),
scaling: on, scaled_termination: off
warm start: on, polish: off, time_limit: off
Calling OSQP code generation routines
Disabling long long data type
Using double data type
Enabling OSQP support for nonfinite MATLAB numbers
Generating OSQP source files into /home/fss_gs/MPC_circum_Robust/2018Tests/quadcopter_example_codegen_ert_linux/osqp_code
Creating target directories... [done]
Copying OSQP source files... [done]
Generating workspace.h/.c... [done]

Build procedure for model: 'quadcopter_example_codegen' aborted due to an error.
Error evaluating registered method 'WriteRTW' of MATLAB S-Function 'osqp_sfun' in 'quadcopter_example_codegen/MPC/OSQP Solver/Level-2 MATLAB S-Function'. The following is the MATLAB call stack (file names and line numbers) that produced this error:
['/home/fss_gs/MPC_circum_Robust/2018Tests/osqp/osqp-0.6.2.post0-matlab-linux64/osqp.m'] [559]
['/home/fss_gs/MPC_circum_Robust/2018Tests/osqp-matlab-im-simulink_cg/simulink/block/osqp_sfun.m'] [388]
['/usr/local/MATLAB/R2015b/toolbox/rtw/rtw/tlc_c.p'] [179]
['/usr/local/MATLAB/R2015b/toolbox/coder/simulinkcoder_core/+coder/+internal/ModelBuilder.m'] [633]
['/usr/local/MATLAB/R2015b/toolbox/coder/simulinkcoder_core/+coder/+internal/@ModelCodegenMgr/make_rtw.m'] [8]
['/usr/local/MATLAB/R2015b/toolbox/rtw/rtw/make_rtw.m'] [18]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/private/build_target.p'] [0]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/private/build_target.p'] [0]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/private/build_standalone_rtw_target.p'] [0]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/private/slbuild_private.p'] [0]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/private/slbuild_private.p'] [0]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/sl.m'] [15]
['/usr/local/MATLAB/R2015b/toolbox/simulink/simulink/slbuild.m'] [61]
['/usr/local/MATLAB/R2015b/toolbox/coder/simulinkcoder_core/+coder/+internal/simulinkcodermenus.p'] [0]
Caused by:
Too many input arguments.

thomaaf avatar Aug 24 '22 23:08 thomaaf

@imciner2 is the right person to answer you, since it has been a very long time since I looked at the simulink wrapper we wrote for OSQP. Two thoughts though:

  • If are using R2015b then there may be some version incompatibility issue with the S functions we supply. I don't think we test versions that far back.

  • We used to have a version that was written as an M-S-function (or whatever the pure matlab S functions are called) around the standard osqp.m interface to the solver. You could try something like that instead if it is still accessible in our matlab interface repo.

goulart-paul avatar Aug 24 '22 23:08 goulart-paul

@goulart-paul thank you I have tried with R2018b as well, with the same error messages.

I have tried many different paths, with just new error messages popping up. To me, it either looks like there may be something wrong with the "make_osqp.m('simulink')", or similarily with "make_emosqp()", as these keep having weird results. E.G make_osqp('simulink') doesnt make the mex file, which I think is necessary? Calling "make_osqp('osqp_mex')", or similar also fails, complaning that it is missing header files. This despite them being there and on path. After moving these missing .h files to the main directory, there are errors in the actual c files, (e.g things not defined, or similar "normal" code errors), of which I for one; do not know how to fix, and two, I assume they are correct, and that there are other things that are actually failing, considering how many manual fixes I have made.

I also have a suspicion that there may have been an error or two in my matlab, but in the end, I receive the same errors (more or less) on a windows computer with R2020b.

For the M-S function, are you talking about the main release branch, and if so, a previous older release?

thomaaf avatar Aug 26 '22 20:08 thomaaf

I think it was this old branch.

That's four years old, but maybe the S-fun and examples in the simulink subdirectory here would get you started. It appears to be 2015b compatible.

goulart-paul avatar Aug 26 '22 21:08 goulart-paul

Sorry, just saw this discussion (I have been busy with the C library changes recently). I definitely can't guarantee it would have worked on 2015b unfortunately (although I do claim the Simulink model would work, but I seriously think we should consider bumping that up a few versions now that we are in 2022), the initial development for it was done for R2018a code generation, and unfortunately the internals of the generation may have changed during that time. I just checked out my im/simulink_cg branch from the matlab repo and it seems to work for me generating the quadcopter_example_codegen model.

Here are some steps to try:

  1. Delete all previous OSQP instances from your path
  2. Do a fresh clone of the MATLAB repo - and clone the submodules (you can't just download the GitHub repo though, that will not include the actual OSQP code in the proper folder).
  3. From the osqp-matlab directory you are working incallmake_osqp` to build everything (that should build the mex file and the Simulink interface).
  4. Then add the proper folders to the path (the root folder for the main mex file/class and the simulink/block folder for the actual Simulink library).
  5. Navigate to the simulink/examples folder and open the quadcopter_example_codegen.mdl file
  6. Generate the code using the generic target -- At this point you should see an error in the terminal about a missing hook file. Follow the instructions to copy the file in the examples directory from the block directory and rename it
  7. Try code generation again -- Now it should have succeeded and you have an executable in the folder named quadcopter_example_codegen
  8. From a terminal (I am assuming running Linux), run that executable -- Now you should have a mat file that contains the results of the simulation

If you get any errors with those steps, we can try to sort them out hopefully. I actually have the Simulink interface on my todo list for a revamp soon along with moving the MATLAB interface to our upcoming v1 API we are building in this repo right now.

imciner2 avatar Aug 26 '22 21:08 imciner2

Thank you very much! I think probably the submodules were one of the errors I have had. However, when trying to fix these things before asking here, I broke cmake with matlab, such that later, when coming across this thread, nothing I have done was working.

I have now finally gotten cmake to work, or atleast be in a better state than it was. But I have now another error that I am unsure of. I doubt it is related to this issue, but since I am working with the @imciner2 branch, I will post it here. Let me know if I should move it

Running cmake gives the error added below. I have tried both R2015b and 2018b with same results. Have seen this https://osqp.discourse.group/t/cmake-error-in-matlab/52/3, but it did not really give anything. Both cmake --Version =3.2.1, and gcc 4.7 is installed.

CMake Error at CMakeLists.txt:201 (message): You need Matlab libraries to build the Matlab interface

thomaaf avatar Aug 30 '22 01:08 thomaaf

hello, thank you very much, following your steps @imciner2 managed to generate and build code in c, however when I try to generate the code for my speedgoat real time machine, an error always appears which I have been trying to solve for the last 4 days, the error is as follows :

C:\Users\8\Desktop\OSqp\osqp\simulink\examples\quadcopter_example_codegen_slrealtime_rtw\instrumented>if "all" == "" (make -f quadcopter_example_codegen.mk all ) else (make -f quadcopter_example_codegen.mk all ) q++ -c -Vgcc_ntox86_64 -g -std=gnu++14 -stdlib=libstdc++ -O2 -fwrapv -DSIMULINK_REAL_TIME -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=quadcopter_example_codegen -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -IC:/Users/8/Desktop/OSqp/osqp/simulink/examples -IC:/Users/8/Desktop/OSqp/osqp/simulink/examples/quadcopter_example_codegen_slrealtime_rtw -IC:/PROGRA~1/MATLAB/R2022a/toolbox/slrealtime/simulink/blocks/dist/include -IC:/PROGRA~1/MATLAB/R2022a/toolbox/slrealtime/target/kernel/dist/include -IC:/PROGRA~1/MATLAB/R2022a/extern/include -IC:/PROGRA~1/MATLAB/R2022a/simulink/include -IC:/PROGRA~1/MATLAB/R2022a/rtw/c/src -IC:/PROGRA~1/MATLAB/R2022a/rtw/c/src/ext_mode/common -IC:/Users/8/Desktop/OSqp/osqp/simulink/examples/quadcopter_example_codegen_slrealtime_rtw/instrumented -o quadcopter_example_codegen.o C:/Users/8/Desktop/OSqp/osqp/simulink/examples/quadcopter_example_codegen_slrealtime_rtw/quadcopter_example_codegen.cpp In file included from C:/Users/8/Desktop/OSqp/osqp/simulink/examples/quadcopter_example_codegen_slrealtime_rtw/quadcopter_example_codegen.cpp:21: C:/Users/8/Desktop/OSqp/osqp/simulink/examples/quadcopter_example_codegen_slrealtime_rtw/quadcopter_example_codegen.h:28:10: fatal error: osqp.h: No such file or directory #include "osqp.h" ^~~~~~~~ compilation terminated. cc: C:/ProgramData/MATLAB/SupportPackages/R2022a/toolbox/slrealtime/target/supportpackage/qnx710//host/win64/x86_64/usr/lib/gcc/x86_64-pc-nto-qnx7.1.0/8.3.0/cc1plus caught signal 1 quadcopter_example_codegen.mk:321: recipe for target 'quadcopter_example_codegen.o' failed make: *** [quadcopter_example_codegen.o] Error 1 C:\Users\8\Desktop\OSqp\osqp\simulink\examples\quadcopter_example_codegen_slrealtime_rtw\instrumented>echo The make command returned an error of 2 The make command returned an error of 2 C:\Users\8\Desktop\OSqp\osqp\simulink\examples\quadcopter_example_codegen_slrealtime_rtw\instrumented>exit /B 1

when I generate the generic C code it recognizes all the .h files requested for the compilation but when I run, for speedgoat it doesn't find them.

I would like to know if someone already managed to run osqp in speedgoat, I would greatly appreciate some advice

sorry for my English

Garciaduvanjames avatar Jan 29 '23 21:01 Garciaduvanjames

Dear all: Sorry for my English Is there any way so that the OSQP simulink can be easily used and can be easily coded and compilied in Aurix Development Studio? I just use the OSQP solver recently, and my purpose is to trans my simulink model into C code and deploy the code onto our MCU, an Infineon 32-bit-tricore-microcontroller-TC397. Actually, we use the method of build a S-Function in simulink to calculate, and simuliate correct. And then the errors occur just for compiling the code generated from the model in simulink in Aurix Development Studio. And the errors show that some .h files lacks, but the .h files are Disorganized for their source. They are from like VC environment like windows.h and MATLAB source and so on. The total amount of these files is very large, and even if they are added, the code will not be recognized during compilation. So we think we can not use the way to compile the code and run the code in the MCU, including the OSQP solver. So we are finding the way to make the OSQP into a simulink inner function, so that our deployment can be proceed smoothly.

EricWang0306 avatar Dec 05 '23 09:12 EricWang0306