A good approach to code generating a library of functions using Simulink Coder

6 views (last 30 days)
Hi all,
I would like to code generate a number of Matlab functions I have using Simulink&Embedded Coder. Typically all the Matlab functions have arrays, nested structures or structures of arrays being passed by reference into them. I have made some progress in building a Simulink model that encapsulates (Matlab Function block) the function and code generates them. I convert Matlab structures into Simulink.Bus and Simulink.Parameter which are placed in the Base Workspace. AliasTypes are used for the custom data types we want to use. A Model Configuration sits in the base workspace, the Top Model and Referenced Model use a Configuration Reference of that Config.
I would like to organize the generated functions in a library. For example I would like to place the structures in the header file:
ifndef _foo_INCLUDED
#define _foo_INCLUDED
#include "custom_types.h"
typedef struct fooDef
{
custom_uint32_t variable1;
custom_float32_t variable2;
}fooStr;
...
extern void fooSet(fooStr *paramPtr);
extern void fooReset(fooStr *paramPtr);
extern void fooOperate(fooStr *paramPtr);
...
The source file then should look something like:
#include "custom_types.h"
#include "foo.h"
...
void fooSet(fooStr *paramPtr)
{
paramPtr->variable1 = 30;
paramPtr->variable2 = 5.0;
}
void fooReset(fooStr *paramPtr)
{
...
}
void fooOperate(fooStr *paramPtr)
{
...
}
...
I am unsure on the optimum way of coding this into Simulink. Do I use the Subsystem block or the Model Reference Block? For example how do I model the above source and header files? Top level "foo" Model Reference block and internally a model for each individual function (ie model reference hierarchy)?
What Data Scope should my Simulink.Bus objects be and what Storage Class should my Simulink.Paramters be to achieve this?
I am going through the documentation on Simulink Coder->Referenced Models, Simulink->Model Referencing, Manage Placement of Data Definitions and Declarations, Block Parameter Representation in the Generated Code, etc...
I am looking for someone to point me to the right the right book, webpage or example. My trial and error approach has its limitations so any help is much appreciated.
Thanks Alex

Answers (1)

Mark McBroom
Mark McBroom on 2 Dec 2017
Edited: Mark McBroom on 2 Dec 2017
for smaller components, the best approach is to create sub-systems and place them in a Simulink library. Configure the sub-systems to generate reusable C functions by doing the following:
1. Set each sub-system to be "Treat as atomic"
2. Set the Code Generation settings for each sub-system to be: Function packaging = "Reuable function", Function Name Options = "auto" ( 17a and later you can specify a name) and File name options = "auto"
3. In the model configuration settings, set Code Generation -> Interface -> Shared code placement = "shared location"
4. In model configuration, set Optimization -> Signals and param3eters -> Pass reuslable system outputs = Individual arguments
==> this should result in a reuslable .c/.h file for each subsystem in your library being placed in /slprj/ert/_sharedutils
Thanks. Mark.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!