Integrate existing C++ code into SimuLink model

I am working with 3D simulation software called OrcaFlex to simulate platform motions at sea. Within this program I am using an external function through a DLL file, programmed in C++. This C++ code extracts data, performs a calculation and then inputs the results back into OrcaFlex.
I now want to extent the calculation part with for example a PID controller. I'd like to do this through SimuLink an eventually compile the resulting SimuLink model into a new DLL that can work with OrcaFlex.
Simply said I need a block that extracts data from the software using the existing C++ code, perform calculations using simple m files and then input the result back into the software again with existing C++ code.
My question is: how can I use functions or variables from the existing C++ code in my SimuLink model? I've seen options such as the S-Function builder and the Legacy Code Tool, but I'm not sure how these will work for me.
Thanks in advance!

 採用された回答

Kaustubha Govind
Kaustubha Govind 2012 年 7 月 10 日

0 投票

I'm not entirely sure whether you want to perform your calculations with a Simulink model or using MATLAB commands (you mentioned both in your question). If it is only a Simulink model, it might be a good idea to generate a generic DLL from the model if you have the Embedded Coder product license. You need to use the ERT Shared Library Target (ert_shrlib.tlc) with your model to generate a DLL.
If you do indeed want to run MATLAB code, and OrcaFlex always runs on a machine with MATLAB installed, you can start the MATLAb Engine from your C/C++ driver application, send/receive data and run MATLAB commands.

6 件のコメント

Mattijs Oud
Mattijs Oud 2012 年 7 月 10 日
Thanks for the answer! I do indeed want to perform my calculations using a SimuLink model, sorry for the confusion.
I've read quite a bit more since I asked this question and I came to a better setup than what I first intended to use:
I will now compile my SimuLink model into a DLL and use functions and variables from this DLL in C++ code that communicates with OrcaFlex. I can and have already compile this C++ code into another DLL that will be used by OrcaFlex. In other words:
Orcaflex DLL retrieves values from OrcaFlex -> SimuLink DLL calculates -> OrcaFlex DLL inputs updates values back into OrcaFlex.
I will have a look at the Embedded Coder to generate a DLL from SimuLink, thanks for the help!
K E
K E 2012 年 7 月 11 日
編集済み: K E 2012 年 7 月 12 日
Mattjis, just a head's up that OrcaFlex also has a Matlab interface if that makes interfacing things easier. You can launch and retrieve results from OrcaFlex simulations from within a Matlab script, and also make external functions within Matlab, for example for an exotic non-linear winch. The Orcina support people are helpful with the Matlab implementation if you email or phone them (unfortunately no nice user forum like this one though). Just mentioning this in case it is useful.
Kaustubha Govind
Kaustubha Govind 2012 年 7 月 11 日
K E
K E 2012 年 7 月 12 日
Yikes, I have corrected this.
Mattijs Oud
Mattijs Oud 2012 年 7 月 16 日
Hi! Thanks for the answer KE! I am aware of the Matlab interface, however this interface only allows me to extract data before and after a simulation. Since I have to use an external function during the simulation I will have to work with either Python or C++ code.
I've now managed to convert my SimuLink model to C++ code using the Real Time Workshop. This should work for me since my external function to OrcaFlex is also written in C++. I used this link: http://blogs.abo.fi/alexeevpetr/2011/09/30/building-simulink-generated-c-code-in-visual-studio/, to implement the model in Visual Studio. I'm however not sure how to actually initialise and update the SimuLink model through the generated C++ code. Do you have any experience with this?
Kaustubha Govind
Kaustubha Govind 2012 年 7 月 16 日
Here is an example of how you can call into the library. You basically call initialize at the beginning, terminate at the end, and the step function several times in between for each data sample.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink Coder についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by