gps-sim.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. # Script to test GPS positioning by simulating output of a GPS module.
  21. #
  22. # Outputs to stdout
  23. #
  24. import getopt
  25. import sys
  26. import time
  27. import math
  28. import datetime
  29. DT = 0.5
  30. DS = 2
  31. DTUPDATE = 2.0
  32. ITER_COUNT = -1
  33. LOCK_DELAY = 0
  34. DSTEP = 16
  35. argv = sys.argv
  36. def show_help(ofp):
  37. ofp.write("\nusage: gps-sim.py [-dt dt] [-ds ds] [-p lat0,lon0] [-p lat1,lon1]\n")
  38. ofp.write("\n")
  39. ofp.write(" -dt dt time between waypoints (default " + str(DT) + "seconds)\n")
  40. ofp.write(" -ds ds time to stop at waypoint (default" + str(DS) + "seconds)\n")
  41. ofp.write(" -p lat,lon[,dt[,ds]] latitudie, longitude of waypoint with optional dt, ds (can specify multiple times)\n")
  42. ofp.write(" -f fn file containing lat,lon positions\n")
  43. ofp.write(" -t t time between print updates (default " + str(DTUPDATE) + " seconds)\n")
  44. ofp.write(" -n n number of iterations (<0 for infinite, default " + str(ITER_COUNT) + ")\n")
  45. ofp.write(" --lock-delay d time to delay before inital gps lock (default " + str(LOCK_DELAY) + ")\n")
  46. ofp.write("\n")
  47. def parse_latlon(s):
  48. v = s.strip("\n").split(",")
  49. if len(v) < 2: return []
  50. if len(v) > 4: return []
  51. lat = float(v[0])
  52. lon = float(v[1])
  53. dt = DT
  54. ds = DS
  55. if len(v) > 2:
  56. dt = float(v[2])
  57. if len(v) > 3:
  58. ds = float(v[3])
  59. return [lat, lon, dt, ds]
  60. def load_latlon_csv(fn):
  61. latlon = []
  62. with open(fn) as fp:
  63. for line in fp:
  64. v = parse_latlon(line)
  65. latlon.append(v)
  66. return latlon
  67. def nmea_checksum(msg):
  68. cksum = 0
  69. for idx in range(len(msg)):
  70. c = ord(msg[idx])
  71. cksum = cksum ^ (c & 0xff)
  72. return ("%02X" % cksum)
  73. #tstmsg = [ "$GPGSV,3,1,12,17,43,264,38,08,12,066,38,28,65,320,35,11,57,059,32*73",
  74. # "$GPGSV,3,2,12,07,23,180,32,19,21,254,32,01,59,075,31,13,08,275,25*7C",
  75. # "$GPGSV,3,3,12,22,18,106,25,30,47,210,15,18,38,053,,04,,,*44",
  76. # "$GPGSV,4,4,13,04,,,*7F",
  77. # "$GPGGA,040341.1,4226.692702,N,07630.419493,W,1,07,1.0,117.5,M,0,M,,*6D",
  78. # "$GPVTG,NaN,T,,M,0.0,N,0.0,K,A*42",
  79. # "$GPRMC,040341.1,A,4226.692702,N,07630.419493,W,0.0,0.0,050500,,,A*75",
  80. # "$GPGSA,A,3,01,07,08,11,17,19,28,,,,,,1.5,1.0,1.2*3E",
  81. # "$GPGSV,3,1,12,08,12,066,38,17,43,264,38,28,65,320,35,11,57,059,33*72",
  82. # "$GPGSV,3,2,12,07,23,180,32,01,59,075,32,19,21,254,32,22,18,106,26*78",
  83. # "$GPGSV,3,3,12,13,08,275,25,30,47,210,17,18,38,053,,04,,,*42",
  84. # "$GPGSV,4,4,13,04,,,*7F",
  85. # "$GPGGA,040344.1,4226.692722,N,07630.419464,W,1,07,1.0,117.3,M,0,M,,*64",
  86. # "$GPVTG,NaN,T,,M,0.0,N,0.0,K,A*42",
  87. # "$GPRMC,040344.1,A,4226.692722,N,07630.419464,W,0.0,0.0,050500,,,A*7A",
  88. # "$GPGSA,A,3,01,07,08,11,17,19,28,,,,,,1.5,1.0,1.2*3E" ]
  89. #
  90. #for msg in tstmsg:
  91. # s = msg[1:-3]
  92. # print s, "(", msg, ")"
  93. # print " >>", nmea_checksum(s)
  94. #
  95. #sys.exit(0)
  96. def print_gps_nmea_messages(ofp, lat, lon, utc_str = ""):
  97. if len(utc_str) == 0:
  98. n = datetime.datetime.utcnow()
  99. utc_str = n.strftime("%H") + n.strftime("%M") + n.strftime("%S") + "." + n.strftime("%f")[0:1]
  100. ofp.write("$GPGSV,4,1,13,08,12,066,38,28,65,320,36,17,43,264,36,01,59,075,32*79\n")
  101. ofp.write("$$GPGSV,4,2,13,11,57,059,32,07,23,180,31,19,21,254,29,22,18,106,29*79\n")
  102. ofp.write("$GPGSV,4,3,13,13,08,275,23,03,09,127,20,30,47,210,20,18,38,053,*7E\n")
  103. ofp.write("$GPGSV,4,4,13,04,,,*7F\n")
  104. msg = "GPGGA," + utc_str + "," + str(lat) + ",N," + str(lon) + ",W,1,08,0.9,116.9,M,0,M,,"
  105. ofp.write("$" + msg + "*" + nmea_checksum(msg) + "\n")
  106. ofp.write("$GPVTG,NaN,T,,M,0.0,N,0.0,K,A*42\n")
  107. msg = "GPRMC," + utc_str + ",A," + str(lat) + ",N," + str(lon) + ",W,0.0,0.0,050500,,,A"
  108. ofp.write("$" + msg + "*" + nmea_checksum(msg) + "\n")
  109. ofp.write("$GPGSA,A,3,01,07,08,11,17,19,22,28,,,,,1.5,0.9,1.1*35\n")
  110. latlon = []
  111. noarg = True
  112. ifn = ""
  113. argvv = []
  114. av = []
  115. for p in range(len(argv[1:])):
  116. if (p%2) == 0:
  117. av = [argv[p+1]]
  118. else:
  119. av.append(argv[p+1])
  120. argvv.append(av)
  121. av = []
  122. for argval in argvv:
  123. arg = argval[0]
  124. val = argval[1]
  125. if arg in ("-h", "--help"):
  126. show_help(sys.stdout)
  127. sys.exit(0)
  128. noarg = False
  129. elif arg in ("-dt"):
  130. DT = int(val)
  131. noarg = False
  132. elif arg in ("-ds"):
  133. DS = int(val)
  134. noarg = False
  135. elif arg in ("-p"):
  136. x = parse_latlon(val)
  137. latlon.append(x)
  138. noarg = False
  139. elif arg in ("-f"):
  140. ifn = val
  141. noarg = False
  142. elif arg in ("-n"):
  143. ITER_COUNT = int(val)
  144. elif arg in ("-t"):
  145. DTUPDATE = float(val)
  146. elif arg in ("--lock-delay"):
  147. LOCK_DELAY = float(val)
  148. if len(ifn)>0:
  149. latlon = load_latlon_csv(ifn)
  150. if len(latlon)==0:
  151. sys.stderr.write("Provide input file or lat/lon points\n")
  152. show_help(sys.stderr)
  153. sys.exit(-1)
  154. if noarg:
  155. show_help(sys.stderr)
  156. sys.exit(1)
  157. print "# dt,ds,t:", DT, DS, DTUPDATE
  158. print "# latlon:", latlon
  159. print "# iter_count:", ITER_COUNT
  160. print "# ifn:", ifn
  161. print "# lock_delay:", LOCK_DELAY
  162. curiter = 0
  163. if ITER_COUNT<0: curiter = ITER_COUNT-1
  164. lastupdate_sec = 0.0
  165. curidx = 0
  166. nidx = len(latlon)
  167. while curiter < ITER_COUNT:
  168. curpos = latlon[curidx]
  169. nxtpos = latlon[ (curidx+1)%nidx ]
  170. dlat = curpos[1] - curpos[0]
  171. dlon = curpos[1] - curpos[0]
  172. dpos = math.sqrt( (dlat*dlat) + (dlon*dlon) )
  173. for _step in range(DSTEP):
  174. _p = float(_step) / float(DSTEP)
  175. _pos = [ curpos[0] + _p*(nxtpos[0] - curpos[0]), curpos[1] + _p*(nxtpos[1] - curpos[1]) ]
  176. cursec = float(time.time())
  177. if (cursec - lastupdate_sec) > DTUPDATE:
  178. lastupdate_sec = cursec
  179. #sys.stdout.write(str(_pos[0]) + " " + str(_pos[1]) + "\n")
  180. print_gps_nmea_messages(sys.stdout, _pos[0], _pos[1])
  181. sys.stdout.flush()
  182. time.sleep(DS)
  183. curidx = (curidx + 1)%nidx
  184. time.sleep(DT)
  185. if ITER_COUNT>=0: curiter+=1