# -*- coding:utf-8 -*- from ctypes import * import ctypes import numpy.ctypeslib as npct import numpy as np import matplotlib.pyplot as plt import time import threading import os from multiprocessing import Process from matplotlib.animation import FuncAnimation import pyqtgraph as pg from pyqtgraph.Qt import QtCore pg.setConfigOption('background', 'w') app = pg.mkQApp() win1 = pg.GraphicsLayoutWidget(show=True) p1 = win1.addPlot(colspan=2) p1.showGrid(x=True, y=True) p1.setXRange(1, 500) p1.setYRange(-5, 5) p1.setLabel(axis='left', text='g') p1.setLabel(axis='bottom', text='ms') p1.setTitle('acc_data') win1.nextRow() p = win1.addPlot(colspan=1) p.showGrid(x=True, y=True) p.setXRange(-1, 1) p.setYRange(-1, 1) p.setLabel(axis='left', text='y / m') # 靠左 p.setLabel(axis='bottom', text='x / m') p.setTitle('Traj') p.setAspectLocked() left_x_data = [] left_y_data = [] right_x_data = [] right_y_data = [] curve1 = p.plot(pen="r", name="left_pos") curve2 = p.plot(pen="b", name="right_pos") curve_acc_1 = p1.plot(pen="r", name="left_acc_z") curve_zupt_1 = p1.plot(pen="g", name="left_zupt") curve_acc_x_1 = p1.plot(pen="b", name="left_acc_x") curve_acc_y_1 = p1.plot(pen="k", name="left_acc_y") curve_acc_2 = p1.plot(pen="r", name="right_acc_z") curve_zupt_2 = p1.plot(pen="g", name="left_zupt") curve_acc_x_2 = p1.plot(pen="b", name="right_acc_x") curve_acc_y_2 = p1.plot(pen="k", name="right_acc_y") x = list(range(500)) right_acc_z_data = [0] * 500 right_acc_y_data = [0] * 500 right_acc_x_data = [0] * 500 left_acc_z_data = [0] * 500 left_acc_y_data = [0] * 500 left_acc_x_data = [0] * 500 left_zupt_data = [0] * 500 right_zupt_data = [0] * 500 is_exit = 0 listen_status = 1 SHOW_STATUS = 2 CAL_STATUS = 1 cur_status = CAL_STATUS R = threading.Lock() def update_pos(): # global left_x_data, left_y_data, right_x_data, right_y_data, curve1, curve2, listen_status, cur_status # global left_zupt_data, left_acc_z_data, left_acc_x_data, left_acc_y_data # global right_zupt_data, right_acc_z_data, right_acc_x_data, right_acc_y_data if (len(left_x_data) > 500): curve1.setData(left_x_data[-500:-1], left_y_data[-500:-1]) curve2.setData(right_x_data[-500:-1], right_y_data[-500:-1]) else: curve1.setData(left_x_data[:-1], left_y_data[:-1]) curve2.setData(right_x_data[:-1], right_y_data[:-1]) curve_zupt_1.setData(x, left_zupt_data) curve_acc_1.setData(x, left_acc_z_data) curve_acc_x_1.setData(x, left_acc_x_data) curve_acc_y_1.setData(x, left_acc_y_data) curve_zupt_2.setData(x, right_zupt_data) curve_acc_2.setData(x, right_acc_z_data) curve_acc_x_2.setData(x, right_acc_x_data) curve_acc_y_2.setData(x, right_acc_y_data) def calGameData(): global is_exit, listen_status, cur_status path = 'F:/work/matlab/手机数据显示/' dir_name = 'android_com.ouj.paoku_1644993592935.txt' ctypes.cdll.LoadLibrary( r"C:\Users\Administrator\source\repos\CMakeTestProject\CMakeTestProject\bin\SDK_LIB.dll") lib = ctypes.cdll.LoadLibrary( r"C:\Users\Administrator\source\repos\CMakeTestProject\CMakeTestProject\bin\python_interface.dll") lib.getGamePos.restype = ctypes.c_float lib.DanceObject(1) # 初始化静态变量为跳舞毯游戏 lib.gameProcess.argtypes = [ctypes.c_int, npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"), npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"), npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"), ctypes.c_int, ctypes.c_int, ctypes.c_int, npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"), npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"), npct.ndpointer(dtype=np.int, ndim=1, flags="C_CONTIGUOUS"), ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int ] with open(path + dir_name, "r") as f: for line in f.readlines(): data = line.strip('\n') data = data.split(',') data = list(map(int, data)) # print(data) right_pos_data = np.array(data[0:3], dtype=np.int) right_att_data = np.array(data[3:6], dtype=np.int) right_acc_data = np.array(data[6:9], dtype=np.int) right_front_mag = int(data[9]) right_back_mag = int(data[10]) right_zupt = int(data[11]) left_pos_data = np.array(data[12:15], dtype=np.int) left_att_data = np.array(data[15:18], dtype=np.int) left_acc_data = np.array(data[18:21], dtype=np.int) left_front_mag = int(data[21]) left_back_mag = int(data[22]) left_zupt = int(data[23]) down = int(data[24]) jump = int(data[25]) rssi = int(data[26]) posTimeStamp = int(data[27]) right_step = int(data[28]) left_step = int(data[29]) lib.gameProcess(posTimeStamp, right_pos_data, right_att_data, right_acc_data, right_zupt, right_front_mag, right_back_mag, left_pos_data, left_att_data, left_acc_data, left_zupt, left_front_mag, left_back_mag, jump, down, rssi) # 缓存加速度 left_acc_z_data[:-1] = left_acc_z_data[1:] left_acc_y_data[:-1] = left_acc_y_data[1:] left_acc_x_data[:-1] = left_acc_x_data[1:] left_acc_z_data[-1] = left_acc_data[2] / 2048.0 - 3 left_acc_y_data[-1] = left_acc_data[1] / 2048.0 - 3 left_acc_x_data[-1] = left_acc_data[0] / 2048.0 - 3 right_acc_z_data[:-1] = right_acc_z_data[1:] right_acc_y_data[:-1] = right_acc_y_data[1:] right_acc_x_data[:-1] = right_acc_x_data[1:] right_acc_z_data[-1] = right_acc_data[2] / 2048.0 + 1 right_acc_y_data[-1] = right_acc_data[1] / 2048.0 + 1 right_acc_y_data[-1] = right_acc_data[0] / 2048.0 + 1 left_zupt_data[0:499] = left_zupt_data[1:500] left_zupt_data[-1] = left_zupt - 3 right_zupt_data[0:499] = right_zupt_data[1:500] right_zupt_data[-1] = right_zupt + 1 left_pos_x = lib.getGamePos(1, 0) left_pos_y = lib.getGamePos(1, 1) left_x_data.append(left_pos_x) left_y_data.append(left_pos_y) right_pos_x = lib.getGamePos(0, 0) right_pos_y = lib.getGamePos(0, 1) right_x_data.append(right_pos_x) right_y_data.append(right_pos_y) time.sleep(0.01) while listen_status == 0: pass is_exit = 1 def listen_keybord_input(): global is_exit, listen_status, timer while is_exit == 0: print("test listen_keybord_input") if (input("stop") == " "): if (listen_status == 1): print("监听事件挂起...\n") listen_status = 0 elif (listen_status == 0): print("进入监听事件...\n") listen_status = 1 if __name__ == '__main__': t1 = threading.Thread(target=calGameData) t1.setDaemon(True) t1.start() # t2 = threading.Thread(target=listen_keybord_input) # t2.setDaemon(True) # t2.start() t = pg.QtCore.QTimer() t.timeout.connect(update_pos) t.start(10) app.exec_() os.system("PAUSE") # print(left_x_data)