Main Content

Reading GPS Data from PX4 Autopilot

This example shows how to obtain the data from a GPS device connected to PX4 flight controller, using UAV Toolbox 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.


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

The GPS module is enabled by default in the startup script (rc.txt ) available with UAV Toolbox Support Package for PX4 Autopilots. In this startup script, only the main GPS device is enabled on the port '/dev/ttyS3'. To enable main GPS on a different serial port or to enable secondary GPS, modify the rc.txt manually. The ports on which the GPS is available for the respective boards are shown in the table below.

Perform these steps to manually modify the rc.txt.

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, update the gps start command.

For Example, in case of Pixhawk 4, GPS port is '/dev/ttyS0'. To enable the GPS on '/dev/ttyS0', update the GPS start command in the startup script as:

gps start -d /dev/ttyS0 -s

Note: For testing purpose, 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.


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.