Revision f8fa1217 client/python/hlrc_client/hlrc_test_gui.py

View differences:

client/python/hlrc_client/hlrc_test_gui.py
1 1
#!/usr/bin/python
2

  
3
"""
4
This file is part of hlrc
5

  
6
Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
7
http://opensource.cit-ec.de/projects/hlrc
8

  
9
This file may be licensed under the terms of the
10
GNU General Public License Version 3 (the ``GPL''),
11
or (at your option) any later version.
12

  
13
Software distributed under the License is distributed
14
on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
15
express or implied. See the GPL for the specific language
16
governing rights and limitations.
17

  
18
You should have received a copy of the GPL along with this
19
program. If not, go to http://www.gnu.org/licenses/gpl.html
20
or write to the Free Software Foundation, Inc.,
21
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22

  
23
The development of this software was supported by the
24
Excellence Cluster EXC 277 Cognitive Interaction Technology.
25
The Excellence Cluster EXC 277 is a grant of the Deutsche
26
Forschungsgemeinschaft (DFG) in the context of the German
27
Excellence Initiative.
28
"""
29

  
30

  
31
import logging
32
from hlrc_client import *
33
import time
34
import errno
2 35
import sys
3 36
import os.path
37

  
4 38
from PyQt4 import QtGui, QtCore
5 39
from PyQt4.QtCore import SIGNAL
6
from hlrc_speak_utterance import hlrc_utterance
7
from hlrc_play_animation import hlrc_animation
8
from hlrc_set_emotion import hlrc_emotion
9 40

  
10
class Example(QtGui.QWidget):
41
#from hlrc_speak_utterance import hlrc_utterance
42
#from hlrc_play_animation import hlrc_animation
43
#from hlrc_set_emotion import hlrc_emotion
44

  
45
class HLRCExample(QtGui.QWidget):
11 46
    
12
    def __init__(self, _scope):
13
        super(Example, self).__init__()
14
        self.scope = _scope
15
        self.initUI()
16
	scope = self.scope_lineedit.text()
17
        self.hlrc_utterance = hlrc_utterance(scope)
18
        self.hlrc_animation = hlrc_animation(scope)
19
        self.hlrc_emotion   = hlrc_emotion(scope)
20

  
21
    def scopeChanged(self):
22
	self.scope = self.scope_lineedit.text()
23
        print "> changed scope to %s" % self.scope
24
        self.hlrc_utterance.set_scope(self.scope)
25
        self.hlrc_animation.set_scope(self.scope)
26
        self.hlrc_emotion.set_scope(self.scope)
27

  
28
    def initUtteranceUI(self):
29
        groupBox = QtGui.QGroupBox("utterances")
30

  
31
        hello = QtGui.QPushButton("hello...")
32
        hello.clicked.connect(self.utterance_button_clicked)
33

  
34
        count = QtGui.QPushButton("12345...")
35
        count.clicked.connect(self.utterance_button_clicked)
36

  
37

  
38
        vbox = QtGui.QVBoxLayout()
39
        vbox.addWidget(hello)
40
        vbox.addWidget(count)
41
        vbox.addStretch(1)
42
        groupBox.setLayout(vbox)
43

  
44
        return groupBox
45

  
46
    def initSpinbox(self, _min, _max, _init, double):
47
        if (double): 
48
            s = QtGui.QDoubleSpinBox()
49
        else:
50
            s = QtGui.QSpinBox()
51

  
52
        s.setMinimum(_min)
53
        s.setMaximum(_max)
54
        s.setValue(_init)
55
        return s
56

  
57
    def initEmotionUI(self):
58
        groupBox = QtGui.QGroupBox("emotion")
59
        neutral   = QtGui.QPushButton("neutral")
60
        neutral.clicked.connect(self.emotion_button_clicked)
61
        surprised = QtGui.QPushButton("surprise")
62
        surprised.clicked.connect(self.emotion_button_clicked)
63
        angry     = QtGui.QPushButton("angry")
64
        angry.clicked.connect(self.emotion_button_clicked)
65
        happy     = QtGui.QPushButton("happy")
66
        happy.clicked.connect(self.emotion_button_clicked)
