reshow_game_traj.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # -*- coding:utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. import ctypes
  4. import numpy.ctypeslib as npct
  5. import time
  6. import threading
  7. import numpy as np
  8. import time
  9. fig = plt.figure()
  10. ax = fig.add_subplot(1, 1, 1)
  11. ax.axis("equal")
  12. plt.ion()
  13. is_exit = 0
  14. listen_status = 1
  15. right_acc_z_data = [0] * 500
  16. left_acc_z_data = [0] * 500
  17. left_zupt_data = [0] * 500
  18. right_zupt_data = [0] * 500
  19. x = list(range(500))
  20. left_x_data = []
  21. left_y_data = []
  22. right_x_data = []
  23. right_y_data = []
  24. CAL_STATUS = 1
  25. SHOW_TATUS = 2
  26. cal_or_read = CAL_STATUS
  27. def update_pos():
  28. plt.ioff()
  29. plt.clf()
  30. ax.scatter(left_x_data, left_y_data, c='r')
  31. ax.scatter(right_x_data, right_y_data, c='b')
  32. plt.show()
  33. def calGameData():
  34. global is_exit, listen_status, cal_or_read
  35. num = 0
  36. path = './data_set/'
  37. dir_name = 'android_com.ouj.dance_1640243695795.txt'
  38. ctypes.cdll.LoadLibrary(
  39. r"C:\Users\Administrator\source\repos\CMakeTestProject\CMakeTestProject\bin\SDK_LIB.dll")
  40. lib = ctypes.cdll.LoadLibrary(r"C:\Users\Administrator\source\repos\CMakeTestProject\CMakeTestProject\bin\python_interface.dll")
  41. lib.getGamePos.restype = ctypes.c_float
  42. lib.DanceObject(2) # 初始化静态变量为跳舞毯游戏
  43. lib.gameProcess.argtypes = [ctypes.c_int,
  44. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  45. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  46. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  47. ctypes.c_int, ctypes.c_int,ctypes.c_int,
  48. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  49. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  50. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  51. ctypes.c_int, ctypes.c_int, ctypes.c_int,
  52. ctypes.c_int, ctypes.c_int, ctypes.c_int
  53. ]
  54. with open(path + dir_name, "r") as f:
  55. for line in f.readlines():
  56. data = line.strip('\n')
  57. data = data.split(',')
  58. data = list(map(int, data))
  59. # print(data)
  60. right_pos_data = np.array(data[0:3], dtype=np.int)
  61. right_att_data = np.array(data[3:6], dtype=np.int)
  62. right_acc_data = np.array(data[6:9], dtype=np.int)
  63. right_front_mag = int(data[9])
  64. right_back_mag = int(data[10])
  65. right_zupt = int(data[11])
  66. left_pos_data = np.array(data[12:15], dtype=np.int)
  67. left_att_data = np.array(data[15:18], dtype=np.int)
  68. left_acc_data = np.array(data[18:21], dtype=np.int)
  69. left_front_mag = int(data[21])
  70. left_back_mag = int(data[22])
  71. left_zupt = int(data[23])
  72. down = int(data[24])
  73. jump = int(data[25])
  74. rssi = int(data[26])
  75. posTimeStamp = int(data[27])
  76. right_step = int(data[28])
  77. left_step = int(data[29])
  78. # 缓存加速度
  79. left_acc_z_data[0:499] = left_acc_z_data[1:500]
  80. left_acc_z_data[-1] = left_acc_data[2] / 2048.0
  81. right_acc_z_data[0:499] = right_acc_z_data[1:500]
  82. right_acc_z_data[-1] = right_acc_data[2] / 2048.0 + 1
  83. left_zupt_data[0:499] = left_zupt_data[1:500]
  84. left_zupt_data[-1] = left_zupt
  85. right_zupt_data[0:499] = right_zupt_data[1:500]
  86. right_zupt_data[-1] = right_zupt + 1
  87. lib.gameProcess(posTimeStamp, right_pos_data, right_att_data, right_acc_data, right_zupt, right_front_mag, right_back_mag,
  88. left_pos_data, left_att_data, left_acc_data, left_zupt, left_front_mag, left_back_mag,
  89. jump, down, rssi)
  90. left_pos_x = lib.getGamePos(1, 0)
  91. left_pos_y = lib.getGamePos(1, 1)
  92. left_x_data.append(left_pos_x)
  93. left_y_data.append(left_pos_y)
  94. right_pos_x = lib.getGamePos(0, 0)
  95. right_pos_y = lib.getGamePos(0, 1)
  96. right_x_data.append(right_pos_x)
  97. right_y_data.append(right_pos_y)
  98. cal_or_read = SHOW_TATUS
  99. update_pos()
  100. time.sleep(0.01)
  101. print("num = ", num)
  102. num=num+1
  103. while listen_status == 0:
  104. pass
  105. is_exit = 1
  106. import os
  107. def file_name(file_dir):
  108. for root, dirs, files in os.walk(file_dir):
  109. print(np.shape(files), type(files), files)
  110. if __name__ == '__main__':
  111. calGameData()
  112. while is_exit == 0:
  113. pass
  114. plt.pause(0)