-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaServer.py
188 lines (159 loc) · 4.34 KB
/
aServer.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
import asyncio
import sys
import serial
import pickle
import time
from canSR import build, disect
fpv=0
can_toggle=1
pv=[1520000]*2 + [1500000]
fpvp = 0
def getSensor():
ch=sob.read()
if ch==b'T':
frame=sob.read(22)
fd=disect(frame)
fd = pickle.dumps(fd)
return fd
else:
return b''
def sendCan(data):
global can_toggle
global fpv
global fpvp
global pv
try:
data = pickle.loads(data)
except Exception:
return
pwm=data["axis"]
button=data["btn"]
hat=data["hat"]
px,py=hat[0],hat[1]
joint=[pwm[1],pwm[4],pwm[0]]
astro=[0,0]
motors=[10,11,17]
astro_motor=[25,26]
ypr_motor=[20,21]
gripper=0
if button[6]==1:
sob.write(bytes('C\r\n','utf-8'))
can_toggle=0
print("Stop")
if button[7]==1:
sob.write(bytes('O\r\n','utf-8'))
can_toggle=1
print("Start")
if can_toggle==1:
if button[4]==1:
if fpvp == 0:
fpvp=1
fpvmsg = build(500,0,40,fpv%4)
fpv+=1
print(fpvmsg)
sob.write(fpvmsg)
elif fpvp == 1:
fpvp=0
time.sleep(0.5)
servo_prev=pv[2]
if joint[2]>1500000 and joint[2]<=2400000 and pv[2]<2400000:
pv[2]+=10000
if joint[2]<1500000 and joint[2]>=600000 and pv[2]>600000:
pv[2]-=10000
#print(pv[2])
if servo_prev!=pv[2]:
msg=build(can_id,pv[2],10,motors[2])
print(msg)
sob.write(msg)
for i in range(2):
if joint[i]!=pv[i]:
msg=build(can_id,joint[i],10,motors[i])
print(msg)
sob.write(msg)
#time.sleep(0.2)
if pwm[3]>1550000 and pwm[3]<1900000:
gripper=1
if pwm[3]<1450000 and pwm[3]>1200000:
gripper=2
if button[0]==1:
astro[0]=2
elif button[2]==1:
astro[0]=1
if button[1]==1:
astro[1]=2
elif button[3]==1:
astro[1]=1
for i in range(2):
if astro[i]!=0:
msg=build(400,astro[i],10,astro_motor[i])
print(msg)
sob.write(msg)
#time.sleep(0.2)
if can_id==200:
if gripper!=0:
msg=build(can_id,gripper,10,27)
print(msg)
sob.write(msg)
if px==1:
msg=build(can_id,1,10,20)
print(msg)
sob.write(msg)
if px==-1:
msg=build(can_id,2,10,20)
print(msg)
sob.write(msg)
if py==1:
msg=build(can_id,1,10,21)
print(msg)
sob.write(msg)
if py==-1:
msg=build(can_id,2,10,21)
print(msg)
sob.write(msg)
async def handle_client(reader, writer):
async def read():
while True:
data = await reader.read(100000)
if not data:
break
sendCan(data)
sys.stdout.flush()
async def write():
while True:
response = await asyncio.to_thread(getSensor)
writer.write(response)
await writer.drain()
sys.stdout.flush()
try:
task_read = asyncio.create_task(read())
task_write = asyncio.create_task(write())
await asyncio.gather(task_read, task_write)
finally:
writer.close()
async def main(ip):
server = await asyncio.start_server(
handle_client, ip, 8888
)
addr = server.sockets[0].getsockname()
async with server:
await server.serve_forever()
if __name__ == '__main__':
cid={'idmo':200,'bio':300}
interface = sys.argv[1]
can_id = cid[sys.argv[2]]
ip=sys.argv[3]
sob=serial.Serial(
port='/dev/ttyACM'+str(interface),
#port= '/dev/pts/5',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
#sob.write(bytes('h\r\n','utf-8'))
#time.sleep(1)
sob.write(bytes('S7\r\n','utf-8'))
time.sleep(1)
sob.write(bytes('O\r\n','utf-8'))
asyncio.run(main(ip))