This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.py
executable file
·187 lines (159 loc) · 4.24 KB
/
collect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from itertools import count
import os
import time
import json
import asyncio
import threading
_BLEAK='_BLEAK'
_READ='_READ'
_SOCKET='_SOCKET'
cfg=json.load(open(os.path.join(os.path.dirname(__file__),'collect.json'),'rb'))
MODE=cfg['mode']
DEBUG=cfg['debug']
def pt(s:str,flush:bool=False)->None:
if flush or DEBUG:
print(s,flush=True)
if MODE==_SOCKET:
import socket
if MODE==_BLEAK:
try:
from bleak import BleakClient
except:
pt('BFailed: No module bleak')
MODE=_READ
alpha=cfg['alpha']
devices=[cfg[i] for i in cfg['collect']]
ADDR=(cfg['ip'],cfg['port'])
KEY=open(os.path.join(os.path.dirname(__file__),'collect.key'),'rb').read()
cache=dict()
cache_flag={i:False for i in devices}
def f(addr:str,data:bytes)->None:
assert len(data)==20
(ox55,ox61,axL,axH,ayL,ayH,azL,azH,wxL,wxH,wyL,wyH,wzL,wzH,RollL,RollH,PitchL,PitchH,YawL,YawH)=data
assert ox55==0x55
assert ox61==0x61
def int2(h:int,l:int)->int:
ans=(h<<8)|l
if ans>32767:
ans-=65536
return ans
ax=int2(axH,axL)
ay=int2(ayH,ayL)
az=int2(azH,azL)
wx=int2(wxH,wxL)
wy=int2(wyH,wyL)
wz=int2(wzH,wzL)
Roll=int2(RollH,RollL)
Pitch=int2(PitchH,PitchL)
Yaw=int2(YawH,YawL)
def linear(x:int,k:int)->float:
return k*x/32768
ax=linear(ax,16)
ay=linear(ay,16)
az=linear(az,16)
wx=linear(wx,2000)
wy=linear(wy,2000)
wz=linear(wz,2000)
Roll=linear(Roll,180)
Pitch=linear(Pitch,180)
Yaw=linear(Yaw,180)
agx=ax
agy=ay
agz=az
alx=0
aly=0
alz=0
if addr in cache:
(lax,lay,laz,lwx,lwy,lwz,lRoll,lPitch,lYaw,lalx,laly,lalz,lagx,lagy,lagz)=cache[addr]
agx=alpha*lagx+(1-alpha)*ax
agy=alpha*lagy+(1-alpha)*ay
agz=alpha*lagz+(1-alpha)*az
alx=ax-agx
aly=ay-agy
alz=az-agz
cache[addr]=(ax,ay,az,wx,wy,wz,Roll,Pitch,Yaw,alx,aly,alz,agx,agy,agz)
cache_flag[addr]=True
def notification_handler(devid):
def handler(sender,data):
f(devid,data)
return handler
async def run(devid):
global MODE
_count=0
while True:
try:
pt('BConnect: %s'%(devid,))
client = BleakClient(devid)
await client.connect()
await client.start_notify(cfg['imuReadUUID'], notification_handler(devid))
_count=0
except:
_count+=1
if _count>=10:
pt('BFailed: %s'%(devid,))
MODE=_READ
return
async def main():
tasks=[run(i) for i in devices]
await asyncio.gather(*tasks)
CSV=os.path.join(os.path.dirname(__file__),'collect.csv')
data=list()
for i in open(CSV,'r').read().split('\n'):
i=i.split(',')
data.append(','.join(i[0:30]+i[75:90]).encode('utf8'))
n=len(data)
i=-1
if MODE==_SOCKET:
con=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
con.connect(ADDR)
except:
pt('SFailed: %s'%(ADDR,))
MODE=_READ
else:
pt('SConnect: %s'%(ADDR,))
def getmsg()->bytes:
global MODE,i
while not all(cache_flag.values()):
if MODE!=_BLEAK:
break
time.sleep(0.01)
if MODE==_SOCKET:
try:
con.send(KEY)
return con.recv(1024)
except:
pt('SFailed: %s'%(ADDR,))
MODE=_READ
if MODE==_READ:
i=(i+1)%n
return data[i]
ans=''
for i in devices:
ans+=','.join([str(j) for j in cache[i]])
ans+='' if i is devices[-1] else ','
cache_flag[i]=False
return ans.encode('utf8')
def mian()->None:
a=time.time()
count=0
while True:
msg=getmsg().decode('utf8')
count+=1
while time.time()-a<0.05:
time.sleep(0.01)
a=time.time()
if DEBUG:
pt(count)
else:
pt(msg,flush=True)
threading.Thread(target=mian,daemon=True).start()
if MODE==_BLEAK:
try:
asyncio.run(main())
except:
pt('BFailed: main')
MODE=_READ
else:
while True:
time.sleep(1000)