This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgps_client.py
82 lines (70 loc) · 2.32 KB
/
gps_client.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
import threading
import gps
import os
class GPSClient():
def __init__(self):
self.gpssession = gps.gps("localhost", "2947")
self.gpssession.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
self.lat = 0
self.alt = 0
self.lon = 0
def update_GPS_coordinates(self):
try:
report = self.gpssession.next()
if report['class'] == 'TPV':
if hasattr(report, 'alt'):
self.alt = report.alt
if hasattr(report, 'lon'):
self.lon = report.lon
if hasattr(report, 'lat'):
self.lat = report.lat
except KeyError:
pass
except StopIteration:
self.gpssession = None
print "GPS has terminated"
def GPS_coordinate_avg(self, nb_iterations):
alt_nb_value = 0
lon_nb_value = 0
lat_nb_value = 0
alt_temp = 0
lon_temp = 0
lat_temp = 0
i = 0
while i < nb_iterations:
try:
report = self.gpssession.next()
# print(report)
if report['class'] == 'TPV':
if hasattr(report, 'alt'):
alt_temp += report.alt
alt_nb_value += 1
if hasattr(report, 'lon'):
lon_temp += report.lon
lon_nb_value += 1
if hasattr(report, 'lat'):
lat_temp += report.lat
lat_nb_value += 1
i += 1
print "GPS Progress", i, "/", nb_iterations
# print(lat_nb_value)
# print(alt_nb_value)
# print(lon_nb_value)
except KeyError:
print("keyerror")
pass
except StopIteration:
self.gpssession = None
print "GPS has terminated"
try:
self.lat = lat_temp / lat_nb_value
except ZeroDivisionError:
print "Lat_div_0"
try:
self.lon = lon_temp / lon_nb_value
except ZeroDivisionError:
print "Lon_div_0"
try:
self.alt = alt_temp / alt_nb_value
except ZeroDivisionError:
print "alt_div_0"