How to initialize SimStruct from C code?

I exported my Simulink model to C code by using "rtwsfcn.tlc". The output code for the S-Function was generated successfully.
Now I want to import this S function directly in a C project. So what I did is something like this.
SimStruct *S = (SimStruct *)malloc(sizeof(SimStruct));
mdlInitializeSizes(S); ... ...
But it gives some runtime error. Can anyone tell me the proper way to initialize SimStruct directly in C without using Matlab?

 采纳的回答

TAB
TAB 2013-3-11
编辑:TAB 2013-3-11
What is your C project ? Is it a embedded project ?
s-function is matlab specific c code which uses lots of function from it library called SimStruct library. It is very difficult and unnecessary to integrate the s-function code into some user project.
Instead twsfcn.tlc, you can use ert.tlc (Embedded coder tool) to generate production quality C code which can be integrated into any custom project.

7 个评论

Thanks for the reply.
What I actually want to do is to export a Simulink model to a C code. Make a DLL from this code and then Import it into my application written in C.
The problem I am facing is that the output code generated by "ert.tlc" or "grt.tlc" is model specific. The data structures and port names are inherited from the model. Moreover, they use global variables.
Due to non-standardized way to access input and output ports across different models doesn't allow me to implement a generic importer that can import any model in my C code. Moreover, global variables in the DLL also limits the number of instances of the model I can have in a single process.
You can try to generate the Reusable code. In reusable code, model is generate as function with Input/Output signals as arguments to the function. Unfortunately, I am not able to access the Mathworks documentation (I don't know why). See similar page here for more info.
Thanks for your time, I tried using the "Generate reusable code" option with "ert.tlc". The code generated does not have any global variables and that is great. However, the output and input data-structures are still not standardized. For example if a model have two outports named "Out1" and "MyOut2" the following data-structure is generated.
typedef struct {
real_T Out1;
real_T MyOut2;
} ExtY_MY_MODEL_NAME_T;
I want something like
typdef struct{
real_T Out[2];
}OutputDataStruct;
So that the interface remains the same for all the models I need to import in my C code. I don't know why I cannot find anything on the Internet.
Looking forward to your helpful reply ...
I think now I am clear about what you need.
To generate the simulink input/output as custom structure you have create a non-virtual bus using Simulink.Bus object with the elements you required. Now set the data type of your input/output port as this bus signal.
For bus signals simulink generates the equivalent structures in generated code.
In your case bus object will look like:
OutputDataStruct_Bus = Simulink.Bus;
Elem1 = Simulink.BusElement;
Elem1.Name= 'Out';
Elem1.Dimensions = 2;
OutputDataStruct_Bus.Elements = Elem1
Thanks for your extremely helpful replies. Really appreciate your help.
I have checked non-virtual buses and they do solve the problem to some extent. However, I still have a problem that in my case the Dimensions of the elements are different across each model. If I change the DimensionMode of an element to 'Variable'
OutputDataStruct_Bus = Simulink.Bus;
Elem1 = Simulink.BusElement;
Elem1.Name = 'Out';
Elem1.Dimensions = 100; %%%%Max Allowed Dimensions %%%%
Elem1.DimensionMode = 'Variable';
OutputDataStruct_Bus.Elements = Elem1
the simulink coder does not allow me to use 'Generate Reusable code' option. The error says "You cannot use variable size elements with Reusable code".
The thing I am confused about is that why there is very little help on this topic. All I want is a consistent interface for all my models. An interface that does not depend at all on the model. I wonder why no one else wants or needs that?
TAB
TAB 2013-3-13
编辑:TAB 2013-3-13
I am realy not getting what do you mean by Dimensions of the elements are different across each model.
If you are creating the instance of same structure for different models, then dimension will remain same.
Or, if you have different structure inputs for each model, then you create separate Simulink.Bus bus objects for each structure.
Code generated by simulink is fully customizable. You can go through the documentation of Simulink Coder, Embedded Coder and Simulink Coder Target Language Compiler for complete details.
PDF documentation is directly available for all registered user.
Lets assume a very simple case. I have a simulink model that takes '2' inputs and add them to return one output.
Another model takes '3' inputs and add them together. Yet another model takes '4' inputs and add them to give one output.
So what I need is an interface that works with all these models.
Making a new Simulink.Bus object for each structure is just not an option in my case.
Thanks

请先登录,再进行评论。

更多回答(1 个)

yh h
yh h 2019-11-6
where can I fined the "SimStruct"at the matlab dir?

类别

帮助中心File Exchange 中查找有关 Simulink Coder 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by