Real-Time Workshop | ![]() ![]() |
Rapid Prototyping Model Functions
The rapid prototyping code defines the following functions that interface with the run-time interface:
Model()
-- The model registration function. This function for initializes the work areas (e.g., allocating and setting pointers to various data structures) needed by the model. The model registration function calls the MdlInitializeSizes
and MdlInitializeSampleTimes
functions. These two functions are very similar to the S-function mdlInitializeSizes
and mdlInitializeSampleTimes
methods.
MdlStart(void)
-- After the model registration functions, MdlInitializeSizes
and MdlInitializeSampleTimes
execute, the run-time interface starts execution by calling MdlStart
. This routine is called once at startup.
MdlStart
has four basic sections:
MdlOutputs(int_T tid)
-- MdlOutputs
updates the output of blocks at appropriate times. The tid
(task identifier) parameter identifies the task that in turn maps when to execute blocks based upon their sample time. This routine is invoked by the run-time interface during major and minor time steps. The major time steps are when the run-time interface is taking an actual time step (i.e., it is time to execute a specific task). If your model contains continuous states, the minor time steps will be taken. The minor time steps are when the solver is generating integration stages, which are points between major outputs. These integration stages are used to compute the derivatives used in advancing the continuous states.
MdlUpdate(int_T tid)
-- MdlUpdate
updates the discrete states and work vector state information (i.e., states that are neither continuous nor discrete) saved in work vectors. The tid
(task identifier) parameter identifies the task that in turn indicates which sample times are active allowing you to conditionally update states of only active blocks. This routine is invoked by the run-time interface after the major MdlOutputs
has been executed.
MdlDerivatives(void)
-- MdlDerivatives
returns the block derivatives. This routine is called in minor steps by the solver during its integration stages. All blocks that have continuous states have an identical number of derivatives. These blocks are required to compute the derivatives so that the solvers can integrate the states.
MdlTerminate(void)
-- MdlTerminate
contains any block shutdown code. MdlTerminate
is called by the run-time interface, as part of the termination of the real-time program.
The contents of the above functions are directly related to the blocks in your model. A Simulink block can be generalized to the following set of equations.
Output, y, is a function of continuous state, xc, discrete state, xd, and input, u. Each block writes its specific equation in the appropriate section of MdlOutput
.
The discrete states, xd, are a function of the current state and input. Each block that has a discrete state updates its state in MdlUpdate
.
The derivatives, x, are a function of the current input. Each block that has continuous states provides its derivatives to the solver (e.g., ode5
) in MdlDerivatives
. The derivatives are used by the solver to integrate the continuous state to produce the next value.
The output, y, is generally written to the block I/O structure. Root-level Outport blocks write to the external outputs structure. The continuous and discrete states are stored in the states structure. The input, u, can originate from another block's output, which is located in the block I/O structure, an external input (located in the external inputs structure), or a state. These structures are defined in the model
.h
file that Real-Time Workshop generates.
Figure 7-3 shows the general content of the rapid prototyping style of C code.
Figure 7-3: Content of model.c for the Rapid Prototyping Code Style
Figure 7-4 shows a flow chart describing the execution of the rapid prototyping generated code.
Figure 7-4: Rapid Prototyping Execution Flow Chart
Each block places code into specific Mdl
routines according to the algorithm that it is implementing. Blocks have input, output, parameters, and states, as well as other general items. For example, in general, block inputs and outputs are written to a block I/O structure (rtB
). Block inputs can also come from the external input structure (rtU
) or the state structure when connected to a state port of an integrator (rtX
), or ground (rtGround
) if unconnected or grounded. Block outputs can also go to the external output structure (rtY
). The following figure shows the general mapping between these items.
Figure 7-5: Data View of the Generated Code
rtB
) -- This structure consists of all block output signals. The number of block output signals is the sum of the widths of the data output ports of all nonvirtual blocks in your model. If you activate block I/O optimizations, Simulink and Real-Time Workshop reduce the size of the rtB
structure by:
Structure field names are determined by either the block's output signal name (when present) or by the block name and port number when the output signal is left unlabeled.
rtX
) contains the continuous state information for any blocks in your model that have continuous states. Discrete states are stored in a data structure called the DWork vector (rtDWork)
.
rtP
) -- The parameters structure contains all block parameters that can be changed during execution (e.g., the parameter of a Gain block).
rtU
) --The external inputs structure consists of all root-level Inport block signals. Field names are determined by either the block's output signal name, when present, or by the Inport block's name when the output signal is left unlabeled.
rtY
) --The external outputs structure consists of all root-level Outport blocks. Field names are determined by the root-level Outport block names in your model.
rtRWork
, rtIWork
, rtPWork
) -- Blocks may have a need for real, integer, or pointer work areas. For example, the Memory block uses a real work element for each signal. These areas are used to save internal states or similar information.
![]() | Rapid Prototyping and Embedded Model Execution Differences | Embedded Model Functions | ![]() |