Documentation

Create and Run Real-Time Application from Simulink Model

This tutorial begins with a non-real-time Simulink® model of a damped oscillator, ex_slrt_nrt_osc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_nrt_osc')))). It assumes that you know how to create, configure, and simulate such a model.

As you go through the tutorial, you transform ex_slrt_nrt_osc into a Simulink Real-Time™ model configured to build as a real-time application. You then build, download, and execute the real-time application on the target computer.

The final model is ex_slrt_rt_osc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_rt_osc')))):

Transform Simulink Model to Real-Time Application

To run a Simulink model as a real-time application under Simulink Real-Time, add and configure a real-time Scope block and set configuration parameters for code generation and target execution.

Add Simulink Real-Time Scope Block

Simulink Real-Time supports a real-time Scope block. There are three types of Simulink Real-Time scopes: target, host, and file. This tutorial uses the target scope, which displays execution data on the target computer monitor.

  1. In the Command Window, type ex_slrt_nrt_osc.

    MATLAB® loads the oscillator model and displays the Simulink block diagram.

  2. In Simulink Editor, from the View menu, click Library Browser.

    The Simulink Library opens.

  3. In the left pane, browse to and double-click node Simulink Real-Time.

    A list of I/O block categories opens.

  4. Click node Displays and Logging.

  5. Click and drag block Scope to the Simulink block diagram.

    Simulink adds a new real-time Scope block to the model with a scope identifier of 1.

  6. Connect the Mux output to the Scope block input.

  7. From the File menu, click Save As. Enter a file name. For example, enter ex_slrt_ucf_osc and then click OK.

    The unconfigured model is ex_slrt_ucf_osc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_ucf_osc')))).

Set Target Scope Block Parameters

Scope block parameters define the signals to trace on the scope and trigger modes. The Simulink Real-Time Scope block dialog box changes depending on which scope type you are configuring: target, host, or file. For this tutorial, configure a target scope.

For more information about real-time target Scope parameters, see Configure Real-Time Target Scope Blocks.

  1. In the Command Window, type ex_slrt_ucf_osc.

    MATLAB loads the oscillator model and displays the Simulink block diagram.

  2. Double-click block Scope.

  3. Select Scope type Target. This value means that the scope display appears on the target computer monitor.

  4. In the block parameters dialog box, select the Start scope when application starts check box.

    This setting is mandatory in Stand Alone mode because the development computer is not available to issue a command to start scopes.

  5. Select Scope mode Graphical redraw.

  6. Select the Grid check box.

  7. Type [0,0] in the Y-axis limits text box. This value means that display scaling is auto.

  8. Type 1000 in the Number of samples text box. For a Scope mode of Graphical redraw, this value means that 1000 samples are acquired before the graph is redrawn.

  9. Type 0 in the Number of pre/post samples text box. This value means that samples are not saved before a trigger or skipped after a trigger.

  10. Type 1 in the Decimation text box. This value means that data is collected at each sample interval.

  11. Select Trigger mode FreeRun. This value means that the trigger event is automatic and no external trigger specification is required.

    The target scope dialog box looks like this figure.

  12. Click OK.

  13. From the File menu, click Save.

Set Configuration Parameters

The example model, ex_slrt_ucf_osc, is a non-real-time model of a damped oscillator. You enter the simulation and real-time run parameters in the Configuration Parameters dialog box. These parameters give information to Simulink Coder™ on how to build the real-time application from the Simulink model.

After you open a Simulink model and start the target computer, you can enter the simulation parameters.

  1. In the Command Window, type ex_slrt_ucf_osc.

  2. In Simulink Editor, click Simulation > Model Configuration Parameters.

  3. In the Configuration Parameters dialog box, click the Solver node.

  4. In the Solver options section, from the Type list, select Fixed-step.

  5. From the Solver list, select a solver. For example, select the general-purpose solver ode4 (Runge-Kutta).

  6. Under Additional Options, in the Fixed-step size (fundamental sample time) box, enter the sample time for the real-time application. For example, enter 0.00025 seconds (250 microseconds). You can change this value after creating the real-time application.

    If you find that a value overloads the CPU on the target computer, try a larger Fixed-step size value, such as 0.0002 seconds.

    The sample times of the model blocks can only be multiples of Fixed-step size. If you enter 'auto' in Fixed-step size, the fundamental sample time is calculated from the sample times of the model blocks.

  7. In the Configuration Parameters dialog box, click the Code Generation node.

    The code generation pane opens.

  8. To build a basic real-time application, in the Target selection section, click Browse at the System target file list. Click slrt.tlc, and then click OK.

    The system target file slrt.tlc, the template makefile slrt_default_tmf, and the make command make_rtw are automatically entered into the page. The Simulink Real-Time Options node appears in the left pane.

  9. Click OK.

  10. From the File menu, click Save As.

    Enter a file name. For example, enter ex_slrt_rt_osc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_rt_osc')))).

