-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps_collector.py
100 lines (75 loc) · 2.68 KB
/
gps_collector.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
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
#
# Copyright (C) 2014, Jaguar Land Rover
#
# This program is licensed under the terms and conditions of the
# Mozilla Public License, version 2.0. The full text of the
# Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
#
#
# GPS Data Collector
# A sample device implementation that collects GPS position data from gpsd
# and sends it to a logger object
#
import threading
from gps import *
import time
class GPSPoller(threading.Thread):
"""
Polls GPS devices via gpsd
"""
def __init__(self):
threading.Thread.__init__(self)
self.session = gps(mode=WATCH_ENABLE)
def shutdown(self):
self._Thread__stop()
def run(self):
while True:
self.session.next()
class GPSCollector:
"""
Collect GPS data
"""
def __init__(self, logger):
self.last_speed = 1.0
self.gps_poller = GPSPoller()
self.logger = logger
def run(self):
# start GPS polling thread
self.gps_poller.start()
# main execution loop
while True:
try:
time.sleep(1)
# process GPS data
session = self.gps_poller.session
if session.fix.mode == MODE_NO_FIX:
print "Waiting for GPS to fix..."
continue
if isnan(session.fix.time):
print "Invalid location:", session
continue
# if (session.fix.speed < 0.1) and (self.last_speed < 0.1):
# print "Waiting for speed..."
# continue
self.last_speed = session.fix.speed
print "Got data tc", session.utc
self.logger.add_sample([('location', {
u'lat': session.fix.latitude,
u'lon': session.fix.longitude,
u'alt': session.fix.altitude
}), ('speed', session.fix.speed)])
# time = session.utc
# location.loc_latitude = session.fix.latitude
# location.loc_longitude = session.fix.longitude
# location.loc_altitude = session.fix.altitude
# location.loc_speed = session.fix.speed
# location.loc_climb = session.fix.climb
# location.loc_track = session.fix.track
# if the time is valid the data record is valid
except KeyboardInterrupt:
print ('\n')
break
def shutdown(self):
if self.gps_poller:
self.gps_poller.shutdown()