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

rtwdemo_throttlecntrlmodel file

Configure the Model for Code Generation

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

  1. Openrtwdemo_throttlecntrland save a copy asthrottlecntrlin a writable location on your MATLAB path. Then, open theSimulink Coder应用程序。

  2. Open the Configuration Parameters dialog box,Solverpane. 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.

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

  3. Set model configuration parameterSystem target filetogrt.tlc.

    Note

    The GRT (Generic Real-Time Target) configuration requires a fixed-step solver. However, thersim.tlcsystem 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 theCodeFormatTLC variable. The GRT configuration requires a fixed step solver and the rsim.tlc supports variable step code generation.

  4. Set model configuration parameterInclude directoriesas follows:

    "$matlabroot$\toolbox\rtw\rtwdemos\EmbeddedCoderOverview\"

    This folder includes files that are required to build an executable for the model.

  5. Close the Model Configuration Parameters dialog box.

Save Your Model Configuration as aMATLABFunction

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

thcntrlAcs = getActiveConfigSet('throttlecntrl'); thcntrlAcs.saveAs('throttlecntrlModelConfig');

You can then use the resulting function (for example,throttlecntrlModelConfig) 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 modelmyModelto match the configuration of the throttle controller model by openingmyModeland entering:

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

For more information, seeSave a Configuration SetandLoad 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 modelthrottlecntrl. Then, open theSimulink Coder应用程序。

  2. In theC Codetab, clickC/C++ Code Advisor. A dialog box opens showing the model system hierarchy.

  3. Clickrtwdemo_throttlecntrland then clickOK. The Code Generation Advisor window opens.

  4. Right-click on theCode Generation Advisorfolder and selectRun Selected Checks.

  5. To review the results for checkCheck 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 inFix a Model Advisor Check Warning or Failure.

Generate Code for the Model

  1. Openthrottlecntrl. Then, open theSimulink Coder应用程序。

  2. Select model configuration parameterGenerate code only.

  3. Select model configuration parameterCreate code generation reportand clickApply.

  4. Generate code.

    消息出现在诊断查看器。鳕鱼e generator produces standard C and header files, and an HTML code generation report. The code generator places the files in abuild folder, a subfolder namedthrottlecntrl_grt_rtwunder your current working folder.

Review the Generated Code

  1. 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

  2. Save and close modelthrottlecntrl.

Consider examining the following files. In the code generation reportContentspane, click the links.

File Description
throttlecntrl.c C file that contains the scheduler, controller, initialization, and interface code
throttlecntrl_data.c C file that assigns values to generated data structures
throttlecntrl.h Header file that defines data structures
throttlecntrl_private.h Header file that defines data used only by the generated code
throttlecntrl_types.h Header file that defines the model data structure

For more information, seeManage Build Process File Dependencies.

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

Generate an Executable

  1. Openthrottlecntrl. Then, open theSimulink Coder应用程序。

  2. Clear model configuration parameterGenerate code onlyand clickApply.

  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 filethrottlecntrl.exe.

  5. Run the executable. In the Command Window, enter!throttlecntrl. 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, seeLog 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, seeRun 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 thegrt.tlcfile requires a fixed-step solver. If the model contains continuous time blocks, you can use a variable-step solver with thersim.tlcsystem target file.

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

  • Use thegetActiveConfigSetfunction 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.

  • 鳕鱼e generator places generated files in a subfolder (model_grt_rtw) of your working folder.

Related Topics