-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPSController.py
87 lines (69 loc) · 2.33 KB
/
GPSController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from gps import *
import time
import threading
import math
import os
class GpsController(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.running = False
def run(self):
self.running = True
while self.running:
# grab EACH set of gpsd info to clear the buffer
self.gpsd.next()
def stopController(self):
self.running = False
@property
def fix(self):
return self.gpsd.fix
@property
def utc(self):
return self.gpsd.utc
@property
def satellites(self):
return self.gpsd.satellites
if __name__ == '__main__':
# create the controller
gpsc = GpsController()
try:
# start controller
gpsc.start()
while True:
if (gpsc.fix.latitude):
print "latitude ", gpsc.fix.latitude
print "longitude ", gpsc.fix.longitude
print "altitude (m)", gpsc.fix.altitude
if gpsc.utc and gpsc.utc<>"None":
print "time utc ", gpsc.utc #, " + ", gpsc.fix.time
sattime = time.mktime(time.strptime(gpsc.utc, "%Y-%m-%dT%H:%M:%S.000Z"))
print("sattime =", sattime)
print("time.time =", time.time())
if (time.time() < sattime):
print "setting time"
os.system('date -s %s' % gpsc.utc)
#2014-03-29T17:33:31.000Z
#print "eps ", gpsc.fix.eps
#print "epx ", gpsc.fix.epx
#print "epv ", gpsc.fix.epv
#print "ept ", gpsc.gpsd.fix.ept
#print "speed (m/s) ", gpsc.fix.speed
#print "climb ", gpsc.fix.climb
#print "track ", gpsc.fix.track
#print "mode ", gpsc.fix.mode
#print "sats ", gpsc.satellites
time.sleep(0.5)
#Ctrl C
except KeyboardInterrupt:
print "User cancelled"
#Error
except:
print "Unexpected error:", sys.exc_info()[0]
raise
finally:
print "Stopping gps controller"
gpsc.stopController()
#wait for the thread to finish
gpsc.join()
print "Done"