-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpykinect.py
More file actions
397 lines (342 loc) · 16.2 KB
/
pykinect.py
File metadata and controls
397 lines (342 loc) · 16.2 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
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
import math
import almath
import time
import argparse
from naoqi import ALProxy
import thread
import itertools
import ctypes
import pykinect
from pykinect import nui
from pykinect.nui import JointId
import pygame
from pygame.color import THECOLORS
from pygame.locals import *
# Global variables
listAngles = []
shoulderLeft = []
elbowLeft = []
wristLeft = []
shoulderRight = []
elbowRight = []
wristRight = []
t = 0
RobotIP="127.0.0.1"
RobotPort=9559
def sendrobot(anglelist, robotIP="127.0.0.1", PORT=9559):
try:
try:
motionProxy = ALProxy("ALMotion", robotIP, PORT) #creates proxy to call specific functions
except Exception, e:
print "Could not create proxy to AlMotion"
print "Error was: ", e
try:
postureProxy = ALProxy("ALRobotPosture", robotIP, PORT) #creates proxy to call specific functions
except Exception, e:
print "Could not create proxy to ALRobotPosture"
print "Error was: ", e
global t # uses global variable t
if (t == 0): # if it is the first time the robot is called upon
motionProxy.setStiffnesses("Body", 0.0) # unstiffens the joints
postureProxy.goToPosture("StandInit", 0.5) # gets the robot into his initial standing position
names = ["RShoulderPitch", "RShoulderRoll", "RElbowRoll", "RElbowYaw", "LShoulderPitch", "LShoulderRoll", "LElbowRoll", "LElbowYaw"]
#list of joints that will get changed
angleLists = [[(anglelist[len(anglelist) - 8]) * almath.TO_RAD], # all the coordinates are saved in one big list
[(anglelist[len(anglelist) - 7]) * almath.TO_RAD], # and in a specific order (see list of joints)
[(anglelist[len(anglelist) - 6]) * almath.TO_RAD], # this gets them out of that list and sent to the right joint
[(anglelist[len(anglelist) - 5]) * almath.TO_RAD],
[(anglelist[len(anglelist) - 4]) * almath.TO_RAD],
[(anglelist[len(anglelist) - 3]) * almath.TO_RAD],
[(anglelist[len(anglelist) - 2]) * almath.TO_RAD],
[(anglelist[len(anglelist) - 1]) * almath.TO_RAD]]
timeLists = [[0.4], [0.4], [0.4], [0.4], [0.4], [0.4], [0.4], [0.4]]
isAbsolute = True
motionProxy.angleInterpolation(names, angleLists, timeLists, isAbsolute)
t += 1
except Exception: # checks for any and all errors
pass # ignores every single one of them, except keyboardInterupt and SystemExit
except (KeyboardInterrupt, SystemExit): # when the program gets terminated
postureProxy.goToPosture("StandInit", 0.5) # set the robot in its initial position
motionProxy.setStiffnesses("Body", 1.0) # stiffen the joints
raise #actually quit
def angleRShoulderPitch(x2, y2, z2, x1, y1, z1): #calulates the Shoulderpitch value for the Right shoulder by using geometry
if(y2<y1):
angle = math.atan(abs(y2 - y1) / abs(z2 - z1))
angle = math.degrees(angle)
angle = -(angle)
if(angle<-118):
angle = -117
return angle
else:
angle = math.atan((z2-z1)/(y2-y1))
angle = math.degrees(angle)
angle = 90-angle
return angle
def angleRShoulderRoll(x2, y2, z2, x1, y1, z1): #calulates the ShoulderRoll value for the Right shoulder by using geometry
if(z2<z1):
test = z2
anderetest = z1
z2=anderetest
z1=test
if (z2 - z1 < 0.1):
z2 = 1.0
z1 = 0.8
angle = math.atan((x2 - x1) / (z2 - z1))
angle = math.degrees(angle)
return angle
def angleLShoulderPitch(x2, y2, z2, x1, y1, z1): #calulates the Shoulderpitch value for the Left shoulder by using geometry
if (y2 < y1):
angle = math.atan(abs(y2 - y1) / abs(z2 - z1))
angle = math.degrees(angle)
angle = -(angle)
if (angle < -118):
angle = -117
return angle
else:
angle = math.atan((z2 - z1) / (y2 - y1))
angle = math.degrees(angle)
angle = 90 - angle
return angle
def angleLShouderRoll(x2, y2, z2, x1, y1, z1): #calulates the ShoulderRoll value for the Left shoulder by using geometry
if (z2 < z1):
test = z2
anderetest = z1
z2 = anderetest
z1 = test
if(z2-z1< 0.1):
z2=1.0
z1=0.8
angle = math.atan((x2-x1)/(z2-z1))
angle = math.degrees(angle)
return angle
def angleRElbowYaw(x2, y2, z2, x1, y1, z1,shoulderpitch): #calulates the ElbowYaw value for the Right elbow by using geometry
if(abs(y2-y1)<0.2 and abs(z2-z1) < 0.2 and (x1<x2) ):
return 0
elif(abs(x2-x1)<0.1 and abs(z2-z1)<0.1 and (y1>y2)):
return 90
elif(abs(x2-x1)<0.1 and abs(z2-z1)<0.1 and (shoulderpitch > 50)):
return 90
elif(abs(y2-y1)<0.1 and abs(z2-z1)<0.1 and (shoulderpitch < 50)):
return 0
elif(abs(x2-x1)<0.1 and abs(y2-y1)<0.1 and (shoulderpitch > 50)):
return 90
else:
angle = math.atan((z2 - z1) / (y2 - y1))
angle = math.degrees(angle)
angle = - angle + (shoulderpitch)
angle = - angle
return angle
def angleRElbowRoll(x3, y3, z3, x2, y2, z2, x1, y1, z1): #calulates the ElbowRoll value for the Right elbow by using geometry
a1=(x3-x2)**2+(y3-y2)**2 + (z3-z2)**2
lineA= a1 ** 0.5 # calculates length of line between 2 3D coordinates
b1=(x2-x1)**2+(y2-y1)**2 + (z2-z1)**2
lineB= b1 ** 0.5 # calculates length of line between 2 3D coordinates
c1=(x1-x3)**2+(y1-y3)**2 + (z1-z3)**2
lineC= c1 ** 0.5 # calculates length of line between 2 3D coordinates
cosB = (pow(lineA, 2) + pow(lineB,2) - pow(lineC,2))/(2*lineA*lineB)
acosB = math.acos(cosB)
angle = math.degrees(acosB)
angle = 180 - angle
return angle
def angleLElbowYaw(x2, y2, z2, x1, y1, z1, shoulderpitch): #calulates the ElbowYaw value for the Left elbow by using geometry
if(abs(y2-y1)<0.2 and abs(z2-z1) < 0.2 and (x1>x2) ):
return 0
elif(abs(x2-x1)<0.1 and abs(z2-z1)<0.1 and (y1>y2)):
return -90
elif(abs(x2-x1)<0.1 and abs(z2-z1)<0.1 and (shoulderpitch > 50)):
return -90
elif(abs(y2-y1)<0.1 and abs(z2-z1)<0.1 and (shoulderpitch > 50)):
return 0
elif(abs(x2-x1)<0.1 and abs(y2-y1)<0.1 and (shoulderpitch > 50)):
return -90
else:
angle = math.atan((z2 - z1) / (y2 - y1))
angle = math.degrees(angle)
angle = - angle + (shoulderpitch)
angle = - angle
return angle
def angleLElbowRoll(x3, y3, z3, x2, y2, z2, x1, y1, z1): #calulates the ElbowRoll value for the Left elbow by using geometry
a1=(x3-x2)**2+(y3-y2)**2 + (z3-z2)**2
lineA= a1 ** 0.5 # calculates length of line between 2 3D coordinates
b1=(x2-x1)**2+(y2-y1)**2 + (z2-z1)**2
lineB= b1 ** 0.5 # calculates length of line between 2 3D coordinates
c1=(x1-x3)**2+(y1-y3)**2 + (z1-z3)**2
lineC= c1 ** 0.5 # calculates length of line between 2 3D coordinates
cosB = (pow(lineA, 2) + pow(lineB,2) - pow(lineC,2))/(2*lineA*lineB)
acosB = math.acos(cosB)
angle = math.degrees(acosB)
angle = -180+ angle
return angle
KINECTEVENT = pygame.USEREVENT
DEPTH_WINSIZE = 320,240
VIDEO_WINSIZE = 640,480
pygame.init()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 10)
SKELETON_COLORS = [THECOLORS["red"],
THECOLORS["blue"],
THECOLORS["green"],
THECOLORS["orange"],
THECOLORS["purple"],
THECOLORS["yellow"],
THECOLORS["violet"]]
LEFT_ARM = (JointId.ShoulderCenter,
JointId.ShoulderLeft,
JointId.ElbowLeft,
JointId.WristLeft,
JointId.HandLeft)
RIGHT_ARM = (JointId.ShoulderCenter,
JointId.ShoulderRight,
JointId.ElbowRight,
JointId.WristRight,
JointId.HandRight)
LEFT_LEG = (JointId.HipCenter,
JointId.HipLeft,
JointId.KneeLeft,
JointId.AnkleLeft,
JointId.FootLeft)
RIGHT_LEG = (JointId.HipCenter,
JointId.HipRight,
JointId.KneeRight,
JointId.AnkleRight,
JointId.FootRight)
SPINE = (JointId.HipCenter,
JointId.Spine,
JointId.ShoulderCenter,
JointId.Head)
skeleton_to_depth_image = nui.SkeletonEngine.skeleton_to_depth_image
def draw_skeleton_data(pSkelton, index, positions, width = 4):
start = pSkelton.SkeletonPositions[positions[0]]
i=0
for position in itertools.islice(positions, 1, None):
next = pSkelton.SkeletonPositions[position.value]
i+=1
curstart = skeleton_to_depth_image(start, dispInfo.current_w, dispInfo.current_h)
curend = skeleton_to_depth_image(next, dispInfo.current_w, dispInfo.current_h)
pygame.draw.line(screen, SKELETON_COLORS[index], curstart, curend, width)
start = next
if hasattr(ctypes.pythonapi, 'Py_InitModule4'):
Py_ssize_t = ctypes.c_int
elif hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
Py_ssize_t = ctypes.c_int64
else:
raise TypeError("Cannot determine type of Py_ssize_t")
_PyObject_AsWriteBuffer = ctypes.pythonapi.PyObject_AsWriteBuffer
_PyObject_AsWriteBuffer.restype = ctypes.c_int
_PyObject_AsWriteBuffer.argtypes = [ctypes.py_object,
ctypes.POINTER(ctypes.c_void_p),
ctypes.POINTER(Py_ssize_t)]
def surface_to_array(surface):
buffer_interface = surface.get_buffer()
address = ctypes.c_void_p()
size = Py_ssize_t()
_PyObject_AsWriteBuffer(buffer_interface,
ctypes.byref(address), ctypes.byref(size))
bytes = (ctypes.c_byte * size.value).from_address(address.value)
bytes.object = buffer_interface
return bytes
def draw_skeletons(skeletons):
for index, data in enumerate(skeletons):
if data.eTrackingState==2:
HeadPos = skeleton_to_depth_image(data.SkeletonPositions[JointId.Head], dispInfo.current_w, dispInfo.current_h)
draw_skeleton_data(data, index, SPINE, 10)
pygame.draw.circle(screen, SKELETON_COLORS[index], (int(HeadPos[0]), int(HeadPos[1])), 20, 0)
draw_skeleton_data(data, index, LEFT_ARM)
draw_skeleton_data(data, index, RIGHT_ARM)
draw_skeleton_data(data, index, LEFT_LEG)
draw_skeleton_data(data, index, RIGHT_LEG)
shoulderLeft = data.SkeletonPositions[JointId.ShoulderLeft]
elbowLeft = data.SkeletonPositions[JointId.ElbowLeft]
wristLeft = data.SkeletonPositions[JointId.WristLeft]
shoulderRight = data.SkeletonPositions[JointId.ShoulderRight]
elbowRight = data.SkeletonPositions[JointId.ElbowRight]
wristRight = data.SkeletonPositions[JointId.WristRight]
listAngles.append(angleRShoulderPitch(shoulderRight.x, shoulderRight.y, shoulderRight.z, elbowRight.x, elbowRight.y,elbowRight.z)) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleRShoulderRoll(shoulderRight.x, shoulderRight.y, shoulderRight.z, elbowRight.x, elbowRight.y, elbowRight.z)) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleRElbowRoll(shoulderRight.x, shoulderRight.y, shoulderRight.z, elbowRight.x, elbowRight.y, elbowRight.z, wristRight.x, wristRight.y, wristRight.z)) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleRElbowYaw(elbowRight.x, elbowRight.y, elbowRight.z, wristRight.x, wristRight.y, wristRight.z, angleRShoulderPitch(shoulderRight.x, shoulderRight.y, shoulderRight.z, elbowRight.x, elbowRight.y, elbowRight.z))) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleLShoulderPitch(shoulderLeft.x, shoulderLeft.y, shoulderLeft.z, elbowLeft.x, elbowLeft.y, elbowLeft.z)) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleLShouderRoll(shoulderLeft.x, shoulderLeft.y, shoulderLeft.z, elbowLeft.x, elbowLeft.y, elbowLeft.z)) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleLElbowRoll(shoulderLeft.x, shoulderLeft.y, shoulderLeft.z, elbowLeft.x, elbowLeft.y, elbowLeft.z, wristLeft.x, wristLeft.y, wristLeft.z)) # calculates the angles via the Function with given coordinates and appends them to the masterlist
listAngles.append(angleLElbowYaw(elbowLeft.x, elbowLeft.y, elbowLeft.z, wristLeft.x, wristLeft.y, wristLeft.z, angleLShoulderPitch(shoulderLeft.x, shoulderLeft.y, shoulderLeft.z, elbowLeft.x, elbowLeft.y, elbowLeft.z))) # calculates the angles via the Function with given coordinates and appends them to the masterlist
sendrobot(listAngles, RobotIP, RobotPort)
time.sleep(0.3)
def depth_frame_ready(frame):
if video_display:
return
with screen_lock:
address = surface_to_array(screen)
frame.image.copy_bits(address)
del address
if skeletons is not None and draw_skeleton:
draw_skeletons(skeletons)
pygame.display.update()
def video_frame_ready(frame):
if not video_display:
return
with screen_lock:
address = surface_to_array(screen)
frame.image.copy_bits(address)
del address
if skeletons is not None and draw_skeleton:
draw_skeletons(skeletons)
pygame.display.update()
if __name__ == '__main__':
full_screen = False
draw_skeleton = True
video_display = False
screen_lock = thread.allocate()
screen = pygame.display.set_mode(DEPTH_WINSIZE,0,16)
pygame.display.set_caption('Python Kinect Demo')
skeletons = None
screen.fill(THECOLORS["black"])
kinect = nui.Runtime()
kinect.skeleton_engine.enabled = True
def post_frame(frame):
try:
pygame.event.post(pygame.event.Event(KINECTEVENT, skeletons = frame.SkeletonData))
except:
pass
kinect.skeleton_frame_ready += post_frame
kinect.depth_frame_ready += depth_frame_ready
kinect.video_frame_ready += video_frame_ready
kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)
kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution320x240, nui.ImageType.Depth)
print('Controls: ')
print(' d - Switch to depth view')
print(' v - Switch to video view')
print(' s - Toggle displaing of the skeleton')
print(' u - Increase elevation angle')
print(' j - Decrease elevation angle')
done = False
while not done:
e = pygame.event.wait()
dispInfo = pygame.display.Info()
if e.type == pygame.QUIT:
done = True
break
elif e.type == KINECTEVENT:
skeletons = e.skeletons
if draw_skeleton:
draw_skeletons(skeletons)
pygame.display.update()
elif e.type == KEYDOWN:
if e.key == K_ESCAPE:
done = True
break
elif e.key == K_d:
with screen_lock:
screen = pygame.display.set_mode(DEPTH_WINSIZE,0,16)
video_display = False
elif e.key == K_v:
with screen_lock:
screen = pygame.display.set_mode(VIDEO_WINSIZE,0,32)
video_display = True
elif e.key == K_s:
draw_skeleton = not draw_skeleton
elif e.key == K_u:
kinect.camera.elevation_angle = kinect.camera.elevation_angle + 2
elif e.key == K_j:
kinect.camera.elevation_angle = kinect.camera.elevation_angle - 2
elif e.key == K_x:
kinect.camera.elevation_angle = 2