Borrar filtros
Borrar filtros

Array size initialization in generated code from Simulink?

14 visualizaciones (últimos 30 días)
Etienne Perron
Etienne Perron el 19 de Nov. de 2021
Respondida: Sameer el 21 de Feb. de 2024
I am trying to build a standalone executable from a simulink model where the signal size are inferred at initialization. The goal here is to be able to uses the executable without the need of matlab to rebuild the code for different array size.
I dont want to uses variable-size signal since it propagates everywhere in my model and introduces a lot of overhead. (I really need my code to be fast)
Expected results:
  • an autocoded simulink model in cpp
  • Array size are initialized when void model_initialize(void) is called
  • Defined extern arraysize parameter
  • Avoid variable-size signal in simulink diagram
model.c should look like... (pseudocode)
extern uint8_T arraysize 16U
void model_initialize(void){
myArray = zeros(arraysize,1)
}

Respuestas (1)

Sameer
Sameer el 21 de Feb. de 2024
Hi Etienne,
From my understanding, you want to create a standalone executable from a Simulink model that allows for the initialization of array sizes at runtime, without the need for recompilation when array sizes change. To achieve this, you can utilize “tunable parameters” within your Simulink model. These parameters can be set externally before running the executable, enabling the model to adapt to different array sizes. This approach allows you to avoid the use of variable-size signals, thus maintaining the execution speed of your model.
You can follow the below steps to achieve this:
Define a Tunable Parameter in Simulink:
Create a Simulink.Parameter object in the MATLAB base workspace or in a data dictionary that your model references. This parameter will act as the array size.
arraySizeParam = Simulink.Parameter;
arraySizeParam.Value = 16; % Default value
arraySizeParam.CoderInfo.StorageClass = 'ExportedGlobal';
arraySizeParam.DataType = 'uint8';
Assign this parameter to the blocks in your model that need to use the array size.
Configure the Model for Code Generation:
Open the Configuration Parameters dialog in Simulink and set up the parameters for code generation. Make sure to select the appropriate system target file for standalone code generation (e.g., ert.tlc for Embedded Coder).
Generate Code and Standalone Executable:
Use the Simulink Coder or Embedded Coder to generate code for your model. Then, compile the generated code into a standalone executable.
Modify the Generated model_initialize Function:
In the generated model.c file, you will find a model_initialize function. You can modify this function to initialize your array with zeros based on the tunable parameter.
extern uint8_T arraySizeParam; // This will be defined in model.h
void model_initialize(void)
{
// Initialize your array with zeros
for (uint8_T i = 0; i < arraySizeParam; i++)
{
myArray[i] = 0;
}
}
Set the Tunable Parameter Externally:
Before running the standalone executable, you will need to set the arraySizeParam to the desired value. This can be done in a separate initialization function or in the main function before calling model_initialize.
extern uint8_T arraySizeParam;
int main(void)
{
// Set the array size before initializing the model
arraySizeParam = 16U; // Or any other size you want to set
// Initialize the model
model_initialize();
// ... rest of your application code ...
}
By following these steps, you can generate a standalone executable that allows for array sizes to be set at initialization without the overhead of variable-size signals in the Simulink model.
Keep in mind that manual modifications to the generated code need to be reapplied if the code is regenerated. To avoid this, you could create a custom TLC file or use Simulink Coder's custom code hooks to automate the insertion of your custom initialization code.
I hope this helps!
Sameer

Categorías

Más información sobre Simulink Coder en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by