Generating C-code using Casadi in Matlab

1,001 views
Skip to first unread message

Mohammad Alsalti

unread,
Oct 7, 2021, 9:47:27 AM10/7/21
to CasADi
Hello!

I am using Casadi for model predictive control and so far it is great to apply it in simulation using Matlab. I now want to implement some results in hardware, so I am trying to read more on how to generate C-code using Casadi in Matlab.

I currently have a model predictive control problem set up as an nlp struct in Matlab, or basically a Casadi function. Is there a way that I can export this as a C-code that can just simply be called in every iteration of the MPC?

If not, what are my alternatives?

Thank you!

Joel Andersson

unread,
Oct 8, 2021, 11:32:04 AM10/8/21
to CasADi
Hi,

There are in principle two ways you can do this:

1. Change the nlpsol plugin to "sqpmethod" and use "osqp" or "qrqp" as the QP solver. This gives a toolchain that can be generated to C code, including a MEX interface, and that can be called directly from MATLAB. This is probably the easiest way, if you can get it to work.

2. From CasADi/MATLAB, generate C code for the functions needed by the solver, including Jacobian and Hessian information. These can then be interfaced directly in C/C++. There should be an example called "embeddable_codegen" in the CasADi repo demonstrating this. This is something that you can get working with e.g. IPOPT as a solver.

Best regards,
Joel

Mohammad Alsalti

unread,
Oct 12, 2021, 10:55:00 AM10/12/21
to CasADi
Thanks Joel!
Reply all
Reply to author
Forward
0 new messages