| 1 | #!/usr/bin/python |
|---|
| 2 | |
|---|
| 3 | # Copyright (C) 2007 by Jaroslaw Zachwieja <grok!warwick.ac.uk> |
|---|
| 4 | # Copyright (C) 2008 by TJ <linux!tjworld.net> |
|---|
| 5 | # Published under the terms of GNU General Public License v2 or later. |
|---|
| 6 | # License text available at http://www.gnu.org/licenses/licenses.html#GPL |
|---|
| 7 | |
|---|
| 8 | import serial |
|---|
| 9 | import string |
|---|
| 10 | import sys |
|---|
| 11 | import getopt |
|---|
| 12 | |
|---|
| 13 | def usage(): |
|---|
| 14 | print "Usage:" |
|---|
| 15 | print " -p | --port <device> e.g. /dev/ttyS0" |
|---|
| 16 | print " -b | --baud <speed> e.g. 4800" |
|---|
| 17 | print " -f | --file <filename> e.g. /tmp/gps.kml" |
|---|
| 18 | print " -h | --help display options" |
|---|
| 19 | |
|---|
| 20 | def main(): |
|---|
| 21 | # defaults |
|---|
| 22 | serial_port = "/dev/ttyS0" |
|---|
| 23 | serial_baud = 4800 |
|---|
| 24 | file = './realtime/Realtime GPS.kml' |
|---|
| 25 | |
|---|
| 26 | try: |
|---|
| 27 | opts, args = getopt.getopt(sys.argv[1:], "p:b:f:h", ["port=", "baud=", "file=", "help"]) |
|---|
| 28 | except getopt.GetoptError: |
|---|
| 29 | usage() |
|---|
| 30 | sys.exit(1) |
|---|
| 31 | else: |
|---|
| 32 | for opt, arg in opts: |
|---|
| 33 | if opt in ("-p", "--port"): |
|---|
| 34 | serial_port = arg |
|---|
| 35 | elif opt in ("-b", "--baud"): |
|---|
| 36 | serial_baud = string.atof(arg) |
|---|
| 37 | elif opt in ("-f", "--file"): |
|---|
| 38 | file = arg |
|---|
| 39 | elif opt in ("-h", "--help"): |
|---|
| 40 | usage() |
|---|
| 41 | sys.exit(0) |
|---|
| 42 | else: |
|---|
| 43 | print "Unknown option" |
|---|
| 44 | |
|---|
| 45 | gps = serial.Serial(serial_port, serial_baud, timeout=1) |
|---|
| 46 | |
|---|
| 47 | print "Serving data from %s (%d baud) to %s" % (serial_port, serial_baud, file) |
|---|
| 48 | |
|---|
| 49 | latitude = 0 |
|---|
| 50 | longitude = 0 |
|---|
| 51 | speed = 0 |
|---|
| 52 | heading_in = 0 |
|---|
| 53 | altitude = 0 |
|---|
| 54 | range = 1000 |
|---|
| 55 | tilt = 30 |
|---|
| 56 | |
|---|
| 57 | while 1: |
|---|
| 58 | line = gps.readline() |
|---|
| 59 | datablock = line.split(',') |
|---|
| 60 | |
|---|
| 61 | if line[0:6] == '$GPRMC': |
|---|
| 62 | latitude_in = string.atof(datablock[3]) |
|---|
| 63 | longitude_in = string.atof(datablock[5]) |
|---|
| 64 | try: |
|---|
| 65 | altitude = string.atof(datablock[8]) |
|---|
| 66 | except ValueError: |
|---|
| 67 | # use last good value |
|---|
| 68 | altitude = altitude |
|---|
| 69 | speed_in = string.atof(datablock[7]) |
|---|
| 70 | try: |
|---|
| 71 | heading_in = string.atof(datablock[8]) |
|---|
| 72 | except ValueError: |
|---|
| 73 | # use last good value |
|---|
| 74 | heading_in = heading_in |
|---|
| 75 | if datablock[4] == 'S': |
|---|
| 76 | latitude_in = -latitude_in |
|---|
| 77 | if datablock[6] == 'W': |
|---|
| 78 | longitude_in = -longitude_in |
|---|
| 79 | |
|---|
| 80 | latitude_degrees = int(latitude_in/100) |
|---|
| 81 | latitude_minutes = latitude_in - latitude_degrees*100 |
|---|
| 82 | |
|---|
| 83 | longitude_degrees = int(longitude_in/100) |
|---|
| 84 | longitude_minutes = longitude_in - longitude_degrees*100 |
|---|
| 85 | |
|---|
| 86 | latitude = latitude_degrees + (latitude_minutes/60) |
|---|
| 87 | longitude = longitude_degrees + (longitude_minutes/60) |
|---|
| 88 | |
|---|
| 89 | speed = int(speed_in * 1.852) |
|---|
| 90 | range = ( ( speed / 100 ) * 350 ) + 650 |
|---|
| 91 | tilt = ( ( speed / 120 ) * 43 ) + 30 |
|---|
| 92 | heading = heading_in |
|---|
| 93 | |
|---|
| 94 | if speed < 10: |
|---|
| 95 | range = 200 |
|---|
| 96 | tilt = 30 |
|---|
| 97 | heading = 0 |
|---|
| 98 | |
|---|
| 99 | output = """<?xml version="1.0" encoding="UTF-8"?> |
|---|
| 100 | <kml xmlns="http://earth.google.com/kml/2.0"> |
|---|
| 101 | <Placemark> |
|---|
| 102 | <name>%s km/h</name> |
|---|
| 103 | <description>^</description> |
|---|
| 104 | <LookAt> |
|---|
| 105 | <longitude>%s</longitude> |
|---|
| 106 | <latitude>%s</latitude> |
|---|
| 107 | <range>%s</range> |
|---|
| 108 | <tilt>%s</tilt> |
|---|
| 109 | <heading>%s</heading> |
|---|
| 110 | </LookAt> |
|---|
| 111 | <Point> |
|---|
| 112 | <coordinates>%s,%s,%s</coordinates> |
|---|
| 113 | </Point> |
|---|
| 114 | </Placemark> |
|---|
| 115 | </kml>""" % (speed,longitude,latitude,range,tilt,heading,longitude,latitude,altitude) |
|---|
| 116 | |
|---|
| 117 | f=open(file, 'w') |
|---|
| 118 | f.write(output) |
|---|
| 119 | f.close() |
|---|
| 120 | |
|---|
| 121 | ser.close() |
|---|
| 122 | |
|---|
| 123 | if __name__ == "__main__": |
|---|
| 124 | main() |
|---|