Real-Time Workshop    

Tutorial 1: Building a Generic Real-Time Program

This tutorial walks through the process of generating C code and building an executable program from the demonstration model. The resultant stand-alone program runs on your workstation, independent of external timing and events.

Working and Build Directories

It is convenient to work with a local copy of the f14 model, stored in its own directory, f14example. This discussion assumes that the f14example directory resides on drive d:. Set up your working directory as follows:

  1. Create the directory from the MATLAB command line by typing
  1. or

  1. Make f14example your working directory (drive d: used as example).
  2. Open the f14 model.
  1. The model appears in the Simulink window.

  1. From the File menu, choose Save As. Save a copy of the f14 model
    as
    d:/f14example/f14rtw.mdl.

Be aware that during code generation, Real-Time Workshop creates a build directory within your working directory. The build directory name is model_target_rtw, derived from the name of the source model and the chosen target. The build directory stores generated source code and other files created during the build process. We examine the build directory and its contents at the end of this tutorial.

Setting Program Parameters

To generate code correctly from the f14rtw model, you must change some of the simulation parameters. In particular, note that generic real-time (GRT) and most other targets require that the model specify a fixed-step solver.

To set parameters, use the Simulation Parameters dialog box as follows:

  1. From the Simulation menu, choose Simulation Parameters. The Simulation Parameters dialog box opens.
  2. Click the Solver tab and enter the following parameter values on the Solver pane.
  1. Start Time: 0.0

    Stop Time: 60

    Solver options: set Type to Fixed-step. Select the ode5 (Dormand-Prince) solver algorithm.

    Fixed step size: 0.05

    Mode: SingleTasking

  1. Click Apply. Then click OK to close the dialog box.
  2. Save the model. Simulation parameters persist with the model, for use in future sessions.

The Solver pane with the correct parameter settings is shown below.

Selecting the Target Configuration

To specify the desired target configuration, you choose a system target file, a template makefile, and a make command.

In these tutorials, you do not need to specify these parameters individually. Instead, you use the ready-to-run generic real-time target configuration. The GRT target is designed to build a stand-alone executable program that runs on your workstation.

To select the GRT target:

  1. From the Simulation menu, choose Simulation Parameters. The Simulation Parameters dialog box opens.
  2. Click on the Real-Time Workshop tab of the Simulation Parameters dialog box. The Real-Time Workshop pane activates.
  3. The Real-Time Workshop pane has several parts, which are selected via the Category menu. Select Target configuration from the Category menu, as shown below.

    .

  4. Click the Browse button next to the System target file field. This opens the System Target File Browser, illustrated below. The browser displays a list of all currently available target configurations. When you select a target configuration, Real-Time Workshop automatically chooses the appropriate system target file, template makefile, and make command.

  5. From the list of available configurations, select Generic Real-Time Target (as shown above) and then click OK.
  6. The Real-Time Workshop pane now displays the correct system target file (grt.tlc), template makefile (grt_default_tmf), and make command (make_rtw), as shown below.
  7. Select General code generation options from the Category menu. The options displayed here are common to all target configurations. Make sure that all options are set to their defaults, as shown below.

  8. Select GRT code generation options from the Category menu. The options displayed here are specific to the GRT target. Make sure that all options are set to their defaults, as below.

  9. Select TLC debugging from the Category menu. Make sure that all options in this category are cleared.
  10. Select Target configuration from the Category menu. Make sure that the Generate code only option is not selected.
  11. Save the model.

Building and Running the Program

The Real-Time Workshop build process generates C code from the model, and then compiles and links the generated program. To build and run the program:

  1. Click the Build button in the Simulation Parameters dialog box to start the build process.
  2. A number of messages concerning code generation and compilation appear in the MATLAB command window. The initial messages are
  1. The content of the succeeding messages depends on your compiler and operating system.The final message is

  1. The working directory now contains an executable, f14rtw.exe (on PC), or f14rtw (on UNIX). In addition, a build directory, f14rtw_grt_rtw, has been created.
  1. To observe the contents of the working directory after the build, type the dir command from the MATLAB command window.

  1. To run the executable from the MATLAB command window, type
  1. The "!" character passes the command that follows it to the operating system, which runs the stand-alone f14rtw program.

    The program produces one line of output:

  1. Finally, to see the contents of the build directory, type

Contents of the Build Directory

The build process creates a build directory and names it model_target_rtw, concatenating the name of the source model and the chosen target. In this example, the build directory is named f14rtw_grt_rtw.

f14rtw_grt_rtw contains these generated source code files:

The build directory also contains other files used in the build process, such as the object (.obj) files and the generated makefile (f14rtw.mk).


  Quick Start Tutorials Tutorial 2: Data Logging