Real-Time Workshop    

Combining Multiple Models

If you want to combine several models (or several instances of the same model) into a single executable, Real-Time Workshop offers several options.

One solution is to use the S-function target to combine the models into a single model, and then generate an executable using either the GRT or GRT malloc targets. Simulink and Real-Time workshop implicitly handle connections between models, sequencing of calls to the models, and multiple sample rates. This is the simplest solution in many cases. See The S-Function Target for further information.

A second option, for embedded systems development, is to generate code from your models using the Real-Time Workshop Embedded Coder target. You can interface the model code to a common harness program by directly calling the entry points to each model. The Real-Time Workshop Embedded Coder target has certain restrictions that may not be appropriate for your application. For more information, see the Real-Time Workshop Embedded Coder documentation.

The GRT malloc target is a third solution. It is appropriate in situations where you want do any or all of the following:

This section discusses how to use the GRT malloc target to combine models into a single program. Before reading this section, you should become familiar with model execution in Real-Time Workshop programs. (See Program Architecture and Models with Multiple Sample Rates.) It will be helpful to refer to grt_malloc_main.c while reading these chapters.

The procedure for building a multiple-model executable is fairly straightforward. The first step is to generate and compile code from each of the models that are to be combined. Next, the makefiles for each of the models are combined into one makefile for creating the final multimodel executable. The next step is create a combined simulation engine by modifying grt_malloc_main.c to initialize and call the models correctly. Finally, the combination makefile links the object files from the models and the main program into an executable. Example Mutliple-Model Program Using the GRT_malloc Target discusses an example implementation.

Sharing Data Across Models

We recommend using unidirectional signal connections between models. This affects the order in which models are called. For example, if an output signal from modelA is used as input to modelB, modelA's output computation should be called first.

Timing Issues

You must generate all the models you are combining with the same solver mode (either all singletasking or all multitasking.) In addition, if the models employ continuous states, the same solver should be used for all models.

Since each model has its own model-specific definition of the rtModel data structure, an alternative mechanism must be used to control model execution. The file rtw/c/src/rtmcmacros.h provides an rtModel API clue that can be used to call the rt_OneStep procedure. The rtmcmacros.h header file defines the rtModelCommon data structure which has the minimum common elements in the rtModel structure required to step a model forward one time step. The #define rtmcsetCommon populates an object of type rtModelCommon by copying the respective similar elements in the model's rtModel object. Your main routine must create one rtModelCommon structure for each model being called by the main routine. The main routine will subsequently invoke rt_OneStep with a pointer to the rtModelCommon structure instead of a pointer to the rtModel structure.

If the base rates for the models are not the same, the main program (such as grt_malloc_main) must set up the timer interrupt to occur at the greatest common divisor rate of the models. The main program is responsible for calling each of the models at the appropriate time interval.

Data Logging and External Mode Support

A multiple-model program can log data to separate MAT-files for each model (as in the example program discussed below.)

Only one of the models in a multiple-model program can use external mode.

Example Mutliple-Model Program Using the GRT_malloc Target

An demonstration of combining multiple-models, distributed with Real-Time Workshop, is located at matlabroot/toolbox/rtw/rtwdemos. This example combines two models, multimallockP (a plant model) and multimallocK (a controller model). Both models have the same base rate and the same number of sample times. Each model logs outputs and simulation time to a separate model.mat file. The plant model also logs states. You can run the demo by typing

at the MATLAB prompt. The interface for multimallocdemo is shown below.

Double-click the models to see each system's blocks and how they work together. Double-click on the blue labels in order from top to bottom to generate code, and see how main programs and makefiles were modified to combine the two models.

When reviewing the differences between grt_malloc_main.c and combine_malloc_main.c, search for comments containing "customize". The string "customize" denotes regions in the main routine which you must change in order to customize this file to work with your models.


  Guidelines for Implementing the Transport Layer DSP Processor Support