Real-Time Workshop | ![]() ![]() |
Rapid Prototyping System-Independent Components
These components are collectively called system independent because all environments use the same application modules to implement these operations. This section steps through the model code (and if the model has continuous states, calls one of the numerical integration routines). This section also includes the code that defines, creates, and destroys the real-time model data structure (rtM
). The model code and all S-functions included in the program define their own SimStruct
.
The model code execution driver calls the functions in the model code to compute the model outputs, update the discrete states, integrate the continuous states (if applicable), and update time. These functions then write their calculated data to the real-time model.
Model Execution
At each sample interval, the main program passes control to the model execution function, which executes one step though the model. This step reads inputs from the external hardware, calculates the model outputs, writes outputs to the external hardware, and then updates the states.
The following diagram illustrates these steps.
Figure 7-7: Executing the Model
Note that this scheme writes the system outputs to the hardware before the states are updated. Separating the state update from the output calculation minimizes the time between the input and output operations.
Integration of Continuous States
The real-time program calculates the next values for the continuous states based on the derivative vector, dx/dt, for the current values of the inputs and the state vector.
These derivatives are then used to calculate the next value of the states using a state-update equation. This is the state-update equation for the first order Euler method (ode1
)
where h is the step size of the simulation, x represents the state vector, and
dx/dt is the vector of derivatives. Other algorithms may make several calls to the output and derivative routines to produce more accurate estimates.
Note, however, that real-time programs use a fixed-step size since it is necessary to guarantee the completion of all tasks within a given amount of time. This means that, while you should use higher order integration methods for models with widely varying dynamics, the higher order methods require additional computation time. In turn, the additional computation time may force you to use a larger step size, which can diminish the accuracy increase initially sought from the higher order integration method.
Generally, the stiffer the equations, (i.e., the more dynamics in the system with widely varying time constants), the higher the order of the method that you must use.
In practice, the simulation of very stiff equations is impractical for real-time purposes except at very low sample rates. You should test fixed-step size integration in Simulink to check stability and accuracy before implementing the model for use in real-time programs.
For linear systems, it is more practical to convert the model that you are simulating to a discrete time version, for instance, using the c2d
function in the Control System Toolbox.
Application Modules for System-Independent Components
The system independent components include these modules:
ode1.c
, ode2.c
, ode3.c
, ode4.c
, ode5.c
-- These modules implement the integration algorithms supported for real-time applications. See the Simulink documentation for more information about these fixed-step solvers.
rt_sim.c
-- Performs the activities necessary for one time step of the model. It calls the model function to calculate system outputs and then updates the discrete and continuous states.
simstruc.h
-- Contains actual definition of the Simulink data structure and the definition of the SimStruct
access macros.
simstruc_types.h
-- Contains definitions of various events, including subsystem enable/disable and zero crossings. It also defines data logging variables.
The system independent components also include code that defines, creates, and destroys the real-time model data structure. All S-functions included in the program define their own SimStruct
.
The SimStruct
data structure encapsulates all the data relating to an S-function, including block parameters and outputs. See Writing S-Functions for more information about the SimStruct
.
![]() | Rapid Prototyping System-Dependent Components | Rapid Prototyping Application Components | ![]() |