-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGpsData.py
More file actions
112 lines (103 loc) · 3.58 KB
/
Copy pathGpsData.py
File metadata and controls
112 lines (103 loc) · 3.58 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# GpsData.py
#
# Solar-boat Project 2019
# created on: 2019/07/29
# Author: Xu Guanglei
#
import copy
import threading
import time
from micropyGPS import MicropyGPS
from serial import Serial
class GpsData:
def __init__(self):
self.timestamp = [0, 0, 0.0]
self.timestamp_string = ""
self.latitude = 0.0
self.longitude = 0.0
self.altitude = 0.0
self.speed = []
self.course = 0.0
self.satellites_used = []
self.satellite_data = {}
try:
self.serial = Serial("/dev/ttyACM0", 9600, timeout=10)
except:
self.serial = Serial("/dev/ttyACM0", 9600, timeout=10)
print(
"Exception occurred in receiving GPS data. Switching to another serial port."
)
self.gps = MicropyGPS(9, "dd")
self.gpsthread = threading.Thread(target=self.run_gps, args=())
self.gpsthread.daemon = True
self.gpsthread.start()
def run_gps(self):
s = self.serial
s.reset_input_buffer()
s.readline()
while True:
try:
sentence = s.readline().decode("utf-8")
except UnicodeDecodeError:
s.reset_input_buffer()
continue
try:
if sentence[0] != "$":
continue
except IndexError as e:
print(
"No data incoming. Check raspi-config and disable Linux serial console: https://www.raspberrypi.org/documentation/configuration/uart.md#:~:text=Disable%20Linux%20serial%20console&text=This%20can%20be%20done%20by,Select%20option%20P6%20%2D%20Serial%20Port."
)
raise e
else:
for x in sentence:
self.gps.update(x)
def read(self):
try:
if self.gps.clean_sentences > 20:
h = (
self.gps.timestamp[0]
if self.gps.timestamp[0] < 24
else self.gps.timestamp[0] - 24
)
self.timestamp[0] = h
self.timestamp[1] = self.gps.timestamp[1]
self.timestamp[2] = self.gps.timestamp[2]
t = self.timestamp
self.timestamp_string = f"{t[0]:2d}:{t[1]:02d}:{t[2]:04.1f}"
self.latitude = self.gps.latitude[0]
self.longitude = self.gps.longitude[0]
self.altitude = self.gps.altitude
self.course = self.gps.course
self.speed = copy.deepcopy(self.gps.speed)
self.satellites_used = copy.deepcopy(self.gps.satellites_used)
self.satellite_data = copy.deepcopy(self.gps.satellite_data)
return True
else:
return False
except:
print("Error during reading from GPS")
def print_log(self):
t = self.timestamp
lat = self.latitude
lon = self.longitude
alt = self.altitude
print(f"time: {t[0]:02}:{t[1]:02}:{t[2]:.1f}")
print(f"latitude: {lat:.5f}, longitude: {lon:.5f}")
print(f"altitude: {alt:f}")
print(f"course: {self.course:f}")
print(f"speed: {self.speed}")
print(f"Satellites Used: {self.satellites_used}")
for k, v in self.satellite_data.items():
print(f"{k:02}: {v}")
print("")
return
if __name__ == "__main__":
gps_data = GpsData()
while True:
time.sleep(1.0)
if gps_data.read():
gps_data.print_log()