Use Simulink Models

A Simulink® model is a graphical representation of your physical system. You create a Simulink model for a non-real-time simulation of your system, and then you use the Simulink model to create a real-time application.

Create a Model

You need to create a Simulink model before you can run a simulation or create a real-time application. This procedure explains how to create a simple Simulink model. You can use this model as an example to learn other procedures that are useful with Real-Time Windows Target™ software.

  1. In the MATLAB® Command Window, type

    simulink
    

    The Simulink Library Browser opens. The left pane shows a hierarchy of libraries and block categories, with the Simulink library at the top. The right pane shows the blocks available in the category selected on the left.

  2. Choose File > New > Model, or click the New model button on the toolbar.

    An empty Simulink window opens.

  3. In the left pane of the Simulink Library Browser window, select Simulink > Sources. Click and drag a Signal Generator block from the browser to the Simulink window.

    Select Continuous. Click and drag a Transfer Fcn block to the Simulink window.

    Select Sinks. Click and drag a Scope block to the Simulink window.

  4. Connect the Signal Generator output to the Transfer Fcn input by clicking-and-dragging a line between the blocks. Likewise, connect the Transfer Fcn output to the Scope input.

  5. Double-click the Transfer Fcn block. The Block Parameters dialog box opens. In the Numerator text box, enter:

    [10000]
    

    In the Denominator text box, enter

    [1 70 10000]
    

    Your Block Parameters dialog box looks like this:

  6. Click OK.

  7. Double-click the Signal Generator block. The Block Parameters dialog box opens. From the Wave form list, select square.

    In the Amplitude text box, enter

    1

    In the Frequency text box, enter

    20

    From the Units list, select rad/sec.

    Your Block Parameters dialog box looks like this:

  8. Click OK.

    The completed Simulink block diagram looks like this, with toolbar and status bar not shown:

  9. From the File menu, click Save As. The Save As dialog box opens. In the File name text box, enter a file name for your Simulink model and click Save. For example, type

    rtwin_model
    

    The Simulink software saves your model in the file rtwin_model.

To specify a default Real-Time Windows Target configuration set for your model, see Specify a Default Configuration Set. If you activate this configuration set for your model, you can build your real-time application later without setting additional configuration parameters.

To manually configure your model, continue to Enter Configuration Parameters Manually, following. That section teaches you how to enter configuration parameters for your Simulink model, then leads you into procedures for entering scope parameters and running a non-real-time simulation of the model.

Reference a Model

The Real-Time Windows Target software supports model referencing. See Overview of Model Referencing.

File System Input/Output

When run in external mode, the Real-Time Windows Target software does not include a file system. Therefore, a Simulink model intended for use in a Real-Time Windows Target application cannot use blocks, such as the To File or From File block, that generate file I/O calls such as fopen or fprintf.

If a Real-Time Windows Target model contains a block that tries to perform file system I/O, an error could occur when you try to compile the model, generate code for it, or use external mode with it. Even if no error occurs, the block has no effect on either simulation or code execution.

You can use file I/O blocks in Simulink normal mode simulation in real time. In this case, the file I/O is not carried out by the real-time kernel, but by Simulink itself. To access files, use the Packet Input, Packet Output, Stream Input, or Stream Output blocks and select the driver Standard Devices > File.

To log signal data without needing a file system, use the techniques described in Log Signals to File. See Run Application from Simulink for information about using external mode to execute a Real-Time Windows Target application.

Configure a Model

After you create a Simulink model, you can enter configuration parameters for the model. These parameters control many properties of the model for simulation and code generation. This section contains the essential information you need when setting configuration parameters for a Real-Time Windows Target application. For information about Simulink configuration , see Manage a Configuration Set.

A configuration set is a named set of values for model parameters, such as solver type and simulation start or stop time. Every new model is created with a default configuration set, called Configuration, that initially specifies default values for the model's model parameters. You can subsequently create additional configuration sets and associate them with the model, as described in Manage a Configuration Reference.

The easiest way to specify configuration parameters for a Real-Time Windows Target model is to programmatically assign the default Real-Time Windows Target configuration set, as described in Specify a Default Configuration Set. You can also set parameters manually, as described in Enter Configuration Parameters Manually. Other sections describe setting configuration patterns for specific purposes.

Specify a Default Configuration Set

