Is there something like a "pre-build" callback function? - matlab

I have a Simulink model, the purpose of which is automated code generation.
My model uses S-functions (developed by another party), which has hard-coded assumptions about the path. For instance, several external data files are needed, which are referenced in the S-function via a relative path like ..\Bin\data\datafile.bin. This makes it necessary to set MATLAB's current working directory to a specific path before the model can be run.
I can automatically check and set the correct path via model callback functions. However, all model callback functions only seem to be related to the simulation process, not the build process. That means that I can run the model irrespective of what directory I'm in, but when I try to build the model, it always fails unless I manually navigate MATLAB back to the correct directory.
Needless to say, that's quite annoying. So I was wondering if there is something like a "preBuildFcn" callback fnuction, a function that is run before starting the build process? Any other solution (that does not involve modifying the S-function) is also very welcome.

There are plenty of hooks into the build process of Simulink / Embedded Coder ('entry', 'before_tlc', 'after_tlc', 'before_make', 'after_make', 'exit', and 'error'). I assume you want an 'entry' hook.
All you need to do is write an M-function with the name your_system_target_file name_make_rtw_hook, as explained in the documentation Customize Build Process with STF_make_rtw_hook File.
In case you can't open the online documentation (login required), here is the path to the HTML in your MATLAB installation: MATLAB root\help\rtw\ug\customizing-the-target-build-process-with-the-stf-make-rtw-hook-file.html

I am not sure whether building simulink models is sufficiently similar to building regular MATLAB programs, but here is what I used in the past:
Set up the project manually
Build the project programmatically
The program that is used to build the project should be able to set the path or do other custom things.

Related

Embedding externally generated C-code within OpenModelica models

We are working with a tool that generates C-code. For example, the C-code could be for the logic: "if a sensor is activated, turn on the LED bulb". The code can be saved into a function that can be called from outside. What is the best way to embed this kind of external C-code into the rest of the Modelica model? How can we compile this code along with the rest of the .mo files preferably using OM Shell script and omc?
You can directly put C code in external functions or link with a library.
See:
https://specification.modelica.org/v3.4/Ch12.html#external-function-interface
https://specification.modelica.org/v3.4/Ch12.html#external-objects
As examples see some of our tests from the testsuite:
https://github.com/OpenModelica/OpenModelica/blob/master/testsuite/simulation/modelica/
Simple example:
https://github.com/OpenModelica/OpenModelica/blob/master/testsuite/simulation/modelica/external_functions/ExternalLibraries.mo
https://github.com/OpenModelica/OpenModelica/blob/master/testsuite/simulation/modelica/external_functions/ExternalLibraries.mos
https://github.com/OpenModelica/OpenModelica/blob/master/testsuite/simulation/modelica/external_functions/ExternalFunc1.c
https://github.com/OpenModelica/OpenModelica/blob/master/testsuite/simulation/modelica/external_functions/ExternalFunc2.c
Some advanced libraries:
https://github.com/modelica-3rdparty/Modelica_DeviceDrivers
https://github.com/modelica-3rdparty/ExternData

Library Startup Skript in Dymola

Using Dymola, I'm looking for a way to automatically execute a script when loading a library. The intention is to define additional displayUnits using the defineUnitConversion() command, which are specific to the library that is loaded. Still I think there are quite some other cases where this could be helpful.
What I figured out in this regard:
I know that it is possible to add conversions to the file in DymolaInstallDir/insert/displayUnits.mos but this comes with the disadvantage that is has to be done again on every new computer or after an update of Dymola. I would like to avoid this.
Other than that I only found the libraryinfo.mos file, which seems to be read during the start-up of Dymola. Therefore I assume it is not the right place to put the conversions, as it contains general information about the library and should only contain the respective functions.
Dymola 2022 has a new (tool-specific) feature that covers exactly this use-case. It is mentioned in the Dymola 2022 release notes in the section "Library startup script" on page 24.
It basically introduces the a new annotation, which allows to specify a path to a .mos script, which is executed, when the respective library is loaded. Here is the example from the release notes:
package ThisPack
annotation(__Dymola_startup =
"modelica://ThisPack/Resources/Scripts/Dymola/startup.mos");
end ThisPack;
The annotation can also be set via the UI...

