Data Structures in the Generated Code - MATLAB & Simulink (original) (raw)

The code generator organizes the data used by the model in data structures. For each model, the code generator stores data, such as signals and states, in global structures that are also referred to as standard data structures. Use these data structures to access the data in the generated code.

Real-time model (rtModel) data structure is a top level data structure that contains instance-specific data, which is used by the model for reusable functions. The code generator uses this data structure for only multi-instance code generation.

Standard Data Structures

In a model, root-level Inport and Outport blocks represent the primary inputs and outputs of the block algorithm. By default, the code generator aggregates these blocks into standard structures that store input and output data.

Signal lines, block parameters, states, and other model data appear in the generated code as fields of standard structures. For general information, see How Generated Code Stores Internal Signal, State, and Parameter Data.

The table shows some common data structures in the generated code. The name of the structure type and, if applicable, structure variable in the code that you generate from a model depends on the model settings. Regardless of the settings, the name of the structure type in the code contains the short name from the Short Name of Structure Type column in the table.

Data Structures Generated for a Model

Short Name of Structure Type Data Represented in the Structure
ExtU Data from root Inport blocks
ExtY Data from root Outport blocks
B Block output signals
ConstB Constant-value block output that could not be inlined
P Block parameters
ConstP Constant-value parameters that could not be inlined
DW Discrete block states
XDis Status of enabled subsystems
InstP Parameter arguments
SharedDSM Shared local data stores

You can exclude data from appearing in these structures by using:

Real-Time Model Data Structure

To access the fields of an S-function's simulation data structure (SimStruct), the Simulink® software provides a set of functions. The code generator replacesSimStruct with an optimized real-time model (rtModel) data structure.

Code generation by using the GRT and ERT system target files uses the real-time model data structure (rtModel). This structure encapsulates model-specific information in a much more compact form than the simulation structureSimStruct. Depending on the model, the model-specific information includes data such as internal data for reusable functions and error state.

When you set the Code interface packaging model configuration parameter to Reusable function, the code generator packages model data (such as block I/O, Dwork, and parameters) into the real-time model data structure. If the model contains blocks that depend on absolute or elapsed time, the code generator packages timer service code into the real-time model data structure regardless of the setting of the Code interface packaging parameter. The code generator passes the real-time model structure by reference to generated model entry-point functions. If you set Pass root-level I/O as toPart of model data structure, the code generator packages root-level model input and output into the real-time model data structure.

Efficient capabilities of the generated code depend on generation ofrtModel rather than SimStruct, including:

The real-time model data structure is defined in the main.c ormain.cpp file that is generated when you build your model. For ERT models, if you clear the model configuration parameter Generate an example main program, rtmodel.h file is generated. The rtmodel.h file contains #include directives required by the rt_main.c orrt_cppclass_main.cpp static main program module. Thert_main.c or rt_cppclass_main.cpp includesrtmodel.h to access model-specific data structures and entry points because the static main program module is not created at code generation time.

The code generator provides a set of macros (C) or methods (C++) for accessing thertModel data structure. These macros or methods are analogous to thessSetxxx and ssGetxxx macros that S-functions use to access SimStruct data, including noninlined S-functions compiled by the code generator.

Use the set of macros rtmGetxxx and rtmSetxxx or methods getxxx and getxxx to access the real-time model data structure. These macros or methods are used in the generated code as well as in the main.c or main.cpp module of your external code, respectively. If you are customizing main.c ormain.cpp (either a static or a generated file), usertmGetxxx and rtmSetxxx macros orgetxxx and setxxx methods instead of thessSetxxx and ssGetxxx macros.

Use the rtmGetxxx and rtmSetxxx macros orgetxxx and setxxx methods in the same way as you use the ssSetxxx and ssGetxxx versions, except that you replace SimStruct S by real-time model data structurertM. This table lists rtmGetxxx andrtmSetxxx macros that are used in rt_main.c, andgetxxx and setxxx methods that are used inrt_main.cpp. Here, rtm is a pointer to the real-time model data structure.

Macros and Methods for Accessing the Real-Time Model Data Structure

