Reading GPS Data from PX4 Autopilot

This example shows how to obtain the data from a GPS device connected to PX4 flight controller, using Embedded Coder Support Package for PX4 Autopilots.

The example uses a pre-defined Simulink model that contains the GPS block. The GPS block gets the GPS data by reading the vehicle_gps_position uORB message.

Prerequisites

Required Hardware

To run this example, you will need the following hardware:

  • Pixhawk Series flight controller

  • Micro USB type-B cable

  • GPS device

  • Micro-SD card (already used during the initial Hardware Setup)

  • Micro-SD card reader

Task 1 - Enable GPS module at PX4 system startup

In this task, you will enable GPS module as part of PX4 system startup from SD card. The SD card already contains the startup script rc.txt in the root-level folder named etc (adding the file rc.txt is explained in this topic).

1. Remove the micro-SD card from the Pixhawk Series flight controller.

2. Connect the micro-SD card to the host computer using a micro-SD card reader.

3. Edit the file rc.txt present in the etc folder, to add the following line:

gps start

Note: If you do not have the GPS device, add the following line instead:

gps start -f

This will start a GPS simulation module that replicates a real GPS data.

4. Save rc.txt file, and reinsert the micro-SD card back into the PX4 flight controller.

Task 2 - Configure and Run the model in External mode and Deploy the Model

In this task, you will configure and run the pre-defined Simulink model model in External mode to verify the GPS data. This model contains the GPS block that reads vehicle_gps_position uORB topic. After you verify the GPS data, you deploy the model to the PX4 hardware board.

1. Open the example model, px4demo_readGPS, from the MATLAB command prompt.

open_system('px4demo_readGPS');

In the example model, the GPS block is used to read GPS values. This block accepts signals from the vehicle_gps_position uORB message, and outputs the required values.

2. Double-click the GPS block. By default, signals corresponding to Latitude, Longitude, and Altitude(MSL) are selected as outputs. You can add the other signals as output by selecting the corresponding checkboxes.

Note: The Status output indicates if the uORB message was received during a previous time step or not. A value of 0 indicates that the uORB data at the output is the latest, and a value of 1 indicates that the uORB data was received during the previous time step. This output can be used to trigger subsystems for processing new messages received in the uORB network.

3. In the Modeling tab, click Model Settings.

4. In the Configuration Parameters dialog box, navigate to the Hardware Implementation pane:

  • Set the Hardware board to the same PX4 hardware board that you selected during Hardware Setup screens.

  • In the Target Hardware Resources section, set the Build options to Build, load and run to automatically download the generated binary file on to the connected Pixhawk Series flight controller.

  • Enter the serial port of the host computer to which the Pixhawk Series flight controller is connected, in the Serial port for firmware upload field.

  • In the External mode pane, select the option Use the same host serial port for External mode as used for firmware upload.

5. Navigate to Solver pane and select the option Treat each discrete rate as a separate task. Click OK.

6. In the Simulation tab, set the Stop time to inf.

7. Connect the USB cable from the PX4 flight controller to the host computer.

8. In the Hardware tab, click the Monitor & Tune to start signal monitoring and parameter tuning.

9. Verify the output GPS values in the Scope display. The values correspond to the current GPS position of the PX4 flight controller.

10. Stop the Monitor and Tune operation by clicking Stop in the Hardware tab.

11. In the Hardware tab, click Build, Deploy & Start. The model is deployed to the PX4 hardware board.