Integrate Python GPS Text Message Parsing Algorithms in Simulink
This example shows how to parse text messages in NMEA GPS format using Simulink® string blocks and Python®. NMEA sentences are used for communication between marine devices, and the model in the example decodes RMB and RMC sentences in NMEA. RMB is the sentence for the recommended navigation data for GPS. RMC is the sentence for the recommended minimum data for GPS sentences.
In the model, you can choose to decode RMB and RMC sentences using Simulink or by incorporating Python code. This example explains the details of incorporating Python for decoding. For more information about the model and using Simulink® blocks for decoding, see Simulink Strings. For more on using Python in MATLAB®, see Access Python Modules from MATLAB - Getting Started.
MATLAB supports the reference implementation of Python, often called CPython. If you are on a Mac or Linux® platform, you already have Python installed. If you are using Windows®, you need to install a distribution, such as those found at https://www.python.org/downloads/. For more information, see Configure Your System to Use Python.
Explore the Model
In the model, the subsystem named NMEA Data Generator
generates data and sends the data to the subsystem named Data Classifier
. The Data Classifier
classifies the sentences as RMB, RMC, or unsupported. The sentences are then sent to RMB Decoder
, RMC decoder
, or Unsupported
, depending on their type.
Model RMB and RMC Decoders Using Python or Simulink String Blocks
If you double-click the block named RMB Decoder
, you can choose to model the decoder using Simulink string blocks or Python code.
Look under the mask. Observe that when you select Python
, the decoder is modeled by using a MATLAB System block.
Open the source code. Observe that the Python code to decode the message is incorporated into the setupImpl
and stepImpl
functions.
function setupImpl(obj) py.importlib.import_module('RMBDecode'); end
function y = stepImpl(obj,sentence) % convert sentence to python string pstr = py.str(sentence);
% call python module for string processing
plist = py.RMBDecode.parseRMBSentence(pstr);
% assign python list elements to Simulink bus y.Status = string(plist{1}); y.Error = str2double(string(plist{2})); y.ErrorDirection = string(plist{3}); y.OriginWaypointID = uint16(str2double(string(plist{4}))); y.DestinationWaypointID = uint16(str2double(string(plist{5}))); y.DestinationWaypointLatitudeDegree = uint16(str2double(string(plist{6}))); y.DestinationWaypointLatitudeMinute = str2double(string(plist{7})); y.DestinationWaypointNS = string(plist{8}); y.DestinationWaypointLongitudeDegree = uint16(str2double(string(plist{9}))); y.DestinationWaypointLongitudeMinute = str2double(string(plist{10})); y.DestinationWaypointEW = string(plist{11}); y.RangeToDestination = str2double(string(plist{12})); y.TrueBearing = str2double(string(plist{13})); y.Velocity = str2double(string(plist{14})); y.ArrivalAlarm = string(plist{15}); y.Checksum = string(plist{16}); end
Simulate Model and Review Results
Simulate the model and observe the decoded RMB and RMC sentences.