Tune Parameters with Simulink External Mode

You use Simulink® external mode to connect your Simulink model to your real-time application. The model becomes a user interface to your real-time application. You set up the Simulink interface in external mode to establish a communication channel between your Simulink model and your real-time application.

In Simulink external mode, when you change parameters in the Simulink model, Simulink downloads those parameters to the real-time application while it is running. You can change parameters in your program without rebuilding the Simulink model to create a new real-time application.

Note

Simulink Real-Time™ does not support parameters of multiword data types.

After you download your real-time application to the target computer, you can connect your Simulink model to the real-time application. This procedure uses the Simulink model xpcosc. You must have already built and downloaded the real-time application for that model.

  1. Open model xpcosc.

  2. Connect to the target computer. On the Real-Time tab, toggle the Disconnected indicator to Connected.

  3. Build and download the real-time application to the target computer. On the Real-Time tab, click Run on Target.

    The real-time application begins running on the target computer, and the target computer displays the following message:

    System: execution started (sample time: 0.000250)
  4. From the Simulation block diagram, double-click the block labeled Gain1

  5. In the Block Parameters: Gain1 parameter dialog box, the Gain text box, enter 800. Click OK.

    When you change a MATLAB® variable and click OK, the changed parameters in the model are downloaded to the real-time application.

  6. To stop the simulation, on the Real-Time tab click Stop.

  7. Disconnect to the target computer. On the Real-Time tab, toggle the Connected indicator to Disconnected.

    The Simulink model is disconnected from the real-time application. If you then change a block parameter in the Simulink model, the real-time application does not change.

Tuning with Batch Mode and Update All Parameters

By using Batch Mode, you can tune multiple parameters and apply the tuning changes at once, instead of tuning one parameter at a time. This example uses model xpcosc (matlab: open_system(docpath(fullfile(docroot, 'toolbox', 'rtw', 'targets', 'xpc', 'xpcdemos', 'xpcosc')))).

  1. Open model xpcosc.

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

  3. On the Real-Time tab, click Prepare > Batch Mode. The editor remains in batch mode until you click Batch Mode again.

    To set parameter values, you can either set values by clicking on each block or by using the Model Data Editor.

  4. On the Real-Time tab, click Prepare > Signal Table.

  5. In the Model Data Editor, click the Parameters tab. Modify parameters values in the Model Data Editor.

  6. On the Real-Time tab, Prepare > Update All Parameters.

  7. To stop the simulation before it ends, on the Real-Time tab, click Stop.

Related Topics