Access Multiple Model Instance C API Information
In certain scenarios, multiple instances of the same model can be used, such as when using instances as referenced models. Metadata information such as element data types and dimensions is consistent across all model instances. Your code accesses the model-specific metadata through the static C API map. In contrast, real-time values of model elements differ from one instance to another. Your code accesses the real-time data through the main C API map. All shared model data is accessible through a single, static model map, available to all instances. Meanwhile, each model instance accesses its unique, instance-specific data through its own private instance-specific map, serving as the exclusive gateway to this data. Instance-specific information is accessed by model instances through indices that are provided in shared C API arrays. The shared indices are identical to all instances of the same model, but each model instance uses them to reference entries in its own private copy of the instance-specific data arrays. In this way, the data separation principle is preserved.
Print Real-time Signal Values of Multiple Model Instances Example
This example demonstrates how to write code that uses instance-specific maps when there is more than one instance of the model.
The example code is also provided as standalone code (see Standalone C Code for Printing Real-time Signal Values of Multiple Model Instances Example) so that you can copy it more easily.
C Code for Printing Real-time Signal Values of Multiple Model Instances Example
Description | Sample C Code with Direct Data Structure Access | Sample C Code with Macros and Get-Functions |
---|---|---|
Include the generated model header so you can access the real-time
model object. Also include rtw_modelmap.h and
rtw_capi.h in which the structures you need to
access are defined. |
|
|
Declare variables to use for storing the map objects and the signals information. |
|
|
Get the pointer to the main C API map of the parent model. |
|
|
Get the information about the number of child models, and the pointer to the children C API map array. Then print the number of child models. |
|
|
Use a for -loop to iterate over the child
models. For each child model, get the signal count and the pointer to
the signal array of the child model. When using direct data access,
you first need to get a pointer to the static C API map of the child
model. |
|
|
Use the dataAddrMap field of the
childCapiMaps entries to get the pointers to
these (primitive type) data address arrays. |
|
|
Print the signal count for the current child model, then start
iterating over the signals. For each signal, get the
dataMapIdx to use for referencing into the
signal array. |
|
|
Use the dataMapIdx index to get the signal
value, and print the value. |
|
|
Here is a schematic illustration of the map structure that is accessed by the code in the example: