coldnut
Posts: 8
Joined: Sun Mar 31, 2013 6:01 pm

GPSD and Adafruit GPS Breakout

Fri Jan 03, 2014 7:38 am

I've been working on a GPSController class in Python that will return various data from the GPS breakout that I purchased from Adafruit. Here's what I have so far:

Code: Select all

from gps import *
import time
import threading
import math

class GpsController(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.running = False

    def run(self):
        self.running = True
        while self.running:
           # grab EACH set of gpsd info to clear the buffer
            self.gpsd.next()

    def stopController(self):
        self.running = False

    def fix(self):
        return self.gpsd.fix

    def utc(self):
        return self.gpsd.utc

    def satellites(self):
        return self.gpsd.satellites

if __name__ == '__main__':
    # create the controller
    gpsc = GpsController()
    try:
        # start controller
        gpsc.start()
        print "Acquiring signal from satellites. Please wait...\n"
        time.sleep(5)
        while True:
            print "Time UTC: ", gpsc.utc
            print "Latitude: ", gpsc.fix.latitude, " N"
            print "Longitude: ", gpsc.fix.longitude, " W"
            print "Altitude: ", gpsc.fix.altitude, " m"
            print "Speed: ", gpsc.fix.speed, " m/s"
            print "Heading: ", gpsc.fix.track, " deg (true)"
            print "Climb: ", gpsc.fix.climb, " m/min"
            print "EPS: ", gpsc.fix.eps
            print "EPX: ", gpsc.fix.epx
            print "EPX: ", gpsc.fix.epx
            print "EPV: ", gpsc.fix.epv
            print "EPT: ", gpsc.gpsd.fix.ept
            print "Mode: ", gpsc.fix.mode
            print "Satellites: ", gpsc.satellites
            print "\n"
            time.sleep(3)

    #Ctrl-C
    except KeyboardInterrupt:
        print "\nUser Cancelled"

    #Error
    except:
        print "Unexpected Error:", sys.exc_info()[0]
        raise


    finally:
        print "Stopping GPS Controller"
        gpsc.stopController()
        #wait for the thread to finish
        gpsc.join()

    print "Done"
I have to thank Martin O'Hanlon for his excellent post over on his blog to get me started with this code. I plan on extending the functionality of this class even more. Before I move forward though, I had a question regarding the Longitude output. For some reason mine displays a negative value. For example, Longitude: 112 W shows up as Longitude: -112 W. Is there any simple explanation for this?

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: GPSD and Adafruit GPS Breakout

Fri Jan 03, 2014 5:34 pm

Try not hard coding those cardinals

Code: Select all

           print "Longitude: ", abs(we) , (" W" if we < 0 else " E") 
ie same for N S
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

coldnut
Posts: 8
Joined: Sun Mar 31, 2013 6:01 pm

Re: GPSD and Adafruit GPS Breakout

Fri Jan 03, 2014 10:29 pm

Thanks for that suggestion ;) . I was planning on making that change in the near future, I just wanted to get an idea of the output that I could expect. I'm having an issue altogether getting the application to run now, I'm not sure what I've done. The stack trace leading me to the error isn't providing me with much insight. Here's what I'm getting:

Code: Select all

Acquiring signal from satellites. Please wait...

Time UTC:  <bound method GpsController.utc of <GpsController(Thread-1, started -1232116624)>>
Unexpected Error: <type 'exceptions.AttributeError'>
Stopping GPS Controller
Traceback (most recent call last):
  File "GPSController.py", line 40, in <module>
    latitude = gpsc.fix.latitude
AttributeError: 'function' object has no attribute 'latitude
'
Again, here's the code that I'm working with:

Code: Select all

from gps import *
import time
import threading
import math

class GpsController(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.running = False

    def run(self):
        self.running = True
        while self.running:
           # grab EACH set of gpsd info to clear the buffer
            self.gpsd.next()

    def stopController(self):
        self.running = False

    def fix(self):
        return self.gpsd.fix

    def utc(self):
        return self.gpsd.utc

    def satellites(self):
        return self.gpsd.satellites

if __name__ == '__main__':
    # create the controller
    gpsc = GpsController()
    try:
        # start controller
        gpsc.start()
        print "Acquiring signal from satellites. Please wait...\n"
        time.sleep(5)
        while True:
            print "Time UTC: ", gpsc.utc
            latitude = gpsc.fix.latitude
            longitude = gpsc.fix.longitude
            print "Latitude: ", abs(latitude), (" N" if latitude > 0 else " S")
            print "Longitude: ", abs(longitude), (" W" if longitude < 0 else " E")
            print "Altitude: ", gpsc.fix.altitude, " m"
            print "Speed: ", gpsc.fix.speed, " m/s"
            print "Heading: ", gpsc.fix.track, " deg (true)"
            print "Climb: ", gpsc.fix.climb, " m/min"
            print "EPS: ", gpsc.fix.eps
            print "EPX: ", gpsc.fix.epx
            print "EPV: ", gpsc.fix.epv
            print "EPT: ", gpsc.gpsd.fix.ept
            print "Mode: ", gpsc.fix.mode
            print "Satellites: ", gpsc.satellites
            print "\n"
            time.sleep(3)

    #Ctrl-C
    except KeyboardInterrupt:
        print "\nUser Cancelled"

    #Error
    except:
        print "Unexpected Error:", sys.exc_info()[0]
        raise


    finally:
        print "Stopping GPS Controller"
        gpsc.stopController()
        #wait for the thread to finish
        gpsc.join()
    
    print "Done"
Can anyone see an obvious reason as to why this code stopped working on me?

coldnut
Posts: 8
Joined: Sun Mar 31, 2013 6:01 pm

Re: GPSD and Adafruit GPS Breakout

Fri Jan 03, 2014 10:40 pm

:lol: easy solution. I was neglecting the "@property" declaration. This fixed it all.

Return to “Python”