Main Content

gpsdev

Connect to GPS receiver on Arduino hardware

Since R2020a

Description

The gpsdev object reads position and time data using a Global Positioning System(GPS) module. The gpsdev object represents a connection to the GPS receiver connected to the serial port of the Arduino® hardware. gpsdev uses GPRMC, GPGGA and GPGSA sentences to read the data. The GPS receiver must output these sentences for successful creation of the gpsdev object. Ensure your GPS module is configured to give the sentences.

Creation

Description

gpsObj = gpsdev(a) creates a GPS object. The object represents the connection to the GPS on the Arduino hardware, a.

gpsObj = gpsdev(a,Name,Value) creates a GPS object with default property values. The object represents the connection to the GPS on the Arduino hardware, a.

Input Arguments

expand all

Arduino hardware connection created using arduino, specified as an object.

Example: gpsObj = gpsdev(a) creates a connection to the GPS receiver on the Arduino object, a.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

The ID of the serial port available on the Arduino hardware specified as a number.

Example: gpsObj = gpsdev(a,'SerialPort',2)

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

This property is read-only.

The baud rate for serial communication. The baud rate is set at 9600 bits/sec. The GPS receiver must be configured to work at 9600 bits/sec. If your GPS receiver is configured to some other baud rate, reconfigure it to 9600 bits/sec to use gpsdev function.

Number of samples read from the GPS in a single execution of the read function. The maximum value is 10.

Tunable: No

Data Types: double

Specify whether to return the latest or the oldest data samples. The number of samples depends on the SamplesPerRead value. The data read from the GPS receiver is stored in the MATLAB® buffer.

  • latest

    Provides the latest data samples available in the buffer. All previous data samples in the buffer are discarded. For example, if SamplesPerRead = 3, the latest three data samples read by the GPS receiver are returned.

    The following figure illustrates how latest data samples are returned assuming S1 is the first GPS receiver data stored in the buffer, S2 is the second data and so on and Sn is the last data stored in the buffer and SamplesPerRead property is set to 3 during GPS receiver object creation.

  • oldest

    Provides the oldest data samples available in the buffer. In this case, no data samples are discarded. For example, if SamplesPerRead = 3, the first three data samples read are returned for the first read, the next three data samples are returned for the second read, and so on.

    The following figure illustrates how oldest data samples are returned assuming S1 is the first GPS receiver data stored in the buffer, S2 is the second data and so on and Sn is the last data stored in the buffer and SamplesPerRead property is set to 3 during GPS receiver object creation.

Tunable: No

Data Types: character vector | string

This property is read-only.

Samples read from the first read. When you release the object, SamplesRead is set to 0.

Data Types: double

This property is read-only.

Samples available in the host buffer. When you release the object, SamplesAvailable is set to 0.

Data Types: double

Set the output format of the data returned by executing the read function.

When the OutputFormat is set to timetable, the timetable returned has the following fields:

  • LLA (Latitude, Longitude, Altitude)

  • Ground Speed

  • Course over ground

  • Dilution of Precisions(DOPs), VDOP,HDOP,PDOP,

  • GPS Receiver Time

  • Time — Time stamps read from the Arduino hardware in datetime or duration format

When the OutputFormat is set to matrix, the data is returned as matrices of Time, LLA, Ground Speed, Course over ground, DOPs, and GPS receiver time. The units for the GPS receiver readings are the same as the timetable format.

Tunable: Yes

Data Types: character vector | string

Set the format of the time displayed when the GPS data is read.

  • datetime — Displays the date and time at which the data is read.

  • duration — Displays the time elapsed in seconds after the GPS object is locked. The GPS object gets locked at the first call of the read function either after the object creation or after the execution of the release function.

Tunable: Yes

Data Types: character vector | string

Usage

Create GPS Connection

Create an Arduino object and include the Serial library.

a = arduino('COM4', 'Mega2560', 'Libraries', 'Serial');

Create the GPS object.

gpsObj = gpsdev(a)
gpsObj = 

  gpsdev with properties: 

                 SerialPort: 1
                   BaudRate: 9600 (bits/s)

             SamplesPerRead: 2  
                   ReadMode: 'oldest'            
                SamplesRead: 0      
           SamplesAvailable: 0 
Show all properties, functions
                     

Create GPS Connection with Additional Name-Value Pair Arguments

Create a GPS object with additional properties specified as name-value pair arguments.

gpsObj = gpsdev(a,'SamplesPerRead',3,'ReadMode','latest')
gpsObj = 

  gpsdev with properties: 

                 SerialPort: 1
                   BaudRate: 9600 (bits/s)

             SamplesPerRead: 3  
                   ReadMode: 'latest'            
                SamplesRead: 0      
           SamplesAvailable: 0 
      

Object Functions

flushFlush the host buffer
infoRead Update Rate, GPS Lock information and number of satellites in View for the GPS receiver
readRead data from GPS
releaseRelease the GPS object
stopStop data collection from GPS module
writeBytesWrite raw data to GPS module

More About

expand all

Version History

Introduced in R2020a