hlrc / client / python / hlrc_client / MiddlewareRSB.py @ e3691d2d
History | View | Annotate | Download (11.214 KB)
1 |
"""
|
---|---|
2 |
This file is part of hlrc
|
3 |
|
4 |
Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
5 |
http://opensource.cit-ec.de/projects/hlrc
|
6 |
|
7 |
This file may be licensed under the terms of the
|
8 |
GNU General Public License Version 3 (the ``GPL''),
|
9 |
or (at your option) any later version.
|
10 |
|
11 |
Software distributed under the License is distributed
|
12 |
on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
13 |
express or implied. See the GPL for the specific language
|
14 |
governing rights and limitations.
|
15 |
|
16 |
You should have received a copy of the GPL along with this
|
17 |
program. If not, go to http://www.gnu.org/licenses/gpl.html
|
18 |
or write to the Free Software Foundation, Inc.,
|
19 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
|
21 |
The development of this software was supported by the
|
22 |
Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
23 |
The Excellence Cluster EXC 277 is a grant of the Deutsche
|
24 |
Forschungsgemeinschaft (DFG) in the context of the German
|
25 |
Excellence Initiative.
|
26 |
"""
|
27 |
|
28 |
from Middleware import * |
29 |
import errno |
30 |
|
31 |
import rsb |
32 |
import rsb.converter |
33 |
import rst |
34 |
|
35 |
from rstsandbox.animation.EmotionExpression_pb2 import EmotionExpression |
36 |
from rstsandbox.animation.HeadAnimation_pb2 import HeadAnimation |
37 |
from rstsandbox.animation.BinocularHeadGaze_pb2 import BinocularHeadGaze |
38 |
from rstsandbox.geometry.SphericalDirectionFloat_pb2 import SphericalDirectionFloat |
39 |
|
40 |
|
41 |
class MiddlewareRSB(Middleware): |
42 |
#######################################################################
|
43 |
def __init__(self, scope, loglevel=logging.WARNING): |
44 |
"""initialise
|
45 |
:param scope: base scope we want to listen on
|
46 |
"""
|
47 |
# init base settings
|
48 |
Middleware.__init__(self, scope, loglevel)
|
49 |
#call mw init
|
50 |
self.init_middleware()
|
51 |
|
52 |
def __del__(self): |
53 |
"""destructor
|
54 |
"""
|
55 |
self.logger.debug("destructor of MiddlewareROS called") |
56 |
|
57 |
#######################################################################
|
58 |
def init_middleware(self): |
59 |
"""initialise middleware
|
60 |
"""
|
61 |
# mute rsb logging:
|
62 |
logging.getLogger("rsb").setLevel(logging.ERROR)
|
63 |
|
64 |
#initialise RSB stuff
|
65 |
self.logger.info(
|
66 |
"initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope)) |
67 |
|
68 |
self.EmotionExpression_converter = rsb.converter.ProtocolBufferConverter(messageClass=EmotionExpression)
|
69 |
rsb.converter.registerGlobalConverter(self.EmotionExpression_converter)
|
70 |
|
71 |
self.HeadAnimation_converter = rsb.converter.ProtocolBufferConverter(messageClass=HeadAnimation)
|
72 |
rsb.converter.registerGlobalConverter(self.HeadAnimation_converter)
|
73 |
|
74 |
self.BinocularHeadGaze_converter = rsb.converter.ProtocolBufferConverter(messageClass=BinocularHeadGaze)
|
75 |
rsb.converter.registerGlobalConverter(self.BinocularHeadGaze_converter)
|
76 |
|
77 |
try:
|
78 |
self.server = rsb.createRemoteServer(self.base_scope + '/set') |
79 |
except ValueError: |
80 |
self.logger.error("ERROR: invalid scope given. server deactivated") |
81 |
self.server.deactivate()
|
82 |
sys.exit(errno.EINVAL) |
83 |
|
84 |
#######################################################################
|
85 |
def publish_emotion(self, em_type, emotion, blocking): |
86 |
"""publish an emotion via mw
|
87 |
:param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
|
88 |
:param emotion: emotion to set
|
89 |
:param blocking: True if this call should block until execution finished on robot
|
90 |
"""
|
91 |
|
92 |
# create emotion & fill it with values:
|
93 |
rsb_em = EmotionExpression() |
94 |
|
95 |
#set value
|
96 |
rsb_em.value = self.convert_emotiontype_to_rsbval(emotion.value)
|
97 |
|
98 |
#set duration
|
99 |
rsb_em.duration = int(emotion.time_ms)
|
100 |
|
101 |
with rsb.createRemoteServer(self.base_scope + '/set') as server: |
102 |
self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
103 |
|
104 |
if (blocking):
|
105 |
#blocking rpc call:
|
106 |
if (em_type == EmotionExpression.TYPE_DEFAULT):
|
107 |
result = server.defaultEmotion(rsb_em) |
108 |
else:
|
109 |
result = server.currentEmotion(rsb_em) |
110 |
|
111 |
self.logger.debug("server reply: '%s'" % result) |
112 |
else:
|
113 |
if (em_type == EmotionExpression.TYPE_DEFAULT):
|
114 |
future = server.defaultEmotion.async(rsb_em) |
115 |
else:
|
116 |
future = server.currentEmotion.async(rsb_em) |
117 |
|
118 |
#we could block here for a incoming result with a timeout in s
|
119 |
#print '> server reply: "%s"' % future.get(timeout = 10);
|
120 |
self.logger.debug("emotion rpc done") |
121 |
|
122 |
def publish_head_HeadAnimation(self, HeadAnimation, blocking): |
123 |
"""publish an head HeadAnimation via mw
|
124 |
:param HeadAnimation: HeadAnimation to set
|
125 |
:param blocking: True if this call should block until execution finished on robot
|
126 |
"""
|
127 |
|
128 |
self.logger.debug("calling the HeadAnimation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
129 |
|
130 |
# create HeadAnimation & fill it with values:
|
131 |
rsb_ani = HeadAnimation() |
132 |
|
133 |
#select ani
|
134 |
rsb_ani.target = self.convert_HeadAnimationtype_to_rsbval(HeadAnimation.value)
|
135 |
rsb_ani.repetitions = HeadAnimation.repetitions |
136 |
rsb_ani.duration_each = HeadAnimation.time_ms |
137 |
rsb_ani.scale = HeadAnimation.scale |
138 |
|
139 |
if (blocking):
|
140 |
#blocking:
|
141 |
result = self.server.HeadAnimation(rsb_ani)
|
142 |
self.logger.debug("server reply: '%s'" % result) |
143 |
else:
|
144 |
future = self.server.HeadAnimation.async(rsb_ani)
|
145 |
#we can block here for a incoming result with a timeout in s
|
146 |
#print '> server reply: "%s"' % future.get(timeout = 10);
|
147 |
|
148 |
self.logger.debug("HeadAnimation rpc done") |
149 |
|
150 |
def publish_default_emotion(self, emotion, blocking): |
151 |
self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
|
152 |
|
153 |
def publish_current_emotion(self, emotion, blocking): |
154 |
self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
|
155 |
|
156 |
def publish_gaze_target(self, gaze, blocking): |
157 |
"""publish a gaze target via mw
|
158 |
:param gaze: gaze to set
|
159 |
:param blocking: True if this call should block until execution finished on robot
|
160 |
"""
|
161 |
self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
162 |
|
163 |
# create gaze target & fill it with values:
|
164 |
rsb_gaze = BinocularHeadGaze() |
165 |
|
166 |
#fill proto
|
167 |
rsb_gaze.target = SphericalDirectionFloat() |
168 |
rsb_gaze.target.elevation = gaze.tilt |
169 |
rsb_gaze.target.azimuth = gaze.pan |
170 |
|
171 |
rsb_gaze.vergence = gaze.vergence |
172 |
|
173 |
rsb_gaze.offset = SphericalDirectionFloat() |
174 |
rsb_gaze.offset.elevation = gaze.tilt_offset |
175 |
rsb_gaze.offset.azimuth = gaze.pan_offset |
176 |
|
177 |
if (blocking):
|
178 |
# blocking:
|
179 |
result = self.server.gaze(rsb_gaze)
|
180 |
self.logger.debug("server reply: '%s'" % result) |
181 |
else:
|
182 |
future = self.server.gaze.async(rsb_gaze)
|
183 |
# we can block here for a incoming result with a timeout in s
|
184 |
#print '> server reply: "%s"' % future.get(timeout = 10);
|
185 |
|
186 |
self.logger.debug("gaze rpc done") |
187 |
|
188 |
"""
|
189 |
def publish_mouth_target(self, mouth, blocking):
|
190 |
publish a mouth target via mw
|
191 |
:param mouth: mouth value to set
|
192 |
:param blocking: True if this call should block until execution finished on robot
|
193 |
|
194 |
self.logger.debug("calling the mouth rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
|
195 |
|
196 |
#create mouth state & fill it with values:
|
197 |
rsb_mouth = MouthTarget()
|
198 |
|
199 |
#fill proto
|
200 |
rsb_mouth.opening_left = mouth.opening_left
|
201 |
rsb_mouth.opening_center = mouth.opening_center
|
202 |
rsb_mouth.opening_right = mouth.opening_right
|
203 |
rsb_mouth.position_left = mouth.position_left
|
204 |
rsb_mouth.position_center = mouth.position_center
|
205 |
rsb_mouth.position_right = mouth.position_right
|
206 |
|
207 |
if (blocking):
|
208 |
#blocking:
|
209 |
result = self.server.mouth(rsb_mouth)
|
210 |
self.logger.debug("server reply: '%s'" % result)
|
211 |
else:
|
212 |
future = self.server.mouth.async(rsb_mouth)
|
213 |
#we can block here for a incoming result with a timeout in s
|
214 |
#print '> server reply: "%s"' % future.get(timeout = 10);
|
215 |
|
216 |
self.logger.debug("mouth rpc done")
|
217 |
"""
|
218 |
|
219 |
|
220 |
def publish_speech(self, text, blocking): |
221 |
"""publish a tts request via mw
|
222 |
:param text: text to synthesize and speak
|
223 |
:param blocking: True if this call should block until execution finished on robot
|
224 |
"""
|
225 |
self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
226 |
|
227 |
if (blocking):
|
228 |
# blocking:
|
229 |
result = self.server.speech(text)
|
230 |
self.logger.debug("server reply: '%s'" % result) |
231 |
else:
|
232 |
future = self.server.speech.async(text)
|
233 |
# we can block here for a incoming result with a timeout in s
|
234 |
#print '> server reply: "%s"' % future.get(timeout = 10);
|
235 |
|
236 |
self.logger.debug("speech rpc done") |
237 |
|
238 |
|
239 |
#######################################################################
|
240 |
def is_running(self): |
241 |
return True |
242 |
|
243 |
|
244 |
#######################################################################
|
245 |
# some helpers
|
246 |
def convert_HeadAnimationtype_to_rsbval(self, value): |
247 |
"""convert RobotHeadAnimation.value to RSB HeadAnimation value
|
248 |
:param value: RobotHeadAnimation.* id to convert to rsb id
|
249 |
"""
|
250 |
# NOTE: this convertion is important as the actual integer values of
|
251 |
# thy python api and the protobuf might be different
|
252 |
|
253 |
if (value == RobotHeadAnimation.IDLE):
|
254 |
return HeadAnimation().IDLE
|
255 |
elif (value == RobotHeadAnimation.HEAD_NOD):
|
256 |
return HeadAnimation().HEAD_NOD
|
257 |
elif (value == RobotHeadAnimation.HEAD_SHAKE):
|
258 |
return HeadAnimation().HEAD_SHAKE
|
259 |
elif (value == RobotHeadAnimation.EYEBLINK_L):
|
260 |
return HeadAnimation().EYEBLINK_L
|
261 |
elif (value == RobotHeadAnimation.EYEBLINK_R):
|
262 |
return HeadAnimation().EYEBLINK_R
|
263 |
elif (value == RobotHeadAnimation.EYEBLINK_BOTH):
|
264 |
return HeadAnimation().EYEBLINK_BOTH
|
265 |
elif (value == RobotHeadAnimation.EYEBROWS_RAISE):
|
266 |
return HeadAnimation().EYEBROWS_RAISE
|
267 |
elif (value == RobotHeadAnimation.EYEBROWS_LOWER):
|
268 |
return HeadAnimation().EYEBROWS_LOWER
|
269 |
else:
|
270 |
self.logger.error("invalid HeadAnimation type %d\n" % (value)) |
271 |
return HeadAnimation().NEUTRAL
|
272 |
|
273 |
|
274 |
def convert_emotiontype_to_rsbval(self, value): |
275 |
"""convert RobotEmotion.value to RSB HeadAnimation value
|
276 |
:param value: RobotEmotion.* id to convert to rsb id
|
277 |
"""
|
278 |
# NOTE: this convertion is important as the actual integer values of
|
279 |
# thy python api and the protobuf might be different
|
280 |
|
281 |
if (value == RobotEmotion.NEUTRAL):
|
282 |
return EmotionExpression().NEUTRAL
|
283 |
elif (value == RobotEmotion.HAPPY):
|
284 |
return EmotionExpression().HAPPY
|
285 |
elif (value == RobotEmotion.SAD):
|
286 |
return EmotionExpression().SAD
|
287 |
elif (value == RobotEmotion.ANGRY):
|
288 |
return EmotionExpression().ANGRY
|
289 |
elif (value == RobotEmotion.SURPRISED):
|
290 |
return EmotionExpression().SURPRISED
|
291 |
elif (value == RobotEmotion.FEAR):
|
292 |
return EmotionExpression().FEAR
|
293 |
else:
|
294 |
self.logger.error("invalid emotion type %d\n" % (value)) |
295 |
return EmotionExpression().NEUTRAL
|