Example 1: Creating an External Model (Control Design and Simulation Module)
- Updated2023-03-14
- 2 minute(s) read
Third parties can use the External Model Interface (EMI) to create an external, or third-party, model in C/C++.
The following code illustrates an example of a C/C++ project that uses the External Model Interface functions to define the dynamic equations for a simple integrator. This code is similar to the EMI_Integrator example, accessible by navigating to the labview\examples\Control and Simulation\Simulation\External Model Interface\EMI_Integrator\Source directory and opening EMI_Integrator.c.
![]() |
Note In many cases, LabVIEW users do not need to create external models. Instead, use the External Model function in LabVIEW to simulate models you receive from a third party. Refer to Example 2 for an example of simulating an external model. |
#include "SIM_EMI_API.h"
void EMI_CB_ModelInterface(emiRef model){
EMI_SetModelName(model, "My Integrator");
EMI_AddScalarInput(model, "Input");
EMI_AddScalarParam(model, "Initial Value", EMI_ParamSource_ConfigPage, 0.0);
EMI_AddScalarOutput(model, "Output", EMI_Feedthrough_Indirect);
}
void EMI_CB_SizeInformation(emiRef model){
EMI_SetNumberOfContinuousStates(model, 1);
}
void EMI_CB_InitializeModel(emiRef model){
double* xc = EMI_GetInitialContinuousStates(model);
const double* p = EMI_GetParam(model, 0);
xc[0] = p[0];
}
void EMI_CB_CalculateDerivatives(emiRef model){
double* d = EMI_GetDerivatives(model);
const double* u = EMI_GetInput(model, 0);
d[0] = u[0];
}
void EMI_CB_CalculateIndirectOutputs(emiRef model){
double* y = EMI_GetOutput(model, 0);
const double* xc = EMI_GetContinuousStates(model);
y[0] = xc[0];
}
Notice that this file first includes the SIM_EMI_API.h header file. This header file is located in the labview\CCodeGen\Simulation directory.
The EMI_CB_ModelInterface function specifies information about the model, such as the number and names of the inputs and outputs. In this example, the EMI_CB_ModelInterface function specifies that the model has the following characteristics:
- model name of My Integrator
- one input named Input
- one parameter named Initial Value whose default source is the configuration dialog box of the External Model function and whose default value is 0.0
- one output named Output with indirect feedthrough behavior
The EMI_CB_SizeInformation function specifies information about the states and zero crossing signals of the model. In this example, the EMI_CB_SizeInformation function specifies that the model has one continuous state.
The EMI_CB_InitializeModel function initializes the continuous state to the value of the first element of the parameter. The EMI_CB_CalculateDerivatives and EMI_CB_CalculateIndirectOutputs functions calculate the derivatives of the continuous states and the indirect outputs of the model, respectively.
The C/C++ project you create must export the EMI_CB_ModelInterface function and any other Callbacks functions as appropriate for the structure of the model. In this example, you must export all five defined Callbacks functions.
After you create the model, build the C/C++ project into a (Windows) .dll, (macOS) .framework or .dylib, or (Linux) .so shared library file and distribute the shared library to end users.
LabVIEW users can reference this shared library from the External Model function and use the External Model function to represent and simulate the model in the Control Design and Simulation Module.
