Main Content

Monitor and Tune the Model Running on PX4 Autopilots

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 InterfaceDescription
XCP on TCP/IPIn Universal Measurement and Calibration Protocol (XCP)-based External mode simulation over TCP/IP, you can use:
  • Dashboard blocks: In addition to Sources and Sink blocks, you can use Dashboard blocks to change parameter values and to monitor the effects of parameter tuning. The Dashboard library contains set of blocks using which you can interactively control and visualize the model.

  • Simulation Data Inspector (SDI): You can inspect and compare data from multiple simulations to validate model designs using Simulation Data Inspector.

TCP/IPIn 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 InterfaceDescription
XCP on SerialIn Universal Measurement and Calibration Protocol (XCP)-based External mode simulation over Serial, you can use:
  • Dashboard blocks: In addition to Sources and Sink blocks, you can use Dashboard blocks to change parameter values and to monitor the effects of parameter tuning. The Dashboard library contains set of blocks using which you can interactively control and visualize the model.

  • Simulation Data Inspector (SDI): You can inspect and compare data from multiple simulations to validate model designs using Simulation Data Inspector.

SerialIn External mode simulation over Serial, use the Sources blocks to change parameter values and the Sink blocks to monitor the effects of parameter tuning.

Prepare a Simulink Model for External Mode

This section explains how to prepare a Simulink model to run in External mode.

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

  2. 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:

    PX4 Host Target selected as hardware board

    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:

    PX4 Pixhawk 1 selected as hardware board

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

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

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

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

  7. Click Apply and then OK to close the Model Configuration Parameters dialog box.

Running the Simulink Model for Monitor and Tune

  1. Connect the PX4 Autopilots hardware to your host computer.

  2. Open the Simulink model and go to Hardware tab.

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

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

Signal Monitoring and Parameter Tuning of Simulink Model

This section explains how to run:

XCP-Based External Mode Simulation over TCP/IP or Serial

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

  4. 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:

      1. Click the Simulation Data Inspector button.

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

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

  6. Find the optimal parameter values by adjusting 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.

External Mode Simulation over TCP/IP or Serial

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.

  1. To start the simulation, open the Hardware tab and click the Monitor & Tune.

    After few seconds, Simulink starts running the model on the hardware.

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

  3. Find the optimal parameter values by adjusting and observing the results in the Sink blocks.

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

Stop Monitor and Tune

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.

Performing Disconnect and Connect

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.

Performing Connect Operation to Run an Unchanged Simulink Model on Hardware

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.