hlrc / client / python / hlrc_client / MiddlewareRSB.py @ 0b6329fb
History | View | Annotate | Download (11.544 KB)
1 | 0c286af0 | Simon Schulz | """
|
---|---|---|---|
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 | ba6302b1 | Simon Schulz | import rsb |
32 | import rsb.converter |
||
33 | 64f5c90e | Simon Schulz | import rst |
34 | c827824f | fl | |
35 | 78cc756c | Simon Schulz | 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 | c827824f | fl | |
40 | 0c286af0 | Simon Schulz | |
41 | class MiddlewareRSB(Middleware): |
||
42 | 3877047d | Simon Schulz | #######################################################################
|
43 | def __init__(self, scope, loglevel=logging.WARNING): |
||
44 | 70c54617 | Simon Schulz | """initialise
|
45 | :param scope: base scope we want to listen on
|
||
46 | """
|
||
47 | e3691d2d | fl | # init base settings
|
48 | Middleware.__init__(self, scope, loglevel)
|
||
49 | 70c54617 | Simon Schulz | #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 | e3691d2d | fl | # mute rsb logging:
|
62 | 70c54617 | Simon Schulz | logging.getLogger("rsb").setLevel(logging.ERROR)
|
63 | |||
64 | #initialise RSB stuff
|
||
65 | e3691d2d | fl | self.logger.info(
|
66 | "initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope)) |
||
67 | 70c54617 | Simon Schulz | |
68 | e3691d2d | fl | self.EmotionExpression_converter = rsb.converter.ProtocolBufferConverter(messageClass=EmotionExpression)
|
69 | 78cc756c | Simon Schulz | rsb.converter.registerGlobalConverter(self.EmotionExpression_converter)
|
70 | 70c54617 | Simon Schulz | |
71 | e3691d2d | fl | self.HeadAnimation_converter = rsb.converter.ProtocolBufferConverter(messageClass=HeadAnimation)
|
72 | 78cc756c | Simon Schulz | rsb.converter.registerGlobalConverter(self.HeadAnimation_converter)
|
73 | 70c54617 | Simon Schulz | |
74 | e3691d2d | fl | self.BinocularHeadGaze_converter = rsb.converter.ProtocolBufferConverter(messageClass=BinocularHeadGaze)
|
75 | 78cc756c | Simon Schulz | rsb.converter.registerGlobalConverter(self.BinocularHeadGaze_converter)
|
76 | 70c54617 | Simon Schulz | |
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 | e3691d2d | fl | # create emotion & fill it with values:
|
93 | 78cc756c | Simon Schulz | rsb_em = EmotionExpression() |
94 | 70c54617 | Simon Schulz | |
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 | 78cc756c | Simon Schulz | if (em_type == EmotionExpression.TYPE_DEFAULT):
|
107 | 70c54617 | Simon Schulz | 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 | 78cc756c | Simon Schulz | if (em_type == EmotionExpression.TYPE_DEFAULT):
|
114 | 70c54617 | Simon Schulz | future = server.defaultEmotion.async(rsb_em) |
115 | else:
|
||
116 | future = server.currentEmotion.async(rsb_em) |
||
117 | |||
118 | e3691d2d | fl | #we could block here for a incoming result with a timeout in s
|
119 | #print '> server reply: "%s"' % future.get(timeout = 10);
|
||
120 | 70c54617 | Simon Schulz | self.logger.debug("emotion rpc done") |
121 | |||
122 | 78cc756c | Simon Schulz | def publish_head_HeadAnimation(self, HeadAnimation, blocking): |
123 | """publish an head HeadAnimation via mw
|
||
124 | :param HeadAnimation: HeadAnimation to set
|
||
125 | 70c54617 | Simon Schulz | :param blocking: True if this call should block until execution finished on robot
|
126 | """
|
||
127 | |||
128 | 78cc756c | Simon Schulz | self.logger.debug("calling the HeadAnimation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
129 | 70c54617 | Simon Schulz | |
130 | e3691d2d | fl | # create HeadAnimation & fill it with values:
|
131 | 78cc756c | Simon Schulz | rsb_ani = HeadAnimation() |
132 | 70c54617 | Simon Schulz | |
133 | #select ani
|
||
134 | 78cc756c | Simon Schulz | 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 | e3691d2d | fl | rsb_ani.scale = HeadAnimation.scale |
138 | 70c54617 | Simon Schulz | |
139 | if (blocking):
|
||
140 | #blocking:
|
||
141 | 78cc756c | Simon Schulz | result = self.server.HeadAnimation(rsb_ani)
|
142 | 70c54617 | Simon Schulz | self.logger.debug("server reply: '%s'" % result) |
143 | else:
|
||
144 | 78cc756c | Simon Schulz | future = self.server.HeadAnimation.async(rsb_ani)
|
145 | 70c54617 | Simon Schulz | #we can block here for a incoming result with a timeout in s
|
146 | #print '> server reply: "%s"' % future.get(timeout = 10);
|
||
147 | |||
148 | 78cc756c | Simon Schulz | self.logger.debug("HeadAnimation rpc done") |
149 | 70c54617 | Simon Schulz | |
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 | e3691d2d | fl | # create gaze target & fill it with values:
|
164 | 78cc756c | Simon Schulz | rsb_gaze = BinocularHeadGaze() |
165 | 70c54617 | Simon Schulz | |
166 | #fill proto
|
||
167 | 0b6329fb | fl | t = SphericalDirectionFloat() |
168 | t.elevation = gaze.tilt |
||
169 | t.azimuth = gaze.pan |
||
170 | rsb_gaze.target = t |
||
171 | 78cc756c | Simon Schulz | |
172 | 0b6329fb | fl | rsb_gaze.eye_vergence = gaze.vergence |
173 | 78cc756c | Simon Schulz | |
174 | 0b6329fb | fl | o = SphericalDirectionFloat() |
175 | o.elevation = gaze.tilt_offset |
||
176 | o.azimuth = gaze.pan_offset |
||
177 | |||
178 | rsb_gaze.offset = o |
||
179 | 70c54617 | Simon Schulz | |
180 | if (blocking):
|
||
181 | e3691d2d | fl | # blocking:
|
182 | 70c54617 | Simon Schulz | result = self.server.gaze(rsb_gaze)
|
183 | self.logger.debug("server reply: '%s'" % result) |
||
184 | else:
|
||
185 | future = self.server.gaze.async(rsb_gaze)
|
||
186 | e3691d2d | fl | # we can block here for a incoming result with a timeout in s
|
187 | 70c54617 | Simon Schulz | #print '> server reply: "%s"' % future.get(timeout = 10);
|
188 | |||
189 | self.logger.debug("gaze rpc done") |
||
190 | |||
191 | 5eb9025f | fl | def publish_speech(self, text, blocking): |
192 | """publish a tts request via mw
|
||
193 | :param text: text to synthesize and speak
|
||
194 | :param blocking: True if this call should block until execution finished on robot
|
||
195 | """
|
||
196 | self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
197 | 70c54617 | Simon Schulz | |
198 | 5eb9025f | fl | if (blocking):
|
199 | # blocking:
|
||
200 | result = self.server.speech(text)
|
||
201 | self.logger.debug("server reply: '%s'" % result) |
||
202 | else:
|
||
203 | future = self.server.speech.async(text)
|
||
204 | # we can block here for a incoming result with a timeout in s
|
||
205 | #print '> server reply: "%s"' % future.get(timeout = 10);
|
||
206 | 70c54617 | Simon Schulz | |
207 | 5eb9025f | fl | self.logger.debug("speech rpc done") |
208 | 70c54617 | Simon Schulz | |
209 | |||
210 | 5eb9025f | fl | #######################################################################
|
211 | def is_running(self): |
||
212 | return True |
||
213 | 70c54617 | Simon Schulz | |
214 | |||
215 | 5eb9025f | fl | #######################################################################
|
216 | # some helpers
|
||
217 | def convert_HeadAnimationtype_to_rsbval(self, value): |
||
218 | """convert RobotHeadAnimation.value to RSB HeadAnimation value
|
||
219 | :param value: RobotHeadAnimation.* id to convert to rsb id
|
||
220 | """
|
||
221 | # NOTE: this convertion is important as the actual integer values of
|
||
222 | # thy python api and the protobuf might be different
|
||
223 | |||
224 | if (value == RobotHeadAnimation.IDLE):
|
||
225 | return HeadAnimation().IDLE
|
||
226 | elif (value == RobotHeadAnimation.HEAD_NOD):
|
||
227 | return HeadAnimation().HEAD_NOD
|
||
228 | elif (value == RobotHeadAnimation.HEAD_SHAKE):
|
||
229 | return HeadAnimation().HEAD_SHAKE
|
||
230 | elif (value == RobotHeadAnimation.EYEBLINK_L):
|
||
231 | return HeadAnimation().EYEBLINK_L
|
||
232 | elif (value == RobotHeadAnimation.EYEBLINK_R):
|
||
233 | return HeadAnimation().EYEBLINK_R
|
||
234 | elif (value == RobotHeadAnimation.EYEBLINK_BOTH):
|
||
235 | return HeadAnimation().EYEBLINK_BOTH
|
||
236 | elif (value == RobotHeadAnimation.EYEBROWS_RAISE):
|
||
237 | return HeadAnimation().EYEBROWS_RAISE
|
||
238 | elif (value == RobotHeadAnimation.EYEBROWS_LOWER):
|
||
239 | return HeadAnimation().EYEBROWS_LOWER
|
||
240 | else:
|
||
241 | self.logger.error("invalid HeadAnimation type %d\n" % (value)) |
||
242 | return HeadAnimation().NEUTRAL
|
||
243 | 70c54617 | Simon Schulz | |
244 | 5eb9025f | fl | def convert_emotiontype_to_rsbval(self, value): |
245 | """convert RobotEmotion.value to RSB HeadAnimation value
|
||
246 | :param value: RobotEmotion.* id to convert to rsb id
|
||
247 | """
|
||
248 | # NOTE: this convertion is important as the actual integer values of
|
||
249 | # thy python api and the protobuf might be different
|
||
250 | |||
251 | if (value == RobotEmotion.NEUTRAL):
|
||
252 | return EmotionExpression().NEUTRAL
|
||
253 | elif (value == RobotEmotion.HAPPY):
|
||
254 | return EmotionExpression().HAPPY
|
||
255 | elif (value == RobotEmotion.SAD):
|
||
256 | return EmotionExpression().SAD
|
||
257 | elif (value == RobotEmotion.ANGRY):
|
||
258 | return EmotionExpression().ANGRY
|
||
259 | elif (value == RobotEmotion.SURPRISED):
|
||
260 | return EmotionExpression().SURPRISED
|
||
261 | elif (value == RobotEmotion.FEAR):
|
||
262 | return EmotionExpression().FEAR
|
||
263 | else:
|
||
264 | self.logger.error("invalid emotion type %d\n" % (value)) |
||
265 | return EmotionExpression().NEUTRAL
|
||
266 | e3691d2d | fl | |
267 | """
|
||
268 | 5eb9025f | fl | def publish_mouth_target(self, mouth, blocking):
|
269 | publish a mouth target via mw
|
||
270 | :param mouth: mouth value to set
|
||
271 | :param blocking: True if this call should block until execution finished on robot
|
||
272 |
|
||
273 | self.logger.debug("calling the mouth rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
|
||
274 |
|
||
275 | #create mouth state & fill it with values:
|
||
276 | rsb_mouth = MouthTarget()
|
||
277 |
|
||
278 | #fill proto
|
||
279 | rsb_mouth.opening_left = mouth.opening_left
|
||
280 | rsb_mouth.opening_center = mouth.opening_center
|
||
281 | rsb_mouth.opening_right = mouth.opening_right
|
||
282 | rsb_mouth.position_left = mouth.position_left
|
||
283 | rsb_mouth.position_center = mouth.position_center
|
||
284 | rsb_mouth.position_right = mouth.position_right
|
||
285 |
|
||
286 | if (blocking):
|
||
287 | #blocking:
|
||
288 | result = self.server.mouth(rsb_mouth)
|
||
289 | self.logger.debug("server reply: '%s'" % result)
|
||
290 | else:
|
||
291 | future = self.server.mouth.async(rsb_mouth)
|
||
292 | #we can block here for a incoming result with a timeout in s
|
||
293 | #print '> server reply: "%s"' % future.get(timeout = 10);
|
||
294 |
|
||
295 | self.logger.debug("mouth rpc done")
|
||
296 | """ |