Use casadi in simulink

1,465 views
Skip to first unread message

Bruno M

unread,
Feb 28, 2017, 12:24:52 PM2/28/17
to CasADi

Hi all,


I have an optimization problem that should run online in a simulink simulation. My idea was to import the matlab function which solves the optimization ( and calls casadi as "import casadi .") in a simulink block, but it turned out that "import" is not accepted for the compilation of the model.

The question is: is there a way interface casadi with simulink? There exist a tutorial and some examples for that?

Thanks very much in advance.


BTW CasADi is awesome!

Bruno

Bruno M

unread,
Mar 1, 2017, 4:30:56 AM3/1/17
to CasADi
I tried to mex the function with the code generator. While checking copatibility of the code Matlab finds some functions inside CasADI wich cannot are not supported for code generation such as isnull  or  builtin inside GenMX.m or Function.m ( see attachment).
Do you know if there is there a workaround?

Thanks!

Best

Bruno
problem_functions_code_generation.png

Joris Gillis

unread,
Mar 1, 2017, 4:36:10 AM3/1/17
to CasADi
Dear Bruno,

There are at least three approaches to use CasADi in simulink.

Approach 1: write casadi-matlab code, and use an 'interpreted matlab function' block in simulink
Approach 2: write casadi-matlab code and use casadi's codegenerator to spit out a pure c function (or mex file). Then, interface that pure c function in simulink
Approach 3: write a mex file from scratch using casadi-c++ code

Approach 1 is the easiest and has been validated by a thesis student of mine.

Best regards,
   Joris

Bruno M

unread,
Mar 1, 2017, 4:41:15 AM3/1/17
to CasADi
Thanks Joris,

I'll try with the first approach then :-)

Best
Bruno

Bruno M

unread,
Mar 1, 2017, 8:37:12 AM3/1/17
to CasADi
Hi Joris,

The interpreted matlab function block seems to accept only one input (of type double). My code is rather complex and I need to give many inputs. Did your student use a single input? I might maybe put everything togheter in one big vector, but I'd like to avoid that. I do not understand why Matworks did not consider multiple inputs.
 
So I tried with the approach 2: I managed to generate c code of functions but not from ODE/DAE integrators (I tried with cvodes and ideas) following the code generation examples in the guide. I got this message:

CasADi warning: "The function "intf", which is of type "N6casadi15CvodesInterfaceE" cannot be code generated. The generation will proceed, but compilation of the code will not be possible." issued  on line 2082 of file "/home/travis/build/casadi/binaries/casadi/casadi/core/function/function_internal.cpp". 


Is the code generation for integrators different or it has not been implemented yet?

Thanks for the support!

Bruno


Am Mittwoch, 1. März 2017 10:36:10 UTC+1 schrieb Joris Gillis:

Joris Gillis

unread,
Mar 8, 2017, 10:45:27 AM3/8/17
to CasADi
Dear Bruno,

I just did a first-ever bloh post on CasADi in simulink:
http://labs.casadi.org/

It does use two inputs.

Best regards,
  Joris

Bruno M

unread,
Mar 10, 2017, 5:09:31 AM3/10/17
to CasADi
Hi Joris,

thank you very much! I will test also this alternative. At the end I found a workaround by defining the function that uses casadi as "extrinsic"' . It seems to work also in this case. 

Gaurav Malik

unread,
Dec 22, 2018, 1:07:09 AM12/22/18
to CasADi
Hi all,
Apparently I have a similar problem. I am using a Matlab function in Simulink and the matlab function is basically solving a convex optimisation problem. Now, when I call the function in Matlab itself it works nicely outputing variables of type 'double' but when I run the simulation, I get an error saying 
'Code generation does not support the 'delete' method. Handle objects are only destroyed when program execution ends, and destructors are never called.'
and a number of other errors. 
I would be very grateful if any of you guys can suggest me a solution. 
Thanks in advance.
Regards
Gaurav Malik 

Gaurav Malik

unread,
Dec 22, 2018, 1:08:54 AM12/22/18
to CasADi
diagnostics.png
Reply all
Reply to author
Forward
0 new messages