Python Script to grab GPSD data and log to file.
#! /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 self.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
gps_file = open('gps_data.csv','w') # Open a file to write the data to
while True:
if str(gpsd.fix.mode) == str(3): # If we have Mode 3, good lock, then write the data out
gps_data = str(gpsd.fix.latitude) + ',' + str(gpsd.fix.longitude) + '\n' # just print lat and lon data
print gps_data
gps_file.write (gps_data)
time.sleep(1) #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
gps_file.close()
print "Done.\nExiting."
Python Script to plot the GPS Data on the screen
#! /usr/bin/python
# Plotting test using pygame
import pygame, sys, os, csv # Import the pygame, sys and os libraries
from pygame.locals import * # Get pygame constants
pygame.init() # Initialise pygame
screenx = 640 # Size of window
screeny = 480
offsetx = screenx/2 # middle of window
offsety = screeny/2
firstrow = True
scale = 40000
surface=pygame.display.set_mode((screenx,screeny)) # Create a surface to draw on
pygame.display.set_caption('pygame drawng test')# Set the window caption
screen = pygame.display.get_surface()
#pygame.draw.line(surface, (0,0,255),(0,0),(200,100)) # Draw a line
#pygame.display.flip() # Display what we have drawn
gps_file = open('gps_test_data.csv','rt') # Open a file to read data from
try: # Read all the lines in
reader = csv.DictReader(gps_file)
for row in reader:
#print row
#print row['lat']
#print row['lon']
while firstrow: # if this is the firstrow us coords for center
firstx = int(float(row['lat'])*scale)
firsty = int(float(row['lon'])*scale)
firstrow = False
lastx=offsetx
lasty=offsety
#print firstx, firsty
else:
x = int(float(row['lat']) * scale)-firstx + offsetx # make this point middle of the screen
y = int(float(row['lon']) * scale)-firsty + offsety
#print firstx,firsty, lastx,lasty,x,y
pygame.draw.line(surface, (0,0,255),(lastx,lasty),(x,y)) # Draw a line
pygame.display.flip()
lastx = x
lasty = y
finally:
gps_file.close()
def input(events): # Function to process pygame events
for event in events:
if event.type == QUIT: # If we click ALT+F4 programme will quit
sys.exit(0)
while True: # Keep looping getting events
input(pygame.event.get())
No comments:
Post a Comment