Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Windows Target   

Using Real-Time Applications

About Real-Time Applications

You create a real-time application to let your system run while synchronized to a real-time clock. This allows your system to control or interact with an external system. This is necessary if you use your system to stabilize a physical plant. The first step is to create a Simulink Model, as described in the previous section, Using Simulink Models.

Entering Simulation Parameters

After you create a Simulink model, you can enter simulation parameters for use by Real-Time Workshop code generation software for creating C code and building a real-time application.

This procedure uses the Simulink model rtwin_model.mdl as an example and assumes you have already loaded that model:

  1. In the Simulink window, and from the Simulation menu, click Configuration Parameters.

  2. Click the Real-Time Workshop node.

    The Real-Time Workshop pane opens.

  3. In the Target selection section, click the Browse button at the RTW system target file list.

    The System Target File Browser opens.

  4. Select the system target file for building a Real-Time Windows Target application, and click OK.

    The dialog automatically enters the system target file rtwin.tlc, the template makefile rtwin.tmf, and the make command make_rtw into the Real-Time Workshop pane.

    If you have the Real-Time Workshop® Embedded Coder™ product, you can build an ERT target application. To build an ERT target application, in the Target selection section, click the Browse button at the System target file list. Click rtwinert.tlc, and then click OK.

    Although not visible in the Real-Time Workshop pane, when you click OK you also configure the external target interface MEX file rtwinext. This allows external mode to pass new parameters to the real-time application and to return signal data from the real-time application. The data is displayed in Scope blocks or saved with signal logging.

    Your Real-Time Workshop pane looks similar to the next figure.

    Do not select Inline parameters on the Optimization node. Inlining parameters is used for custom targets when you want to reduce the amount of RAM or ROM with embedded systems. Also, if you select inlining parameters, you disable the parameter tuning feature. Since PCs have more memory than embedded systems, we recommend that you do not inline parameters.

  5. Click the Hardware Implementation node. The following values are set by default:

    • Device vendorGeneric

    • Device type32-bit x86 compatible

    • Emulation hardwareNone

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

Entering Scope Parameters for Signal Tracing

You enter or change scope parameters to format the x-axis and y-axis in a Scope window. Other parameters include the number of graphs in a one Scope window and whether the scope is connected to a continuous or discrete model.

If you entered the scope parameters for running a simulation, you can skip this procedure. This information is repeated here if you did not run a simulation.

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.

    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 similar to the next figure.

  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. From the menu, click Axes Properties.

    The Scope properties: axis 1 dialog box opens.

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

  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.

Creating a Real-Time Application

The Real-Time Workshop code generation software creates C code from your Simulink model, then the Open Watcom C/C++ compiler compiles and links that C code into a real-time application.

After you enter parameters into the Configuration Parameters dialog box for use by the Real-Time Workshop code generation software, you can build a real-time application. This procedure uses the Simulink model rtwin_model.mdl as an example, and assumes you have loaded that model:

  1. In the Simulink window, from the Tools menu, point to Real-Time Workshop, and then click Build Model.

    The build process does the following:

    • The Real-Time Workshop code generation software creates the C code source files rtwin_model.c and rtwin_model.h.

    • The make utility make_rtw.exe creates the makefile rtwin_model.mk from the template makefile rtwin.tmf.

    • The make utility make_rtw.exe builds the real-time application rtwin_model.rwd using the makefile rtwin_model.mk created above. The file rtwin_model.rwd is a binary file that we refer to as your real-time application. You can run the real-time application with the Real-Time Windows Target kernel.

  2. Connect your Simulink model to your real-time application. See Entering Additional Scope Parameters for Signal Tracing.

After you create a real-time application, you can exit the MATLAB environment, start the MATLAB environment again, and then connect and run the executable without having to rebuild.

Entering Additional Scope Parameters for Signal Tracing

Simulink external mode connects your Simulink model to your real-time application. This connection allows you to use the Simulink block diagram as a graphical user interface to your real-time application.

After you have created a real-time application, you can enter scope parameters for signal tracing with Simulink external mode:

  1. In the Simulation window, and from the Simulation menu, click Configuration Parameters.

  2. Select the Real-Time Windows Target node.

    The Real-Time Windows Target pane opens.

  3. Select the External mode check box.

    The MEX-file name label should have an entry of rtwinext. The MEX-file rtwinext.mex* is supplied with the Real-Time Windows Target software to work with Simulink external mode and support uploading signal data and downloading parameter values.

    The Real-Time Windows Target pane should appear as follows.

  4. Click OK.

  5. In the Simulation window, and from the Tools menu, click External Mode Control Panel.

    The External Mode Control Panel dialog box opens.

  6. Click the Signal & Triggering button.

    The External Signal & Triggering dialog box opens.

  7. Select the Select all check box. From the Source list, choose manual. From the Mode list, choose normal.

    The X under Signal selection indicates that a signal is tagged for data collection, and T indicates that the signal is tagged as a trigger signal.

  8. In the Duration field, enter the number of sample points in a data buffer. For example, to specify a sample rate of 1000 samples/second and a stop time of 10 seconds, enter

    10001
    
  9. Select the Arm when connecting to target check box.

    If you do not select this check box, data is not displayed in the Scope window.

    The External Signal & Triggering dialog box looks like this:

  10. Do one of the following:

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

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

    You must click the Apply or Close button on the External Signal & Triggering dialog box for the changes you made to take effect. Generally it is not necessary to rebuild your real-time application.

Running a Real-Time Application

You run your real-time application to observe the behavior of your model in real time with the generated code. The process of connecting consists of

After you build the real-time application, you can run your model in real time. This procedure uses the Simulink model rtwin_model.mdl as an example, and assumes you have created a real-time application for that model:

  1. From the Simulation menu:

    • Select External mode simulation.

    • Choose Connect To Target.

      (You can connect to the target from the toolbar by clicking  ).

    The MATLAB Command Window displays the message

    Model rtwin_model loaded
    
  2. From the Simulation menu, choose Start Real-Time Code.

    (You can also start the code from the toolbar by clicking  ).

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

    In this example, the Scope window displays 1000 samples in 1 second, increases the time offset, and then displays the samples for the next 1 second.

      Note   Transfer of data is less critical than calculating the signal outputs at the selected sample interval. Therefore, data transfer runs at a lower priority in the remaining CPU time after real-time application computations are performed while waiting for another interrupt to trigger the next real-time application update. The result may be a loss of data points displayed in the Scope window.

  3. Do one of the following:

    • Let the execution run until it reaches the stop time.

    • From the Simulation menu, click Stop Real-time Code.

    The real-time application stops.

  4. In the Simulation window, and from the Simulation menu, click Disconnect From Target.

  5. From the Simulation menu, click External.

    The MATLAB Command Window displays the message

    Model rtwin_model unloaded

Running an Application from the Command Line

You can use the MATLAB command-line interface as an alternative to using the Simulink GUI. Enter commands directly in the MATLAB Command Window or enter them in an M-file.

After you build the real-time application, you can run your model in real time. This procedure uses the Simulink model rtwin_model.mdl as an example, and assumes you have created a real-time application for that model:

  1. In the MATLAB Command Window, type

    set_param(gcs,'SimulationMode','external')
    

    The Simulink software changes to external mode.

  2. Type

    set_param(gcs,'SimulationCommand','connect')
    

    The MATLAB environment loads the real-time application, connects it to the Simulink block diagram, and displays the message

    Model rtwin_model loaded
    
  3. Type

    set_param(gcs,'SimulationCommand','start')
    

    The Simulink software starts running the real-time application.

  4. Type

    set_param(gcs,'SimulationCommand','stop')
    

    The Simulink software stops the real-time application.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS