GPS Sample

From Digi Developer

Jump to: navigation, search

This page contains a sample of how to use the GPS API on a Digi device.

## The returned values from the gps_location call are NMEA parsed into a 
## tuple: (latitude, longitude, altitude, timestamp)
## ConnectPort X3 /X-Trak 3 devices the returned values from the gps_location call are NMEA parsed into  
## a tuple: (latitude, longitude, altitude, timestamp, speed, direction,fix type)
## The sample returned is the latest received from the GPS device.  The 
## timestamp is assigned to it when it was successfully parsed.
## In cases where there are no sample available, an exception is raised.
    GPS Location API Sample
    This example reads and displays all the values from the device's 
    integrated GPS each 5 seconds, using the GPS Location API.
    Displays Latitude, Longitude, Altitude, Timestamp in GMT/GPS time
    FixType  1D 2D 3D 
    Speed  in meter per second
    Direction in Degrees
# imports
import sys
import os 
import time 
import digihw   ## From the digi embedded library, import the parsed gps_location
# variables
is_reading = True
# Ensure to set TRUE for Variants of X-Trak 3, X3 & X3R devices
# Ensure to set FALSE for X4 & X5 devices 
Extended_GpsInfo = True  
def get_formmated_value(value):
        Returns the given value formatted in degrees, minutes and seconds.
    working_value = value
    if working_value < 0:
        working_value = working_value * -1
    deg = working_value;
    gpsdeg = int(deg)
    remainder = deg - (gpsdeg * 1.0)
    gpsmin = remainder * 60.0
    remainder2 = gpsmin - int(gpsmin)*1.0
    gpsseg = int(remainder2*60.0)
    final_value = "%sdeg %smin %ssec" %(gpsdeg, int(gpsmin), gpsseg)
    return final_value
def get_latitude_hemisphere(latitude):
        Returns the hemisphere depending on the latitude (S or N) 
    if latitude < 0:
        return "S"
    return "N"
def get_longitude_hemisphere(longitude):
        Returns the hemisphere depending on the longitude (W or E) 
    if longitude < 0:
        return "W"
    return "E"
# Read GPS information every 5 seconds
while is_reading:
        print "Reading GPS data...\r\n"
        gps_data = digihw.gps_location()
        if Extended_GpsInfo == True:
            latitude, longitude, altitude, timestamp, Speed, Direction, FixType = gps_data
            latitude, longitude, altitude, timestamp = gps_data
        print "Latitude  : %s %s" %(get_formmated_value(latitude), 
        print "Longitude : %s %s" %(get_formmated_value(longitude), 
        print "Altitude  : %d meters" %altitude
        print "Date      : % s" %time.ctime(timestamp)
        if Extended_GpsInfo == True:
            print "Speed     : %f meter/s" %Speed
            print "Direction : %d degrees" %Direction
            print "FixType   : %dD" %FixType
        # Wait 1 seconds
        print "Please, wait 1 seconds...\r\n"
        print "Couldn't read GPS data. You need to get a better GPS \
               signal.\nPlease, ensure the GPS antenna is correctly connected and \
               has an open view of the sky.\r\n"
        # Wait 20 seconds
        print "Please, wait 20 seconds...\r\n\r\n"
Personal tools
Wiki Editing