67
        sad     = QtGui.QPushButton("sad")
68
        sad.clicked.connect(self.emotion_button_clicked)
69
        fear      = QtGui.QPushButton("fear")
70
        fear.clicked.connect(self.emotion_button_clicked)
71

  
72
        self.duration_spinbox = self.initSpinbox(100, 10000, 1000, 0)
73

  
74
        vbox = QtGui.QVBoxLayout()
75
        vbox.addWidget(self.duration_spinbox)
76
        vbox.addWidget(neutral)
77
        vbox.addWidget(surprised)
78
        vbox.addWidget(angry)
79
        vbox.addWidget(happy)
80
        vbox.addWidget(sad)
81
        vbox.addWidget(fear)
82
        vbox.addStretch(1)
83
        groupBox.setLayout(vbox)
84
        return groupBox
85

  
86

  
87
    def initAnimationButton(self, name):
88
        b = QtGui.QPushButton(name)
89
        b.clicked.connect(self.animation_button_clicked)
90
        return b
91

  
92
    def initAnimUI(self):
93
        groupBox = QtGui.QGroupBox("animations")
94
  
95
        #options
96
        options = QtGui.QHBoxLayout()
97
        vbox_r = QtGui.QVBoxLayout()
98
        vbox_r.addWidget(QtGui.QLabel("repetitions"))
99
        self.repetitions_spinbox = self.initSpinbox(1,10,1,0)
100
        vbox_r.addWidget(self.repetitions_spinbox)
101

  
102
        vbox_d = QtGui.QVBoxLayout()
103
        vbox_d.addWidget(QtGui.QLabel("duration/ms (each)"))
104
        self.duration_each_spinbox = self.initSpinbox(100,10000,1000,0)
105
        vbox_d.addWidget(self.duration_each_spinbox)
106

  
107
        vbox_s = QtGui.QVBoxLayout()
108
        vbox_s.addWidget(QtGui.QLabel("scale"))
109
        self.scale_spinbox = self.initSpinbox(0.1, 5.0, 1.0, 1)
110
        vbox_s.addWidget(self.scale_spinbox)
111

  
112

  
113
        options.addLayout(vbox_r)
114
        options.addLayout(vbox_d)        
115
        options.addLayout(vbox_s)
116

  
117
        #buttons
118
        vbox = QtGui.QVBoxLayout()
119
        vbox.addLayout(options)
120
        vbox.addWidget(self.initAnimationButton("head shake"))
121
        vbox.addWidget(self.initAnimationButton("head nod"))
122
        vbox.addWidget(self.initAnimationButton("eyeblink left"))
123
        vbox.addWidget(self.initAnimationButton("eyeblink right"))
124
        vbox.addWidget(self.initAnimationButton("eyeblink both"))
125
	vbox.addWidget(self.initAnimationButton("eyebrows raise"))
126
	vbox.addWidget(self.initAnimationButton("eyebrows lower"))
127
        vbox.addStretch(1)
128
        groupBox.setLayout(vbox)
129

  
130
        return groupBox
131

  
132
    def initUI(self):        
133

  
134
        self.scope_lineedit = QtGui.QLineEdit(self.scope);
135
        self.connect(self.scope_lineedit, SIGNAL("editingFinished()"),self.scopeChanged)
136

  
137
	vbox = QtGui.QVBoxLayout();
47
	def __init__(self, scope, mw):
48
		super(HLRCExample, self).__init__()
138 49
	
139
	hboxl = QtGui.QHBoxLayout();
140
	hboxl.addWidget(QtGui.QLabel("base scope: "));
141
	hboxl.addWidget(self.scope_lineedit);
142
	vbox.addLayout(hboxl);
143

  
144
        hbox = QtGui.QHBoxLayout()
145
	vbox.addLayout(hbox);
50
		self.scope = scope
51
		self.mw = mw
52
		self.initUI()
53
		self.robot_controller = RobotController(self.mw, self.scope, logging.DEBUG)
54

  
55
	def initUtteranceUI(self):
56
		groupBox = QtGui.QGroupBox("utterances")
57

  
58
		hello = QtGui.QPushButton("hello...")
