-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicRobotMain.py
486 lines (420 loc) · 13.7 KB
/
MicRobotMain.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# coding=UTF-8
#system
import sys,os,time
import math
import numpy as np
import openni
import serial
from functools import partial
#PyQt5
from PyQt5 import QtCore
from PyQt5.QtCore import Qt,QObject,QTimer,QUrl,QFileInfo
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QImage,QPixmap,QPen,QColor,QPainter,QIcon
#Project
import KinectSensor as KS
from MicRobot import MicRobot
import mapTable
class AngleInfo(QObject):
swingAngleV=0
armAngleV=0
armSpringAngleV=0
armFrontAngleV=0
KMConnectedV=False
def _swingAngle(self):
return self.swingAngleV;
def _armAngle(self):
return self.armAngleV;
def _armSpringAngle(self):
return self.armSpringAngleV;
def _armFrontAngle(self):
return self.armFrontAngleV;
def _KMConnected(self):
return self.KMConnectedV;
def _swingAngleS(self,v):
self.swingAngleV=v;
def _armAngleS(self,v):
self.armAngleV=v;
def _armSpringAngleS(self,v):
self.armSpringAngleV=v;
def _armFrontAngleS(self,v):
self.armFrontAngleV=v;
#return swingAngleV;
swingAngle=QtCore.pyqtProperty(float, fget=_swingAngle,fset=_swingAngleS);
armAngle=QtCore.pyqtProperty(float, fget=_armAngle,fset=_armAngleS);
armSpringAngle=QtCore.pyqtProperty(float,
fget=_armSpringAngle,fset=_armSpringAngleS);
armFrontAngle=QtCore.pyqtProperty(float,
fget=_armFrontAngle,fset=_armFrontAngleS);
KMConnected=QtCore.pyqtProperty(bool, fget=_KMConnected);
class RefreshTimerThread(QtCore.QThread):
#trigger=QtCore.pyqtSignal(int)
KinectFail=QtCore.pyqtSignal()
KinectReady=QtCore.pyqtSignal()
joint_request=['head','neck',
'torso','left_shoulder','left_elbow','left_hand']
joint_list=[]
joint_dic={}
refreshInterval=0.041
def __init__(self,parent=None):
super(RefreshTimerThread,self).__init__(parent)
#self.initTimer()
def setup(self, m):
self.m=m
def run(self):
while True:
#time.sleep(0.001)
time.sleep(self.refreshInterval)
self.refresh();
def initTimer(self):
self.timer=QTimer()
self.timer.timeout.connect(self.refresh)
self.timer.start(11)
#self.kstimer=QTimer()
#self.kstimer.timeout.connect(self.refresh)
#self.kstimer.start(41)
def dist(self,a,b,d=3):
s=0
for i in range(d):
s+=(a[i]-b[i])*(a[i]-b[i])
return math.sqrt(s)
def armTranspose(self):
if len(self.joint_request) != len(self.joint_list):
#print "need all %d points for transpose" % len(self.joint_request)
return
for i in range(len(self.joint_request)):
self.joint_dic[self.joint_request[i]]=self.joint_list[i]
j=self.joint_dic
u=self.dist(j['head'], j['torso'])/2
head_x=j['head'][0]
head_y=j['head'][1]
head_d=j['head'][2]
hand_x=j['left_hand'][0]
hand_y=j['left_hand'][1]
hand_d=j['left_hand'][2]
torso_x=j['torso'][0]
torso_y=j['torso'][1]
torso_d=j['torso'][2]
self.m.angleInfo.swingAngleV=math.atan2(torso_d-hand_d, torso_x-hand_x)#+math.pi/2
h=(head_y-torso_y)
u=h/1.5
dx=-1*math.sqrt((torso_x-hand_x)*(torso_x-hand_x)+(torso_d-hand_d)*(torso_d-hand_d))
dy=-1*(torso_y-hand_y)
ux=dx/u
uy=dy/u
mx=ux*230/3;
mx+=250;
my=uy*230/3;
try:
if int(mx)>=-230 and int(mx)<230 and int(my)>0 and int(my)<230 and mapTable.mapTable[int(mx)][int(my)][0] > -3.14:
self.m.angleInfo.armAngleV=mapTable.mapTable[int(mx)][int(my)][0]/180.0*math.pi
self.m.angleInfo.armSpringAngleV=mapTable.mapTable[int(mx)][int(my)][1]/180.0*math.pi
self.m.angleInfo.armFrontAngleV=mapTable.mapTable[int(mx)][int(my)][2]/180.0*math.pi
except IndexError:
print "out of robot range"
def hmapping(self,value, ulimit=3000, llimit=1500):
if value < llimit:
value = llimit
elif value >ulimit:
value = ulimit
return ((value-llimit)*300)/(ulimit-llimit)
def refresh(self):
#self.m.angleInfo.swingAngleV+=0.05
#self.m.angleInfo.armAngleV=math.pi/6;
#self.m.angleInfo.armSpringAngleV=math.pi/6;
#self.m.angleInfo.armFrontAngleV=math.pi/6;
refreshDepthImage=None
refreshImageImage=None
if self.m.KSAvailable!=True:
refreshDepthImage=self.m.imgBlank
refreshImageImage=self.m.imgBlank
else:
try:
#RGB Image
imageFrame=KS.readImage()
self.m.imageList[self.m.imgpos]=(
QImage(imageFrame,640,480,
QImage.Format_RGB888).rgbSwapped())
refreshImageImage=self.m.imageList[self.m.imgpos]
#Depth image
frame=KS.readDepth()
f=np.fromstring(frame,dtype=np.uint8)
self.m.depthList[self.m.imgpos]=QImage(
f.repeat(4),640,480,QImage.Format_RGB32)
refreshDepthImage=self.m.depthList[self.m.imgpos]
#Draw points on depth image
skeleton_list = []
KS.ctx.wait_and_update_all()
user_list=[]
for u_id in KS.userNode.users:
readRet=KS.readSkeleton(self.joint_request, u_id)
user_list.append(readRet)
max_u=[[],[]]
for u in user_list:
if len(u[0])>=len(max_u[0]):
max_u=u
skeleton_list = max_u[0]
self.joint_list=max_u[1]
modifier = QPainter()
color=QColor()
modifier.begin(refreshDepthImage)
for ske_joint in skeleton_list:
if ske_joint[1] < 0:
continue
depth=self.hmapping(ske_joint[2])
color.setHsv(depth,255,255)
modifier.setPen(
QPen(color,
15,
cap=Qt.RoundCap)
)
modifier.drawPoint(ske_joint[0],ske_joint[1])
modifier.end()
except openni.OpenNIError:
self.m.KSAvailable=False
print 'Kinect Sensor disconnected.'
self.m.KSCheck('failurecheck');
return
self.armTranspose()
#Depth
self.m.depthLabel.setPixmap(
QPixmap.fromImage(refreshDepthImage.scaled(
self.m.depthLabel.width(),self.m.depthLabel.height(),
Qt.KeepAspectRatio))
)
#Image
self.m.imageLabel.setPixmap(
QPixmap.fromImage(refreshImageImage.scaled(
self.m.imageLabel.width(),self.m.imageLabel.height(),
Qt.KeepAspectRatio))
)
self.m.imgpos=1-self.m.imgpos;
class RefreshRobotThread(QtCore.QThread):
refreshInterval=0.08
def __init__(self,m):
super(RefreshRobotThread,self).__init__()
self.m=m
def run(self):
while True:
time.sleep(self.refreshInterval)
if self.m.MRConnected and self.m.Robot.connected:
self.refresh()
def refresh(self):
print self.m.angleInfo.swingAngleV
print self.m.angleInfo.armAngleV
print self.m.angleInfo.armSpringAngleV
print self.m.angleInfo.armFrontAngleV
cmd="A %d %d %d %d %d" % (
-1,
int(self.m.angleInfo.armFrontAngleV/math.pi*180),
int(self.m.angleInfo.armSpringAngleV/math.pi*180),
int(self.m.angleInfo.armAngleV/math.pi*180),
int(self.m.angleInfo.swingAngleV/math.pi*180)+90)
print cmd
try:
self.m.Robot.write(cmd)
self.m.Robot.ser.flushInput()
self.m.Robot.ser.flushOutput()
except serial.serialutil.SerialException:
print "Refresh Robot Write Fail"
class MicRobotMain(QMainWindow):
#For Kinect
imgpos=0
depthList=[]
imageList=[]
KSAvailable=False
#For robot
#pl=R.listActivePorts()
#R.setPar(pl[0],115200)
#R.connect()
def __init__(self):
super(MicRobotMain, self).__init__()
self.initUI()
def initSim(self):
self.simView=QWebView()
self.angleInfo=AngleInfo()
self.simView.page().mainFrame().\
addToJavaScriptWindowObject("angleInfo",self.angleInfo)
self.simPath=QFileInfo("./html/index.html").absoluteFilePath()
self.simView.setUrl(QUrl.fromLocalFile(self.simPath))
self.simView.setWindowFlags(Qt.FramelessWindowHint)
def initRobot(self):
self.Robot=MicRobot()
'''
self.testLabel=QLabel()
self.testLabel.setText("abc")
self.testSlider=QSlider(Qt.Horizontal)
self.testSlider.setRange(0,180)
self.testSlider.setValue(0)
self.testSlider.valueChanged.connect(self.fun)
'''
def initKinectUI(self):
#UI For Kinect RGB image and Depth image
self.imgBlank=QImage("./image/kinect.png")
self.depthLabel=QLabel()
self.imageLabel=QLabel()
self.depthLabel.setAlignment(Qt.AlignCenter)
self.imageLabel.setAlignment(Qt.AlignCenter)
self.depthList.append(self.imgBlank)
self.depthList.append(self.imgBlank)
self.imageList.append(self.imgBlank)
self.imageList.append(self.imgBlank)
self.depthLabel.setPixmap(QPixmap.fromImage(self.imgBlank).scaled(
self.depthLabel.width(),
self.depthLabel.height(),
Qt.KeepAspectRatio))
self.imageLabel.setPixmap(QPixmap.fromImage(self.imgBlank).scaled(
self.depthLabel.width(),
self.depthLabel.height(),
Qt.KeepAspectRatio))
def initCommLayerUI(self):
self.imgKinectAvailable=QImage("./image/kinect_available.png")
self.imgKinectDisable=QImage("./image/kinect_disable.png")
self.imgRobotAvailable=QImage("./image/robot_available.png")
self.imgRobotDisable=QImage("./image/robot_disable.png")
self.imgMain=QImage("./image/microbot_long.png")
self.commKinectLabel=QLabel()
self.commMainLabel=QLabel()
self.commRobotLabel=QLabel()
self.commKinectLabel.setPixmap(QPixmap.fromImage(self.imgKinectAvailable))
self.commMainLabel.setPixmap(QPixmap.fromImage(self.imgMain))
self.commRobotLabel.setPixmap(QPixmap.fromImage(self.imgRobotDisable))
#K Kinect
#M Main
#R Robot
#self.commKMLabel=QLabel()
#self.commMRLabel=QLabel()
#self.imgConnected=QImage("./image/connected.png")
#self.imgDisconnected=QImage("./image/disconnected.png")
#self.commKMLabel.setPixmap(QPixmap.fromImage(self.imgDisconnected))
#self.commMRLabel.setPixmap(QPixmap.fromImage(self.imgDisconnected))
self.pmConnected=QPixmap("./image/connected.png")
self.pmDisconnected=QPixmap("./image/disconnected.png")
self.iconConnected=QIcon(self.pmConnected)
self.iconDisconnected=QIcon(self.pmDisconnected)
self.commKMButton=QPushButton()
self.commMRButton=QPushButton()
self.commKMButton.setIcon(self.iconDisconnected)
self.commMRButton.setIcon(self.iconDisconnected)
self.commKMButton.setIconSize(self.pmDisconnected.rect().size())
self.commMRButton.setIconSize(self.pmDisconnected.rect().size())
self.commKMButton.setDisabled(True)
self.commMRButton.setDisabled(True)
self.commLayerLayout=QHBoxLayout()
self.commLayerLayout.addWidget(self.commKinectLabel)
#self.commLayerLayout.addWidget(self.commKMLabel)
self.commLayerLayout.addWidget(self.commKMButton)
self.commLayerLayout.addWidget(self.commMainLabel)
#self.commLayerLayout.addWidget(self.commMRLabel)
self.commLayerLayout.addWidget(self.commMRButton)
self.commLayerLayout.addWidget(self.commRobotLabel)
self.commLayerLayout.setAlignment(Qt.AlignCenter)
self.commLayerWidget=QWidget()
self.commLayerWidget.setLayout(self.commLayerLayout)
self.MRConnected=False
self.KMConnected=False
self.commMRButton.clicked.connect(self.MRButtonClick)
self.commKMButton.clicked.connect(self.KMButtonClick)
def disconnectKM(self):
self.KMConnected=False
self.angleInfo.KMConnectedV=False
self.commKMButton.setIcon(self.iconDisconnected)
def connectKM(self):
self.KMConnected=True
self.angleInfo.KMConnectedV=True
self.commKMButton.setIcon(self.iconConnected)
def disconnectMR(self):
self.MRConnected=False
self.commMRButton.setIcon(self.iconDisconnected)
def connectMR(self):
self.MRConnected=True
self.commMRButton.setIcon(self.iconConnected)
def KMButtonClick(self):
if self.KMConnected:
self.disconnectKM()
else:
self.connectKM()
def MRButtonClick(self):
if self.MRConnected:
self.disconnectMR()
else:
self.connectMR()
def initRefreshTimer(self):
self.refreshThread=RefreshTimerThread()
self.kstimer=QTimer()
self.refreshThread.setup(self)
self.refreshThread.start()
self.kstimer.start(1000)
def initKSCheckTimer(self):
self.kschecktimer=QTimer()
self.kschecktimer.timeout.connect(
partial(self.KSCheck,trigger='timercheck'))
self.kschecktimer.start(40)
def initRobotTimer(self):
self.robotTimer=QTimer()
self.robotTimer.timeout.connect(self.Robot.checkAvailable)
self.robotTimer.start(1000)
self.refreshRobotThread=RefreshRobotThread(self)
self.robottimer=QTimer()
self.refreshRobotThread.start()
def initUI(self):
self.initSim()
self.initRobot()
self.initKinectUI()
self.initCommLayerUI()
#Construcet Left UI
self.leftLayout=QVBoxLayout()
self.leftLayout.addWidget(self.commLayerWidget)
self.leftLayout.addWidget(self.imageLabel)
self.leftLayout.addWidget(self.depthLabel)
self.leftSide=QWidget()
self.leftSide.setLayout(self.leftLayout)
self.leftSide.setWindowFlags(Qt.FramelessWindowHint)
#Construct splitter
self.split=QSplitter(Qt.Horizontal)
self.split.addWidget(self.leftSide)
self.split.addWidget(self.simView)
#Init timers
self.initRefreshTimer()
self.connectComm()
self.initKSCheckTimer()
self.initRobotTimer()
#show
self.split.show()
#self.split.showFullScreen()
self.commKinectLabel.setScaledContents(True)
def kinectReady(self):
self.commKinectLabel.setPixmap(QPixmap.fromImage(self.imgKinectAvailable))
self.commKMButton.setEnabled(True)
def kinectFail(self):
self.commKinectLabel.setPixmap(QPixmap.fromImage(self.imgKinectDisable))
self.disconnectKM()
self.commKMButton.setDisabled(True)
def robotReady(self):
self.commRobotLabel.setPixmap(QPixmap.fromImage(self.imgRobotAvailable))
self.commMRButton.setEnabled(True)
def robotFail(self):
self.commRobotLabel.setPixmap(QPixmap.fromImage(self.imgRobotDisable))
self.disconnectMR()
self.commMRButton.setDisabled(True)
def connectComm(self):
self.refreshThread.KinectReady.connect(self.kinectReady)
self.refreshThread.KinectFail.connect(self.kinectFail)
self.Robot.serReady.connect(self.robotReady)
self.Robot.serFail.connect(self.robotFail)
#Check kinect availability
def KSCheck(self,trigger):
if trigger=='timercheck':
if self.KSAvailable!=True and KS.deviceAvailable():
####Here may also fail
KS.startAll()
self.KSAvailable=True
self.refreshThread.KinectReady.emit()
elif trigger=='failurecheck':
print "failurecheck"
KS.reInit()
if not KS.deviceAvailable():
print "failurecheck failed"
self.KSAvailable=False
self.refreshThread.KinectFail.emit()