This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Prepare Variant-Containing Model for Code Generation

Using Embedded Coder®, you can generate code from Simulink® models containing one or more variant choices. The generated code contains preprocessor conditionals that control the activation of each variant choice.

Note

Simulink supports using multi-instance referenced models with variant Simulink Functions for code generation.

For information on using STF_make_rtw_hook file to customize build process, see Customize Build Process with STF_make_rtw_hook File (Simulink Coder)

Convert Variant Control Variables into Simulink.Parameter Objects

MATLAB® variables allow you to rapidly prototype variant control expressions when you are building your model and generate preprocessor conditionals for code generation. However, if you want to specify other code generation attributes (such as data type), you can convert MATLAB variables into Simulink.Parameter objects.

  1. Specify the model in which you want to replace MATLAB variant control variables with Simulink.Parameter objects.

    model = 'my_model_containing_variant_choices';
    open_system(model);
    
  2. Get the variables that are referenced in variant control expressions.

    vars = Simulink.VariantManager.findVariantControlVars(model)
    vars = 
    
    4x1 struct array with fields:
    
        Name
        Value
        Exists
        Source
        SourceType
  3. Create an external header file for specifying variant control values so that the variable definitions are imported when the code runs.

    headerFileName = [model '_importedDefines.h'];
    headerPreamble = strrep(upper(headerFileName),'.','_');
    
    fid = fopen(headerFileName,'w+');
    fidErr = (fid == -1);
    if (fidErr)
        fprintf('There was an error creating header file %s:...
    \n', headerFileName);
    else
        fprintf('+++ Creating header file ''%s'' with variant control...
    		variable definitions.\n\n', headerFileName);
        fprintf(fid, '#ifndef %s\n', headerPreamble);
        fprintf(fid, '#define %s\n', headerPreamble);
    end

    Variant control variables defined as Simulink.Parameter objects can have one of these storage classes.

    • Define or ImportedDefine with header file specified

    • CompilerFlag

    • SystemConstant (AUTOSAR)

    • Your own custom storage class that defines data as a macro

  4. Loop through all the MATLAB variables to convert them into Simulink.Parameter objects.

    count = 0;
    for countVars = 1:length(vars)
        var = vars(countVars).Name;
        val = vars(countVars).Value;
        if isa(val, 'Simulink.Parameter')
            % Do nothing 
            continue;
        end   
        count = count+1;
    		
    % Create and configure Simulink.Parameter objects 
    % corresponding to the control variable names.
    % Specify the custom storage class as Define (Custom).
    	newVal = Simulink.Parameter(val);
    	newVal.DataType = 'int16';
    	newVal.CoderInfo.StorageClass = 'Custom';
    	newVal.CoderInfo.CustomStorageClass = 'Define (Custom)';
    	newVal.CoderInfo.CustomAttributes.HeaderFile = headerFileName;
    		
    		Simulink.data.assigninGlobal(model, var, newVal);
    		
    		if ~fidErr
        		fprintf(fid, '#endif\n');
        		fclose(fid);
    		end
    end

Note

The header file can be empty for the Define custom storage class.

Configure Model for Generating Preprocessor Conditionals

If you represent variant choices inside a Variant Subsystem block or a Variant Model block, code generated for each variant choice is enclosed within C preprocessor conditionals #if, #else, #elif, and #endif.

If you represent variant choices using a Variant Source block or a Variant Sink block, code generated for each variant choice is enclosed within C preprocessor conditionals #if and #endif.

Therefore, the active variant is selected at compile time and the preprocessor conditionals determine which sections of the code to execute.

Note

You must have an Embedded Coder® license to generate code.

  1. In the Simulink editor, select Simulation > Model Configuration Parameters.

  2. Select the Code Generation pane, and set System target file to ert.tlc.

  3. In the Report pane, select Create code generation report.

    Note

    In the Code Placement pane, if Compact option is selected from File packaging format drop-down list, model_types.h file is not generated and contents of model_types.h file are moved to model.h file.

  4. Select the Code Generation pane, and clear Ignore custom storage classes and Apply.

  5. In your model, right-click the block containing the variant choices (Variant Subsystem, Variant Source, Variant Sink, or Variant Model) and select Block Parameters.

  6. Ensure that Expression (default option) is selected for Variant control mode parameter .

  7. Select the option Analyze all choices during update diagram and generate preprocessor conditionals.

    Simulink analyzes all variant choices during an update diagram or simulation. This analysis provides early validation of the code generation readiness for all variant choices.

  8. Build the model.

Related Examples

More About