forked from jswon/training_robot_vrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrep_python.py
349 lines (295 loc) · 12.1 KB
/
vrep_python.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
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
try:
import vrep
except:
print('--------------------------------------------------------------')
print('"vrep.py" could not be imported. This means very probably that')
print('either "vrep.py" or the remoteApi library could not be found.')
print('Make sure both are in the same folder as this file,')
print('or appropriately adjust the file "vrep.py"')
print('--------------------------------------------------------------')
print('')
import math
import time
import random
import socket
import numpy as np
from PIL import Image as Im
Objects_name = ['O_00_Big_USB', 'O_01_Black_Tape', 'O_02_Blue_Glue', 'O_03_Brown_Box', 'O_04_Green_Glue',
'O_05_Pink_Box', 'O_06_Red_Cup', 'O_07_Small_USB', 'O_08_White_Tape', 'O_09_Yellow_Cup' ]
Objects_handle = []
UR3_handle = []
UR3_joint_handle = []
Tray_handle = []
vision_sensor = []
end_effector = []
collision_handle = []
clientID =[]
Shuffle_Count = 0
shuffle_num = [0,1,2,3,4,5,6,7,8,9]
def connect():
print("Connecting to remote API server")
del clientID[:]
lan_ip = socket.gethostbyname(socket.gethostname())
vrep.simxFinish(19997) # just in case, close all opened connections
#ID = vrep.simxStart('192.168.0.3', 19997, True, True, 5000, 5) #IP address
ID = vrep.simxStart(lan_ip, 19997, True, True, 5000, 5) #IP address
clientID.append(ID)
vrep.simxStartSimulation(clientID[0], vrep.simx_opmode_oneshot)
if (clientID == -1):
print('Not connected')
print('Connect again')
else:
del Objects_handle[:]
del UR3_joint_handle[:]
del UR3_handle[:]
del Tray_handle[:]
del vision_sensor[:]
del collision_handle[:]
del end_effector[:]
#Object Handle
for i in Objects_name:
err, Handle = vrep.simxGetObjectHandle(clientID[0], i, vrep.simx_opmode_blocking)
Objects_handle.append(Handle)
#UR3 Handle
err, UR3 = vrep.simxGetObjectHandle(clientID[0], 'UR3', vrep.simx_opmode_blocking)
UR3_handle.append(UR3)
#Tray Handle
err, Tray = vrep.simxGetObjectHandle(clientID[0], 'Tray', vrep.simx_opmode_blocking)
Tray_handle.append(Tray)
#End Effector Handle - endeffector
err, end = vrep.simxGetObjectHandle(clientID[0], 'End_Effector', vrep.simx_opmode_blocking)
end_effector.append(end)
#UR3 Joint Handle
for i in range(1,7):
err, Joint_handle = vrep.simxGetObjectHandle(clientID[0], "UR3_joint%d" % i,
vrep.simx_opmode_oneshot_wait)
UR3_joint_handle.append(Joint_handle)
#Vision camera Handle
for i in range(1,3):
err, vision = vrep.simxGetObjectHandle(clientID[0], 'Vision_sensor_0%d' % i, vrep.simx_opmode_blocking)
vision_sensor.append(vision)
#Collision Handle
for i in range(0,14):
err, collision = vrep.simxGetCollisionHandle(clientID[0],'Collision%d' %i,vrep.simx_opmode_oneshot_wait)
collision_handle.append(collision)
#Start Simulation
vrep.simxStartSimulation(clientID[0], vrep.simx_opmode_oneshot)
def shutdown():
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else :
vrep.simxStopSimulation(clientID[0], vrep.simx_opmode_oneshot)
time.sleep(1)
vrep.simxFinish(clientID[0])
del Objects_handle[:]
del UR3_joint_handle[:]
del UR3_handle[:]
del Tray_handle[:]
del end_effector[:]
del clientID[:]
def startDataStreaming():
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
#Position
for i in Objects_handle :
vrep.simxGetObjectPosition(clientID[0], i, UR3_handle[0], vrep.simx_opmode_streaming)
#Dummy position
vrep.simxGetObjectPosition(clientID[0], end_effector[0], UR3_handle[0], vrep.simx_opmode_streaming)
#vision
for i in vision_sensor :
vrep.simxGetVisionSensorImage(clientID[0], i, 0, vrep.simx_opmode_streaming)
#collision
for i in collision_handle :
vrep.simxReadCollision(clientID[0], i, vrep.simx_opmode_streaming)
#UR3 Joint
for i in UR3_joint_handle :
vrep.simxGetJointPosition(clientID[0],i,vrep.simx_opmode_streaming)
def stopDataStreaming():
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
#Position
for i in Objects_handle :
vrep.simxGetObjectPosition(clientID[0], i, UR3_handle, vrep.simx_opmode_discontinue)
#Dummy position
vrep.simxGetObjectPosition(clientID[0], end_effector[0], UR3_handle, vrep.simx_opmode_discontinue)
#vision
for i in vision_sensor :
vrep.simxGetVisionSensorImage(clientID[0], i, 0, vrep.simx_opmode_discontinue)
#collision
for i in collision_handle :
vrep.simxReadCollision(clientID[0], i, vrep.simx_opmode_discontinue)
def getPosition(Objects_num): # only for objects
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
if(Objects_num <10 and Objects_num >= 0):
Object_Handle = Objects_handle[Objects_num]
else:
print('wrong object number')
return 0
while (vrep.simxGetConnectionId(clientID[0]) != -1):
err, Output_Postion = vrep.simxGetObjectPosition(clientID[0], Object_Handle, UR3_handle[0], vrep.simx_opmode_buffer)
if err == vrep.simx_return_ok:
time.sleep(1)
print('Position')
print(Output_Postion)
return Output_Postion
elif err == vrep.simx_return_novalue_flag:
print("no value")
else:
print("not yet")
def getEndEffector(): # only for objects
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
Object_Handle = end_effector[0]
while (vrep.simxGetConnectionId(clientID[0]) != -1):
err, Output_Postion = vrep.simxGetObjectPosition(clientID[0], Object_Handle, UR3_handle[0], vrep.simx_opmode_buffer)
if err == vrep.simx_return_ok:
time.sleep(1)
print('Position')
print(Output_Postion)
return Output_Postion
elif err == vrep.simx_return_novalue_flag:
print("no value")
else:
print("not yet")
def shuffleObject():##############################check
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
Past_number =[]
change_num = [-3, -2, -1, 1, 2, 3]
for i in Objects_handle:
SufflePosition = []
L_x = 0.075 + 0.02*(2*random.random() - 1)
L_y_positive = (0.25 / 2) * (1 * random.random())
L_y_negative = (0.25 / 2) * (-1 * random.random())
L_z_positive = (0.70 / 2) * (1 * random.random())
L_z_negative = (0.70 / 2) * (-1 * random.random())
Suffle_part = random.randrange(1,5)
if(Suffle_part == Past_number):
Suffle_part = (Suffle_part + random.choice(change_num))%4
Past_number = Suffle_part
if (Suffle_part == 1):
SufflePosition.append(L_x)
SufflePosition.append(L_y_positive)
SufflePosition.append(L_z_positive)
elif (Suffle_part == 2):
SufflePosition.append(L_x)
SufflePosition.append(L_y_negative)
SufflePosition.append(L_z_positive)
elif (Suffle_part == 3):
SufflePosition.append(L_x)
SufflePosition.append(L_y_positive)
SufflePosition.append(L_z_negative)
else:
SufflePosition.append(L_x)
SufflePosition.append(L_y_negative)
SufflePosition.append(L_z_negative)
vrep.simxSetObjectPosition(clientID[0], i, Tray_handle[0], SufflePosition, vrep.simx_opmode_oneshot)
time.sleep(0.05)
def movejoint(Degree): # move joint of ur3
#############################################################################check
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
elif(len(Degree) != 6) :
print('Wrong input')
else:
num = 0 # maximum of num is 6
for i in Degree:
JointTargetPosition = math.radians(i)
vrep.simxSetJointTargetPosition(clientID[0], UR3_joint_handle[num], JointTargetPosition,
vrep.simx_opmode_blocking)
num = num + 1
def getimageinformation(sensor_index):
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
if(sensor_index == 1 or sensor_index == 2) :
while (vrep.simxGetConnectionId(clientID[0]) != -1):
err, resolution, image = vrep.simxGetVisionSensorImage(clientID[0], vision_sensor[sensor_index - 1], 0,
vrep.simx_opmode_buffer)
# Save image to txt file => But it have a problem
if err == vrep.simx_return_ok:
# save image information list
print('vision information imported')
image_byte_array = np.asarray(image).astype('uint8')
result_image = Im.frombuffer("RGB", (resolution[0], resolution[1]), image_byte_array, "raw", "RGB", 0, 1)
return result_image
elif err == vrep.simx_return_novalue_flag:
print("no vision information")
else:
print('Error')
print(err)
else:
print('Wrong index number')
def all_jointangle_list(): ########### check
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
angle_list = []
for i in UR3_joint_handle:
ret, UR3_joint_CurrentAngle = vrep.simxGetJointPosition(clientID[0], i ,
vrep.simx_opmode_buffer)
angle_list.append(math.degrees(UR3_joint_CurrentAngle))
return angle_list #return as degree value
def collsion_Read():
if (not clientID):
print('connect first')
elif(clientID[0] == -1):
print('connect first')
else:
for i in collision_handle :
check = []
while (vrep.simxGetConnectionId(clientID[0]) != -1):
err, collision = vrep.simxReadCollision(clientID[0], i, vrep.simx_opmode_buffer)
if err == vrep.simx_return_ok:
print('collision information imported')
del check[:]
check.append(collision)
break
elif err == vrep.simx_return_novalue_flag:
print("no collision information")
else:
print('Error')
print(err)
if(check[0] == 1):
print('Collision detected')
return 1
print('No Collision')
return 0
def rendering(True_or_False):
if (not clientID):
print('connect first')
elif (clientID[0] == -1):
print('connect first')
else:
err = vrep.simxSetBooleanParameter(clientID[0],vrep.sim_boolparam_display_enabled , True_or_False, vrep.simx_opmode_oneshot)
def kickoff() :
if (not shuffle_num):
for i in range(10):
shuffle_num.append(i)
random_num = random.randrange(0,len(shuffle_num))
vrep.simxSetObjectPosition(clientID[0],Objects_handle[shuffle_num[random_num]],UR3_handle[0],[0.8,0,0],vrep.simx_opmode_oneshot)
del shuffle_num[random_num]