-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
260 lines (202 loc) · 5.59 KB
/
client.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
import sys
from PyQt4.QtGui import QWidget, QImage, QApplication, QPainter
from PyQt4 import QtCore
from naoqi import ALProxy
import vision_definitions
import socket
import struct
import os
import glob
HOST = '172.16.21.209' # The remote host
PORT = 50009 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
i = 0
flag = 0
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
class ImageWidget(QWidget):
def __init__(self, IP, PORT, CameraID, parent=None):
"""
Initialization.
"""
QWidget.__init__(self, parent)
self.initUI()
self._image = QImage()
self.setWindowTitle('Nao')
self._imgWidth =100
self._imgHeight =100
self._cameraID = CameraID
self.resize(self._imgWidth, self._imgHeight)
# Proxy to ALVideoDevice.
self._videoProxy = None
# Our video module name.
self._imgClient = ""
# This will contain this alImage we get from Nao.
self._alImage = None
self.nao_ip = IP
self.nao_port = PORT
self._registerImageClient(IP, PORT)
# Trigget 'timerEvent' every 100 ms.
self.startTimer(100)
def _registerImageClient(self, IP, PORT):
"""
Register our video module to the robot.
"""
self._videoProxy = ALProxy("ALVideoDevice", IP, PORT)
resolution = vision_definitions.kQQVGA
colorSpace = vision_definitions.kRGBColorSpace
self._imgClient = self._videoProxy.subscribe("_client", resolution, colorSpace, 5)
# Select camera.
self._videoProxy.setParam(vision_definitions.kCameraSelectID,
self._cameraID)
def _unregisterImageClient(self):
"""
Unregister our naoqi video module.
"""
if self._imgClient != "":
self._videoProxy.unsubscribe(self._imgClient)
def paintEvent(self, event):
"""
Draw the QImage on screen.
"""
# painter = QPainter(self)
# painter.drawImage(painter.viewport(), self._image)
def _updateImage(self):
"""
Retrieve a new image from Nao.
"""
global i
global flag
self._alImage = self._videoProxy.getImageRemote(self._imgClient)
self._image = QImage(self._alImage[6], # Pixel array.
self._alImage[0], # Width.
self._alImage[1], # Height.
QImage.Format_RGB888)
if flag == 1:
if i < 10:
filename = './temp/test_img/0' + str(i) + '.png'
else:
filename = './temp/test_img/' + str(i) + '.png'
self._image.save(filename)
i +=1
def timerEvent(self, event):
"""
Called periodically. Retrieve a nao image, and update the widget.
"""
self._updateImage()
self.update()
def __del__(self):
"""
When the widget is deleted, we unregister our naoqi video module.
"""
self._unregisterImageClient()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Event handler')
self.show()
def keyPressEvent(self, e):
global flag
global i
if e.key() == QtCore.Qt.Key_S:
flag = 1
i = 0
print ("Start Capture")
elif e.key() == QtCore.Qt.Key_Q:
flag = 0
print ("End Capture")
s.sendall('Event')
data = s.recv(1024)
ans = int(data.encode('hex'), 16)
print(ans)
tts = ALProxy("ALTextToSpeech", self.nao_ip, self.nao_port)
if ans == 0:
# no gesture
tts.say("No Gesture Detected")
elif ans == 1:
# left
pi_2 = -3.1415/2
postureProxy = ALProxy("ALRobotPosture", self.nao_ip, self.nao_port)
posture = postureProxy.getPosture()
if(posture == 'Sit'):
tts.say("I need to stand first")
postureProxy.goToPosture("Stand", 1.0)
tts.say("Moving to your Left")
motion = ALProxy("ALMotion", self.nao_ip, self.nao_port)
motion.moveInit()
motion.moveTo(0, 0, pi_2)
motion.moveTo(0.2, 0, 0)
elif ans == 2:
# right
pi_2 = 3.1415/2
postureProxy = ALProxy("ALRobotPosture", self.nao_ip, self.nao_port)
posture = postureProxy.getPosture()
if(posture == 'Sit'):
tts.say("I need to stand first")
postureProxy.goToPosture("Stand", 1.0)
tts.say("Moving to your Right")
motion = ALProxy("ALMotion", self.nao_ip, self.nao_port)
motion.moveInit()
motion.moveTo(0, 0, pi_2)
motion.moveTo(0.2, 0, 0)
elif ans == 3:
# down
tts.say("Sit Down")
postureProxy = ALProxy("ALRobotPosture", self.nao_ip, self.nao_port)
postureProxy.goToPosture("Sit", 1.0)
elif ans == 4:
# up
tts.say("Stand Up")
postureProxy = ALProxy("ALRobotPosture", self.nao_ip, self.nao_port)
postureProxy.goToPosture("Stand", 1.0)
elif ans == 5:
# shaking hand
tts.say("Hi")
elif ans == 6:
# stop
postureProxy = ALProxy("ALRobotPosture", self.nao_ip, self.nao_port)
posture = postureProxy.getPosture()
if(posture == 'Sit'):
tts.say("I need to stand first")
postureProxy.goToPosture("Stand", 1.0)
tts.say("Moving Back")
motion = ALProxy("ALMotion", self.nao_ip, self.nao_port)
motion.moveInit()
motion.moveTo(-0.2, 0, 0)
elif ans == 7:
# forward
postureProxy = ALProxy("ALRobotPosture", self.nao_ip, self.nao_port)
posture = postureProxy.getPosture()
if(posture == 'Sit'):
tts.say("I need to stand first")
postureProxy.goToPosture("Stand", 1.0)
tts.say("Moving Forward")
motion = ALProxy("ALMotion", self.nao_ip, self.nao_port)
motion.moveInit()
motion.moveTo(0.2, 0, 0)
files = glob.glob('./temp/test_img/*')
for f in files:
os.remove(f)
self.close()
s.close()
if __name__ == '__main__':
IP = "172.16.21.202" # Replace here with your NaoQi's IP address.
PORT = 9559
CameraID = 0
# Read IP address from first argument if any.
if len(sys.argv) > 1:
IP = sys.argv[1]
# Read CameraID from second argument if any.
if len(sys.argv) > 2:
CameraID = int(sys.argv[2])
aware = ALProxy("ALBasicAwareness",IP,PORT)
aware.setEngagementMode("FullyEngaged")
aware.setStimulusDetectionEnabled("Sound",False)
aware.startAwareness()
app = QApplication(sys.argv)
myWidget = ImageWidget(IP, PORT, CameraID)
myWidget.show()
sys.exit(app.exec_())