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)
 
(6 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 3: Zeile 3:
==Installation==
==Installation==
GPS Programm Installieren
GPS Programm Installieren
  sudo apt-get install python gpsd gpsd-clients
sudo apt-get install python gpsd gpsd-clients
Plug the USB GPS into the pi, you should be able to see it detected in
Plug the USB GPS into the pi, you should be able to see it detected in
  sudo lsusb
sudo lsusb
Mine came up as: Bus 001 Device 004: ID 1546:01a6 U-Blox AG
Mine came up as: Bus 001 Device 004: ID 1546:01a6 U-Blox AG
Using:
Using:
  dmesg | grep -i usb
dmesg | grep -i usb
I worked out that my USB GPS was paired to ttyACM0
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:
At this point you can test your GPS device is functional and sending data by:
  sudo cat /dev/ttyACM0
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.
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
sudo gpsd /dev/ttyACM0 -n -F /var/run/gpsd.sock
this command connects the output of ttyACM0 to the gpsd socket
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.
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
You should now be able to test the GPS using
  cgps -s
cgps -s
Which will bring up a small window showing the GPS data.
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
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
sudo killall gpsd
and then
and then
  sudo gpsd /dev/ttyACM0 -n -F /var/run/gpsd.sock
sudo gpsd /dev/ttyACM0 -n -F /var/run/gpsd.sock
   
   
which will restart the gpsd service and pick up the new settings.
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.
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:
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
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==
==Python Scripts==
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div">
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div">
#! /usr/bin/python
import gps, os, time
# Written by Dan Mandle http://dan.mandle.me September 2012
session = gps.gps()
# License: GPL 2.0
while 1:
    os.system('clear')
import os
     session.query('admosy')  
from gps import *
     # a = altitude, d = date/time, m=mode,  
from time import *
     # o=postion/fix, s=status, y=satellites
import time
     print
import threading
     print ' GPS reading'
    print '----------------------------------------'
gpsd = None #seting the global variable
    print 'latitude    ' , session.fix.latitude
    print 'longitude  ' , session.fix.longitude
os.system('clear') #clear the terminal (optional)
    print 'time utc    ' , session.utc, session.fix.time
    print 'altitude   ' , session.fix.altitude
class GpsPoller(threading.Thread):
    print 'eph         ' , session.fix.eph
  def __init__(self):
    print 'epv        ' , session.fix.epv
     threading.Thread.__init__(self)
    print 'ept        ' , session.fix.ept
     global gpsd #bring it in scope
    print 'speed       ' , session.fix.speed
    gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
    print 'climb      ' , session.fix.climb
    self.current_value = None
    print
    self.running = True #setting the thread running to true
    print ' Satellites (total of', len(session.satellites) , ' in view)'
   
    for i in session.satellites:
  def run(self):
        print '\t', i
    global gpsd
     time.sleep(3)
     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.""
 
</syntaxhighlight>
</syntaxhighlight>


Zeile 100: Zeile 66:
   python /home/pi/gegpsd.py
   python /home/pi/gegpsd.py
   python ~/gegpsd.py  & cd /tmp/ && python -m SimpleHTTPServer
   python ~/gegpsd.py  & cd /tmp/ && python -m SimpleHTTPServer
===Python Script===
<syntaxhighlight lang="python" line start="1" highlight="5" enclose="div">
#!env python
import gps
file = '/tmp/nmea.kml'
session = gps.gps(host="localhost", port="2947")
session.stream(flags=gps.WATCH_JSON)
for report in session:
  if report['class'] == 'TPV':
      latitude  = report['lat']
      longitude = report['lon']
      altitude = report['alt']
      speed_in = report['speed']
      heading = report['track']
      speed = int(speed_in * 1.852)
      range = ( ( speed / 100  ) * 350 ) + 650
      tilt = ( ( speed / 120 ) * 43 ) + 30
      if speed < 10:
          range = 200
          tilt = 30
          heading = 0
      output = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
<name>%s km/h</name>
<description>^</description>
<LookAt>
<longitude>%s</longitude>
<latitude>%s</latitude>
<range>%s</range>
<tilt>%s</tilt>
<heading>%s</heading>
</LookAt>
<Point>
<coordinates>%s,%s,%s</coordinates>
</Point>
</Placemark>
</kml>""" % (speed,longitude,latitude,range,tilt,heading,longitude,latitude,altitude)
      f=open(file, 'w')
      f.write(output)
      f.close()
del session
</syntaxhighlight>

Aktuelle Version vom 30. September 2017, 13:39 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

import gps, os, time
session = gps.gps()
while 1:
    os.system('clear')
    session.query('admosy') 
    # a = altitude, d = date/time, m=mode,  
    # o=postion/fix, s=status, y=satellites
    print
    print ' GPS reading'
    print '----------------------------------------'
    print 'latitude    ' , session.fix.latitude
    print 'longitude   ' , session.fix.longitude
    print 'time utc    ' , session.utc, session.fix.time
    print 'altitude    ' , session.fix.altitude
    print 'eph         ' , session.fix.eph
    print 'epv         ' , session.fix.epv
    print 'ept         ' , session.fix.ept
    print 'speed       ' , session.fix.speed
    print 'climb       ' , session.fix.climb
    print
    print ' Satellites (total of', len(session.satellites) , ' in view)'
    for i in session.satellites:
        print '\t', i
    time.sleep(3)

NMEA Export als KML

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

Python Script

#!env python
import gps

file = '/tmp/nmea.kml'

session = gps.gps(host="localhost", port="2947")

session.stream(flags=gps.WATCH_JSON)

for report in session:
   if report['class'] == 'TPV':
       latitude  = report['lat']
       longitude = report['lon']
       altitude = report['alt']
       speed_in = report['speed']
       heading = report['track']

       speed = int(speed_in * 1.852)
       range = ( ( speed / 100  ) * 350 ) + 650
       tilt = ( ( speed / 120 ) * 43 ) + 30

       if speed < 10:
           range = 200
           tilt = 30
           heading = 0

       output = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
	<Placemark>
		<name>%s km/h</name>
		<description>^</description>
		<LookAt>
			<longitude>%s</longitude>
			<latitude>%s</latitude>
			<range>%s</range>
			<tilt>%s</tilt>
			<heading>%s</heading>
		</LookAt>
		<Point>
			<coordinates>%s,%s,%s</coordinates>
		</Point>
	</Placemark>
</kml>""" % (speed,longitude,latitude,range,tilt,heading,longitude,latitude,altitude)

       f=open(file, 'w')
       f.write(output)
       f.close()

del session