-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgps_decode.py
61 lines (52 loc) · 2.13 KB
/
gps_decode.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
import serial, pynmea2
from math import radians, cos, sin, asin, sqrt
def haversine(ln1, lt1, ln2, lt2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
ln1, lt1, ln2, lt2 = map(radians, [ln1, lt1, ln2, lt2])
# haversine formula
dlon = ln2 - ln1
dlat = lt2 - lt1
a = sin(dlat/2)**2 + cos(lt1) * cos(lt2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c # this is in KM, use 3959 for miles
return km
if __name__ == '__main__':
# set up serial connection - define the COM port/rfcomm device to use
# windows
# ser = serial.Serial('COM3', 9600)
# linux
ser = serial.Serial('/dev/rfcomm0', 9600)
streamreader = pynmea2.NMEAStreamReader()
lat1 = None
lat2 = None
lon1 = None
lon2 = None
while True:
gpsdata = ser.readline().decode('utf-8')
for msg in streamreader.next(gpsdata):
# not interested in proprietary messages
if isinstance(msg, pynmea2.nmea.ProprietarySentence):
continue
else:
if msg.sentence_type == 'GGA':
print('~~~~~~~~~~~~~~~')
# if this is the first reading then define lat2 and lon2
if lat2 is None:
lat2 = msg.latitude
lon2 = msg.longitude
else:
lat1 = lat2
lon1 = lon2
lat2 = msg.latitude
lon2 = msg.longitude
print('Distance travelled: ' + haversine(lon1, lat1, lon2, lat2).__str__())
print('Timestamp: ' + str(msg.timestamp))
print('Latitude: ' + msg.latitude.__str__())
print('Longitude: ' + msg.longitude.__str__())
# print('GPS quality: ' + msg.gps_qual.__str__())
print('Number of satellites: ' + msg.num_sats.__str__())
# print('Altitude: ' + msg.altitude.__str__() + msg.altitude_units.__str__())