gps-sim.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. DT = 0.5
  29. DS = 2
  30. DTUPDATE = 2.0
  31. ITER_COUNT = -1
  32. LOCK_DELAY = 0
  33. DSTEP = 16
  34. argv = sys.argv
  35. def show_help(ofp):
  36. ofp.write("\nusage: gps-sim.py [-dt dt] [-ds ds] [-p lat0,lon0] [-p lat1,lon1]\n")
  37. ofp.write("\n")
  38. ofp.write(" -dt dt time between waypoints (default " + str(DT) + "seconds)\n")
  39. ofp.write(" -ds ds time to stop at waypoint (default" + str(DS) + "seconds)\n")
  40. ofp.write(" -p lat,lon[,dt[,ds]] latitudie, longitude of waypoint with optional dt, ds (can specify multiple times)\n")
  41. ofp.write(" -f fn file containing lat,lon positions\n")
  42. ofp.write(" -t t time between print updates (default " + str(DTUPDATE) + " seconds)\n")
  43. ofp.write(" -n n number of iterations (<0 for infinite, default " + str(ITER_COUNT) + ")\n")
  44. ofp.write(" --lock-delay d time to delay before inital gps lock (default " + str(LOCK_DELAY) + ")\n")
  45. ofp.write("\n")
  46. def parse_latlon(s):
  47. v = s.strip("\n").split(",")
  48. if len(v) < 2: return []
  49. if len(v) > 4: return []
  50. lat = float(v[0])
  51. lon = float(v[1])
  52. dt = DT
  53. ds = DS
  54. if len(v) > 2:
  55. dt = float(v[2])
  56. if len(v) > 3:
  57. ds = float(v[3])
  58. return [lat, lon, dt, ds]
  59. def load_latlon_csv(fn):
  60. latlon = []
  61. with open(fn) as fp:
  62. for line in fp:
  63. v = parse_latlon(line)
  64. latlon.append(v)
  65. return latlon
  66. latlon = []
  67. noarg = True
  68. ifn = ""
  69. argvv = []
  70. av = []
  71. for p in range(len(argv[1:])):
  72. if (p%2) == 0:
  73. av = [argv[p+1]]
  74. else:
  75. av.append(argv[p+1])
  76. argvv.append(av)
  77. av = []
  78. for argval in argvv:
  79. arg = argval[0]
  80. val = argval[1]
  81. if arg in ("-h", "--help"):
  82. show_help(sys.stdout)
  83. sys.exit(0)
  84. noarg = False
  85. elif arg in ("-dt"):
  86. DT = int(val)
  87. noarg = False
  88. elif arg in ("-ds"):
  89. DS = int(val)
  90. noarg = False
  91. elif arg in ("-p"):
  92. x = parse_latlon(val)
  93. latlon.append(x)
  94. noarg = False
  95. elif arg in ("-f"):
  96. ifn = val
  97. noarg = False
  98. elif arg in ("-n"):
  99. ITER_COUNT = int(val)
  100. elif arg in ("-t"):
  101. DTUPDATE = float(val)
  102. elif arg in ("--lock-delay"):
  103. LOCK_DELAY = float(val)
  104. if len(ifn)>0:
  105. latlon = load_latlon_csv(ifn)
  106. if len(latlon)==0:
  107. sys.stderr.write("Provide input file or lat/lon points\n")
  108. show_help(sys.stderr)
  109. sys.exit(-1)
  110. if noarg:
  111. show_help(sys.stderr)
  112. sys.exit(1)
  113. print "# dt,ds,t:", DT, DS, DTUPDATE
  114. print "# latlon:", latlon
  115. print "# iter_count:", ITER_COUNT
  116. print "# ifn:", ifn
  117. print "# lock_delay:", LOCK_DELAY
  118. curiter = 0
  119. if ITER_COUNT<0: curiter = ITER_COUNT-1
  120. lastupdate_sec = 0.0
  121. curidx = 0
  122. nidx = len(latlon)
  123. while curiter < ITER_COUNT:
  124. curpos = latlon[curidx]
  125. nxtpos = latlon[ (curidx+1)%nidx ]
  126. dlat = curpos[1] - curpos[0]
  127. dlon = curpos[1] - curpos[0]
  128. dpos = math.sqrt( (dlat*dlat) + (dlon*dlon) )
  129. for _step in range(DSTEP):
  130. _p = float(_step) / float(DSTEP)
  131. _pos = [ curpos[0] + _p*(nxtpos[0] - curpos[0]), curpos[1] + _p*(nxtpos[1] - curpos[1]) ]
  132. cursec = float(time.time())
  133. if (cursec - lastupdate_sec) > DTUPDATE:
  134. lastupdate_sec = cursec
  135. #print _p, _pos
  136. sys.stdout.write(str(_pos[0]) + " " + str(_pos[1]) + "\n")
  137. sys.stdout.flush()
  138. time.sleep(DS)
  139. curidx = (curidx + 1)%nidx
  140. time.sleep(DT)
  141. if ITER_COUNT>=0: curiter+=1