Skip to content

Instantly share code, notes, and snippets.

@KitWallace
Created September 22, 2012 08:46
Show Gist options
  • Save KitWallace/3765575 to your computer and use it in GitHub Desktop.
Save KitWallace/3765575 to your computer and use it in GitHub Desktop.
Acquiring postion data with a GPS receiver
#!/usr/bin/python
import serial
class Position(object) :
def __init__(self) :
self.latitude = 0.0
self.longitude = 0.0
self.altitude = 0.0
self.courseOverGround = 0.0
self.speedOverGround = 0.0
self.HDOP = 0.0
self.satellites = 0
self.dateTime = ""
def update_with_RMC(self, sent) :
data= sent.split(",")
latitude=data[3]
latd=latitude[0:2]
latm=latitude[2:]
lat_dir = 1
if data[4] == "S" :
lat_dir = -1
self.latitude = (int(latd) + float(latm) / 60) * lat_dir
longitude=data[5]
longd=longitude[0:3]
longm=longitude[3:]
long_dir = 1
if data[6] == "W" :
long_dir = -1
self.longitude = (int(longd) + float(longm) / 60) * long_dir
time =data[1]
h = time[0:2]
m = time[2:4]
s = time[4:len(time)]
self.time = ":".join((h,m,s))
self.seconds = (int(h)*60 + int(m) )* 60 + float(s)
date= data[9]
day = date[0:2]
month = date[2:4]
year = date[4:6]
self.date = "-".join(("20"+year,month,day))
self.dateTime = self.date+"T"+self.time
self.speedOverGround = data[7]
self.courseOverGround = data[8]
def update_with_GGA(self, sent) :
data = sent.split(",")
self.altitude = data[9]
self.satellites = data[7]
self.HDOP = data[8]
def __str__(self) :
return " ".join((self.dateTime,str(round(self.latitude,5)),str(round(self.longitude,5)),self.altitude,"["+self.satellites+":"+ self.HDOP+"]"))
s = serial.Serial("/dev/ttyUSB0",4800)
position = Position()
while True:
nmea = s.readline()
if nmea.startswith("$GPRMC") :
position.update_with_RMC(nmea)
print position
elif nmea.startswith("$GPGGA") :
position.update_with_GGA(nmea)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment