Main Content

Tune and Monitor Model Running on Hardware

This example shows you how to use the External mode (Monitor and Tune) to tune parameters and monitor a Simulink® model running on your target hardware.

Monitor and Tune enables you to tune model parameters and evaluate the effects of different parameter values on model results in real-time. When you change parameter values in a model, the modified parameter values are communicated to the target hardware immediately. You can monitor the effects of different parameter values by viewing the output signals on Sink blocks or in Simulation Data Inspector (SDI). Doing so helps you find the optimal values for performance. This process is called parameter tuning.

Monitor and Tune accelerates parameter tuning. You do not have to rerun the model each time you change parameters. You can also use Monitor and Tune to develop and validate your model using the actual data and hardware for which it is designed. This software-hardware interaction is not available solely by simulating a model.

The support package supports Monitor and Tune simulation over this communication interface:

Model

Open the bbcmicrobit_xcp_monitor_and_tune model.

Prepare a Simulink Model for External Mode

This section explains how to prepare a Simulink model to run in External mode (Monitor and Tune).

1. Create or open a Simulink model.

2. In the Simulation tab of the Simulink model, set the simulation Stop Time parameter. The default value is 10.0 seconds. To run the model for an indefinite period, enter Inf.

3. Configure the Model Configuration Parameters for the hardware as described in Model Configuration Parameters for Simulink Support Package for BBC micro:bit.

4. When you set the Communication interface to an XCP-based External mode, the Set logging buffer size automatically parameter becomes available. Select this parameter to automatically set the number of bytes to preallocate for the buffer in the hardware during simulation. By default, the Set logging buffer size automatically parameter is selected. If you clear this parameter, Logging buffer size (in bytes) parameter becomes available, where you can manually specify the memory buffer size for XCP-based External mode simulation.

5. You can send multiple contiguous samples in same packet to enhance signal logging performance in models containing signals of high sample rates. To do so, click Hardware tab, in the Prepare gallery, select Control Panel and then click Signal & Triggering. In the External & Signal Triggering dialog box, select Send multiple contiguous samples in same packet.

Signal Monitoring and Parameter Tuning of Simulink Model

This section explains how to run XCP-Based External Mode Simulation over Serial Connection.

Before you begin, complete the Prepare a Simulink Model for External Mode section.

1. In the Simulink model, identify the signals to be logged for monitoring during simulation. Select the identified signal, open its context menu, and click the icon corresponding to Enable Data Logging.

For instructions on logging the signal using other methods, refer to Mark a Signal for Logging. Simulink displays a logged signal indicator for each logged signal.

2. (Optional) Place one or more Sink blocks in the model, and then mark the signals connected to them also for logging. For example, connect Display or Scope blocks and mark the signals connected to them for logging.

3. To start simulation, on the Hardware tab of the Simulink model, in the Mode section, click Monitor & Tune.

If none of the signals in the model is marked for logging, the MATLAB Command Window displays a warning message. For example:

Warning: No data has been selected for uploading.

In C:\Program Files (x86)\MATLAB\R2022b Student1\toolbox\realtime\realtime\+realtime\extModeAutoConnect.p>extModeAutoConnect at 17

In C:\Program Files (x86)\MATLAB\R2022b Student1\toolbox\realtime\realtime\sl_customization.p>myRunCallback at 149

You can disregard this warning or mark signals for logging. For instructions on logging the signal, see Mark a Signal for Logging.

After a while, Simulink starts running the model on the hardware.

4. View the simulation output in Sink blocks or in SDI.

  • Sink blocks - To view the simulation output, double-click the Sink blocks in the model.

  • SDI - To view the new simulation data, perform these steps:

a. On the *Simulation* tab, under *Review Results*, click *Data Inspector*.

b. A new simulation run appears in the *Inspect* pane. The *Inspect* pane lists all logged signals in rows, organized by simulation run. You can expand or collapse any of the runs to view the signals in a run. For more information on signal grouping, see <https://www.mathworks.com/help/simulink/ug/configure-simulation-data-inspector.html#mw_38527952-67be-47d2-a3eb-99fc039f69b0 Signal Grouping>.
We recommend you use SDI rather than using Sink blocks for the following reasons:
- Streaming data to SDI does not store data in memory, making more efficient use of the memory available on the hardware. Sink blocks such as Scope stores data in buffers before sending the data to the host.
- Using SDI, you can stream signals from top models and reference models simultaneously. Scope blocks can only log signals from a top-level model.

5. Change the parameter values in the model. Observe the corresponding changes in the simulation output.

6. Find the optimal parameter values by making adjustments and observing the results in the Sink blocks.

7. After you are satisfied with the results, stop the Monitor and Tune action, and save the model.

Note: Monitor and Tune action increases the processing burden of the model running on the board. If the software reports an overrun, stop the Monitor and Tune action.

Stop Monitor and Tune

To stop the model that is running in Monitor and Tune, on the Hardware tab, click Stop.

If the simulation Stop Time parameter is set to a specific number of seconds, Monitor and Tune stops when that time elapses.