Zum Inhalt springen

GPS Verbinden U-BLOX Raspberry Pi: Unterschied zwischen den Versionen

Aus Computech CMS Wiki
Simon Zeberli (Diskussion | Beiträge)
Simon Zeberli (Diskussion | Beiträge)
Zeile 39: Zeile 39:
# Written by Dan Mandle http://dan.mandle.me September 2012
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0
# License: GPL 2.0
 
import os
import os
from gps import *
from gps import *
Zeile 45: Zeile 45:
import time
import time
import threading
import threading
 
gpsd = None #seting the global variable
gpsd = None #seting the global variable
 
os.system('clear') #clear the terminal (optional)
os.system('clear') #clear the terminal (optional)
 
class GpsPoller(threading.Thread):
class GpsPoller(threading.Thread):
   def __init__(self):
   def __init__(self):
Zeile 57: Zeile 57:
     self.current_value = None
     self.current_value = None
     self.running = True #setting the thread running to true
     self.running = True #setting the thread running to true
 
   def run(self):
   def run(self):
     global gpsd
     global gpsd
     while gpsp.running:
     while gpsp.running:
       gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
       gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
 
if __name__ == '__main__':
if __name__ == '__main__':
   gpsp = GpsPoller() # create the thread
   gpsp = GpsPoller() # create the thread
Zeile 70: Zeile 70:
       #It may take a second or two to get good data
       #It may take a second or two to get good data
       #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc
       #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc
 
       os.system('clear')
       os.system('clear')
 
       print
       print
       print ' GPS reading'
       print ' GPS reading'
Zeile 90: Zeile 90:
       print
       print
       print 'sats        ' , gpsd.satellites
       print 'sats        ' , gpsd.satellites
 
       time.sleep(5) #set to whatever
       time.sleep(5) #set to whatever
 
   except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
   except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
     print "\nKilling Thread..."
     print "\nKilling Thread..."
Zeile 98: Zeile 98:
     gpsp.join() # wait for the thread to finish what it's doing
     gpsp.join() # wait for the thread to finish what it's doing
   print "Done.\nExiting.""
   print "Done.\nExiting.""
</syntaxhighlight>
</syntaxhighlight>



Version vom 26. Januar 2014, 21:50 Uhr

Beispiele

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

Installation

GPS Programm Installieren

 sudo apt-get install python gpsd gpsd-clients

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

Beim Start funktiniert es nicht.

 sudo nano /etc/rc.local
 sudo killall gpsd
 sudo gpsd /dev/ttyACM0 -n -F /var/run/gpsd.sock

Python Scripts

#! /usr/bin/python
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0

import os
from gps import *
from time import *
import time
import 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.""

NMEA Export als KML

 wget http://www.intestinate.com/gegpsd.py
 python /home/pi/gegpsd.py
 python ~/gegpsd.py  & cd /tmp/ && python -m SimpleHTTPServer