-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeapLeftur3.py
More file actions
executable file
·360 lines (302 loc) · 13.8 KB
/
LeapLeftur3.py
File metadata and controls
executable file
·360 lines (302 loc) · 13.8 KB
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
# -*- coding: UTF-8 -*-
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# import for LeapMotion
import Leap, sys, thread, time
from Leap import CircleGesture, KeyTapGesture, ScreenTapGesture, SwipeGesture, Vector
# import for UR3
import urx
import math
from math import pi
# create thread to execute the two UR3 simualtanious
import threading
from threading import Thread
import time
# data collection / paper
class UR3:
Left_UR3_IP = "158.132.172.193"
Right_UR3_IP = "158.132.172.214"
# Left_UR3_IP = "localhost1"
# Right_UR3_IP = "localhost2"
@classmethod
def connectLeftUR(cls):
rob = urx.Robot(cls.Left_UR3_IP)
rob.set_tcp((0,0,0,0,0,0))
rob.set_payload(1.5, (0,0,0))
time.sleep(0.2)
printer.header("==================== Left UR3 connected. ====================")
intial_joints = [-0.03303844133485967, -0.4258616606341761, 1.2580962181091309, -3.8896873633014124, -1.6458943525897425, 9.371907297764913]
# [-0.027399841939107716, -0.9967621008502405, 1.7950401306152344, -3.9452269713031214, -1.5623214880572718, 9.370444122944967]
rob.movej(intial_joints, acc= 0.3, vel=0.2)
printer.header("==================== Left UR3 finished initial setting. ====================")
return rob
@classmethod
def connectRightUR(cls):
rob = urx.Robot(cls.Right_UR3_IP)
rob.set_tcp((0,0,0,0,0,0))
rob.set_payload(1.5, (0,0,0))
time.sleep(0.2)
printer.header("==================== Right UR3 connected and finished setting. ====================")
return rob
@classmethod
def moveLeftPose(cls, rob, pos_displace, angle = 0.0):
try:
l = 0.02
v = 0.12 # velocity
a = 0.22 # acceleration
pose = rob.getl()
t = rob.get_pose()
# t0 = rob.get_pose()
print("Left UR: the current pose is : %s" % t.pos)
# print("Translate in -x and rotate")
# t0.orient.rotate_zb(pi/18)
for i in range(3):
if i == 0: # in / out
t.pos[i] += pos_displace[i]/3.0
elif i == 1: # up / down
t.pos[i] += pos_displace[i]/1.5
else: # left / right
t.pos[i] += pos_displace[i]/2.0
print("Left UR: the target pose is %s with a displacement %s" % (t.pos, pos_displace))
# pose=list(t.pos)
# rob.movel(pose, vel=v, acc=a)
angle_rad = angle/180 * pi
t.orient.rotate_zt(angle_rad)
rob.set_pose(t, vel=v, acc=a)
print("Left UR: the changed pose is : %s" % rob.getl())
printer.okg("Left UR: ************** Left UR Moved Successfully! ***************")
# time.sleep(0.005)
# time.sleep(0.0001)
# forward pos[0] +
# backward pos[0] -
# up pos[1] -
# down pos[1] +
# left pos[2] +
# right pos[2] -
# move out
# rob.set_pose(t0, vel=v, acc=a)
finally:
pass
# rob.close()
# print("The UR3 robot is disconnected")
@classmethod
def moveRightPose(cls, rob, pos_displace):
try:
l = 0.02
v = 0.1 # velocity
a = 0.2 # acceleration
pose = rob.getl()
t = rob.get_pose() # transformation from Base to TCP
# t0 = rob.get_pose()
print("Right UR: the current pose is : %s" % pose)
# print("Translate in -x and rotate")
# t0.orient.rotate_zb(pi/18)
for i in range(3):
if i == 0: # in / out
pose[i] += pos_displace[i]/3.0
elif i == 1: # up / down
pose[i] += pos_displace[i]/1.5
else: # left / right
pose[i] += pos_displace[i]/2.0
print("Right UR: the target pose is %s with a displacement %s" % (pose, pos_displace))
rob.movel(pose, vel=v, acc=a)
print("Right UR: the current pose is : %s" % rob.getl())
printer.okg("Right UR: ************** Right UR Moved Successfully! ***************")
time.sleep(0.0001)
# forward pos[0] +
# backward pos[0] -
# up pos[1] -
# down pos[1] +
# left pos[2] +
# right pos[2] -
# move out
# rob.set_pose(t0, vel=v, acc=a)
finally:
pass
# rob.close()
# print("The UR3 robot is disconnected")
# class MyThread(Thread):
# """docstring for MyThread"""
# def __init__(self, name):
# super(MyThread, self).__init__()
# self.name = name
# # self.pos_displace = pos_displace
# def run(self):
# if str.lower(self.name) == "left":
# UR3.moveLeftPose(self.leftrob, left_pos_displace)
# elif str.lower(self.name) == "right":
# UR3.moveRightPose(self.rightrob, right_pos_displace)
class printer:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
@classmethod
def okb(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.OKBLUE + t + ' ' + text + cls.ENDC)
@classmethod
def okg(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.OKGREEN + t + ' ' + text + cls.ENDC)
@classmethod
def warn(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.WARNING + t + ' ' + text + cls.ENDC)
@classmethod
def header(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.HEADER + t + ' ' + text + cls.ENDC)
@classmethod
def fail(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.FAIL + t + ' ' + text + cls.ENDC)
@classmethod
def bold(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.BOLD + t + ' ' + text + cls.ENDC)
@classmethod
def underline(cls, text):
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(cls.UNDERLINE + t + ' ' + text + cls.ENDC)
class SampleListener(Leap.Listener):
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
state_names = ['STATE_INVALID', 'STATE_START', 'STATE_UPDATE', 'STATE_END']
logger = logging.getLogger(__name__)
leftrob = UR3.connectLeftUR()
# rightrob = UR3.connectRightUR()
def on_init(self, controller):
printer.header("======================== LeapMotion Initialized ========================")
# leftrob = UR3.connectLeftUR()
def on_connect(self, controller):
printer.okg("======================== Connected ========================")
# Enable gestures
controller.enable_gesture(Leap.Gesture.TYPE_CIRCLE)
controller.enable_gesture(Leap.Gesture.TYPE_KEY_TAP)
controller.enable_gesture(Leap.Gesture.TYPE_SCREEN_TAP)
controller.enable_gesture(Leap.Gesture.TYPE_SWIPE)
def on_disconnect(self, controller):
# Note: not dispatched when running in a debugger.
printer.warn("======================== Leap Motion Disconnected ========================")
self.leftrob.close()
printer.header("The UR3 left robot is disconnected")
# self.rightrob.close()
# printer.header("The UR3 right robot is disconnected")
def on_exit(self, controller):
printer.warn("======================== Leap Motion Listerner Exited ========================")
self.leftrob.close()
printer.header("The UR3 left robot is disconnected. ")
# self.rightrob.close()
# printer.header("The UR3 right robot is disconnected. ")
def on_frame(self, controller):
# Get the most recent frame and report some basic information
cur_frame = controller.frame() # the current frame
pre_frame = controller.frame(59) # the previous frame 1~59
"""
# A unique ID assigned to this Hand object,
# whose value remains the same across consecutive frames while the tracked hand remains visible.
# If tracking is lost (for example, when a hand is occluded by another hand or
# when it is withdrawn from or reaches the edge of the Leap Motion Controller field of view),
# the Leap Motion software may assign a new ID when it detects the hand in a future frame.
"""
if cur_frame.id % 20 == 0:
fps = cur_frame.current_frames_per_second
# print("Current FPS: %d " % int(fps))
# print("Current Frame id: %d, hands: %d, gestures: %d" % (cur_frame.id, len(cur_frame.hands), len(cur_frame.gestures())))
# print("Previous Frame id: %d, hands: %d, gestures: %d" % (pre_frame.id, len(pre_frame.hands), len(pre_frame.gestures())))
# two hands start otherwise do nothing
if len(cur_frame.hands) == 1 and len(pre_frame.hands) == 1:
if cur_frame.hands[0].is_left:
left_hand_c = cur_frame.hands[0]
else:
pass
if pre_frame.hands[0].is_left:
left_hand_p = pre_frame.hands[0]
else:
pass
print('') # separate the log
print("Current Frame id: %d, hands: %d, gestures: %d" % (cur_frame.id, len(cur_frame.hands), len(cur_frame.gestures())))
printer.header(" ========================= Current Frame id: %d, hands: %d ========================= " % (cur_frame.id, len(cur_frame.hands)))
print("Current Left Hand %s, %s, %s" % (left_hand_c.palm_position, left_hand_c.direction, left_hand_c.palm_normal))
print("Previous Left Hand %s, %s, %s" % (left_hand_p.palm_position, left_hand_p.direction, left_hand_p.palm_normal))
left_c_norm = left_hand_c.palm_normal
left_p_norm = left_hand_p.palm_normal
lh_displace = left_hand_c.palm_position - left_hand_p.palm_position
printer.warn("LeapMotion: the left hand displacement is %s" % lh_displace)
left_pos_displace = [-lh_displace[2]/500.0, -lh_displace[1]/500.0, -lh_displace[0]/500.0]
if left_c_norm[1] > 0:
printer.fail("Leap Motion: Please put your hand palm toward ground and hide UR3 job !")
else:
# print(str(abs(left_c_norm[1]/left_c_norm[0])))
angle_c = math.acos( left_c_norm[1]*left_c_norm[1]/(left_c_norm[0] * left_c_norm[0] + left_c_norm[1] * left_c_norm[1]))
final_angle_c = left_c_norm[0]/abs(left_c_norm[0])*angle_c/pi * 180
printer.okg("Leap Motion: the hand palm normal is %s " % final_angle_c)
angle_p = math.acos( left_p_norm[1]*left_p_norm[1]/(left_p_norm[0] * left_p_norm[0] + left_p_norm[1] * left_p_norm[1]))
final_angle_p = left_p_norm[0]/abs(left_p_norm[0])*angle_p/pi * 180
printer.okg("Leap Motion: the hand palm normal is %s " % final_angle_p)
final_angle = final_angle_c - final_angle_p
if self.leftrob.is_program_running():
pass
else:
UR3.moveLeftPose(self.leftrob, left_pos_displace, final_angle)
printer.okg("Left UR starts to run!")
# ============ start of UR3 executor ================
"""
# for real time performance
threads = []
threads.append(threading.Thread(target=UR3.moveLeftPose(self.leftrob, left_pos_displace)))
threads.append(threading.Thread(target=UR3.moveRightPose(self.rightrob, right_pos_displace)))
# Check the robot is runing
if self.leftrob.is_program_running():
pass
else:
threads[0].start()
printer.okb("Left UR thread starts to run!")
if self.rightrob.is_program_running():
pass
else:
threads[1].start()
printer.okb("Right UR thread starts to run!")
"""
# =========== end of UR3 excutor ================
else:
pass
# two hands end
def state_string(self, state):
if state == Leap.Gesture.STATE_START:
return "STATE_START"
if state == Leap.Gesture.STATE_UPDATE:
return "STATE_UPDATE"
if state == Leap.Gesture.STATE_STOP:
return "STATE_STOP"
if state == Leap.Gesture.STATE_INVALID:
return "STATE_INVALID"
# End of SampleListener for LeapMotion
def runLeapMotion():
# Create a sample listener and controller
listener = SampleListener()
controller = Leap.Controller()
# Have the sample listener receive events from the controller
controller.add_listener(listener)
# Keep this process running until Enter is pressed
print "Press Enter to quit..."
try:
sys.stdin.readline()
except KeyboardInterrupt:
pass
finally:
# Remove the sample listener when done
controller.remove_listener(listener)
def main():
runLeapMotion()
# leftrob = UR3.connectLeftUR()
# UR3.movePose(leftrob)
pass
if __name__ == "__main__":
main()