πŸ’ΎCode Generation

Collimator generates C code from Submodels. With a Submodel instantiated in a model, select it, then in CODE GENERATION section of the right pane, click Generate code from this Submodel. This will result in your browser downloading a zip file.

For a Submodel named my_submodel, instantiated in a model named my_model, the zip file will be named my_model.my_submodel-<date-time>.zip, and it will contain the following files:

  • my_submodel.c

  • my_submodel.h

  • my_submodel_python_script.h

  • cml_types.h

  • my_submodel.txt

  • cppcheck.log

The concept is that the C code generated from the Submodel is to be used as a translation unit in a C code project. Alone, the code generated from Collimator is insufficient to deploy your Submodel algorithm to hardware, you need to write a C program with an entry point, and other typical constructs, and this program will call the functions defined in the generated code.

C code egenration is limited to discrete and agnostic blocks. Code generation form Python Script blocks is not presently supported. If you inclide a Python Script block in your submodel, Collimator will treat it as a black box function, and generated template C code for it. You must manully

The detailed role of each file is explained below.

C code files

my_submodel.c

This file defines the initialization and step functions which reproduce the algorithm of your Submodel. my_submodel_initialize() must called to initialize any states and/or outputs, before calling my_submodel_step().

The function names, and type definitions use the Submodel name so that you can generate code from several Submodels in your model, and you will not get C name clashes in your C project if you include multiple translation units form Collimator.

my_submodel.h

Declarations and type definitions for the content of my_submodel.c.

my_submodel_python_script.h

If you incluede da Pythin Script block in your Submodel, the function template generated for the block is in this file.

cml_types.h

Some types used by the C code.

my_submodel.txt

This is a helper file with some convenience code that you may choose to copy/paste in your C program.

cppcheck.log

Collimator C code is checked for MISRA C 2012 compliance using Cppcheck. Tis file contains the output from running Cppcheck on the .c and .h files.

For a copy of Collimator's MISRA C 2012 compliance matrix, please contact sales.

Discrete Step

Any discrete blocks that depend on time to perform their update, e.g. discrete integrator, will use the discrete step assigned to the Submodel instance in the model.

If your Submodel has the discrete_step setting enabled, it will used that, otherwise it will use the Global Discrete Step of the model.

Example program

Below is a simple example program that calls the functions in the generated code.

```
#include "my_submodel.h"

int main( void)
{
  // initialize time and other stimulous variables
  float64_t stop_time = 10.0;
  float64_t discrete_step = 0.1;
  float64_t time = 0.0;
  float64_t time1 = 5.0;
  float64_t time2 = 7.0;

  // call initialize function
  my_submodel_initialize(my_submodel_inputs_ptr);

  while(time<=stop_time){
    // compute inputs
    my_submodel_inputs_ptr->my_input = (time >= time) && (time <= time);
    
    // call step function
    my_submodel_step(my_submodel_inputs_ptr, my_submodel_outputs_ptr);
    
    // optionally read the outputs
    if (my_submodel_outputs_ptr->my_stop_signal) break;

    // increment step time
    time = time + discrete_step;
  }
}
```

Last updated