Main Content

Generate Code by Using Embedded Coder Quick Start

Model rtwdemo_roll represents an autopilot control system for an aircraft. You prepare rtwdemo_roll for embedded code generation by using Embedded Coder® Quick Start, which chooses fundamental code generation settings based on your goals and application.

Generate Code with Quick Start Tool

  1. Open model rtwdemo_roll.

  2. Save a copy of the model to a writable location on the search MATLAB path.

  3. If the C Code tab is not already open, in the Apps gallery, under Code Generation, click Embedded Coder.

  4. On the C Code tab, click Quick Start.

  5. Advance through the steps of the Quick Start tool. Each step asks questions about the code that you want to generate. For this tutorial, use the defaults that are already selected. The tool validates your selections against the model and presents the parameter changes required to generate code.

  6. In the Generate Code step, apply the proposed changes and generate code from rtwdemo_roll by clicking Next.

  7. Click Finish, then return to the C Code tab. From this tab you can configure code generation customizations, and then check the results in the Code view next to the model.

Inspect the Generated Code

The generated code appears in two primary files: rtwdemo_roll.c and rtwdemo_roll.h. In your MATLAB® current folder, the rtwdemo_roll_ert_rtw folder contains these primary files.

In your current folder, the code generator creates the slprj folder. This folder contains the rtwtypes.h file, which defines standard data types that the generated code uses by default. In general, this sibling folder contains generated files that can or must be shared between multiple models.

The code that you generate from a model includes entry-point functions, which you call from your application code. For a rate-based model, these functions include an initialization function, an execution function, and, optionally, terminate and reset functions. The functions exchange data with your application code through a data interface that you control.

Open the Code Mappings editor by clicking Code Mappings below the model diagram. On the Functions tab, you can see the individual entry-point functions that the code generator produces. You call these generated functions from external code or from a version of a generated main function that you modify. For the base-rate step function of a rate-based model and for step functions for export function models, you can customize the function name and arguments.

Review the list of entry-point functions that the code generator produces for the model. Use this view to selectively specify for each function a function customization template (code definition) and name. For this tutorial, the code generator uses default (shipped) settings for the customization template and entry-point function names. The code generator names the initialize function rtwdemo_roll_initialize and the execution (step) function rtwdemo_roll_step. Both entry-point functions have a void-void interface (they pass no arguments). The functions gain access to data through shared data structures. Examples of such data include system-level input and output that the functions exchange with application code.

To see these entry-point functions in the generated code:

  1. On the right side of the Simulink® Editor window, in the Code view pane, locate the search bar.

  2. In the search bar, type rtwdemo_roll_step. To find each instance of the step function name across the generated code files, click the search suggestion.

  3. Use the arrows on the right to step through each instance, including the step function definition in rtwdemo_roll.c and the declaration in rtwdemo_roll.h. You can also see the number of search hits in each file from the file menu in the upper left corner.

  4. Repeat these search steps to locate the initialize function, rtwdemo_roll_initialize in the generated code.

Next, configure the data interface for code generation and review the generated code.