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 (open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_nrt_osc')))). To complete the tutorial, you must already 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 (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 MATLAB Command Window, type ex_slrt_nrt_osc.

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

  2. In Simulink Editor, from the Real-Time tab Prepare section, click Library Browser.

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

    A list of I/O block categories opens.

  4. From the list of I/O block categories, 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. On the Simulation tab, from the Save button, 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 (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 MATLAB 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. In the Y-axis limits text box, type [0,0]. This value means that the display scaling is auto.

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

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

  10. In the Decimation text box, type 1. 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, and then from the Simulation tab, 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 provide 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 MATLAB Command Window, type ex_slrt_ucf_osc.

  2. In Simulink Editor, from the Real-Time tab, click Hardware Settings.

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

  4. In the Solver selection 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 Solver details, 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). After creating the real-time application, you can change this value.

    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 be multiples only 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.

  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. In the Simulation tab, from the Save button, click Save As.

    Enter a file name. For example, enter ex_slrt_rt_osc (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 by using the Simulink Real-Time Explorer, you can start the target computer and establish the development computer connection to the target computer. Before building the real-time application, you start the target computer. After the build process completes, you download the real-time application to the target computer.

  1. Start the target computer.

  2. Observe that the target computer displays a Simulink Real-Time screen.

  3. If you have a keyboard attached to the target computer, you can activate that keyboard or console for target computer commands by typing C. Press the Page Up and Page Down keys to page up and down in the target computer monitor. For more information, see Target Computer Commands.

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 by Using Run on Target

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

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 MATLAB Command Window, type ex_slrt_rt_osc.

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

  2. In Simulink Editor, from the Real-Time tab, click Run on Target.

    On the development computer, you can view the build status after completing a build in the Diagnostics Viewer. On the Debug tab, click Diagnostics.

    If you have a monitor connected to your target computer, the monitor screen displays a Simulink Real-Time screen with the application name.

  3. In the MATLAB Command Window, type:

    tg = slrt('TargetPC1')

    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. For a list of these functions, such as load, see SimulinkRealTime.target.

Execute Real-Time Application in Simulink External Mode by Using Step by Step Commands

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.

The model connects to the target computer that you have selected in the Simulink Editor on the Real-Time tab.

Note

To avoid target computer communication issues, do not use Simulink external mode while Simulink Real-Time Explorer is running. Use only one interface.

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 (open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_rt_osc')))). See Build and Download Real-Time Application by Using Run on Target. You must already have specified the required target computer environment. Specifically, you must select 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. In the Simulink Editor, connect to the target computer. In the Real-Time tab, click Toggle target computer connection . The connection status toggles to the connected state .

  2. In the Real-Time tab, from the Run on Target, start the Step by Step Commands by selecting Build Application.

  3. After the model build completes, in the Real-Time tab, from the Run on Target, select Deploy to Target.

  4. After the real-time application deploys to the target, in the Real-Time tab, from the Run on Target, select Connect Model.

    After you connect the model to the real-time application, the status bar displays External to indicate that the model is ready for external mode simulation. In this mode, the model serves as the interface to the application. This connection permits tuning parameters in the real-time application without rebuilding the application. This connection also permits observing signal activity in the real-time application by using, for example, SimulinkScope blocks in the model.

  5. In the Real-Time tab, from the Start Application (Run on Target), select Start Application.

    The current Simulink model parameters are downloaded from the development computer to the real-time application. The real-time application begins running, and the Simulink Scope block displays output.

  6. To stop execution, in the Real-Time tab, from the Stop Application (Run on Target), select Stop Application.

When the real-time application deployed to the target computer, the Step by Step Commands provide several additional workflow options.

Commands

Step by Step Operations

In the Step by Step Commands, after you deploy the real-time application to the target computer, you can connect the model to the real-time application. The Connect Model operation lets you use the model as an interface to the real time application for external mode simulation.

In the Step by Step Commands, after you connect the model to the real-time application on the target computer, you can disconnect the model from the real-time application. The Disconnect Model operation lets you keep the real-time simulation running on the target computer and re-connect to the model at a later time.

In the Step by Step Commands, after you deploy the real-time application to the target computer and connect the model to the real-time application, you can start the real-time application. The Start Application operation lets you start the real time application for external mode simulation.

When you are connected in external mode, to restart the real-time application from time t=0, click the Restart Application button. This button restarts the application and maintains the external mode connection.

See Also

| | | | |

Related Topics