59
		hello.clicked.connect(self.utterance_button_clicked)
60

  
61
		count = QtGui.QPushButton("12345...")
62
		count.clicked.connect(self.utterance_button_clicked)
63

  
64

  
65
		vbox = QtGui.QVBoxLayout()
66
		vbox.addWidget(hello)
67
		vbox.addWidget(count)
68
		vbox.addStretch(1)
69
		groupBox.setLayout(vbox)
70

  
71
		return groupBox
72

  
73
	def initSpinbox(self, _min, _max, _init, double):
74
		if (double): 
75
			s = QtGui.QDoubleSpinBox()
76
		else:
77
			s = QtGui.QSpinBox()
78

  
79
		s.setMinimum(_min)
80
		s.setMaximum(_max)
81
		s.setValue(_init)
82
		return s
83

  
84
	def initPushButton(self, base_layout, text, action):
85
		button = QtGui.QPushButton(text)
86
		button.clicked.connect(action)
87
		base_layout.addWidget(button)
88
		
89
	def initEmotionUI(self):
90
		vbox = QtGui.QVBoxLayout()
91
		self.duration_spinbox = self.initSpinbox(100, 10000, 1000, 0)
92
		vbox.addWidget(self.duration_spinbox)
93
		
94
		self.duration_spinbox = self.initSpinbox(100, 10000, 1000, 0)
95
		self.initPushButton(vbox, "neutral",  self.emotion_button_clicked)
96
		self.initPushButton(vbox, "surprise", self.emotion_button_clicked)
97
		self.initPushButton(vbox, "angry",    self.emotion_button_clicked)
98
		self.initPushButton(vbox, "happy",    self.emotion_button_clicked)
99
		self.initPushButton(vbox, "sad",      self.emotion_button_clicked)
100
		self.initPushButton(vbox, "fear",     self.emotion_button_clicked)
101
		vbox.addStretch(1)
102
		
103
		groupBox = QtGui.QGroupBox("emotion")
104
		groupBox.setLayout(vbox)
105
		return groupBox
106

  
107
	def initAnimationButton(self, base_layout, name):
108
		b = QtGui.QPushButton(name)
109
		b.clicked.connect(self.animation_button_clicked)
110
		base_layout.addWidget(b)
111

  
112
	def initAnimUI(self):
113
		groupBox = QtGui.QGroupBox("animations")
146 114
	
147
        #emotion
148
        hbox.addWidget(self.initEmotionUI())
149

  
150
        #utterance:
151
        hbox.addWidget(self.initUtteranceUI())
152

  
153
        #animations:
154
        hbox.addWidget(self.initAnimUI())
155

  
156
        self.setLayout(vbox)
157
        #sld.valueChanged.connect(lcd.display)
158
        
159
        self.setGeometry(400, 100, 250, 150)
160
        self.setWindowTitle("HLC Server test GUI")
161
        self.show()
162

  
163
    def utterance_button_clicked(self):
164
        button = self.sender().text()
165
        
166
        fn_praat = "b.praat"
167
        fn_wav   = "b.wav"
168

  
169
        if (button == "12345..."):
170
            print("123")
171
            fn_praat = "1234.praat"
172
            fn_wav   = "1234.wav"
173

  
174
        elif (button == "hello..."):
175
            print "hello"
176
            fn_praat = "hello_my_name.praat"
177
            fn_wav   = "hello_my_name.wav"
178

  
179
        else:
180
            print "invalid button '%s'" % (button)
181
            return
182

  
183
        #try to find the wav/praat files:
184
        path = os.path.dirname(os.path.realpath(__file__)) + "/../share/hlrc_server/examples/"
185
        if (not os.path.isfile(path + fn_wav)):
186
            #try fallback solution for running from current folder:
187
            path = "examples/"
188

  
189
        self.hlrc_utterance.trigger_utterance(path + fn_praat, path + fn_wav, 0)
190

  
191
    def emotion_button_clicked(self):
192
        button = self.sender().text()
193
        duration = self.duration_spinbox.value()
194

  
195

  
196
        if (button == "angry"):
197
            self.hlrc_emotion.set_emotion(3, duration, 0)
198
        elif (button == "neutral"):
199
            self.hlrc_emotion.set_emotion(0, duration, 0)
200
        elif (button == "happy"):
201
            self.hlrc_emotion.set_emotion(1, duration, 0)
202
        elif (button == "sad"):
203
            self.hlrc_emotion.set_emotion(2, duration, 0)
204
        elif (button == "surprise"):
205
            self.hlrc_emotion.set_emotion(4, duration, 0)
206
        elif (button == "fear"):
207
            self.hlrc_emotion.set_emotion(5, duration, 0)
208
        else:
209
            print "invalid emotion id '%s'" % (button)
210
            return
211

  
212
    def animation_button_clicked(self):
213
        button = self.sender().text()
214

  
215
        repetitions = self.repetitions_spinbox.value()
216
        duration_each = self.duration_each_spinbox.value()
217
        scale = self.scale_spinbox.value()
218

  
219
        if (button == "head nod"):
220
            self.hlrc_animation.trigger_animation(1, repetitions, duration_each, scale, 0)
221
        elif (button == "head shake"):
222
            self.hlrc_animation.trigger_animation(2, repetitions, duration_each, scale, 0)
223
        elif (button == "eyeblink left"):
224
            self.hlrc_animation.trigger_animation(3, repetitions, duration_each, scale, 0)
225
        elif (button == "eyeblink right"):
226
            self.hlrc_animation.trigger_animation(4, repetitions, duration_each, scale, 0)
227
        elif (button == "eyeblink both"):
228
            self.hlrc_animation.trigger_animation(5, repetitions, duration_each, scale, 0)
229
        elif (button == "eyebrows raise"):
230
            self.hlrc_animation.trigger_animation(6, repetitions, duration_each, scale, 0)
231
        elif (button == "eyebrows lower"):
232
            self.hlrc_animation.trigger_animation(7, repetitions, duration_each, scale, 0)
233
        else:
234
            print "invalid button '%s'" % (button)
235
            return
115
		#options
116
		options = QtGui.QHBoxLayout()
117
		vbox_r = QtGui.QVBoxLayout()
118
		vbox_r.addWidget(QtGui.QLabel("repetitions"))
119
		self.repetitions_spinbox = self.initSpinbox(1,10,1,0)
120
		vbox_r.addWidget(self.repetitions_spinbox)
121

  
122
		vbox_d = QtGui.QVBoxLayout()
123
		vbox_d.addWidget(QtGui.QLabel("duration/ms (each)"))
124
		self.duration_each_spinbox = self.initSpinbox(100,10000,1000,0)
125
		vbox_d.addWidget(self.duration_each_spinbox)
126

  
127
		vbox_s = QtGui.QVBoxLayout()
128
		vbox_s.addWidget(QtGui.QLabel("scale"))
129
		self.scale_spinbox = self.initSpinbox(0.1, 5.0, 1.0, 1)
130
		vbox_s.addWidget(self.scale_spinbox)
131

  
132

  
133
		options.addLayout(vbox_r)
134
		options.addLayout(vbox_d)        
135
		options.addLayout(vbox_s)
136

  
137
		#buttons
138
		vbox = QtGui.QVBoxLayout()
139
		vbox.addLayout(options)
140
		self.initAnimationButton(vbox, "head shake")
141
		self.initAnimationButton(vbox, "head nod")
142
		self.initAnimationButton(vbox, "eyeblink left")
143
		self.initAnimationButton(vbox, "eyeblink right")
144
		self.initAnimationButton(vbox, "eyeblink both")
145
		self.initAnimationButton(vbox, "eyebrows raise")
146
		self.initAnimationButton(vbox, "eyebrows lower")
147
		vbox.addStretch(1)
148
		groupBox.setLayout(vbox)
149

  
150
		return groupBox
151

  
152
	def initUI(self):
153
		vbox = QtGui.QVBoxLayout()
154
		
155
		hboxl = QtGui.QHBoxLayout()
156
		hboxl.addWidget(QtGui.QLabel("base scope: "))
157
		hboxl.addWidget(QtGui.QLabel(self.scope))
158
		vbox.addLayout(hboxl)
159

  
160
		hbox = QtGui.QHBoxLayout()
