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 (Simulink) blocks or in Simulation Data Inspector (SDI) (Simulink). 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 XCP on TCP/IP 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 (Simulink) blocks: In addition to Sources (Simulink) and Sink (Simulink) blocks, you can use Dashboard (Simulink) 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) (Simulink): You can inspect and compare data from multiple simulations to validate model designs using Simulation Data Inspector.

The support package supports Monitor and Tune simulation over XCP on Serial when Pixhawk® 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer is selected as hardware board:

Communication InterfaceDescription
XCP on SerialIn Universal Measurement and Calibration Protocol (XCP)-based External mode simulation over Serial, you can use:
  • Dashboard (Simulink) blocks: In addition to Sources (Simulink) and Sink (Simulink) blocks, you can use Dashboard (Simulink) 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) (Simulink): You can inspect and compare data from multiple simulations to validate model designs using Simulation Data Inspector.

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 is XCP on 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 is XCP on 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. Select Use the same host serial port for External mode as used for firmware upload parameter to set the same host serial port for External mode as the one used for serial port for firmware upload. By default, this parameter is selected. If you clear this parameter, Host Serial port parameter becomes available, where you can explicitly define the serial port for External mode on the host computer. Use the same host serial port for External mode as used for firmware upload and Host Serial port parameters are available only when Pixhawk 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer is selected as hardware board.

  5. If the communication interface is XCP on Serial, 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. Set logging buffer size automatically and Logging buffer size (in bytes) parameters are available only when Pixhawk 1, Pixhawk 2.1 (Cube), Pixhawk 4, Pixhawk Series, or Pixracer is selected as hardware board.

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

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

    Note

    If the serial port used for Monitor and Tune communication on hardware board is a port other than the USB port, then make sure that PX4 is not using that port. For example, MAVLink is enabled by default in TELEM1 port and if you want to use the TELEM1 port for Monitor and Tune communication, then disable MAVlink on TELEM1. To do so, set the parameter MAV_0_CONFIG to 0 on QGC. Similarly, to use the GPS1 port, set the parameter GPS_1_CONFIG to 0.

  8. Ensure that Enable MAVLink on /dev/ttyACM0 is cleared if the hardware board serial port that is selected for External Mode Communication is /dev/ttyACM0

    Enable MAVLink

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

  10. If the communication interface is XCP on Serial or XCP on TCP/IP, 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, perform these steps.

    1. Click Hardware tab, in the Prepare gallery, select Control Panel.

      Open Control Panel

    2. In the External Mode Control Panel screen click Signal & Triggering.

      Control Panel

    3. In the External & Signal Triggering dialog box, select Send multiple contiguous samples in same packet and enter a value for Duration.

      This enables sending multiple contiguous samples in a single packet whose length is the value specified for Duration parameter.

      Packed mode

    4. Click Apply and OK.

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. On the Hardware tab, in the Mode section, select Run on board and then 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 Signals for Logging (Simulink). Simulink displays a logged signal indicator for each logged signal.

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

  3. To start the simulation, on the Hardware tab, in the Mode section, select Run on board and then click Monitor & Tune.

    For instructions on logging the signal, see Mark Signals for Logging (Simulink).

    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 Signal Groups (Simulink).

      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 (Simulink) 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.

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.