Created
May 2, 2018 15:30
-
-
Save edipetres/9236de659770497c9a1c63faa7a27dd3 to your computer and use it in GitHub Desktop.
gps data logging from py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 | |
log_file = open('~/Desktop/gps_log.csv', 'a') | |
log_file.write('time, latitude, longitude, altitude') | |
while True: | |
#It may take a second or two to get good data | |
#print gpsd.fix.latitude,', ',gpsd.fix.longitude,' Time: ',gpsd.utc | |
time = str(datetime.datetime.now()) | |
latitude = gpsd.fix.latitude | |
longitude = gpsd.fix.longitude | |
altitude = gpsd.fix.altitude | |
log_file.write('{}, {}, {}'.format(time, latitude, longitude, altitude)) | |
os.system('clear') | |
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 '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." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment