-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpythonServer.txt
410 lines (376 loc) · 14.3 KB
/
pythonServer.txt
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 7 12:25:14 2022
@author: Admin01
"""
import socket
import time
#from Grasp_rep_CV import get_grasp
#from mathFunc import *
import math
import numpy as np
# In[]
# Switching between clients
def connect_Rob(serverSocket,grasp,count):
robIP = "192.168.125.1"
clientSocket, clientAddr = serverSocket.accept()
#Keep rejecting connection from DL until robot client requests for connection
cnt=1
while clientAddr[0] != robIP:
cnt+=1
print(cnt)
clientSocket.shutdown(socket.SHUT_RDWR)
clientSocket, clientAddr = serverSocket.accept()
time.sleep(1)
#clientSocket.close()
#if clientAddr[0] != robIP:
#clientSocket.shutdown(SHUT_RDWR)
#clientSocket.close()
#("Connected to: ",c)
if clientAddr[0]==robIP:
# do what ever you want here
print('Connected to Robot',clientAddr)
grasp = grasp.split(',')
print("________________TASK-"+str(count)+"________________")
trans = str(grasp[0])+","+str(grasp[1])+","+str(grasp[2])+","
#print("Sending trans to robot...")
#__________________________________________________S1
print("Sending pose and robot configuration")
clientSocket.send(trans.encode())
# receive data from the server and decoding to get the string.
#__________________________________________________R1
x = clientSocket.recv(1024).decode() #Receive confirmation for trans
#print (x)
rot =str(grasp[3])+","+str(grasp[4])+","+str(grasp[5])+","+str(grasp[6])+","
#print("Sending rot to robot...")
#__________________________________________________S2
clientSocket.send(rot.encode())
# receive data from the server and decoding to get the string.
#__________________________________________________R2
x = clientSocket.recv(1024).decode() #Receive confirmation for rot
#print (x)
#__________________________________________________S3
config_rot = str(grasp[7])+","+str(grasp[8])+","+str(grasp[9])+","+str(grasp[10])+","+str(grasp[11])+","
clientSocket.send(config_rot.encode())
# receive data from the server and decoding to get the string.
#__________________________________________________R3
x = clientSocket.recv(1024).decode() #Receive confirmation for config and ext
print ("Robot received pose")
time.sleep(0.2)
#__________________________________________________R3
x = clientSocket.recv(1024).decode() #Receive confirmation for reachability
print (x)
time.sleep(0.2)
task = clientSocket.recv(1024).decode() #Task done
print(task)
else:
print("Unable to connect to Robot due to simultaneous client connection requests")
return task,clientSocket
# DL and Cam client
def connect_DL(serverSocket):
clientSocket, clientAddr = serverSocket.accept()
print('Connected to DL and Cam',clientAddr)
clientSocket.send('CAPTURE'.encode())
time.sleep(0.1)
#Keep checking for 'b'string!=0
grasp = clientSocket.recv(1024).decode()
print("Grasp received: ", grasp)
time.sleep(0.1)
clientSocket.close()
return grasp
# CUSTOM MATH FUNCTIONS
def rtx2qt(rot):
#print(rot)
qw = math.sqrt(1+rot[0][0]+rot[1][1]+rot[2][2])/2
qx = (rot[2][1]-rot[1][2])/(4*qw)
qy = (rot[0][2]-rot[2][0])/(4*qw)
qz = (rot[1][0]-rot[0][1])/(4*qw)
quat = [qw,qx,qy,qz]
#print(quat)
return quat
def tfm2qt(rot):
#print(rot)
qw = math.sqrt(1+rot[0][0]+rot[1][1]+rot[2][2])/2
qx = (rot[2][1]-rot[1][2])/(4*qw)
qy = (rot[0][2]-rot[2][0])/(4*qw)
qz = (rot[1][0]-rot[0][1])/(4*qw)
quat = [qx,qy,qz,qw]
#print(quat)
return quat
def qt2rtx(qt):
q0 = qt[0]
q1 = qt[1]
q2 = qt[2]
q3 = qt[3]
# First row of the rotation matrix
r00 = 2 * (q0 * q0 + q1 * q1) - 1
r01 = 2 * (q1 * q2 - q0 * q3)
r02 = 2 * (q1 * q3 + q0 * q2)
# Second row of the rotation matrix
r10 = 2 * (q1 * q2 + q0 * q3)
r11 = 2 * (q0 * q0 + q2 * q2) - 1
r12 = 2 * (q2 * q3 - q0 * q1)
# Third row of the rotation matrix
r20 = 2 * (q1 * q3 - q0 * q2)
r21 = 2 * (q2 * q3 + q0 * q1)
r22 = 2 * (q0 * q0 + q3 * q3) - 1
# 3x3 rotation matrix
rot = np.array([[r00, r01, r02],
[r10, r11, r12],
[r20, r21, r22]])
return rot
def toHomTrans(rot,trans):
Tco = np.asarray([[rot[0][0], rot[0][1],rot[0][2],trans[0][0]],[rot[1][0], rot[1][1],rot[1][2],trans[1][0]],[rot[2][0], rot[2][1],rot[2][2],trans[2][0]],[0,0,0,1]])
return Tco
def splitGraspString(grasp):
grasp = grasp.split(",")
trans = [[float(grasp[0])],[float(grasp[1])],[float(grasp[2])]]
axis = [float(grasp[3]),float(grasp[4]),float(grasp[5])]
#s = axis.index(max(axis[0],axis[1]))
#axis[s] = -axis[s]
rz = np.asarray([[math.cos(axis[2]),-math.sin(axis[2]),0],[math.sin(axis[2]),math.cos(axis[2]),0],[0,0,1]])
ry = np.asarray([[math.cos(axis[1]),0,math.sin(axis[1])],[0,1,0],[-math.sin(axis[1]),0,math.cos(axis[1])]])
rx = np.asarray([[1,0,0],[0,math.cos(axis[0]),-math.sin(axis[0])],[0,math.sin(axis[0]),math.cos(axis[0])]])
R = np.dot(rx,np.dot(ry,rz))
#print("ROTATION MATRIX GRASP W.R.T CAM: ", R)
#qt = rtx2qt(R)
#print("ROTATED GRASP W.R.T CAM: ", qt)
#qt = [float(grasp[3]),float(grasp[4]),float(grasp[5]),float(grasp[6])]
return trans,R
def flexPoseToHomTrans(world_pose_wrt_robot):
trans = [[-world_pose_wrt_robot[0]],[world_pose_wrt_robot[1]],[-world_pose_wrt_robot[2]]]
qt = [world_pose_wrt_robot[3],world_pose_wrt_robot[4],world_pose_wrt_robot[5],world_pose_wrt_robot[6]]
Trw = toHomTrans(qt2rtx(qt),trans)
Trw[0][0] = 1
Trw[0][1] = 0
Trw[0][2] = 0
Trw[1][0] = 0
Trw[1][1] = 1
Trw[1][2] = 0
Trw[2][0] = 0
Trw[2][1] = 0
Trw[2][2] = 1
return Trw
def inf2rob(grasp):
grasp_rob_str=""
trans,R = splitGraspString(grasp)
#rot = qt2rtx(qt)
print(R)
Tco = toHomTrans(R,trans)
print("Grasp w.r.t Camera",Tco)
#T_grasp_rot = np.asarray([[1,0,0,0],[0,0,1,0],[0,-1,0,0],[0,0,0,1]])
#Tco = np.dot(Tco,T_grasp_rot) # Final grasp frame after re-orientation
Tcw = np.load('Tcw_closer.npy')
Twc = np.linalg.inv(Tcw)
Trw = np.load('Trw_closer.npy')
Two = np.dot(Twc,Tco)
Tro = np.dot(Trw,Two)
print("Grasp w.r.t Robot",Tro)
quat_rob= tfm2qt(Tro)
print("Grasp orientation w.r.t Robot",quat_rob)
conf=[-1,1,-1,4]
#print(grasp_rob_str)
grasp_rob_str=str(round(Tro[0][3],2))+","+str(round(Tro[1][3],2))+","+str(round(Tro[2][3],2))+","+str(round(quat_rob[0],6))+","+str(round(quat_rob[1],6))+","+str(round(quat_rob[2],6))+","+str(round(quat_rob[3],6))+","+str(conf[0])+","+str(conf[1])+","+str(conf[2])+","+str(conf[3])+","
return grasp_rob_str
# In[]
def main():
# Reserving port >1024
port = 60064
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind to the port
serverSocket.bind(('', port))
print ("Socket binded to Port: %s" %(port))
# Server listen to requestscoming from other computers on the network
serverSocket.listen(10)
print ("Server is listening")
connect = True
count=0
while connect:
#print("Enter preference for Grasp inference: \n")
#print("0 : CV\n")
# print("1 : GQCNN\n")
# inf = input()
# if inf=="1":
# pref = "GQCNN"
# elif inf=="0":
# pref = "CV"
count+=1
grasp = connect_DL(serverSocket)
#grasp = float(grasp.split(","))
#grasp = "411.06,0,40,0.01972,-0.85417,-0.51782,-0.04308,-1,-1,0,0,-1.74"
print("Waiting for Robot")
grasp4robot = inf2rob(grasp)
print("GRASP REP TO ROBOT: ",grasp4robot)
task,rob_clientSocket = connect_Rob(serverSocket,grasp4robot,count)
if task=="TASK DONE":
if count<2:
rob_clientSocket.send(b'CONTINUE')
rob_clientSocket.close()
else:
rob_clientSocket.send(b'STOP')
rob_clientSocket.close()
time.sleep(1)
print("____________ ALL TASKS COMPLETED ___________")
connect=False
elif task=="CURRENT TASK TERMINATED":
rob_clientSocket.send(b'STOP')
rob_clientSocket.close()
connect=False
print("ALL TASKS COMPLETED")
serverSocket.close()
print("SERVER SOCKET CLOSED")
if __name__ == "__main__":
main()
# In[]
#grasp = "411.06,0,40,0.01972,-0.85417,-0.51782,-0.04308,-1,-1,0,0,-1.74"
#grasp = "411.06,0,40,0.01972,-0.85417,-0.51782,-0.04308,-1,-1,0,0,-1.74"
#grasp = "31.49,-15.09,860.58,0.6814,-0.18893,-0.6814,-0.18893,-1,-1,0,0,-1.74"
# CV BOTTLE
#grasp = "-10.1212,2.71397,869.554,0,0,0.69437,0.71961,-1,-1,0,0,-1.74"
# MALLET _ DL
grasp = "-63.3,-65.39,859.57,0.41197,0.5747,-0.41197,0.5747,-1,-1,0,0,-1.74"
# MALLET _CV
grasp = "-62.6987,10.2711,876.134,0,0,0.38268,0.92387,-1,-1,0,0,-1.74"
#grasp = "-11.349,10.623,84.745,0.70705,-0.00901,-0.70705,-0.00901,-1,-1,-1,0"
#___________________MALLET DL__________________
grasp = "-57.87,28.08,866.9,-0.16177,0.68835,0.16177,0.68835,-1,-1,-1,0"
#__________________MALLET_ROBOT_________________1
grasp = "-156.52,-58.52,867.32,0.19997,0.67824,-0.19997,0.67824,-1,-1,-1,0"
#__________________MALLET_ROBOT_________________2
grasp = "4.87,-76.84,872.03,0.69846349,-0.11022137,0.69846349,-0.11022137,-1,-1,-1,0"
#__________________MALLET_ROBOT_________________3
grasp = "-71.55,-12.26,869.85,-0.12051,0.69676,0.12051,0.69676,-1,-1,-1,0,"
#____________ORIENTATION CHECK______________
grasp = "27.22,-61.433,866.426,0.32128,0.62991,-0.32128,0.62991,-1,-1,-1,0,"
trans,qt = splitGraspString(grasp)
rot = qt2rtx(qt)
Tco = toHomTrans(rot,trans)
T_grasp_rot = np.asarray([[1,0,0,0],[0,0,1,0],[0,-1,0,0],[0,0,0,1]])
Tco = np.dot(Tco,T_grasp_rot) # Final grasp frame after re-orientation
# Read camera extrinsic parameters:
#Twc = np.load('Cam_ext_par_IR_new.npy')
Tcw = np.load('Tcw_closer.npy')
#T_W_2W = np.asarray([[-1, 0,0,0],[0,-1,0,0],[0, 0,1,0],[0,0,0,1]])
Twc = np.linalg.inv(Tcw)
#Twc_new=Twc.copy()
#Cam_intr = np.load('Cam_int_par_IR.npy')
#np.save('Tcw_closer.npy',Tcw)
#flex = [411.97,66.07,12.72,0.00683,-0.99738,0.07184,0.00401]
#np.save('Cam_ext_par_IR_new_1.npy',Twc_new)
#robot_pose_wrt_world = np.load('Trw.npy')
Trw = np.load('Trw_closer.npy')
#Twr = flexPoseToHomTrans(robot_pose_wrt_world)
#Trw = np.linalg.inv(Twr)
#np.save('Trw_closer.npy',Trw)
# Tro = Trw*Twc*Tco
#Twc = np.linalg.inv(Tcw)
Tbc = np.dot(Trw,Twc)
Two = np.dot(Twc,Tco)
Tro = np.dot(Trw,Two)
quat_rob= tfm2qt(Tro)
print(quat_rob)
conf=[-1,1,-1,4]
grasp_rob_str=str(round(Tro[0][3],2))+","+str(round(Tro[1][3],2))+","+str(round(Tro[2][3],2))+","+str(round(Tro[3][3],2))+","+str(round(quat_rob[0],6))+","+str(round(quat_rob[1],6))+","+str(round(quat_rob[2],6))+","+str(round(quat_rob[3],6))+","+str(conf[0])+","+str(conf[1])+","+str(conf[2])+","+str(conf[3])+","
# In[]
#posx = 349.23
#posy = 106.19
#posz = 10
#
##q1=0.15027
##q2=-0.65145
##q3=0.74182
##q4=0.05234
#q1 = 0.66097
#q2 = 0.25122
#q3 = -0.66097
#q4 = 0.25122
##q1=0.538265
##q2=-0.0922167
##q3=-0.48791
##q4=0.680963
#
#cf1=0
#cf2=1
#cf4=2
#cf6=0
#count=0
#
#ext = 0
## In[]
## a forever loop until we interrupt it or an error occurs
#
#while True:
# #Establish connection with client.
# clientSocket, clientAddr = serverSocket.accept()
# print ('Got connection from', clientAddr )
#
# ThreadCount += 1
# print("________________TASK-"+str(count+1)+"________________")
# trans = str(posx)+","+str(posy)+","+str(posz)+","
# print("Sending trans to robot...")
# #__________________________________________________S1
# clientSocket.send(trans.encode())
# # receive data from the server and decoding to get the string.
# #__________________________________________________R1
# x = clientSocket.recv(1024).decode() #Receive confirmation for trans
# print (x)
# #__________________________________________________S2
#
# rot =str(q1)+","+str(q2)+","+str(q3)+","+str(q4)+","
# print("Sending rot to robot...")
# #__________________________________________________S1
# clientSocket.send(rot.encode())
# # receive data from the server and decoding to get the string.
# #__________________________________________________R1
# x = clientSocket.recv(1024).decode() #Receive confirmation for rot
# print (x)
#
# #__________________________________________________S2
# config_rot = str(cf1)+","+str(cf2)+","+str(cf4)+","+str(cf6)+","+str(ext)+","
# print("Sending config and ext...")
# clientSocket.send(config_rot.encode())
# # receive data from the server and decoding to get the string.
# #__________________________________________________R2
# x = clientSocket.recv(1024).decode() #Receive confirmation for config and ext
# print (x)
# time.sleep(0.2)
#
# #__________________________________________________R3
# x = clientSocket.recv(1024).decode() #Receive confirmation for config and ext
# print (x)
# time.sleep(0.2)
#
# count+=1
# task = clientSocket.recv(1024).decode() #Task done
# print(task)
# if task=="TASK DONE":
# #Get depth image
#
# #Pass to GQCNN or CV for grasp inference
#
# #Update grasp representation to send in next iteration
# clientSocket.send(b'CONTINUE')
# if count>=3:
# clientSocket.send(b'STOP')
# time.sleep(1)
# break
# elif task=="CURRENT TASK TERMINATED":
# clientSocket.send(b'STOP')
#clientSocket.close()
#print("Socket closed")
# In[]
#def multi_threaded_client(connection):
# connection.send(str.encode('Server is working:'))
# while True:
# data = connection.recv(2048)
# response = 'Server message: ' + data.decode('utf-8')
# if not data:
# break
# connection.sendall(str.encode(response))
# connection.close()
#
#while True:
# Client, address = serverSocket.accept()
# print('Connected to: ' + address[0] + ':' + str(address[1]))
# start_new_thread(multi_threaded_client, (Client, ))
# print('Thread Number: ' + str(ThreadCount))