-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrobotControl.py
354 lines (303 loc) · 14 KB
/
robotControl.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
349
350
351
352
353
354
#-*- coding:utf-8 -*-
"""
keyboard Instructions:
robot moving velocity: <=5(advise)
Q,W: joint 0
A,S: joint 1
Z,X: joint 2
E,R: joint 3
D,F: joint 4
C,V: joint 5
P: exit()
T: close RG2
Y: open RG2
L: reset robot
SPACE: save image
"""
import os
import cv2
import sys
import math
import time
import random
import string
import pygame
import vrep
import numpy as np
class UR5_RG2:
# variates
resolutionX = 640 # Camera resolution: 640*480
resolutionY = 480
joint_angle = [0,0,0,0,0,0] # each angle of joint
RAD2DEG = 180 / math.pi # transform radian to degrees
# Handles information
jointNum = 6
baseName = 'UR5'
rgName = 'RG2'
jointName = 'UR5_joint'
camera_rgb_Name = 'kinect_rgb'
camera_depth_Name = 'kinect_depth'
# communication and read the handles
def __init__(self):
jointNum = self.jointNum
baseName = self.baseName
rgName = self.rgName
jointName = self.jointName
camera_rgb_Name = self.camera_rgb_Name
camera_depth_Name = self.camera_depth_Name
print('Simulation started')
vrep.simxFinish(-1) # 关闭潜在的连接
# 每隔0.2s检测一次, 直到连接上V-rep
while True:
# simxStart的参数分别为:服务端IP地址(连接本机用127.0.0.1);端口号;是否等待服务端开启;连接丢失时是否尝试再次连接;超时时间(ms);数据传输间隔(越小越快)
clientID = vrep.simxStart('127.0.0.1', 19999, True, True, 5000, 5)
if clientID > -1:
print("Connection success!")
break
else:
time.sleep(0.2)
print("Failed connecting to remote API server!")
print("Maybe you forget to run the simulation on vrep...")
vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot) # 仿真初始化
# 读取Base和Joint的句柄
jointHandle = np.zeros((jointNum, 1), dtype=np.int)
for i in range(jointNum):
_, returnHandle = vrep.simxGetObjectHandle(clientID, jointName + str(i+1), vrep.simx_opmode_blocking)
jointHandle[i] = returnHandle
_, baseHandle = vrep.simxGetObjectHandle(clientID, baseName, vrep.simx_opmode_blocking)
_, rgHandle = vrep.simxGetObjectHandle(clientID, rgName, vrep.simx_opmode_blocking)
_, cameraRGBHandle = vrep.simxGetObjectHandle(clientID, camera_rgb_Name, vrep.simx_opmode_blocking)
_, cameraDepthHandle = vrep.simxGetObjectHandle(clientID, camera_depth_Name, vrep.simx_opmode_blocking)
# 读取每个关节角度
jointConfig = np.zeros((jointNum, 1))
for i in range(jointNum):
_, jpos = vrep.simxGetJointPosition(clientID, jointHandle[i], vrep.simx_opmode_blocking)
jointConfig[i] = jpos
self.clientID = clientID
self.jointHandle = jointHandle
self.rgHandle = rgHandle
self.cameraRGBHandle = cameraRGBHandle
self.cameraDepthHandle = cameraDepthHandle
self.jointConfig = jointConfig
def __del__(self):
clientID = self.clientID
vrep.simxFinish(clientID)
print('Simulation end')
# show Handles information
def showHandles(self):
RAD2DEG = self.RAD2DEG
jointNum = self.jointNum
clientID = self.clientID
jointHandle = self.jointHandle
rgHandle = self.rgHandle
cameraRGBHandle = self.cameraRGBHandle
cameraDepthHandle = self.cameraDepthHandle
print('Handles available!')
print("==============================================")
print("Handles: ")
for i in range(len(jointHandle)):
print("jointHandle" + str(i+1) + ": " + jointHandle[i])
print("rgHandle:" + rgHandle)
print("cameraRGBHandle:" + cameraRGBHandle)
print("cameraDepthHandle:" + cameraDepthHandle)
print("===============================================")
# show each joint's angle
def showJointAngles(self):
RAD2DEG = self.RAD2DEG
jointNum = self.jointNum
clientID = self.clientID
jointHandle = self.jointHandle
for i in range(jointNum):
_, jpos = vrep.simxGetJointPosition(clientID, jointHandle[i], vrep.simx_opmode_blocking)
print(round(float(jpos) * RAD2DEG, 2), end = ' ')
print('\n')
# get RGB images
def getImageRGB(self):
clientID = self.clientID
cameraRGBHandle = self.cameraRGBHandle
resolutionX = self.resolutionX
resolutionY = self.resolutionY
res1, resolution1, image_rgb = vrep.simxGetVisionSensorImage(clientID, cameraRGBHandle, 0, vrep.simx_opmode_blocking)
image_rgb_r = [image_rgb[i] for i in range(0,len(image_rgb),3)]
image_rgb_r = np.array(image_rgb_r)
image_rgb_r = image_rgb_r.reshape(resolutionY,resolutionX)
image_rgb_r = image_rgb_r.astype(np.uint8)
image_rgb_g = [image_rgb[i] for i in range(1,len(image_rgb),3)]
image_rgb_g = np.array(image_rgb_g)
image_rgb_g = image_rgb_g.reshape(resolutionY,resolutionX)
image_rgb_g = image_rgb_g.astype(np.uint8)
image_rgb_b = [image_rgb[i] for i in range(2,len(image_rgb),3)]
image_rgb_b = np.array(image_rgb_b)
image_rgb_b = image_rgb_b.reshape(resolutionY,resolutionX)
image_rgb_b = image_rgb_b.astype(np.uint8)
result_rgb = cv2.merge([image_rgb_b,image_rgb_g,image_rgb_r])
# 镜像翻转, opencv在这里返回的是一张翻转的图
result_rgb = cv2.flip(result_rgb, 0)
return result_rgb
# get depth images
def getImageDepth(self):
clientID = self.clientID
cameraDepthHandle = self.cameraDepthHandle
resolutionX = self.resolutionX
resolutionY = self.resolutionY
res2, resolution2, image_depth = vrep.simxGetVisionSensorImage(clientID, cameraDepthHandle, 0, vrep.simx_opmode_blocking)
image_depth_r = [image_depth[i] for i in range(0,len(image_depth),3)]
image_depth_r = np.array(image_depth_r)
image_depth_r = image_depth_r.reshape(resolutionY,resolutionX)
image_depth_r = image_depth_r.astype(np.uint8)
image_depth_g = [image_depth[i] for i in range(1,len(image_depth),3)]
image_depth_g = np.array(image_depth_g)
image_depth_g = image_depth_g.reshape(resolutionY,resolutionX)
image_depth_g = image_depth_g.astype(np.uint8)
image_depth_b = [image_depth[i] for i in range(2,len(image_depth),3)]
image_depth_b = np.array(image_depth_b)
image_depth_b = image_depth_b.reshape(resolutionY,resolutionX)
image_depth_b = image_depth_b.astype(np.uint8)
result_depth = cv2.merge([image_depth_b,image_depth_g,image_depth_r])
# 镜像翻转, opencv在这里返回的是一张翻转的图
result_depth = cv2.flip(result_depth, 0)
# 黑白取反
height, width, channels = result_depth.shape
for row in range(height):
for list in range(width):
for c in range(channels):
pv = result_depth[row, list, c]
result_depth[row, list, c] = 255 - pv
return result_depth
# open rg2
def openRG2(self):
rgName = self.rgName
clientID = self.clientID
res, retInts, retFloats, retStrings, retBuffer = vrep.simxCallScriptFunction(clientID, rgName,\
vrep.sim_scripttype_childscript,'rg2Open',[],[],[],b'',vrep.simx_opmode_blocking)
# close rg2
def closeRG2(self):
rgName = self.rgName
clientID = self.clientID
res, retInts, retFloats, retStrings, retBuffer = vrep.simxCallScriptFunction(clientID, rgName,\
vrep.sim_scripttype_childscript,'rg2Close',[],[],[],b'',vrep.simx_opmode_blocking)
# joint_angle是这种形式: [0,0,0,0,0,0], 所有的关节都旋转到对应的角度
def rotateAllAngle(self, joint_angle):
clientID = self.clientID
jointNum = self.jointNum
RAD2DEG = self.RAD2DEG
jointHandle = self.jointHandle
# 暂停通信,用于存储所有控制命令一起发送
vrep.simxPauseCommunication(clientID, True)
for i in range(jointNum):
vrep.simxSetJointTargetPosition(clientID, jointHandle[i], joint_angle[i]/RAD2DEG, vrep.simx_opmode_oneshot)
vrep.simxPauseCommunication(clientID, False)
self.jointConfig = joint_angle
# 将第num个关节正转angle度
def rotateCertainAnglePositive(self, num, angle):
clientID = self.clientID
RAD2DEG = self.RAD2DEG
jointHandle = self.jointHandle
jointConfig = self.jointConfig
vrep.simxSetJointTargetPosition(clientID, jointHandle[num], (jointConfig[num]+angle)/RAD2DEG, vrep.simx_opmode_oneshot)
jointConfig[num] = jointConfig[num] + angle
self.jointConfig = jointConfig
# 将第num个关节反转angle度
def rotateCertainAngleNegative(self, num, angle):
clientID = self.clientID
RAD2DEG = self.RAD2DEG
jointHandle = self.jointHandle
jointConfig = self.jointConfig
vrep.simxSetJointTargetPosition(clientID, jointHandle[num], (jointConfig[num]-angle)/RAD2DEG, vrep.simx_opmode_oneshot)
jointConfig[num] = jointConfig[num] - angle
self.jointConfig = jointConfig
# convert array from vrep to image
def arrayToImage(self):
path = "imgTemp\\frame.jpg"
if os.path.exists(path):
os.remove(path)
ig = self.getImageRGB()
cv2.imwrite(path, ig)
# convert array from vrep to depth image
def arrayToDepthImage(self):
path = "imgTempDep\\frame.jpg"
if os.path.exists(path):
os.remove(path)
ig = self.getImageDepth()
cv2.imwrite(path, ig)
# control robot by keyboard
def main():
robot = UR5_RG2()
resolutionX = robot.resolutionX
resolutionY = robot.resolutionY
#angle = float(eval(input("please input velocity: ")))
angle = 1
pygame.init()
screen = pygame.display.set_mode((resolutionX, resolutionY))
screen.fill((255,255,255))
pygame.display.set_caption("Vrep yolov3 ddpg pytorch")
# 循环事件,按住一个键可以持续移动
pygame.key.set_repeat(200,50)
while True:
robot.arrayToImage()
ig = pygame.image.load("imgTemp\\frame.jpg")
#robot.arrayToDepthImage()
#ig = pygame.image.load("imgTempDep\\frame.jpg")
screen.blit(ig, (0, 0))
pygame.display.update()
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
# 关闭程序
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
sys.exit()
# joinit 0
elif event.key == pygame.K_q:
robot.rotateCertainAnglePositive(0, angle)
elif event.key == pygame.K_w:
robot.rotateCertainAngleNegative(0, angle)
# joinit 1
elif event.key == pygame.K_a:
robot.rotateCertainAnglePositive(1, angle)
elif event.key == pygame.K_s:
robot.rotateCertainAngleNegative(1, angle)
# joinit 2
elif event.key == pygame.K_z:
robot.rotateCertainAnglePositive(2, angle)
elif event.key == pygame.K_x:
robot.rotateCertainAngleNegative(2, angle)
# joinit 3
elif event.key == pygame.K_e:
robot.rotateCertainAnglePositive(3, angle)
elif event.key == pygame.K_r:
robot.rotateCertainAngleNegative(3, angle)
# joinit 4
elif event.key == pygame.K_d:
robot.rotateCertainAnglePositive(4, angle)
elif event.key == pygame.K_f:
robot.rotateCertainAngleNegative(4, angle)
# joinit 5
elif event.key == pygame.K_c:
robot.rotateCertainAnglePositive(5, angle)
elif event.key == pygame.K_v:
robot.rotateCertainAngleNegative(5, angle)
# close RG2
elif event.key == pygame.K_t:
robot.closeRG2()
# # open RG2
elif event.key == pygame.K_y:
robot.openRG2()
# save Images
elif event.key == pygame.K_SPACE:
rgbImg = robot.getImageRGB()
depthImg = robot.getImageDepth()
# 随机生成8位ascii码和数字作为文件名
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
cv2.imwrite("saveImg\\rgbImg\\"+ran_str+"_rgb.jpg", rgbImg)
cv2.imwrite("saveImg\\depthImg\\"+ran_str+"_depth.jpg", depthImg)
print("save image")
# reset angle
elif event.key == pygame.K_l:
robot.rotateAllAngle([0,0,0,0,0,0])
angle = float(eval(input("please input velocity: ")))
else:
print("Invalid input, no corresponding function for this key!")
if __name__ == '__main__':
main()