Configure Model to Add C API Signals in Code Generation
In this step of the example, you configure the model to include the C API elements for model signals and model parameters, in the generated model code.
Open the model CapiGetStarted
.
capiMdl = "CapiGetStarted";
open_system(capiMdl);
Enable C API Code Generation for Model Signals and Model Parameters
To enable C API code generation for model signals and model parameters:
Open the Configuration Parameters dialog box.
Navigate to the Code Generation > Interface pane.
In the Data exchange interface > Generate C API for section, select signals and parameters.
Click OK.
Alternatively, enter this in the Command Window:
set_param(capiMdl,RTWCAPISignals=true) set_param(capiMdl,RTWCAPIParams=true)
Configure Signals as Test Points
To prevent the code generator from optimizing the signals away, configure them to be test points (see Configure Signals as Test Points):
In the model, select the signals.
Open the Signal tab of the Simulink® canvas.
In the Monitor group, click Test Point.
Alternatively, use the capiGetStartHelper
MATLAB® program to programmatically configure the signals as test points.
capiGetStartHelper("signals")
Specify Model Parameters Storage Class
To prevent the code generator from optimizing the parameters away, specify their Storage Class as Model default
.
Open the Code Mappings editor (see Open the Code Mappings Editor – C) and select the Parameters tab.
Specify Storage Class for each parameter as
Model default
.
Alternatively, use the capiGetStartHelper
MATLAB® program to programmatically set the storage classes of the parameters.
capiGetStartHelper("parameters")
Generate Model Code
Use the slbuild
command to generate code from your model. Use evalc
to suppress the output of the slbuild
command.
evalc("slbuild(capiMdl,GenerateCodeOnly=true)");
Verify That Signals and Parameters Are Included in C API
The C API structure arrays that corresponds to model signals and model parameters are rtBlockSignals
and rtModelParameters
, respectively. To view the instantiation code for these structures, examine the generated C API source file CapiGetStarted_capi.c
(located in the folder CapiGetStarted_grt_rtw
).
This is the instantiation code for the structures in this file:
/* Block output signal information */ static const rtwCAPI_Signals rtBlockSignals[] = { /* addrMapIndex, sysNum, blockPath, * signalName, portNumber, dataTypeIndex, dimIndex, fxpIndex, sTimeIndex */ { 0, 0, TARGET_STRING("CapiGetStarted/Amplifier_1"), TARGET_STRING("mySig1"), 0, 0, 0, 0, 0 }, { 1, 0, TARGET_STRING("CapiGetStarted/Amplifier_2"), TARGET_STRING("mySig2"), 0, 0, 0, 0, 0 }, { 0, 0, (NULL), (NULL), 0, 0, 0, 0, 0 } }; /* Tunable variable parameters */ static const rtwCAPI_ModelParameters rtModelParameters[] = { /* addrMapIndex, varName, dataTypeIndex, dimIndex, fixPtIndex */ { 2, TARGET_STRING("Amp_coef_1"), 0, 0, 0 }, { 3, TARGET_STRING("Amp_coef_2"), 0, 0, 0 }, { 0, (NULL), 0, 0, 0 } };
In the next step of the example, you write external code that interacts with the generated C API code. To continue to the next step of the example, use this at the Command Window:
openExample("simulinkcoder/CapiGetStarted03Example")