main.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding:utf-8 -*-
  2. from ctypes import *
  3. import ctypes
  4. import numpy.ctypeslib as npct
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. import time
  8. import threading
  9. import os
  10. from multiprocessing import Process
  11. from matplotlib.animation import FuncAnimation
  12. import pyqtgraph as pg
  13. from pyqtgraph.Qt import QtCore
  14. pg.setConfigOption('background', 'w')
  15. app = pg.mkQApp()
  16. win1 = pg.GraphicsLayoutWidget(show=True)
  17. p1 = win1.addPlot(colspan=2)
  18. p1.showGrid(x=True, y=True)
  19. p1.setXRange(1, 500)
  20. p1.setYRange(-5, 5)
  21. p1.setLabel(axis='left', text='g')
  22. p1.setLabel(axis='bottom', text='ms')
  23. p1.setTitle('acc_data')
  24. win1.nextRow()
  25. p = win1.addPlot(colspan=1)
  26. p.showGrid(x=True, y=True)
  27. p.setXRange(-1, 1)
  28. p.setYRange(-1, 1)
  29. p.setLabel(axis='left', text='y / m') # 靠左
  30. p.setLabel(axis='bottom', text='x / m')
  31. p.setTitle('Traj')
  32. p.setAspectLocked()
  33. left_x_data = []
  34. left_y_data = []
  35. right_x_data = []
  36. right_y_data = []
  37. curve1 = p.plot(pen="r", name="left_pos")
  38. curve2 = p.plot(pen="b", name="right_pos")
  39. curve_acc_1 = p1.plot(pen="r", name="left_acc_z")
  40. curve_zupt_1 = p1.plot(pen="g", name="left_zupt")
  41. curve_acc_x_1 = p1.plot(pen="b", name="left_acc_x")
  42. curve_acc_y_1 = p1.plot(pen="k", name="left_acc_y")
  43. curve_acc_2 = p1.plot(pen="r", name="right_acc_z")
  44. curve_zupt_2 = p1.plot(pen="g", name="left_zupt")
  45. curve_acc_x_2 = p1.plot(pen="b", name="right_acc_x")
  46. curve_acc_y_2 = p1.plot(pen="k", name="right_acc_y")
  47. x = list(range(500))
  48. right_acc_z_data = [0] * 500
  49. right_acc_y_data = [0] * 500
  50. right_acc_x_data = [0] * 500
  51. left_acc_z_data = [0] * 500
  52. left_acc_y_data = [0] * 500
  53. left_acc_x_data = [0] * 500
  54. left_zupt_data = [0] * 500
  55. right_zupt_data = [0] * 500
  56. is_exit = 0
  57. listen_status = 1
  58. SHOW_STATUS = 2
  59. CAL_STATUS = 1
  60. cur_status = CAL_STATUS
  61. R = threading.Lock()
  62. def update_pos():
  63. # global left_x_data, left_y_data, right_x_data, right_y_data, curve1, curve2, listen_status, cur_status
  64. # global left_zupt_data, left_acc_z_data, left_acc_x_data, left_acc_y_data
  65. # global right_zupt_data, right_acc_z_data, right_acc_x_data, right_acc_y_data
  66. if (len(left_x_data) > 500):
  67. curve1.setData(left_x_data[-500:-1], left_y_data[-500:-1])
  68. curve2.setData(right_x_data[-500:-1], right_y_data[-500:-1])
  69. else:
  70. curve1.setData(left_x_data[:-1], left_y_data[:-1])
  71. curve2.setData(right_x_data[:-1], right_y_data[:-1])
  72. curve_zupt_1.setData(x, left_zupt_data)
  73. curve_acc_1.setData(x, left_acc_z_data)
  74. curve_acc_x_1.setData(x, left_acc_x_data)
  75. curve_acc_y_1.setData(x, left_acc_y_data)
  76. curve_zupt_2.setData(x, right_zupt_data)
  77. curve_acc_2.setData(x, right_acc_z_data)
  78. curve_acc_x_2.setData(x, right_acc_x_data)
  79. curve_acc_y_2.setData(x, right_acc_y_data)
  80. def calGameData():
  81. global is_exit, listen_status, cur_status
  82. path = 'F:/work/matlab/手机数据显示/'
  83. dir_name = 'android_com.ouj.paoku_1644993592935.txt'
  84. ctypes.cdll.LoadLibrary(
  85. r"C:\Users\Administrator\source\repos\CMakeTestProject\CMakeTestProject\bin\SDK_LIB.dll")
  86. lib = ctypes.cdll.LoadLibrary(
  87. r"C:\Users\Administrator\source\repos\CMakeTestProject\CMakeTestProject\bin\python_interface.dll")
  88. lib.getGamePos.restype = ctypes.c_float
  89. lib.DanceObject(1) # 初始化静态变量为跳舞毯游戏
  90. lib.gameProcess.argtypes = [ctypes.c_int,
  91. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  92. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  93. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  94. ctypes.c_int, ctypes.c_int, ctypes.c_int,
  95. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  96. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  97. npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"),
  98. ctypes.c_int, ctypes.c_int, ctypes.c_int,
  99. ctypes.c_int, ctypes.c_int, ctypes.c_int
  100. ]
  101. with open(path + dir_name, "r") as f:
  102. for line in f.readlines():
  103. data = line.strip('\n')
  104. data = data.split(',')
  105. data = list(map(int, data))
  106. # print(data)
  107. right_pos_data = np.array(data[0:3], dtype=np.int)
  108. right_att_data = np.array(data[3:6], dtype=np.int)
  109. right_acc_data = np.array(data[6:9], dtype=np.int)
  110. right_front_mag = int(data[9])
  111. right_back_mag = int(data[10])
  112. right_zupt = int(data[11])
  113. left_pos_data = np.array(data[12:15], dtype=np.int)
  114. left_att_data = np.array(data[15:18], dtype=np.int)
  115. left_acc_data = np.array(data[18:21], dtype=np.int)
  116. left_front_mag = int(data[21])
  117. left_back_mag = int(data[22])
  118. left_zupt = int(data[23])
  119. down = int(data[24])
  120. jump = int(data[25])
  121. rssi = int(data[26])
  122. posTimeStamp = int(data[27])
  123. right_step = int(data[28])
  124. left_step = int(data[29])
  125. lib.gameProcess(posTimeStamp, right_pos_data, right_att_data, right_acc_data, right_zupt, right_front_mag,
  126. right_back_mag,
  127. left_pos_data, left_att_data, left_acc_data, left_zupt, left_front_mag, left_back_mag,
  128. jump, down, rssi)
  129. # 缓存加速度
  130. left_acc_z_data[:-1] = left_acc_z_data[1:]
  131. left_acc_y_data[:-1] = left_acc_y_data[1:]
  132. left_acc_x_data[:-1] = left_acc_x_data[1:]
  133. left_acc_z_data[-1] = left_acc_data[2] / 2048.0 - 3
  134. left_acc_y_data[-1] = left_acc_data[1] / 2048.0 - 3
  135. left_acc_x_data[-1] = left_acc_data[0] / 2048.0 - 3
  136. right_acc_z_data[:-1] = right_acc_z_data[1:]
  137. right_acc_y_data[:-1] = right_acc_y_data[1:]
  138. right_acc_x_data[:-1] = right_acc_x_data[1:]
  139. right_acc_z_data[-1] = right_acc_data[2] / 2048.0 + 1
  140. right_acc_y_data[-1] = right_acc_data[1] / 2048.0 + 1
  141. right_acc_y_data[-1] = right_acc_data[0] / 2048.0 + 1
  142. left_zupt_data[0:499] = left_zupt_data[1:500]
  143. left_zupt_data[-1] = left_zupt - 3
  144. right_zupt_data[0:499] = right_zupt_data[1:500]
  145. right_zupt_data[-1] = right_zupt + 1
  146. left_pos_x = lib.getGamePos(1, 0)
  147. left_pos_y = lib.getGamePos(1, 1)
  148. left_x_data.append(left_pos_x)
  149. left_y_data.append(left_pos_y)
  150. right_pos_x = lib.getGamePos(0, 0)
  151. right_pos_y = lib.getGamePos(0, 1)
  152. right_x_data.append(right_pos_x)
  153. right_y_data.append(right_pos_y)
  154. time.sleep(0.01)
  155. while listen_status == 0:
  156. pass
  157. is_exit = 1
  158. def listen_keybord_input():
  159. global is_exit, listen_status, timer
  160. while is_exit == 0:
  161. print("test listen_keybord_input")
  162. if (input("stop") == " "):
  163. if (listen_status == 1):
  164. print("监听事件挂起...\n")
  165. listen_status = 0
  166. elif (listen_status == 0):
  167. print("进入监听事件...\n")
  168. listen_status = 1
  169. if __name__ == '__main__':
  170. t1 = threading.Thread(target=calGameData)
  171. t1.setDaemon(True)
  172. t1.start()
  173. # t2 = threading.Thread(target=listen_keybord_input)
  174. # t2.setDaemon(True)
  175. # t2.start()
  176. t = pg.QtCore.QTimer()
  177. t.timeout.connect(update_pos)
  178. t.start(10)
  179. app.exec_()
  180. os.system("PAUSE")
  181. # print(left_x_data)