Main Content

Integrate Python Packages to Parse NMEA GPS Messages Using Python Importer

This example shows how to use Python® Importer to import a Python package containing algorithms to parse NMEA GPS messages into Simulink®.

In the baseline model ex_nmea_gps_data_parser_simulink.slx, built-in Simulink blocks are used to decode RMB and RMC sentences. For more information about the model and using Simulink blocks, see Parse NMEA GPS Text Message. This example uses Python Importer to generate custom MATLAB System blocks from Python packages that implement RMB and RMC decoding. The generated custom blocks replace the built-in Simulink blocks in the baseline model.

To call Python modules in MATLAB, you must have a compatible version of Python installed. For more information on supported Python versions and setting up your system to use Python in MATLAB, see Configure Your System to Use Python.

Explore Baseline Model

In the baseline model, the subsystem named NMEA Data Generator generates data and sends the data to the subsystem named Data Classifier. The Data Classifier subsystem classifies the sentences as RMB, RMC, or unsupported. The sentences are then sent to RMB Decoder, RMC decoder, or Unsupported, depending on their type. The RMB Decoder and RMC Decoder subsystems contain RMBDecoder and RMCDecoder blocks that are built-in Simulink blocks implementing RMB and RMC decoding, respectively.

open_system('ex_nmea_gps_data_parser_simulink');

Use Python Importer to Integrate Python Functions into Simulink

1. Open Python Importer as a standalone application and click Start.

obj = Simulink.PythonImporter;
obj.view();

For more information on Python Importer, see Import Python Code to Simulink Using Python Importer Wizard.

Load the bus objects used for this example.

load('pyNMEABuses.mat'); 

2. On the Settings page, in Simulink library file name box, specify the library file name. In the Output folder box, specify the path of folder where you want the Simulink library and artifacts to be located. Click Next.

PyImporterImportPyPackage.png

3. On the Specify Custom Code page, in the Packages to Import box, specify GPSPackage folder. The GPSPackage folder contains a Python package that defines modules that implement RMB and RMC decoding using python. Click Next.

PyImporterImportPyPackage.png

4. On the Import page, select the GPSPackage.RMBDecoder.decode and GPSPackage.RMCDecoder.decode functions for import. Click Next.

PyImporterImportFunc.png

5. Specify the data type of input as string for both the functions. For the GPSPackage.RMBDecode.decode function, specify data type of the output as Bus: NMEA_GPRMB. For the GPSPackage.RMCDecode.decode function, specify data type of the output as Bus: NMEA_GPRMC for . Click Next.

PyImporterImportConfig.png

6. On the Create Simulink Library page, check the specification of the library to be generated. Click Next to generate the library.

7. The Python Importer generates a custom Simulink library containing MATLAB System blocks for the selected functions. Click Finish.

Open the library containing the generated blocks.

open_system("PythonImporterDecoderLibrary.slx"); 

8. In the baseline model, replace the RMBDecoder and RMCDecoder blocks with the generated GPSPackage_RMBDecoder_decode and GPSPackage_RMCDecoder_decode blocks, respectively.

Open this model to explore the implementation of the generated blocks in place of the built-in Simulink blocks for decoding GPS messages.

open_system("ex_nmea_gps_data_parser_py");