Main Content

Configure Model and Generate Code

About This Example

Learning Objectives

  • Configure a model for code generation.

  • Use model checking tools to discover conditions and configuration settings resulting in generation of inaccurate or inefficient code.

  • Generate code from a model.

  • Locate and identify generated code files.

  • Review generated code.

Prerequisites

  • Ability to open and modify Simulink® models and subsystems.

  • Ability to set model configuration parameters.

  • Ability to use the Code Generation Advisor.

  • Ability to read C code.

  • An installed, supported C compiler.

Required Files

ThrottleControl model file

Open Example Model

This example uses a simple, but functionally complete, example model of a throttle controller. The model features redundant control algorithms. The model highlights a standard model structure and a set of basic blocks in algorithm design.

Open the model ThrottleControl.

open_system('ThrottleControl')

Configure the Model for Code Generation

Model configuration parameters determine the method for generating the code and the resulting format.

  1. Open the Simulink Coder app.

  2. Open the Configuration Parameters dialog box, Solver pane. To generate code for a model, you must configure the model to use a fixed-step solver. The following table shows the solver configuration for this example.

    ParameterSettingEffect on Generated Code
    TypeFixed-stepMaintains a constant (fixed) step size, which is required for code generation
    Solverdiscrete (no continuous states)Applies a fixed-step integration technique for computing the state derivative of the model
    Fixed-step size.001Sets the base rate; must be the lowest common multiple of the rates in the system

  3. Set model configuration parameter System target file to grt.tlc.

    Note

    The GRT (Generic Real-Time Target) configuration requires a fixed-step solver. However, the rsim.tlc system target file supports variable step code generation.

    The system target file defines an environment for generating and building code for execution on a certain hardware or operating system platform. For example, one property of a system target file is the value for the CodeFormat TLC variable. The GRT configuration requires a fixed step solver and the rsim.tlc supports variable step code generation.

  4. Close the Model Configuration Parameters dialog box.

Save Your Model Configuration as a MATLAB Function

You can save the settings of model configuration parameters as a MATLAB® function by using the getActiveConfigSet function. In the MATLAB Command Window, enter:

thcntrlAcs = getActiveConfigSet('ThrottleControl');
thcntrlAcs.saveAs('throttleControlModelConfig');

You can then use the resulting function (for example, throttleControlModelConfig) to:

  • Archive the model configuration.

  • Compare different model configurations by using differencing tools.

  • Set the configuration of other models.

For example, you can set the configuration of model myModel to match the configuration of the throttle controller model by opening myModel and entering:

myModelAcs = throttleControlModelConfig;
attachConfigSet('myModel', myModelAcs, true);
setActiveConfigSet('myModel', myModelAcs.Name);

For more information, see Save a Configuration Set and Load a Saved Configuration Set.

Check Model Conditions and Configuration Settings

Before generating code for a model, use the Code Generation Advisor to check the model conditions and configuration settings. The check identify issues that can result in inaccurate or inefficient code.

  1. Open the Simulink Coder app.

  2. In the C Code tab, click C/C++ Code Advisor. A dialog box opens showing the model system hierarchy.

  3. Click ThrottleControl and then click OK. The Code Generation Advisor window opens.

  4. Right-click on the Code Generation Advisor folder and select Run Selected Checks.

  5. To review the results for check Check model configuration settings against code generation objectives, select the check in the left pane. The results are displayed in the right pane.

  6. After reviewing the check results, you can choose to fix warnings and failures as described in Fix a Model Advisor Check Warning or Failure.

Generate Code for the Model

  1. Open the Simulink Coder app.

  2. Select model configuration parameter Generate code only.

  3. Select model configuration parameter Create code generation report and click Apply.

  4. Generate code.

    Messages appear in the Diagnostics Viewer. The code generator produces standard C and header files, and an HTML code generation report. The code generator places the files in a build folder, a subfolder named ThrottleControl_grt_rtw under your current working folder.

Review the Generated Code

In the code generation report, click the link for the generated C model file and review the generated code. Look for these items in the report:

  • Identification, version, timestamp, and configuration comments.

  • Links to help you navigate within and between files

  • Data definitions

  • Scheduler code

  • Controller code

  • Model initialization and termination functions

  • Call interface for the GRT system target file — output, update, initialization, start, and terminate

Consider examining the following files. In the code generation report Contents pane, click the links.

FileDescription
ThrottleControl.cC file that contains the scheduler, controller, initialization, and interface code
ThrottleControl_data.cC file that assigns values to generated data structures
ThrottleControl.hHeader file that defines data structures
ThrottleControl_private.hHeader file that defines data used only by the generated code
ThrottleControl_types.hHeader file that defines the model data structure

For more information, see Manage Build Process File Dependencies.

At this point, consider logging data to a MAT-file. For an example, see Log Data for Analysis.

Generate an Executable

  1. Open the Simulink Coder app.

  2. Clear model configuration parameter Generate code only and click Apply.

  3. Build the model. Watch the messages in the Diagnostics Viewer. The code generator uses a template make file associated with your system target file selection to create an executable file. You can run this program on your workstation, independent of external timing and events.

  4. Check your working folder for the file ThrottleControl.exe.

  5. Run the executable. In the Command Window, enter !ThrottleControl. The ! character passes the command that follows it to the operating system, which runs the standalone program.

    The program produces one line of output in the Command Window:

    ** starting the model **

    At this point, consider logging data to a MAT-file. For an example, see Log Data for Analysis.

Tip

For UNIX® platforms, run the executable program in the Command Window with the syntax !./executable_name. If preferred, run the executable program from an OS shell with the syntax ./executable_name. For more information, see Run External Commands, Scripts, and Programs.

Key Points

  • To generate code, change the model configuration to specify a fixed-step solver then select a system target file. Using the grt.tlc file requires a fixed-step solver. If the model contains continuous time blocks, you can use a variable-step solver with the rsim.tlc system target file.

  • After debugging a model, consider configuring a model with parameter inlining enabled.

  • Use the getActiveConfigSet function to save a model configuration for future use or to apply it to another model.

  • Before generating code, consider checking a model with the Code Generation Advisor.

  • The code generator places generated files in a subfolder (model_grt_rtw) of your working folder.

Related Topics