Exchange Structured and Enumerated Data Between Generated and External Code - MATLAB & Simulink (original) (raw)

This example shows how to generate code that exchanges data with external, existing code. Construct and configure a model to match data types with the external code and to avoid duplicating type definitions and memory allocation (definition of global variables). Then, compile the generated code together with the external code into a single application.

Inspect External Code

Create the file ex_cc_algorithm.c in your current folder.

#include "ex_cc_algorithm.h"

inSigs_T inSigs;

float_32 my_alg(void) { if (inSigs.err == TMP_HI) { return 27.5; } else if (inSigs.err == TMP_LO) { return inSigs.sig1 * calPrms.cal3; } else { return inSigs.sig2 * calPrms.cal3; } }

The C code defines a global structure variable named inSigs. The code also defines a function, my_alg, that uses inSigs and another structure variable named calPrms.

Create the file ex_cc_algorithm.h in your current folder.

#ifndef ex_cc_algorithm_h #define ex_cc_algorithm_h

typedef float float_32;

typedef enum { TMP_HI = 0, TMP_LO, NORM, } err_T;

typedef struct inSigs_tag { err_T err; float_32 sig1; float_32 sig2; } inSigs_T;

typedef struct calPrms_tag { float_32 cal1; float_32 cal2; float_32 cal3; } calPrms_T;

extern calPrms_T calPrms; extern inSigs_T inSigs;

float_32 my_alg(void);

#endif

The file defines float_32 as an alias of the C data typefloat. The file also defines an enumerated data type,err_T, and two structure types, inSigs_T and calPrms_T.

The function my_alg is designed to calculate a return value by using the fields of inSigs and calPrms, which are global structure variables of the types inSigs_T and calPrms_T. The function requires another algorithm to supply the signal data that inSigs stores.

This code allocates memory for inSigs, but not forcalPrms. Create a model whose generated code:

  1. So that you can create enumerated and structured data in the SimulinkĀ® model, first create Simulink representations of the data types that the external code defines. Store the Simulink types in a new data dictionary namedex_cc_integ.sldd.
    Simulink.importExternalCTypes('ex_cc_algorithm.h',...
    'DataDictionary','ex_cc_integ.sldd');
    The data dictionary appears in your current folder.
  2. To inspect the dictionary contents in the Model Explorer, in your current folder, double-click the file,ex_cc_integ.sldd.
    The Simulink.importExternalCTypes function creates Simulink.Bus, Simulink.AliasType, and Simulink.data.dictionary.EnumTypeDefinition objects that correspond to the custom C data types fromex_cc_algorithm.h.
  3. Create a new model and save it in your current folder asex_struct_enum_integ.
  4. Link the model to the data dictionary. On theModeling tab, underDesign, click Link to Data Dictionary.
  5. Add algorithmic blocks that calculate the fields ofinSigs.

Now that you have the algorithm model, you must:

Configure Generated Code to Write Output Data to Existing Structure Variable

  1. Add a Bus Creator block near the existingOutport blocks. The output of a Bus Creator block is a bus signal, which you can configure to appear in the generated code as a structure.
  2. In the Bus Creator block, set these parameters:
    • Number of inputs to3
    • Output data type to Bus: inSigs_T
    • Output as nonvirtual bus to selected
  3. Delete the three existing Outport blocks (but not the signals that enter the blocks).
  4. Connect the three remaining signal lines to the inputs of theBus Creator block.
  5. Add an Outport block after the Bus Creator block. Connect the output of the Bus Creator to the Outport.
  6. In the Outport block, set the Data type parameter to Bus: inSigs_T.
  7. On the Modeling tab, click Model Data Editor.
  8. On the Inports/Outports tab, for theInport blocks labeled In2 andIn3, change Data Type fromInherit: auto tofloat_32.
  9. For the Outport block, set Signal Name to inSigs.
  10. Open the Embedded Coder app. in theApps gallery, under Code Generation, click Embedded Coder. The C Code tab opens.
  11. Open the Code Mappings editor. Select >
  12. On the Outports tab, select the row for the outport. Set Storage Class toImportFromFile.
  13. Click the Icon to configure additional code mapping properties and set Header File to ex_cc_algorithm.h.
  14. In the model, select the output signal of the Multiport Switch block.
  15. In the Model Data Editor, for the selected signal, setName to err.
  16. Set the name of the output signal of the Gain block tosig1.
  17. Set the name of the output signal of the Gain1 block tosig2.

When you finish, the model stores output signal data (such as the signals err and sig1) in the fields of a structure variable named inSigs.

Because you set Storage Class to ImportFromFile, the generated code does not allocate memory for inSigs.

Configure Generated Code to Define Parameter Data

Configure the generated code to define the global structure variable,calPrms, that the external code needs.

  1. In the Model Explorer Model Hierarchy pane, under the dictionary node ex_cc_integ, select theDesign Data node.
  2. In the Contents pane, select theSimulink.Bus object named calPrms_T.
  3. In the Dialog pane (the right pane), click Open Type Editor.
  4. In the Type Editor, select calPrms_T.
  5. In the Type Editor toolstrip, click Simulink Parameter.
  6. In the MATLABĀ® Editor, copy the generated MATLAB code and run the code at the command prompt. The code creates aSimulink.Parameter object in the base workspace.
  7. In the Model Explorer Model Hierarchy pane, select Base Workspace.
  8. Use the Model Explorer to move the parameter object,calPrms_T_Param, from the base workspace to the Design Data section of the data dictionary.
  9. With the data dictionary selected, in theContents pane, rename the parameter object ascalPrms.
  10. In the Model Data Editor, select the Parameters tab.
  11. For the Gain block, replace the value13.8900013 withcalPrms.cal1.
  12. In the other Gain block, usecalPrms.cal2.
  13. While editing the value of the other Gain block, next to calPrms.cal2, click the action button and select > .
  14. In the calPrms property dialog box, next to theValue box, click the action button and select .
  15. Use the Variable Editor to set the field values in the parameter object.
  1. In the property dialog box, set Storage class toExportedGlobal. ClickOK.
  2. Use the Model Explorer to save the changes that you made to the dictionary.

Generate, Compile, and Inspect Code

  1. Configure the model to include ex_cc_algorithm.c in the build process. Set > > > > to ex_cc_algorithm.c.
  2. Generate code from the model.
  3. Inspect the generated file ex_struct_enum_integ.c. The file defines and initializes calPrms.
    /* Exported block parameters /
    calPrms_T calPrms = {
    13.8900013F,
    0.998300076F,
    15.23F
    } ; /
    Variable: calPrms

The algorithm then calculates and stores data in the fields ofinSig.

Replace Data Type Names Throughout Model

To generate code that uses float_32 instead of the default, real32_T, instead of manually specifying the data types of block output signals and bus elements, you can use data type replacement ( > > ). For more information, see Replace and Rename Simulink Coder Data Types to Conform to Coding Standards.

See Also

Simulink.importExternalCTypes

Topics