1

I am developing an application in raspberry pi to use it as a location tracker. I am using neo-6m GPS via USB interface to get the position data in raspberry pi. For this I am setting up GPSD to point to the USB-Serial device. (See instructions)

The following python script polls the GPSD daemon and sends the location data to the parent process via a Unix Domain Socket:

#!/usr/bin/python
import os
from gps import *
from time import *
import time
import threading
import socket
import math
t1, t2 = socket.socketpair()
gpsd = None #seting the global variable


host = "localhost"
port = 8888
class GpsPoller(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    global gpsd #bring it in scope
    gpsd = gps(mode=WATCH_ENABLE,host=host,port=port) #starting the stream of info
    self.current_value = None
    self.running = True #setting the thread running to true
 
  def run(self):
    print("%%%%%%%GPS RUN")
    global gpsd
    while self.running:
      gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
      time.sleep(3)
 

def poll_gps(socket):
  print('GPS POLL')
  gpsp = GpsPoller() # create the thread
  gpsp.start()
  try:
    while True:
      #It may take a second or two to get good data
      #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc
      #print 'latitude    ' , gpsd.fix.latitude
      #print 'longitude   ' , gpsd.fix.longitude
      if gpsd.fix.latitude is not None and gpsd.fix.longitude is not None and not math.isnan(gpsd.fix.longitude ) and not math.isnan(gpsd.fix.latitude ) and gpsd.fix.latitude != 0.0 and  gpsd.fix.longitude != 0.0 :

            gps_str='{0:.8f},{1:.8f}'.format(gpsd.fix.latitude, gpsd.fix.longitude)
            dict_str="{'type' : 'gps', 'value' : '"+gps_str+"'}"
            dict_str_new="{'type' : 'gps', 'value' : '"+str(gpsd.fix.latitude)+","+str(gpsd.fix.longitude)+"'}"
            print("GPS123_OLD" +dict_str)
            print("GPS123_NEW" +dict_str_new)
            if socket == None:
                print("GOT GPS VALUE")
                sys.exit(0)
            socket.sendall(dict_str+'\n')                        
      else:
            print('%%%%%%%%%%%%%%%%%%GPS reading returnd None!')     
 
      time.sleep(3) #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."
  
if __name__ == '__main__':
    poll_gps(None)    

When I run this code and move the raspberry pi setup to 1 Kilometre away, I can see new distinct lat-long values printed in console. But when I plot these values, I see that all these locations are at the starting point. ie all values are bunched around the same starting point. I am not able to see a clear path to the point 1 km away.

To check if the problem is with my code or not, I installed "navit" software in raspberry pi and pointed it to the GPSD daemon. When I used navit to plot my path, it showed my progress correctly in the map. So I concluded that the problem is with my code.

Can someone take a look and let me know if there is any issue with my code

Community
  • 1
  • 1
Kiran
  • 119
  • 1
  • 9

1 Answers1

0

I figured out the issue. In the "run" method of the "GpsPoller" class, I am invoking a sleep call. It seems this delay make the python client lag behind the GPSD demon in retrieving the location data queued by the GPSD daemon. I just removed the sleep and I started getting the correct locations in time.

  def run(self):
    print("%%%%%%%GPS RUN")
    global gpsd
    while self.running:
      gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
      #time.sleep(3) REMOVE/COMMENT THIS LINE TO GET CORRECT GPS VALUES
Kiran
  • 119
  • 1
  • 9