161
		vbox.addLayout(hbox)
162
		
163
		#emotion
164
		hbox.addWidget(self.initEmotionUI())
165

  
166
		#utterance:
167
		hbox.addWidget(self.initUtteranceUI())
168

  
169
		#animations:
170
		hbox.addWidget(self.initAnimUI())
171

  
172
		self.setLayout(vbox)
173
		#sld.valueChanged.connect(lcd.display)
174
		
175
		self.setGeometry(400, 100, 250, 150)
176
		self.setWindowTitle("HLRC Server test GUI - %s" % self.scope)
177
		self.show()
178

  
179
	def utterance_button_clicked(self):
180
		button = self.sender().text()
181
		
182
		
183
		if (button == "12345..."):
184
			self.robot_controller.set_speak("1 2 3 4 5 6 7 8 9 10", False)
185
		elif (button == "hello..."):
186
			self.robot_controller.set_speak("Hello my name is flobi. How can i help you?", False)
187
		else:
188
			self.robot_controller.set_speak("invalid button name", False)
189
			print "invalid button '%s'" % (button)
190

  
191
	def emotion_button_clicked(self):
192
		button = self.sender().text()
193
		
194
		re = RobotEmotion()
195
		re.time_ms = self.duration_spinbox.value()
196
		
197
		if (button == "angry"):
198
			re.value = RobotEmotion.ANGRY
199
		elif (button == "neutral"):
200
			re.value = RobotEmotion.NEUTRAL
201
		elif (button == "happy"):
202
			re.value = RobotEmotion.HAPPY
203
		elif (button == "sad"):
204
			re.value = RobotEmotion.SAD
205
		elif (button == "surprise"):
206
			re.value = RobotEmotion.SURPRISED
207
		elif (button == "fear"):
208
			re.value = RobotEmotion.FEAR
209
		else:
210
			print "invalid emotion id '%s'" % (button)
211
			return 
212
		
213
		self.robot_controller.set_current_emotion(re, False)
214
		
215
	def animation_button_clicked(self):
216
		button = self.sender().text()
217
		
218
		ra = RobotAnimation()
219
		ra.time_ms =  self.duration_each_spinbox.value()
220
		ra.repetitions = self.repetitions_spinbox.value()
221
		ra.scale = self.scale_spinbox.value()
222

  
223
		if (button == "head nod"):
224
			ra.value = RobotAnimation.HEAD_NOD
225
		elif (button == "head shake"):
226
			ra.value = RobotAnimation.HEAD_SHAKE
227
		elif (button == "eyeblink left"):
228
			ra.value = RobotAnimation.EYEBLINK_L
229
		elif (button == "eyeblink right"):
230
			ra.value = RobotAnimation.EYEBLINK_R
231
		elif (button == "eyeblink both"):
232
			ra.value = RobotAnimation.EYEBLINK_BOTH
233
		elif (button == "eyebrows raise"):
234
			ra.value = RobotAnimation.EYEBROWS_RAISE
235
		elif (button == "eyebrows lower"):
236
			ra.value = RobotAnimation.EYEBROWS_LOWER
237
		else:
238
			print "invalid button '%s'" % (button)
239
			return
240
		
241
		self.robot_controller.set_head_animation(ra, False)
236 242

  
237 243
def main():
238
    scope = "/home/wardrobe/flobi"
239
    if (len(sys.argv) == 2):
244
	if (len(sys.argv) != 3):
245
		print("usage:   hlrc_test_gui.py <middleware> <scope>\n\n")
246
		print("example: hlrc_test_gui.py ROS /flobi\n\n")
247
		sys.exit(errno.EINVAL)
248
		
240 249
	scope = sys.argv[1]
250
	mw    = sys.argv[2]
241 251

  
242
    app = QtGui.QApplication(sys.argv)
243
    ex = Example(scope)
244
    sys.exit(app.exec_())
252
	app = QtGui.QApplication(sys.argv)
253
	ex = HLRCExample(mw, scope)
254
	sys.exit(app.exec_())
245 255

  
246 256

  
247 257
if __name__ == '__main__':
248
    main()
258
	main()

Also available in: Unified diff