MATLAB: How to embed the Real-Time Workshop (RTW) generated code created by ERT into the own code

embeddedrealsimulink codersystemtime

How do I embed the RTW generated code created by ERT into my own code?
When I do a 'build' using ert.tlc using RTW, I get an .exe file. How is the .exe file embbeded into other code?
What I had in mind was having RTW generate a subroutine that can be called and linked by a driving program. Is this possible with ERT? If not, how can I modify the file grt_main.c to accomplish this?

Best Answer

Starting Release 2010a (R2010a), information regarding the various options to integrate Real-Time Workshop generated code with existing code can be obtained from the following documentation link titled 'Exporting Generated Algorithm Code for Embedded Applications':
For previous releases, read below for any additional information:
The exe file option was added for you to see that, once the Simulink portion was compiled, linked, and made into an executable file, the file works to your specifications. If you are going to embed this Simulink model into your own code, instead use the .c file that was created.
The ert_main.c file is the best file to examine. This file can act as your main file, calling the Simulink ERT code you have generated. You will see that ert_main.c file calls the generated ERT code through three main function calls:
- MODEL_INITIALIZE
- MODEL_STEP
- MODEL_TERMINATE
The ert_main.c file is well documented; follow this as an example.
Also review the ert template makefile for instructions on how to compile everything together, along with any other lib files that are required, to compile all of the ERT code with your code.
Note that the ert_main.c file and the template makefiles are located in the
$MATLAB\rtw\c\ert directory
(where $MATLAB = MATLAB root directory on your machine)