Reading GPS Data from PX4 Autopilot

This example shows how to obtain the data from a GPS device connected to Pixhawk Series 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 vehicle_gps_position uORB message. You can reuse the same GPS block in your own Simulink model to read GPS data.

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 to verify the GPS data

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.

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

open_system('px4_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 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 Pixhawk series controller 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 Pixhawk Series 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 Pixhawk Series flight controller:

  • Latitude and Longitude are int32 values, represented in 1e-7 degrees

  • Altitude is an int32 value, represented in 1e-3 meters above MSL (millimeters)

  • Velocity is a float32 value, represents GPS ground speed (metres/sec)

  • Number of satellites is a uint8 value, represents the number of satellites used for global positioning

  • Timestamp is a uint64 value, in microseconds, obtained from the GPS module. It might be unavailable right after cold start, indicated by a value of 0.

  • GPS Fix Type is a uint8 value. The following table represents the values for this signal:

Value Type of fix
0-1 No fix
2 2D fix
3 3D fix
4 RTCM code differential
5 Real-Time Kinematic, float
6 Real-Time Kinematic, fixed
8 Extrapolated. Some applications will not use the value of this field unless it is at least two, so always correctly fill in the fix.

Task 3 - Use the GPS block in your Simulink models

In this task, you will learn how to read GPS data for any Simulink model that you develop using Embedded Coder Support Package for PX4 Autopilots.

1. Open the Simulink model in which you want to obtain the data from a GPS device (connected to the Pixhawk Series flight controller).

2. Open the example model, px4_readGPS. You can also launch it from the MATLAB command prompt - enter the command: open_system('px4_readGPS').

3. From the px4_readGPS model, copy the GPS block.

4. Paste the GPS block to your Simulink model that you opened in Step 1.

5. Double-click the GPS block and select the required output ports.

6. Connect the output ports to other blocks that read or display the data. Save the model.

7. In the Modeling tab, click Model Settings.

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

  • Set the Hardware board to the same Pixhawk Series flight controller 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.

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

10. In the Hardware tab, click Build, Deploy & Start. The model is deployed to the Pixhawk Series flight controller.