Embedded Coder not recognizing tokens in default code generation template

I recently obtained a license to use Embedded Coder with an existing Simulink model that we have developed. In attempting to generate C code for the first time from the model, I am working through several errors. At first, we had no code generation templates (.cgt) files defined in the model parameters. After some hunting, I found the default template that comes with MATLAB (matlabroot/toolbox/rtw/targets/ecoder/ert_code_template.cgt).
The latest is that I get errors on nearly every token in this default code generation template.
Since I'm just trying to get something to build, at first I commented out the offending lines (things like RTWFileVersion, etc), but now I am noticing that it's giving me errors for things that are mandatory (ie. Types). Types is one of several required items that must be in the .cgt file, so what's wrong that causes MATLAB to not recognize these tokens? I'm guessing something may be messed up with my installation, such as a path.
Other details:
Simulink R2013A x32
Target is a Freescale device
Thanks to Matthias W for getting me to check other configuration options. Turns out I had selected a .tlc file that was probably incompatible with Embedded Coder.
In Code Generation for "System target file" I have selected the ert.tlc file and now I am able to build the parts of my model I'm interested in.

Deployment of Simulink Models

I have been trying to find out how to deploy a Simulink model. There are possibilities and problems as well.
If I use Simulink Coder how can I find the generated code on my computer? Where is it saved as a file or package of files?
Can we deploy Simulink as .NET Assembly? If we can, where can I find a detailed documentation about it.
Is there any other way to use my Simulink model standalone?
Thank you for any effort.
By default all the code gets placed into a folder, in the current directory, called ModelName_CodeTarget, where ModelName is the name of your model and CodeTarget is the particular type of code you are generating. (This folder is created during the build process if it doesn't already exist.)
There is no mechanism for automatically generating a .Net assembly. Simulink Coder generates C code; what you do with it is then up to you (just as it would be with hand written code). Note that the process is fully customizable, so you could create your own build process to autmatically wrap the C code into an assembly (but it would be a very advanced maneuvre to do this).
There are a couple of Simulink Coder "targets" that generate standalone executable files. For instance, the GRT (Generic Real-Time) target generates a simple .exe file; the RSIM target (the Rapid Simulation Target) generates an exe where you can specify different .mat files that can be used to specify different model parameters for different simulation runs, and have the results of the different runs stored to different output .mat files.
Most of these questions are answered by looking at the early chapters of the Simulink Coder documentation.
It would be worth you taking some time to look at it to get a feel for how the code generation process works.

Compiling Simulink Code into .ELF object form

I have a simple model from simulink and I would like to generate code using the code generator in the simulink and then compile it using gcc into a .ELF object file. How can I proceed?
Thanks
You need the product called Simulink Coder (around matlab 2011b) or Real-time Workshop (for older matlab versions). Typing ver at the matlab command window will show what products and licences you have installed.
If Simulink Coder or RTW are installed, you use the menu Simulation->Configuration Parameters to set up the model for code generation.
If you have Embedded Coder you can set System Target File to ert.tlc, and this will produce a very concise main() routine to call your model code. Otherwise, use grt.tlc which produces a lot more bloat then ert, but is the only useful one available for on Windows.
There are a lot of options to go through and check - it really needs someone with a bit of experience to be present!
As you are requesting an ELF file, is this for an embedded system? If so, there is a lot more work to be done. If the target is not one of the already supported targets, then you need a target package, which will take either a lot of time and experience, or money to buy one.
Custom target development - a world of it's own:
http://www.mathworks.co.uk/help/toolbox/rtw/ug/bse3b2z.html