Using model callback with Simulink Coder - matlab

I am using Matlab 2012a and the Simulink Coder (aka Real-Time Workshop). I want to compile the model using Simulink Coder but preserve the functionality of model callbacks.
Consider the following simple example. I have a Simulink model, callBackTest, which reads in a constant and outputs to a since. input1 is defined in myValues.m and loaded into the model workspace using the PreLoadFcn model callback. The PreLoadFcn callback is executed when the model is first opened. By using the PreLoadFcn callback, input1 will automatically be defined every time the model is opened.
Suppose myValues.m is originally coded as input1=1. When you run the simulation, yout will be an array of 1s. Also if I compile the model using the Simulink coder, the output will also be an array of 1s. However if I modify myValues.m so that input1 = 2 and do not recompile, the realtime output is still 1. This is wrong, so how can I read variables from a file into the model workspace with a compiled model?

You cannot generate code for model callbacks. If you do not want to regenerate code every time you change your input you can try using "From File" block which can read data from a .mat file. When you want to change your data you can then run your MATLAB code and save the output data into the same .mat file. There are some restrictions on what kind of data is supported for code generation from this block. Check out the doc for that block for details.
If your data is not too big you can also edit the generated source to modify the data. Data from Constant block is usually in-lined in the generated source code. After editing you can compile the generated code to produce new binary.
Another approach is to write your custom C S-Function where you can read from your own data sources. You need to write a TLC file to support code generation for this S-Function.
You need to recompile your model if these does not work for you. Documentation at http://www.mathworks.com/help/simulink/ug/importing-signal-data-in-simulink.html lists different ways of importing signal data into Simulink.

This does not answer your question about Model callbacks, but it might be helpful anyway.
If the "Inline Parameters" option is checked in:
Preferences -> Optimization -> Signals and Parameters
there is no way to change values in an already compiled model because they are hardcoded. Once you have this option turned off and you have recompiled, you could for example connect with external mode and run your myValues.m script and the values will be updated (unless you have marked them as non-tuneable).

Related

Initialize buildable Matlab Simulink Model with parameters from SQLite Database

