Zum Inhalt springen

GPS Verbinden U-BLOX Raspberry Pi

Aus Computech CMS Wiki

Beispiele

 http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/

Installation

Plug the USB GPS into the pi, you should be able to see it detected in

 sudo lsusb

Mine came up as: Bus 001 Device 004: ID 1546:01a6 U-Blox AG Using:

 dmesg | grep -i usb

I worked out that my USB GPS was paired to ttyACM0 At this point you can test your GPS device is functional and sending data by:

 sudo cat /dev/ttyACM0

The next thing we need to do is pipe the GPS feed into GPSD (the gps demon) for it to interpret the data and hopefully give us something useful to use later.

 sudo gpsd /dev/ttyACM0 -n -F /var/run/gpsd.sock

this command connects the output of ttyACM0 to the gpsd socket NB: I had to ensure that the -n flag was present in this command, as trying without the flag resulted in a time out. You should now be able to test the GPS using

 cgps -s

Which will bring up a small window showing the GPS data. Things to note; If you have any problems and cgps always displays ‘NO FIX’ under status and then aborts after a few seconds, you may need to restart the gpsd service you may have to kill and reboot the gps demon by typing

 sudo killall gpsd

and then

 sudo gpsd /dev/ttyACM0 -n -F /var/run/gpsd.sock

which will restart the gpsd service and pick up the new settings. Now you should be able to use the GPS data for whatever your project needs; in my case I want to build a GPS fence so that if my beehive is detected leaving a known area (such as a field) then it will alert me and provide me with a GPS co-ordinate feed. I used the Lady Ada guide on how to setup GPS devices to help me; the link is:

 http://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi/setting-everything-up

Python Scripts

#! /usr/bin/python# Written by Dan Mandle http://dan.mandle.me September 2012# License: GPL 2.0 import osfrom gps import *from time import *import timeimport threading gpsd = None #seting the global variable os.system('clear') #clear the terminal (optional) class GpsPoller(threading.Thread):  def __init__(self):    threading.Thread.__init__(self)    global gpsd #bring it in scope    gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info    self.current_value = None    self.running = True #setting the thread running to true   def run(self):    global gpsd    while gpsp.running:      gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer if __name__ == '__main__':  gpsp = GpsPoller() # create the thread  try:    gpsp.start() # start it up    while True:      #It may take a second or two to get good data      #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc       os.system('clear')       print      print ' GPS reading'      print '----------------------------------------'      print 'latitude    ' , gpsd.fix.latitude      print 'longitude   ' , gpsd.fix.longitude      print 'time utc    ' , gpsd.utc,' + ', gpsd.fix.time      print 'altitude (m)' , gpsd.fix.altitude      print 'eps         ' , gpsd.fix.eps      print 'epx         ' , gpsd.fix.epx      print 'epv         ' , gpsd.fix.epv      print 'ept         ' , gpsd.fix.ept      print 'speed (m/s) ' , gpsd.fix.speed      print 'climb       ' , gpsd.fix.climb      print 'track       ' , gpsd.fix.track      print 'mode        ' , gpsd.fix.mode      print      print 'sats        ' , gpsd.satellites       time.sleep(5) #set to whatever   except (KeyboardInterrupt, SystemExit): #when you press ctrl+c    print "\nKilling Thread..."    gpsp.running = False    gpsp.join() # wait for the thread to finish what it's doing  print "Done.\nExiting."