Configure Data Interface
Embedded Coder® reduces the effort for configuring data and function interfaces by providing a way to specify default configurations for categories of data elements and functions across a model. Applying default configurations can save time and reduce the risk of introducing errors in code, especially for larger models and models from which you generate multi-instance code. After applying default configurations, you can selectively override the default settings for individual data elements and functions.
Customize the data interface of model RollAxisAutopilot
by configuring
function RollAxisAutopilot_step
to:
Read input data from global variables that are declared and defined in external files
roll_input_data.h
androll_input_data.c
.Write output data to global variables that the code generator declares in
output_data.h
and defines inoutput_data.c
.
To make the changes, use these external code files in your current MATLAB® working folder.
roll_input_data.c
roll_input_data.h
roll_heading_mode.c
roll_heading_mode.h
The data interface configuration changes that you make depend on these files being accessible for code generation and the build process. The build process compiles the generated code with the code that is in these files.
Configure Default Code Generation for Data
Configure default code generation configurations for model inports and outports.
In the C Code tab, select Code Interface > Default Code Mappings.
Configure Inport blocks at the root level of the model to appear in the generated code as separate global variables defined by external code. In the Code Mappings editor, under Inports and Outports, select category Inports. Set the default storage class to
ImportFromFile
.With this setting, the generated code does not define global variables that represent the inport data. Instead, a
#include
statement includes a header file that declares the input variables. You specify the name of the header file with the Property Inspector.Click the icon and set HeaderFile to
roll_input_data.h
.To see how the
extern
declarations in external header fileroll_input_data.h
name the input variables, in the MATLAB Command Window, openroll_input_data.h
located in your current working folder.extern boolean_T AP_Eng; extern real32_T HDG_Ref; extern real32_T Rate_FB; extern real32_T Phi; extern real32_T Psi; extern real32_T TAS; extern real32_T Turn_Knob;
Configure the code generation naming rule for global variables. By default, the code generator names global variables with the prefix
rt
. For the code generator to produce code that matches the external variable declarations inroll_input_data.h
, configure the code generation naming rule for global variables accordingly.Open the Model Configuration Parameters dialog box. In the toolstrip, on the C Code tab, click Settings.
Navigate to the Code Generation > Identifiers pane.
Set parameter Global variables to the naming rule
$N$M
(remove thert
prefix). Token$N
represents the name of a data element in the model, for example, the name of an Inport or Outport block. Token$M
represents name-mangling text that the code generator inserts, if necessary, to avoid name collisions with other global variables in the code.Apply the change.
Configure Outport blocks at the root level of the model to appear in the generated code as separate global variables. In the Code Mappings editor, on the Data Defaults tab, for category Outports, set Storage Class to
ExportToFile
.The generated code declares and defines the output variables in header and definition files that you specify with the Property Inspector.
Click the icon and set HeaderFile to
roll_output_data.h
and DefinitionFile toroll_output_data.c
.Configure code generation for the model to include the external source files
roll_input_data.c
androll_heading_mode.c
. In the Configuration Parameters dialog box, set Code Generation > Custom Code > Code information > Source files toroll_input_data.c roll_heading_mode.c
. Then, click Apply and OK.Save the model. Regenerate the code by clicking Build.
A compiler error indicates that variable
HDG_Mode
is not declared. That variable is not declared in header fileroll_input_data.h
, which you declared as the default header file for inports. You fix this error in the next section of this tutorial.The model is configured to open the code generation report after code generation is complete. Minimize this report window for exploration later in this tutorial.
You configured Inport blocks to use an external header file to declare and define input variables. In the Code view, confirm that the generated code includes this external header file by searching for
roll_input_data.h
.Search for the root level Inport block name,
HDG_Ref
. As you type, choose the search suggestion with the greenV
icon. This search suggestion finds instances ofHDG_Ref
used as a variable in the generated code. Confirm thatHDG_Ref
is defined as a separate global variable.In the model,
RollAxisAutopilot
, click the Outport blockAil_Cmd
. Place your cursor over the ellipsis menu above the block and click Navigate To Code. The Code view highlights code inRollAxisAutopilot.c
that corresponds to the block. In the code, place your cursor over the output variableAil_Cmd
. The traceability dialog box displays variable definitions. The dialog box confirms thatAil_Cmd
is defined as a separate global variable. Click the definition code to see the definition inroll_output_data.c
.
Override Default Settings for Individual Data Elements
The settings that you choose for a category under Data Defaults apply to elements in that category across a model. To override the default settings for an individual data element, use the Code Mappings editor.
When you generated code after configuring default settings for inports and outports, a
compiler error indicated that variable HDG_Mode
is not declared. You can
fix that error by overriding the default configuration for Inport block
HDG_Mode
.
In the Code Mappings editor, on the Inports tab, select source
HDG_Mode
.Set Storage Class to
ImportFromFile
.Click the icon and set Identifier to
HDG_Mode
and Header File toroll_heading_mode.h
.Based on these settings, the code generator imports the declaration for external variable
HDG_Mode
from header fileroll_heading_mode.h
.extern boolean_T HDG_Mode;
Save the model and regenerate the code.
Minimize the code generation report window for exploration later in this tutorial.
In the Code view, search for
roll_heading_mode.h
and confirm that it is included in the generated code with the default configuration fileroll_input_data.h
.Search for
HDG_Mode
and confirm that it is defined as a separate global variable.
Next, configure a model parameter to be a global variable in the generated code. As a global variable, you can tune the parameter value at run time.