-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpstrack.py
75 lines (64 loc) · 2.82 KB
/
gpstrack.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
#
# Python GPS Tracking Example
# SparkFun Electronics, A.Weiss
# Beerware: if you use this and meet me, you must buy me a beer
#
# Function:
# Takes GPS position and altitude data and plots it on a scaled map image of your
# choice. Altitude can also be displayed in a separate graph.
#
# The program has a console menu that allows you to configure your connection.
# The program will run with either a GPS moudle connected or no moudle connected.
# If a GPS is connected, the position and altitude data is automatically saved
# to a file called nmea.txt. If no GPS is connected, you must create your own
# nmea.txt and fill it with GPGGA NMEA sentences.
# A map needs to be created and saved as a file called map.png. When you create
# your map, note the lat and long of the bottom left and top right corner, in decimal
# degrees. Then enter this information into the global variables below. This way,
# your the border of your map image can be used as the graph mins and maxs.
# Once you have a map loaded and a GPS connected, you can run the program and select
# either your position to be displayed on your map, or display altitude on a separate
# graph. The maps are not updated in realtime, so you must close the map and run
# the map command again in order to read new data.
import sys
from pynmea import nmea
import matplotlib.pyplot as plt
import serial, time, sys, threading, datetime, shutil
######Global Variables#####################################################
# you must declare the variables as 'global' in the fxn before using#
ser = 0
lat = 0
long = 0
pos_x = 0
pos_y = 0
alt = 0
i = 0 #x units for altitude measurment
#adjust these values based on your location and map, lat and long are in decimal degrees
TRX = -105.1621 #top right longitude
TRY = 40.0868 #top right latitude
BLX = -105.2898 #bottom left longitude
BLY = 40.001 #bottom left latitude
BAUDRATE = 1200
lat_input = 0 #latitude of home marker
long_input = 0 #longitude of home marker
count = 0
######FUNCTIONS############################################################
def position():
#opens a the saved txt file, parses for lat and long, displays on map
global lat, long, lat_input, long_input, pos_x, pos_y, altitude
global BLX, BLY, TRX, TRY,count
f1 = open('nmea.txt', 'r') #open and read only
f2 = open('map.png', 'w'); #write info
for line in f1:
if(line[0] == '\0'):
f1.close()
break
elif(line[4] == 'G'): # $GPGGA
print line
gpgga = nmea.GPGGA()
gpgga.parse(line)
lats = gpgga.latitude
longs = gpgga.longitude
f2.write(lats)
f2.write(longs)
position()