Generate C++ Code by Using Embedded Coder Quick Start
Prepare the CppClassWorkflowKeyIgnition
model for embedded code generation by using the Embedded Coder® Quick Start tool. The Quick Start tool chooses fundamental code generation settings based on your goals and application.
For this step of the tutorial, you generate code for the CppClassWorkflowKeyIgnition
model, and then inspect the generated files. The CppClassWorkflowKeyIgnition
model represents an ignition system for a vehicle.
Generate C++ Code by Using Quick Start Tool
Open the model
CppClassWorkflowKeyIgnition
.openExample("CppClassWorkflowKeyIgnition.slx")
Save a copy of the model to a writable location on the MATLAB search path.
If the C++ Code tab is not already open, on the Apps tab, in the Apps gallery, under Code Generation, click Embedded Coder.
On the C++ Code tab, click Settings and select C/C++ Code generation settings.
The Configuration Parameters dialog box opens.
On the Configuration Parameters dialog box, open the Code Generation pane, and under Target selection, verify the parameter Language is set to
C++
. Click OK to close the dialog box.On the C++ Code tab, click Quick Start.
Advance through the Quick Start tool by clicking Next in each step.
Each step asks questions about the code that you want to generate. For this tutorial, use the defaults. The tool validates your selections against the model and presents the parameter changes required to generate code.
In the last step of the Quick Start tool (the Generate Code step), apply the proposed changes and generate code from
CppClassWorkflowKeyIgnition
by clicking Next.Click Finish, then return to the C++ Code tab.
Inspect Generated C++ Code
The code generator converts the CppClassWorkflowKeyIgnition
model into a C++ class, which you access from your application code. The model data elements appear as class members. The Simulink® functions appear as class methods.
The CppClassWorkflowKeyIgnition
model incorporates elements of rate-based and export-function modeling. For rate-based modeling, the entry-point class methods, which you call from your application code, include an initialization method, an execution method, a terminate method, and, optionally, a reset method. To integrate with external code or interface requirements, you can customize the generated class interface.
For this tutorial, the code generated by using the Quick Start tool uses default settings for the class elements. The default name for the C++ class is the model name CppClassWorkflowKeyIgnition
.
To inspect the C++ class information generated for the model:
On the right side of the Simulink Editor window, in the Code view, locate the search bar.
In the search bar, type the model class name
CppClassWorkflowKeyIgnition
to find each instance of the class name across the generated code, and then click the highlighted search suggestion.Use the arrows on the right under the search bar to step through each instance, including the class definition in
CppClassWorkflowKeyIgnition.h
and the class instantiation inert_main.cpp
.You can also see the number of search results in each file from the file menu in the upper-left corner.
Review the data and function code mappings.
In the Simulink Editor window, click Code Interface, and select Code Mappings to open the Code Mappings editor.
Click the Data tab to view class member visibility and access methods.
Simulink data elements are grouped into categories of modeling elements:
Inports: Root-level data input ports of a model.
Outports: Root-level data output ports of a model.
Model parameter arguments: Workspace variables that appear as instance (nonstatic) class data members.
Model parameters: Workspace variables that are shared across instances of the model class that are generated as static class data members.
Signals, states, and internal data: Data elements that are internal to a model, such as block output signals, discrete block states, data stores, and zero-crossing signals.
Click the Functions tab to view class methods.
The generated class methods are entry-point methods and locations in code where a transfer of program control occurs.
The default name for the initialize class method is
initialize
, the execution (step) methods arestep0
andstep1
, and the terminate method isterminate
.Repeat the search steps to locate class methods
initialize
,step0
,step1
,f
, andterminate
in the generated code.
Next, configure a customized class interface for code generation and review the generated code.