Simsim.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import logging
  2. from collections import deque
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from matplotlib import cm
  6. from matplotlib.ticker import LinearLocator
  7. from target import Target
  8. from tracker import Tracker
  9. import sys
  10. import numpy
  11. numpy.set_printoptions(threshold=sys.maxsize)
  12. class Simsim:
  13. class Topics:
  14. def __init__(self) -> None:
  15. self.topics = dict()
  16. def add_topic(self, topic_name):
  17. self.topics[topic_name] = deque(maxlen=20)
  18. def del_topic(self, topic_name):
  19. try:
  20. del self.topics[topic_name]
  21. except:
  22. pass
  23. def publish(self, topic_name, msg):
  24. self.topics[topic_name].append(msg)
  25. def subs(self, topic_name):
  26. return self.topics[topic_name]
  27. def __init__(self) -> None:
  28. self.trackers = []
  29. self.edges = []
  30. self.targets = []
  31. self.map_size = [1000, 1000]
  32. self.rate = 30
  33. self.topics = self.Topics()
  34. plt.ion()
  35. # self.fig, (self.plt_sim, self.plt_pm) = plt.subplots(
  36. # 1, 2, figsize=(10, 5), dpi=160)
  37. fig = plt.figure(figsize=(20, 8))
  38. self.plt_sim = fig.add_subplot(121)
  39. # plt.axis('equal')
  40. self.plt_pm = fig.add_subplot(122, projection='3d')
  41. # plt.axis('equal')
  42. self.plt_sim.axis('equal')
  43. def add_tracker(self, name, position, sensor_rad):
  44. tracker = Tracker(self, name, len(self.trackers),
  45. position, sensor_rad)
  46. self.trackers.append(tracker)
  47. def add_edges(self, edges):
  48. self.edges.extend(edges)
  49. for e in edges:
  50. self.trackers[e[0]].neighbor.add(e[1])
  51. self.trackers[e[1]].neighbor.add(e[0])
  52. def add_target(self, name, position):
  53. target = Target(self, name, len(self.targets), position)
  54. self.targets.append(target)
  55. def _update_all(self):
  56. """Simulate once, update all trackers and targets
  57. """
  58. for tracker in self.trackers:
  59. tracker.job()
  60. for target in self.targets:
  61. target.job()
  62. def run(self, log_lvl=logging.WARN, ground_truth=False):
  63. logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
  64. datefmt='%m/%d/%Y %H:%M:%S', level=log_lvl)
  65. while 1:
  66. self._update_all()
  67. self.plt_sim.cla()
  68. self.plt_pm.cla()
  69. self.plt_sim.set_xlim(0, self.map_size[0])
  70. self.plt_sim.set_ylim(0, self.map_size[1])
  71. self.plt_pm.set_xlim(1000, 2000)
  72. self.plt_pm.set_ylim(1000, 2000)
  73. if ground_truth:
  74. for target in self.targets:
  75. self.plt_sim.scatter(
  76. target.position[0], target.position[1], marker='x', s=2, color='r')
  77. for e in self.edges:
  78. t0 = self.trackers[e[0]]
  79. t1 = self.trackers[e[1]]
  80. self.plt_sim.plot([t0.position[0], t1.position[0]],
  81. [t0.position[1], t1.position[1]],
  82. linewidth=1, color='g', alpha=0.5)
  83. Z = np.zeros([2000, 2000])
  84. for t in self.trackers:
  85. # draw the trackers
  86. self.plt_sim.scatter(t.position[0], t.position[1],
  87. marker='s', s=20, c='b')
  88. self.plt_sim.annotate(t.id, (t.position[0], t.position[1]+10))
  89. # draw the camera coverage
  90. circle = plt.Circle(
  91. (t.position), t.sensor.coverage_radius, fill=False, color='grey', alpha=0.3)
  92. self.plt_sim.add_patch(circle)
  93. for est in t.target_estimates:
  94. det_pos = est[0:2]
  95. # det_abs_pos = det_pos+t.position
  96. self.plt_sim.scatter(det_pos[0], det_pos[1],
  97. marker='^', s=2)
  98. # t = self.trackers[2]
  99. for ind in t.prob_map.prob_map:
  100. Z[ind] = t.prob_map.prob_map[ind]
  101. for dx in [-2, -1, 0, 1, 2]:
  102. for dy in [-2, -1, 0, 1, 2]:
  103. if Z[ind[0]+dx, ind[1]+dy] <= t.prob_map.prob_map[ind]:
  104. Z[ind[0]+dx, ind[1] +
  105. dy] = t.prob_map.prob_map[ind]
  106. # print(Z)
  107. X = np.arange(1000, 2000, 1)
  108. Y = np.arange(1000, 2000, 1)
  109. X, Y = np.meshgrid(X, Y)
  110. self.plt_pm.set_zlim(0, 1.01)
  111. # print(Z)
  112. self.plt_pm.plot_surface(
  113. X, Y, Z[1000:, 1000:], cmap='RdBu_r', rcount=150, ccount=150, antialiased=True)
  114. self.plt_pm.view_init(elev=35., azim=0)
  115. plt.gca().invert_yaxis()
  116. # self.plt_pm.plot_surface(X, Y, Z, cmap=cm.bwr, linewidth=5, antialiased=True)
  117. self.plt_pm.zaxis.set_major_locator(LinearLocator(10))
  118. plt.pause(1/self.rate)