You can use Monitor and Tune (External Mode) action 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 the below communication interfaces when PX4 Host Target is selected as hardware board:
| Communication Interface | Description |
|---|---|
XCP on TCP/IP | In Universal Measurement and Calibration Protocol (XCP)-based
External mode simulation over TCP/IP, you can use:
|
TCP/IP | In External mode simulation over TCP/IP, use the Sources blocks to change parameter values and the Sink blocks to monitor the effects of parameter tuning. |
The support package supports Monitor and Tune simulation over the below communication interfaces when Pixhawk 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer is selected as hardware board:
Note
When Crazyflie 2.0 is selected as hardware board, only
Serial communication interface is
supported.
| Communication Interface | Description |
|---|---|
XCP on Serial | In Universal Measurement and Calibration Protocol (XCP)-based
External mode simulation over Serial, you can use:
|
Serial | In External mode simulation over Serial, use the Sources blocks to change parameter values and the Sink blocks to monitor the effects of parameter tuning. |
This section explains how to prepare a Simulink model to run in External mode.
Configure the Model Configuration Parameters to set the Hardware Board as one of the supported PX4 hardware boards, as explained in Model Configuration Parameters for PX4 Flight Controller.
In the Model Configuration Parameters dialog box, under Hardware board settings > Target Hardware Resources, select External Mode tab.
When PX4 Host Target is selected as hardware board, Communication
interface options for External Mode are XCP on
TCP/IP and TCP/IP:

When Pixhawk 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer
is selected as hardware board, Communication interface options for External
Mode are XCP on Serial and
Serial:

Ensure that you select the Verbose check box to view the external mode execution progress and updates in the Diagnostic Viewer or in the Command Window.
If the Communication interface is XCP on
Serial, specify the number of bytes to allocate for the buffer
in the hardware during simulation in the Logging buffer size (in
bytes) parameter. This parameter is available only when
Pixhawk 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer is
selected as hardware board.
Select the required hardware board serial port. This parameter is available only when Pixhawk 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer is selected as hardware board.
If the Serial port for External Mode communication on hardware board is a
port other than /dev/ttyACM0 mention the
corresponding COM port on host.
Click Apply and then OK to close the Model Configuration Parameters dialog box.
Connect the PX4 Autopilots hardware to your host computer.
Open the Simulink model and go to Hardware tab.
Set a value for the Simulation stop time parameter.
The default value is 10.0 seconds. To run the model for
an indefinite period, enter inf.
Click Monitor & Tune to run the model on external mode.
Simulink automatically:
Runs the model on the target hardware.
Runs the model on the host computer for Monitor and Tune operation.
Creates a real-time connection between the model on target hardware and the model on the host computer.

This section explains how to run:
Before you begin, complete the Prepare a Simulink Model for External Mode section.
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.
(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.
To start the simulation, open the Hardware tab and click the Monitor & Tune.

For instructions on logging the signal, see Mark a Signal for Logging.
After several minutes, Simulink starts running the model on the hardware.
During simulation, when new simulation data becomes available in SDI,
the Simulation Data Inspector button
appears highlighted.
View the simulation output in Sink blocks or in SDI.
Note
For XCP- Based External Mode over TCP/IP or Serial, it is recommended to use Signal Data Inspector (SDI) to view and log signals.
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:
Click the Simulation Data Inspector button.
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 .
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.
Change the parameter values in the model. Observe the corresponding changes in the simulation output.
Find the optimal parameter values by adjusting and observing the results in the Sink blocks.
After you are satisfied with the results, stop the Monitor and Tune action, and save the model.
Before you begin, complete the section Prepare a Simulink Model for External Mode and ensure that you have placed Sink blocks in the model to monitor the simulation output.
To start the simulation, open the Hardware tab and click the Monitor & Tune.

After few seconds, Simulink starts running the model on the hardware.
Change the parameter values in the model. Observe the corresponding changes in the simulation output.
Find the optimal parameter values by adjusting and observing the results in the Sink blocks.
After you are satisfied with the results, stop the Monitor and Tune action, and save the model.
To stop the model that is running in Monitor and Tune, open the
Hardware tab and click the Stop button
.
If the Simulation stop time parameter is set to a specific number of seconds, Monitor and Tune stops when that time elapses.
When you perform Monitor and Tune operation, you can use the Disconnect button to temporarily stop transferring the updated parameter values to the PX4 Autopilot. You can click Connect again to establish communication to send the updated values to PX4 Autopilot.
In some cases, the Monitor and Tune operation needs to be performed again even though there is no change to the Simulink model that was running on the PX4 Autopilot hardware. The reasons include:
Hardware reboot of PX4 Autopilot while Monitor and Tune is in progress
Disconnection of the port on the host computer while Monitor and Tune is in progress
You can resume Monitor and Tune operation again by:
Restarting the hardware board.
If Simulink code generated for Monitor & Tune in the previous run is already flashed on hardware, click Connect in Simulink instead of clicking Monitor & Tune again to establish External mode simulation. By clicking Connect, the Simulink code for the model is not generated again, and this avoids the time taken to build the code compared to the whole Monitor and Tune process.