rtm Macro Syntax rtm Method Syntax Description
rtmGetdX(rtm) rtm->getdX() Get the derivatives of block continuous states
rtmGetOffsetTimePtr(rtm) rtm->getOffsetTimePtr() Return the pointer to vector that stores sample time offsets of the model associated with rtM
rtmGetNumSampleTimes(rtm) rtm->getNumSampleTimes() Get the number of sample times of a block
rtmGetPerTaskSampleHitsPtr(rtm) rtm->getPerTaskSampleHitsPtr() Return a pointer to NumSampleTime ×NumSampleTime matrix
rtmGetRTWExtModeInfo(rtm) rtm->getRTWExtModeInfo() Return an external mode information data structure of the model (used by code generator only for external mode)
rtmGetRTWLogInfo(rtm) rtm->getRTWLogInfo() Return a data structure used by code generator logging (used by code generator only)
rtmGetRTWRTModelMethodsInfo(rtm) rtm->getRTWRTModelMethodsInfo() Return a data structure of real-time model methods information (used by code generator only)
rtmGetRTWSolverInfo(rtm) rtm->getRTWSolverInfo() Return data structure containing model solver information (used by code generator only)
rtmGetSampleHitPtr(rtm) rtm->getSampleHitPtr() Return a pointer to Sample Hit flag vector
rtmGetSampleTime(rtm,TID) rtm->getSampleTime(int TID) Get task sample time
rtmGetSampleTimePtr(rtm) rtm->getSampleTimePtr() Get pointer to a task sample time
rtmGetSampleTimeTaskIDPtr(rtm) rtm->getSampleTimeTaskIDPtr() Get pointer to a task ID
rtmGetSimTimeStep(rtm) rtm->getSimTimeStep() Return simulation step type ID (MINOR_TIME_STEP,MAJOR_TIME_STEP)
rtmGetStepSize(rtm) rtm->getStepSize() Return the fundamental step size of the model
rtmGetT(rtm) rtm->getT() Get the current simulation time
rtmSetT(rtm,t) rtm->setT(time_T t) Set the time of the next sample hit
rtmGetTaskTime(rtm,tid) rtm->getTaskTime(int tid) Get the current time for the current task
rtmGetTFinal(rtm) rtm->getTFinal() Get the simulation stop time
rtmSetTFinal(rtm,finalT) rtm->setTFinal(time_T aTFinal) Set the simulation stop time
rtmGetTimingData(rtm) rtm->getTimingData() Return a data structure used by timing engine of the model (used by code generator only)
rtmGetTPtr(rtm) rtm->getTPtr() Return a pointer to the current time
rtmGetTStart(rtm) rtm->getTStart() Get the simulation start time
rtmIsContinuousTask(rtm) rtm->isContinuousTask() Determine whether a task is continuous
rtmIsMajorTimeStep(rtm) rtm->isMajorTimeStep() Determine whether the simulation is in a major step
rtmIsSampleHit(rtm,tid) rtm->isSampleHit(int tid) Determine whether the sample time is hit
rtmGetErrorStatus(rtm) rtm->getErrorStatus() Get the current error status
rtmSetErrorStatus(rtm,val) rtm->setErrorStatus(const char_T* const aErrorStatus) Set the current error status
rtmGetErrorStatusPointer(rtm) rtm->getErrorStatusPtr() Return a pointer to the current error status
rtmGetStopRequested(rtm) rtm->getStopRequested() Return whether a stop is requested
rtmSetStopRequested(rtm,val) rtm->setStopRequested(int val) Set the stop requested flag
rtmGetBlockIO(rtm) rtm->getBlockIO() Get the block I/O data structure
rtmSetBlockIO(rtm,val) rtm->setBlockIO(B_modelName_T* aBlockIO) Set the block I/O data structure
rtmGetContStates(rtm) rtm->getContStates() Get the continuous states data structure
rtmSetContStates(rtm,val) rtm->setContStates(X_modelName_T* aContStates) Set the continuous states data structure
rtmGetDefaultParam(rtm) rtm->getDefaultParam() Get the default parameters data structure
rtmSetDefaultParam(rtm,val) rtm->setDefaultParam(P_modelName_T* aDefaultParam) Set the default parameters data structure
rtmGetPrevZCSigState(rtm) rtm->getPrevZCSigState() Get the previous zero-crossing signal state data structure
rtmSetPrevZCSigState(rtm,val) rtm->setPrevZCSigState(PrevZCSigStates_modelName_T* aPrevZCSigState) Set the previous zero-crossing signal state data structure
rtmGetRootDWork(rtm) rtm->getRootDWork() Get the DWork data structure
rtmSetRootDWork(rtm,val) rtm->setRootDWork(DW_modelName_T* aRootDWork) Set the DWork data structure
rtmGetU(rtm) rtm->getU() Get the root inputs data structure (when root inputs are passed as part of the model data structure)
rtmSetU(rtm,val) rtm->setU(ExtU_modelName_T* aU) Set the root inputs data structure (when root inputs are passed as part of the model data structure)
rtmGetY(rtm) rtm->getY() Get the root outputs data structure (when root outputs are passed as part of the model data structure)
rtmSetY(rtm,val) rtm->setY(ExtY_modelName_T* aY) Set the root outputs data structure (when root outputs are passed as part of the model data structure)

For more information, see S-Function SimStruct Functions.