2023-06-16 15:53:48 +02:00
|
|
|
from ipyleaflet import Map, Marker
|
|
|
|
import pynmea2
|
|
|
|
import serial
|
|
|
|
import io
|
2023-06-17 17:49:43 +02:00
|
|
|
import gpxpy
|
|
|
|
import gpxpy.gpx
|
|
|
|
import signal
|
|
|
|
import sys
|
2023-06-16 15:53:48 +02:00
|
|
|
|
2023-06-17 17:49:43 +02:00
|
|
|
def signal_handler(sig, frame):
|
|
|
|
gpx_file.write(gpx.to_xml())
|
|
|
|
gpx_file.truncate()
|
|
|
|
gpx_file.close()
|
|
|
|
print("Quitting")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
gpx_file = open('track.gpx', 'w')
|
|
|
|
gpx_file.seek(0)
|
|
|
|
gpx = gpxpy.gpx.GPX()
|
|
|
|
gpx_track = gpxpy.gpx.GPXTrack()
|
|
|
|
gpx.tracks.append(gpx_track)
|
|
|
|
gpx_segment = gpxpy.gpx.GPXTrackSegment()
|
|
|
|
gpx_track.segments.append(gpx_segment)
|
|
|
|
|
|
|
|
ser = serial.Serial('/dev/ttyACM1', 9600, timeout=5.0)
|
2023-06-16 15:53:48 +02:00
|
|
|
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
|
2023-06-17 17:49:43 +02:00
|
|
|
last_time = 0
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
2023-06-16 15:53:48 +02:00
|
|
|
|
2023-06-17 13:05:18 +02:00
|
|
|
while True:
|
2023-06-16 15:53:48 +02:00
|
|
|
try:
|
2023-06-17 13:05:18 +02:00
|
|
|
line = sio.readline().rstrip('\n') # Remove newline character
|
2023-06-16 15:53:48 +02:00
|
|
|
msg = pynmea2.parse(line)
|
2023-06-17 13:05:18 +02:00
|
|
|
latitude = msg.latitude
|
|
|
|
longitude = msg.longitude
|
2023-06-17 17:49:43 +02:00
|
|
|
# altitude = msg.altitude
|
|
|
|
# time = f"{msg.datestamp}T{msg.timestamp}Z"
|
2023-06-16 15:53:48 +02:00
|
|
|
except serial.SerialException as e:
|
2023-06-17 13:05:18 +02:00
|
|
|
# print('Device error: {}'.format(e))
|
2023-06-16 15:53:48 +02:00
|
|
|
break
|
|
|
|
except pynmea2.ParseError as e:
|
2023-06-17 13:05:18 +02:00
|
|
|
# print('Parse error: {}'.format(e))
|
2023-06-16 15:53:48 +02:00
|
|
|
continue
|
2023-06-17 10:55:28 +02:00
|
|
|
except Exception as e:
|
|
|
|
continue
|
2023-06-16 15:53:48 +02:00
|
|
|
else:
|
2023-06-17 17:49:43 +02:00
|
|
|
if last_time == 0:
|
2023-06-17 10:55:28 +02:00
|
|
|
center = (latitude, longitude)
|
|
|
|
m = Map(center=center, zoom=15)
|
|
|
|
marker = Marker(location=center, draggable=False)
|
2023-06-17 13:05:18 +02:00
|
|
|
m.add_layer(marker)
|
2023-06-17 10:55:28 +02:00
|
|
|
display(m)
|
2023-06-17 13:05:18 +02:00
|
|
|
last_time = time
|
2023-06-17 17:49:43 +02:00
|
|
|
last_latitude=latitude
|
|
|
|
last_longitude=longitude
|
|
|
|
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(latitude, longitude))
|
2023-06-17 10:55:28 +02:00
|
|
|
else:
|
2023-06-17 13:05:18 +02:00
|
|
|
marker.location = (latitude, longitude)
|
|
|
|
last_time = time
|
2023-06-17 17:49:43 +02:00
|
|
|
if not last_latitude==latitude or not last_longitude==longitude:
|
|
|
|
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(latitude, longitude))
|
|
|
|
print("Added gpx point")
|
|
|
|
last_latitude=latitude
|
|
|
|
last_longitude=longitude
|