00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 import struct
00026 import kml
00027 import sys
00028 import getopt
00029 import os, pwd
00030
00031
00032
00033
00034
00035 try:
00036 import serial
00037 except ImportError:
00038 print("You need to have python-serial installed to run km.")
00039 print("Get it from your package manager or http://pyserial.sourceforge.net.")
00040 sys.exit(2)
00041
00042
00043 download_dir = os.getenv('HOME')
00044
00045 def file_to_serial(f, s):
00046 """Send the given file (f) to the given open serial port (s)"""
00047 Str=f.read()
00048 s.write(Str)
00049
00050 def read_waypoints(ser):
00051
00052 f=open('../../share/km/waypoints.bin','rb')
00053 file_to_serial(f, ser)
00054 f.close()
00055
00056 binary_garbage = open(download_dir + '/waypoint_garbage.bin', 'ab')
00057 ways = open(download_dir + '/waypoints.text','a')
00058
00059 binary_garbage.write(ser.read(3))
00060 waypoint_count = 0
00061 while True:
00062
00063 temp_hundredth = struct.unpack(">B", ser.read(1))[0]
00064
00065
00066 check_next = ser.read(1)
00067 if check_next == '':
00068 break
00069 else:
00070 waypoint_count = waypoint_count + 1
00071 temp_tenth = struct.unpack(">B", check_next)[0]
00072 temp_unit = struct.unpack(">B", ser.read(1))[0]
00073 waypoint_number = (temp_hundredth - 48) * 100 + (temp_tenth - 48) * 10 + (temp_unit - 48) * 1
00074 ways.write('\n')
00075 ways.write(str(waypoint_number))
00076
00077 binary_garbage.write(ser.read(3))
00078
00079 icon = struct.unpack(">H", ser.read(2))[0]
00080 ways.write(' ')
00081 ways.write(str(icon))
00082
00083 elevation = struct.unpack(">H", ser.read(2))[0]
00084 ways.write(' ')
00085 ways.write(str(elevation))
00086
00087 x = struct.unpack(">i", ser.read(4))[0]
00088 ways.write(' ')
00089 ways.write(str(x))
00090 y = struct.unpack(">i", ser.read(4))[0]
00091 ways.write(' ')
00092 ways.write(str(y))
00093 ways.close()
00094 binary_garbage.close()
00095 print " Got ", waypoint_count, " WayPoints."
00096
00097 def read_track(ser):
00098 tracks = open(download_dir + '/trackpoints.text','a')
00099 tracks.write('\n\n')
00100 binary_garbage = open(download_dir + '/trackpoint_garbage.bin', 'ab')
00101
00102 binary_garbage.write(ser.read(1))
00103
00104 binary_garbage.write(ser.read(2))
00105
00106 year = struct.unpack(">B", ser.read(1))[0]
00107 tracks.write('20'+str(year))
00108 tracks.write('-')
00109
00110 month = struct.unpack(">B", ser.read(1))[0]
00111 tracks.write(str(month))
00112 tracks.write('-')
00113
00114 day = struct.unpack(">B", ser.read(1))[0]
00115 tracks.write(str(day))
00116 tracks.write(':')
00117
00118 hour = struct.unpack(">B", ser.read(1))[0]
00119 tracks.write(str(hour))
00120 tracks.write(':')
00121
00122 minute = struct.unpack(">B", ser.read(1))[0]
00123 tracks.write(str(minute))
00124 tracks.write(':')
00125
00126 second = struct.unpack(">B", ser.read(1))[0]
00127 tracks.write(str(second))
00128 tracks.write(' ')
00129
00130 duration = struct.unpack(">i", ser.read(4))[0]
00131
00132 kml_name = download_dir+ '/20' + str(year) + '-' + str(month) + '-' + str(day) + 'T' + str(hour) + ':' + str(minute) + ':' + str(second)
00133
00134 kml_file = kml.kml(kml_name)
00135
00136 tracks.write(' \nTraining duration : ')
00137 tracks.write(str(duration))
00138
00139 distance = struct.unpack(">i", ser.read(4))[0]
00140 tracks.write(' \nTraining distance : ')
00141 tracks.write(str(distance))
00142
00143 number_of_points = struct.unpack(">H", ser.read(2))[0]
00144 tracks.write(' \nNumber of points : ')
00145 tracks.write(str(number_of_points + 1))
00146
00147 binary_garbage.write(ser.read(2))
00148
00149 for i in range(0,number_of_points):
00150 x = struct.unpack(">i", ser.read(4))[0]
00151 tracks.write(' \n')
00152 tracks.write(str(i))
00153 tracks.write(' : ')
00154 tracks.write(str(x))
00155 y = struct.unpack(">i", ser.read(4))[0]
00156 tracks.write(' ')
00157 tracks.write(str(y))
00158
00159 kml_file.add_point(y/100000.0, x/100000.0)
00160
00161
00162 binary_garbage.write(ser.read(1))
00163 kml_file.close()
00164 binary_garbage.close()
00165 tracks.close()
00166 print " Got ", number_of_points + 1, " points."
00167 return (year + month + day + hour + minute + second + duration + distance + number_of_points)
00168
00169
00170 def read_trackpoints(ser):
00171 print(" Getting TrackPoints set 1.")
00172
00173 f=open('../../share/km/firsttrackpoints.bin','rb')
00174 file_to_serial(f, ser)
00175 f.close()
00176
00177
00178 is_valid = read_track(ser)
00179
00180 trackpoints_count = 1
00181
00182
00183 while is_valid != 0:
00184
00185 trackpoints_count = trackpoints_count + 1
00186 print " Getting TrackPoints set ", trackpoints_count
00187 f=open('../../share/km/nexttrackpoints.bin','rb')
00188 file_to_serial(f, ser)
00189 f.close()
00190 is_valid = read_track(ser)
00191
00192
00193 def configure_serial_port(which_port):
00194 try:
00195 ser = serial.Serial(which_port, 4800)
00196 except serial.serialutil.SerialException:
00197 print "Serial port ", which_port, "can't be opened."
00198 print("You can specify which serial port to use with -d option.")
00199 sys.exit(2)
00200 print("Configuring serial port")
00201 ser.bytesize=serial.EIGHTBITS
00202 ser.parity=serial.PARITY_NONE
00203 ser.stopbits=serial.STOPBITS_ONE
00204 ser.timeout=2
00205 ser.xonxoff=0
00206 ser.rtscts=0
00207 ser.flushInput()
00208 ser.flushOutput()
00209 return ser
00210
00211
00212 def usage():
00213 print("Usage km-downloader [OPTION]")
00214 print(" -h, --help print this help and exit")
00215 print(" -v, --version print version information and exit.")
00216 print(" -d, --device specify which serial device to use.")
00217 print(" -o, --output-directory specify where to write output files.")
00218 print(" -w, --waypoints download waypoints.")
00219 print(" -t, --trackpoints download trackpoints.")
00220 print("For more informations, please see:")
00221 print("<http://gpsd4.tuxfamily.org>.")
00222
00223
00224 def version():
00225 print("km 0.9")
00226 print("Copyright (C) 2008 Julien TOUS.")
00227 print("This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.")
00228 print("Written by Julien TOUS and Vincent-Xavier JUMEL")
00229
00230
00231 def main(argv):
00232 try:
00233 opts, args = getopt.getopt(argv, "hvd:o:wt", ["help", "version", "device=", "output-directory=", "waypoints", "trackpoints"])
00234 except getopt.GetoptError:
00235 usage()
00236 sys.exit(2)
00237
00238
00239
00240 serial_port = '/dev/ttyUSB0'
00241
00242
00243 get_waypoints = False
00244 get_trackpoints = False
00245
00246 for opt, arg in opts:
00247 if opt in ("-h", "--help"):
00248 usage()
00249 sys.exit()
00250 elif opt in ("-v", "--version"):
00251 version()
00252 sys.exit()
00253 elif opt in ("-d", "--device"):
00254 serial_port = arg
00255 elif opt in ("-w", "--waypoints"):
00256 get_waypoints = True
00257 elif opt in ("-t", "--trackpoints"):
00258 get_trackpoints = True
00259 elif opt in ("-o", "--output-directory"):
00260 download_dir = arg
00261
00262
00263 if ( (get_trackpoints == False) and (get_waypoints == False) ):
00264 get_waypoints = True
00265 get_trackpoints = True
00266
00267
00268
00269 ser = configure_serial_port(serial_port)
00270
00271 if get_trackpoints:
00272
00273 print "Getting TrackPoints"
00274 read_trackpoints(ser)
00275
00276 if get_waypoints:
00277
00278 print "Getting WayPoints"
00279 read_waypoints(ser)
00280
00281
00282 ser.close()
00283
00284
00285 quit()
00286
00287 if __name__ == '__main__':
00288 main(sys.argv[1:])