123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- # -*- 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)
|