Generate Code by Using Embedded Coder Quick Start
Model RollAxisAutopilot
represents an autopilot control system for an
aircraft. You prepare RollAxisAutopilot
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
Open the model
RollAxisAutopilot
by typing this command:openExample('ecoder/GenerateCCodeFromSimulinkModelsExample')
If the C Code tab is not already open, in the Apps gallery, under Code Generation, click Embedded Coder.
On the C Code tab, click Quick Start.
Advance through the steps of the Quick Start tool, stopping at the Generate Code step. 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.
In the Generate Code step, apply the proposed changes and generate code from
RollAxisAutopilot
by clicking Next.The Code Generation Complete page presents possible next steps and informs you that the tool configured the model for code generation that applies a data code interface. A data interface configuration maps model data elements to storage classes that specify the appearance and placement of the data elements in the generated code. When generating code for an export-function or rate-based model, you have the option of configuring the model to use a service code interface. Benefits of a service interface include:
Customizations that align with target platform service interface requirements
Code that maintains data coherence
Entry-point functions that can access time values provided by the target platform
For examples of configuring a model to use a service code interface configuration, see Deploy Export-Function Component Configured for C Service Interface Code Generation and Deploy Single-Rate, Rate-Based Component Configured for C Service Interface Code Generation.
Click Finish.
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: RollAxisAutopilot.c
and RollAxisAutopilot.h
. In your MATLAB® current folder, the RollAxisAutopilot_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 - Component Interface 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 RollAxisAutopilot_initialize
and the
execution (step) function RollAxisAutopilot_step
. Both entry-point
functions have a void-void
interface (they do not pass 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:
On the right side of the Simulink® Editor window, in the Code view, locate the search bar.
In the search bar, type
RollAxisAutopilot_step
. To find each instance of the step function name across the generated code files, click the search suggestion.Use the arrows on the right to step through each instance, including the step function definition in
RollAxisAutopilot.c
and the declaration inRollAxisAutopilot.h
. You can also see the number of search hits in each file from the file menu in the upper left corner.Repeat these search steps to locate the initialize function,
RollAxisAutopilot_initialize
in the generated code.
Next, configure the data interface for code generation and review the generated code.