test3d.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import mysql.connector
  2. import time
  3. import threading
  4. from queue import Queue
  5. from trilateration3d import trilateration3d
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. # 基站列表及坐标
  9. reader_ids = ['10001', '10006', '10005', '10007']
  10. # 基站坐标
  11. MapBSList = {'10001': [8.43, 14.35, 11.5], '10006': [5.59, -4.65, 1.5], '10005': [13.85, 0, 1.5], '10007': [0, 0, 1.5]}
  12. # 场地坐标
  13. field_vertices = {'begin_x': 0, 'begin_y': -10, 'begin_z': 0, 'end_x': 20, 'end_y': 20, 'end_z': 30}
  14. x = []
  15. y1 = []
  16. y2 = []
  17. y3 = []
  18. y4 = []
  19. loc_x = []
  20. loc_y = []
  21. loc_z = []
  22. class DbThread(threading.Thread):
  23. def __init__(self, dist_queue, interval):
  24. threading.Thread.__init__(self)
  25. self.dist_queue = dist_queue
  26. self.interval = interval
  27. def run(self):
  28. # while True:
  29. self.conn = mysql.connector.connect(
  30. # host="172.31.239.56",
  31. host="121.42.8.157",
  32. user="root",
  33. # port=13306,
  34. port=13307,
  35. password="~cs3x@2022#123~",
  36. database="xcjkdba"
  37. )
  38. self.cursor = self.conn.cursor()
  39. # now = datetime.now() # 获取当前时间
  40. # two_seconds_ago = (now - timedelta(seconds=2)).strftime('%Y-%m-%d %H:%M:%S') # 获取两秒之前的时间
  41. # one_second_ago = (now - timedelta(seconds=1)).strftime('%Y-%m-%d %H:%M:%S')
  42. # sqlstr = "SELECT card_id, loc_time, distances FROM his_location_cell_card_tof_aodi where card_id = 0001 and loc_time >= '" + two_seconds_ago + "' AND loc_time < '" + one_second_ago + "'" # + "' limit 1"
  43. sqlstr = """SELECT card_id, loc_time, distances FROM his_location_cell_card_tof_aodi WHERE card_id = '0001' AND loc_time BETWEEN '2023-05-06 17:19:00' AND '2023-05-06 18:21:00' """
  44. self.cursor.execute(sqlstr)
  45. results = self.cursor.fetchall()
  46. # print(results)
  47. for row in results:
  48. # print(row)
  49. templist = row[2].split(",")
  50. dist_data = {}
  51. for i in range(0, len(templist), 2):
  52. dist_data[templist[i + 1]] = float(templist[i])
  53. self.dist_queue.put(dist_data)
  54. self.cursor.close()
  55. self.conn.close()
  56. time.sleep(self.interval)
  57. def update_dist_data(dist_data):
  58. if "10001" in dist_data:
  59. y1.append(dist_data['10001'])
  60. else:
  61. y1.append(None)
  62. if "10006" in dist_data:
  63. y3.append(dist_data['10006'])
  64. else:
  65. y3.append(None)
  66. if "10005" in dist_data:
  67. y4.append(dist_data['10005'])
  68. else:
  69. y4.append(None)
  70. if "10007" in dist_data:
  71. y2.append(dist_data['10007'])
  72. else:
  73. y2.append(None)
  74. if len(y1) > 100:
  75. x.pop(0)
  76. y1.pop(0)
  77. y3.pop(0)
  78. y4.pop(0)
  79. y2.pop(0)
  80. x.append(x[-1] + 1)
  81. else:
  82. x.append(len(x) + 1)
  83. def update_loc_data(dist_data):
  84. if len(dist_data) >= 4:
  85. center = trilateration3d(MapBSList, dist_data)
  86. if center is not None:
  87. loc_x.append(center.x)
  88. loc_y.append(center.y)
  89. loc_z.append(center.z)
  90. print(center.x, center.y, center.z)
  91. return center.x, center.y, center.z
  92. else:
  93. print("center is None")
  94. return None
  95. if len(loc_y) > 10:
  96. loc_x.pop(0)
  97. loc_y.pop(0)
  98. loc_z.pop(0)
  99. dist_queue = Queue()
  100. loc_queue = Queue()
  101. db_thread = DbThread(dist_queue, interval=0.5)
  102. db_thread.start()
  103. fig = plt.figure()
  104. ax = fig.add_subplot(111, projection='3d')
  105. plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
  106. ax.set_title(" 3D Scatter Plot", size=15)
  107. ax.set_xticks(np.arange(0, 10, 1))
  108. ax.set_yticks(np.arange(0, 10, 1))
  109. ax.set_zticks(np.arange(0, 10, 1))
  110. ax.tick_params(labelsize=8)
  111. ax.set_xlim(0, 10)
  112. ax.set_ylim(0, 10)
  113. ax.set_zlim(0, 10)
  114. # 初始化一个空的散点图对象
  115. sc = ax.scatter([], [], [], c='r', marker='o')
  116. # 初始化数据
  117. x_data = []
  118. y_data = []
  119. z_data = []
  120. while True:
  121. if not dist_queue.empty():
  122. dist_data = dist_queue.get()
  123. update_dist_data(dist_data)
  124. uld = update_loc_data(dist_data)
  125. if uld:
  126. x_data.append(uld[0])
  127. y_data.append(uld[1])
  128. z_data.append(uld[2])
  129. if len(x_data) > 5:
  130. x_data.pop(0)
  131. y_data.pop(0)
  132. z_data.pop(0)
  133. # 更新散点图的数据
  134. # scatter._offsets3d = (x_data, y_data, z_data)
  135. sc.remove()
  136. sc = ax.scatter(x_data, y_data, z_data, c='r', marker='o', s=5)
  137. # 重新绘制图形
  138. plt.draw()
  139. plt.pause(0.25)
  140. time.sleep(0.25)