Start Target Computer

Starting the target computer loads and starts the Simulink Real-Time kernel on the target computer. The loader then waits for the Simulink Real-Time software to download the real-time application from the development computer.

After you have configured the Simulink Real-Time product using the Simulink Real-Time Explorer and created a target boot disk for that setup, you can start the target computer. Before building the real-time application, you must start the target computer because the build process automatically downloads the real-time application to the target computer. Be sure that you have followed the instructions from System Configuration.

  1. Insert the target boot disk into the target computer disk drive.

  2. Turn on the target computer or press the Reset button.

    The target computer displays a screen like this screen.

    If you have a keyboard attached to the target computer, you can activate that keyboard by typing C. Press the Page Up and Page Down keys to page up and down in the target computer monitor.

The status window shows that the kernel is in loader mode and waiting to load a real-time application. The memory value is the number of bytes of target computer memory available for the heap, for running scopes, and for data acquisition buffers.

Note

Target computer memory for the real-time application executable, the kernel, and other uses is limited to a maximum of 4 GB.

Build and Download Real-Time Application

The example model is a real-time model of a damped oscillator, ex_slrt_rt_osc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_rt_osc')))). You use the Simulink Real-Time build process to generate C code, compile, link, and download the real-time application to the target computer.

After you enter changes in the Configuration Parameters dialog box, you can build the real-time application. By default, the build procedure downloads the real-time application to the default target computer, as designated in Simulink Real-Time Explorer. For further details on setting the target computer for a real-time application, see Simulink Real-Time Options Pane.

  1. In the Command Window, type ex_slrt_rt_osc.

    MATLAB loads the oscillator model and displays the Simulink block diagram.

  2. In Simulink Editor, on the toolbar, click the Build Model button on the toolbar.

    On the development computer, after completing a build without detecting an error, MATLAB displays lines like the following:

    ### Starting Simulink Real-Time build procedure for model: 
    ex_slrt_rt_osc
    . . .
    ### Successful completion of Simulink Real-Time build procedure for 
    model: ex_slrt_rt_osc

    After compiling, linking, and downloading the real-time application, Simulink Real-Time creates a target object in the MATLAB workspace. The default name of the target object is tg. For more information about the target object, see Real-Time Application Objects.

    If you have a monitor connected to your target computer, the monitor screen looks like this screen.

  3. In the Command Window, type:

    tg

    MATLAB displays a list of properties for the target object tg.

If the software detects an error during build and download, see Troubleshooting in Simulink Real-Time.

If you download a real-time application built with a Simulink Real-Time version different from the version of the kernel on the target computer, the software prints an error:

Mismatch between model and kernel versions

To prevent this version mismatch, rebuild real-time applications with each new Simulink Real-Time release.

During the build process, the Simulink Real-Time software creates a target object that represents the real-time application running on the target computer. You control the real-time application and computer by setting the target object properties and calling target object functions.

Execute Real-Time Application with Simulink External Mode

Control of the real-time application with Simulink is limited to connecting a Simulink model to a real-time application through external mode, and then starting the real-time application. Using Simulink external mode is one method to tune parameters. By default, the model connects to the default target computer, as specified in Simulink Real-Time Explorer.

Note

Do not use Simulink external mode while Simulink Real-Time Explorer is running. Use only one interface or the other.

After you build and download a real-time application to the target computer, you can run the real-time application. This procedure uses the Simulink model ex_slrt_rt_osc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_rt_osc')))). See Build and Download Real-Time Application. You must already have specified the required target computer environment on the Simulink Real-Time Options node of the Simulink Real-Time parameters dialog box. In particular, you must specify the target computer to which you want to connect. See the Build for default target computer check box description in Simulink Real-Time Options Pane.

  1. Select Simulation > Mode > External.

    A check mark appears next to the menu item External. Simulink external mode is activated. Simulink external mode connects the Simulink model to the real-time application as a simple user interface.

  2. On the toolbar, click the Connect To Target button.

    The current Simulink model parameters are downloaded from the development computer to the real-time application.

  3. On the toolbar, click the Run button.

    The real-time application begins running. The Simulink Scope block show output like the figure.

  4. To stop execution, on the toolbar, click the Stop button .

See Also

| | |

Was this topic helpful?