Real-Time Workshop User's Guide    

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

    or

  1. Make f14example your working directory.
  2. Open the f14 model.

    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, the 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 the Real-Time Workshop requires the use of 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 page.

    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: Single Tasking

  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.

Figure 1-9 shows the Solver page with the correct parameter settings.

Figure 1-9: Solver Page of Simulation Parameters Dialog Box

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 (GRT) 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 page activates.
  3. The Real-Time Workshop page has several sub-pages, which are selected via the Category pull-down menu. Select Target configuration from the Category menu.

Figure 1-10: Real-Time Workshop Page (Target Configuration Category)

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

Figure 1-11: The System Target File Browser

  1. From the list of available configurations, select Generic Real-Time Target (as in Figure 1-11) and then click OK.
  2. The Real-Time Workshop page now displays the correct system target file (grt.tlc), template makefile (grt_default_tmf), and make command (make_rtw), as in Figure 1-10.
  3. Select General code generation options from the Category menu. The options displayed here are common to all target configurations. Check to make sure that all options are set to their defaults, as below.

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

  5. Select TLC debugging from the Category menu. Make sure that all options in this category are deselected.
  6. Select Target configuration from the Category menu. Make sure that the Generate code only option is deselected.
  7. 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

    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.

    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:

    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).


 Getting Started: Basic Concepts and Tutorials Tutorial 2: Data Logging