Context: I have a huge Simulink Model that is going to be used for automated simulations on a Debian 10. Therefore it has to be built as standalone C-Code using the Matlab Coder. This code is then called to start the simulation.
What I need: I need to find a way to initialize my built model with ~500 parameters. These change with each simulation run and are stored in a SQLite file. The goal is to have parameters written to the database, then start the Model which reads the parameters from SQLite during initialization (presumably using the InitFcn Model Callback, although I'm open to alternatives).
What I have tried:
Direct SQL interface: I tried to use a direct Matlab-SQL interface such as JDBC (since I don't have access to the Database-toolbox) but those are not supported for Code generation.
Write a C-function that reads the SQLite file, then call the function during initialization in the InitFcn Callback using coder.ceval like this:
data = 0;
err = coder.ceval('read_function',4, 2, 12, coder.wref(data));
parameter = data;
Problem here is that coder.wref is not supported in Matlab and therefore doesn't work in the InitFcn. (Please correct me if I'm wrong)
This only seems to work inside a Matlab-Function-Block:
Error evaluating 'InitFcn' callback of block_diagram 'Model'.
Caused by:
The coder.wref function is not supported in MATLAB.
So my problem with the second approach is, that I can't call the C-function during initialization.
Using a Matlab-function-Block to read the parameters isn't really an option, since I would have to route all the signals out which makes maintaining and further development of the model really hard. Also my suggestion is, that the model would not even run because the parameters are needed to initialize the model.
Questions:
Is there a way to make one of the above approaches work? If yes, how? Where is my mistake?
Is there another (simpler) option to pass the data as an array or struct to my model?
Database looks like this:
Identifier Default
latitude 52.5
longitude 13.4
electricity_consumption 4000.0
ventilation_stream 50.0
PV_peak 30.0
PV_orientation 0.0
no_vessels 28.0
heatpump_exists 1.0
hotwater_consumption 1000.0
.
.
.
After having spent so much time on this issue, I would like to share my experience on this problem:
SQLite: This approach did not work out for me because the direct SQL-Matlab interfaces are not supported for code generation.
It is in fact possible to write a C-function, that reads from SQLite and call that function in a Matlab-function-block via coder.ceval wich allows to read in a signal during simulation. This works for code generation (Simulink coder) as well. However this will not work for initialization (see question).
So none of my original approaches ended up working.
Workaround: I ended up switching to an approach based on the Simulink RSIM-target wich generates code (also for Linux) and can be parametrized via a .mat file wich contains all the parameters. The .mat file can be modified to update parameters. This required some additional code wich automates this step. Also the model configuration for RSIM is a bit tricky.

When to use "to workspace" block vs "outport" block

I can use both blocks for getting output from simulink to matlab, but if there are two of them there should be difference in the way they are used but I can not figure it out.
Using an Outport block allows you to,
use your model as a Model Reference
when generating code (using Simulink Coder) from the model, interface the model with other code
If you're not needing to do either of the above - for instance you only want to dump data to the MATLAB Workspace - then the To Workspace block is arguably easier to use. Plus it shows the user what the resulting variable will be without them having to open the model properties window.
Note also that To Workspace can be used at any level of model hierarchy, whereas Outport can only be used to get data out of the highest level of a model. (Outport are used in SubSystems but to interface a sub-level of a model to a higher level, not to get data out of the model.)

Impossible to tune Simulink Parameter at simulation time

I have a problem. I have an embedded function in my simulink model which has a structure (struct) as parameter. It contains only numerical values and I generate an S-Function of the embedded function by right clicking on the block and C/C++ code --> Generate S-function.
I then have the compiled block, if I try to change some values of my struct nothing changes (the fields of my struct stay the same as when I first compiled my embedded function).
When I compiled the embedded function block I selected the parameter to be tunable. I selected the parameter to be tunable in the Model Explorer. I tried to follow this video tutorial by mathworks: http://fr.mathworks.com/videos/tunable-structure-parameters-68947.html (The video is for r2010a while I am at r2015b) It is a bit different the interface in r2015b (from the one in the video) but when I click on Configure , like the guy does in the video, nothing happens.
Could you help me please?
Thanks a lot.
Once I had also decided to reduce the number of tunable parameters by checking the 'inline parameters' checkbox and then specifying the exception variables (variable that have the permission to be tunable even when 'inline parameters' is turned on. It did not work.
In case you aim does not heavily rely on optimization, it would be better if you simply turn off 'inline parameters'.
After that, the constant blocks (i suppose you are giving the input to your s-function from constant blocks) will become tunable.
Another advice: add mex in the init function of your model callbacks. It will save you from getting weird output (usually due to uncleared/un-reset variables from previous runs).
Hope it helps!

Save array to base workspace from simulink model

I'm using a MATLAB function block within a Simulink model. I build this model and run it on a dspace system with 1 kHz. To evaluate my experiment I need the data (20x20 double array) that is calculated in my MATLAB function block. Is it possible to export the data to the base workspace?
To read a variable from your system, the easiest way to do so is using ControlDesk. Create a project and download/start your experiment using ControlDesk, then it is automatically aware of the running application and can read the variable. You now have to configure a Measurement (or Capture in old versions) and export the results to MAT. You can find detailed instructions in the documentation from dSPACE, called HelpDesk.
Alternatively you can use the XIL-API or HIL-API to automate the above steps.

Matlab/Simulink - Create S-function from DSpace Code

I know that it is possible to create S-function from C code that I provide.
But is it possible to create one S-function from C Code, which is generated from a Simulink Model for dSPACE ECUs, with low effort. The reason is, I am trying to test the dspace code with matlab/simulink.
Furthermore I'm aksing because the generated code from this simulink model consists multiple .c and .h files and I don't know how to integrate these files in one s-function block.
There are several methods of doing this. I suggest using the Legacy Code Tool as its structure helps to guide you through the process. You could also explore calling the function(s) from Stateflow or a Matlab Function block depending on the application.
With respect to multiple c/h files. I assume they will all need to be visible to Matlab through pointing to the source paths, but you should be able to identify the 'entry' function that you're interested in and utilize that in your model.