Main Content

Getting Started with Connected I/O for PX4 Autopilot

This example shows you how to enable and use Connected I/O simulation to read data from PX4 Autopilot during normal mode simulation.

Introduction

The UAV Toolbox Support Package for PX4 Autopilots enables you to communicate with the PX4 Autopilot during normal mode simulation by enabling Connected I/O. Connected I/O is not based on Simulink code generation, hence Embedded coder license is not required to use Connected I/O simulation. For more information and list of supported blocks see Communicate with Hardware Using Connected I/O.

In this example, you will:

  • Enable Connected I/O simulation

  • Observe the accelerometer and Read Parameter data from PX4 Autopilot by running the model in Normal mode simulation

Prerequisites

Task 1 - Configure and Run a Model to Read Accelerometer Data Using Accelerometer Block

In this task, you will configure a Simulink model to read accelerometer sensor data from PX4 Autopilot using an Accelerometer block. You will also observe the data over Connected I/O simulation.

1. From the MATLAB toolstrip, select Home > New > Simulink Model to open the Simulink Start Page. Click Blank Model to launch a new Simulink model.

2. On the Simulation tab, click Library Browser. In the Library Browser, select UAV Toolbox Support Package for PX4 Autopilots > PX4 Sensor Blocks, and then add an Accelerometer block to the model.

3. In the Library Browser, Select Simulink >, then drag and drop four Display blocks to the model. Connect the three outputs of the Accelerometer block to the three Display blocks. Connect the Status output to the fourth Display block as shown in the image below.

4. On the Modeling tab, click Model Settings to open the Configuration Parameters dialog box.

5. In the Configuration Parameters dialog box, select Hardware Implementation.

  • Verify that the Hardware board parameter is set to a required PX4 board.

6. On the Modeling tab, set the StopTime to inf.

7. To run this model in the Connected I/O mode, on the Hardware tab, in the Mode section, select Connected IO and then click Run with IO.

  • Wait for the code generation to be completed. If the dialog box appears instructing you to reconnect the flight controller to the serial port, ensure that you click OK on the dialog box within 5 seconds after reconnecting the flight controller. This dialog box will not appear if the connected I/O server is flashed onto the hardware.

After the Pixhawk series controller is flashed, wait for the signal monitoring to start.

  • Once the Simulation begins, you can view the values in the display.

8. Stop the simulation after observing values. Do not close the PX4 Autopilot and jMAVSim windows.

Task 2 - Add PX4 Read Parameter Block to the Configured Model to View Read Parameter Values

Since Connected I/O is not based on Simulink code generation, the PX4 Autopilot connected during Task 1 is independent of the model. If you add or delete blocks or modify the model, Simulink connects to the already connected PX4 Autopilot and reads data for updated model. For example, add PX4 Read Parameter block to the earlier configured model in task 1 and run the simulation again.

1. On the Simulation tab, click Library Browser. In the Library Browser, Select UAV Toolbox Support Package for PX4 Autopilots and then add a Read Parameter block to the model.

2. Double-click the Read Parameter block to open the Block Parameters dialog box. Change the Parameter Name to BAT_LOW_THR and then click Apply and OK.

3. In the Library Browser, Select Simulink and then drag and drop two Display blocks to the model. Connect the outputs of the Read Parameter block to Display blocks as shown in the image below.

Task 3 - Run Connected I/O Simulation Close to Real-Time Using Simulation Pacing Option

In tasks one and two, Simulink runs slower than the wall clock. This section helps you to run the Connected I/O simulation close to real-time using Simulation Pacing option. Perform these steps to run the model using Simuluation Pacing option.

1. On the Hardware tab, select Simulation Pacing.

2. In the Simulation Pacing Options dialog, select Enable pacing to slow down simulation. On enabling, the specified pace gets automatically applied to the simulation. Set the value for Simulation time per wall clock second as 1.

3. Along with enabling Simulink pacer on and setting the Simulation time per wall clock second as 1, sample time for the block has to be chosen specifically for getting close to real-time simulation. There is a two-way communication between Simulink and the Autopilot while running Connected I/O simulation. This introduces a inherent time overhead for a particular block execution. Thus, there is a specific minimum time required for a block execution. This varies with the machine performance and can be around 20-30 milliseconds. Now the sample time of these blocks should be more than this block execution time for close to real-time connected I/O simulation. Assuming the block Execution time is 30 milliseconds for block, since we have two blocks here, the minimum sample time of the model we can keep to get close to real-time behavior is 30*2 = 60 milliseconds. Set the sample time of the blocks as 60 milliseconds.

4. Simulate the model. Observe that Simulink time is close to wall clock.

  • Simulink connects to the PX4 Autopilot and reads the data.

Other Things to Try

You can use preconfigured Simulink models that helps you to read and write to uORB topics. You can run Connected I/O simulation in the preconfigured models. For more information on uORB blocks, see Getting Started with uORB Blocks for PX4 Autopilots Support Package.

Connected I/O simulation is not suitable for designing closed loop controllers and systems with dynamic feedback, where blocks must run at much higher rate. Hence it is recommended not to try the controller examples with Connected I/O.