After you create a Simulink model, you can use the rtwinconfigset function to specify a default Real-Time Windows Target configuration set for the model. In most cases, using rtwinconfigset provides the configuration parameter values that the model needs. The following procedure uses the Simulink model rtwin_model as an example and assumes you have already loaded that model (see Create a Model):

  1. If you have not already saved the model, from the File menu, click Save As. The Save As dialog box opens. In the File name text box, enter a file name for your Simulink model and click Save. For example, type

    rtwin_model
    

    The Simulink software saves your model in the file rtwin_model.

  2. In the MATLAB Command Window, type

    rtwinconfigset('rtwin_model')
    

    The default Real-Time Windows Target configuration set, RTWin, is now active for the rtwin_model model. (Alternatively, you can set the default Real-Time Windows Target configuration set by setting the Configuration Parameters System target file option to rtwin.tlc.) You do not need to perform other configuration for a Real-Time Windows Target application.

  3. Save the model.

See Create Real-Time Application for a description of how to build your Real-Time Windows Target application.

To revert to the default configuration set, Configuration, or other configuration set you have for the model, use Model Explorer. This is an alternative tool that you can use to enter simulation parameters for a model. See the Simulink documentation for a description of how to use Model Explorer.

    Note:   Your model uses a Real-Time Windows Target configuration set when you change the System target file value to a Real-Time Windows Target one, such as rtwin.tlc or rtwinert.tlc. The software creates the Real-Time Windows Target configuration set, RTWin or RTWinERT, only if one does not already exist.

Enter Configuration Parameters Manually

The configuration parameters give information to Simulink software for running a simulation. This procedure uses the Simulink model rtwin_model as an example and assumes you have already loaded that model:

  1. In the Simulink window, and from the Simulation menu, click Model Configuration Parameters. In the Configuration Parameters dialog box, click the Solver tab.

    The Solver pane opens.

  2. In the Start time field, enter 0.0. In the Stop time field, enter the amount of time you want your model to run. For example, enter 10.0 seconds.

  3. From the Type list, choose Fixed-step. Simulink Coder™ code generation software does not support variable step solvers.

  4. From the Solver list, choose a solver. For example, choose the general purpose solver ode5 (Dormand-Prince).

  5. In the Fixed step size field, enter a sample time. For example, enter 0.001 seconds for a sample rate of 1000 samples/second.

  6. From the Tasking Mode list, choose SingleTasking. (For models with blocks that have different sample times, choose MultiTasking.)

    Your Solver pane looks like this:

  7. Do one of the following:

    • Click Apply to apply the changes to your model and leave the dialog box open.

    • Click OK to apply the changes to your model and close the dialog box.

Enter Scope Parameters for Signal Tracing

You enter or change scope parameters to specify the x-axis and y-axis in a Scope window. Other properties include the number of graphs in one Scope window and the sample time for models with discrete blocks.

After you add a Scope block to your Simulink model, you can enter the scope parameters for signal tracing:

  1. In the Simulink window, double-click the Scope block.

    A Scope window opens.

  2. Click the Parameters button   on the toolbar.

    A Scope parameters dialog box opens.

  3. Click the General tab. In the Number of axes field, enter the number of graphs you want in one Scope window. For example, enter 1 for a single graph. Do not select the Floating scope check box.

    In the Time range field, enter the upper value for the time range. For example, enter 1 second. From the Tick labels list, choose all.

    From the Sampling list, choose Sample time and enter 0 in the text box. Entering 0 indicates that the Simulink software evaluates this block as a continuous time block. If you have discrete blocks in your model, enter the Fixed step size you entered in the Configuration Parameters dialog box.

    Your Scope parameters dialog box looks like this:

  4. Do one of the following:

    • Click Apply to apply the changes to your model and leave the dialog box open.

    • Click OK to apply the changes to your model and close the dialog box.

  5. In the Scope window, point to the y-axis and right-click.

  6. From the pop-up menu, click Axes Properties.

  7. The Scope properties: axis 1 dialog box opens. In the Y-min and Y-max text boxes, enter the range for the y-axis in the Scope window. For example, enter -2 and 2.

    Your Scope axis dialog box looks like this:

  8. Do one of the following:

    • Click Apply to apply the changes to your model and leave the dialog box open.

    • Click OK to apply the changes to your model and close the dialog box.

Run Simulation

You use Simulink normal mode to run a non-real-time simulation. Running a simulation lets you observe the behavior of your model in nonreal time.

After you load your Simulink model into the MATLAB workspace, you can run a simulation. This procedure uses the Simulink model rtwin_model as an example and assumes you have loaded that model:

  1. In the Simulink window, double-click the Scope block.

    The Simulink software opens a Scope window with an empty graph.

  2. Select Simulation > Mode > Normal.

  3. Select Simulation > Run to begin simulation or click the Run button   on the toolbar.

    The Simulink software runs the simulation and plots the signal data in the Scope window.

    During the simulation, the Scope window displays the samples for one time range, increases the time offset, and then displays the samples for the next time range.

  4. Do one of the following:

    • Let the simulation run to the stop time.

    • From the Simulation menu, click Stop.

    The simulation stops.

Was this topic helpful?