Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / MiddlewareRSB.py @ 13362ced

History | View | Annotate | Download (11.615 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 73f57e84 fl
# 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 73f57e84 fl
        self.EmotionExpression_converter = None
50
        self.HeadAnimation_converter = None
51
        self.BinocularHeadGaze_converter = None
52 70c54617 Simon Schulz
        #call mw init
53 ee1e3286 fl
        self.server = None
54 70c54617 Simon Schulz
        self.init_middleware()
55
56
    def __del__(self):
57
        """destructor
58
        """
59
        self.logger.debug("destructor of MiddlewareROS called")
60
61
    #######################################################################
62
    def init_middleware(self):
63
        """initialise middleware
64
        """
65 e3691d2d fl
        # mute rsb logging:
66 70c54617 Simon Schulz
        logging.getLogger("rsb").setLevel(logging.ERROR)
67
68
        #initialise RSB stuff
69 e3691d2d fl
        self.logger.info(
70
            "initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope))
71 70c54617 Simon Schulz
72 e3691d2d fl
        self.EmotionExpression_converter = rsb.converter.ProtocolBufferConverter(messageClass=EmotionExpression)
73 78cc756c Simon Schulz
        rsb.converter.registerGlobalConverter(self.EmotionExpression_converter)
74 70c54617 Simon Schulz
75 e3691d2d fl
        self.HeadAnimation_converter = rsb.converter.ProtocolBufferConverter(messageClass=HeadAnimation)
76 78cc756c Simon Schulz
        rsb.converter.registerGlobalConverter(self.HeadAnimation_converter)
77 70c54617 Simon Schulz
78 e3691d2d fl
        self.BinocularHeadGaze_converter = rsb.converter.ProtocolBufferConverter(messageClass=BinocularHeadGaze)
79 78cc756c Simon Schulz
        rsb.converter.registerGlobalConverter(self.BinocularHeadGaze_converter)
80 70c54617 Simon Schulz
81
        try:
82
            self.server = rsb.createRemoteServer(self.base_scope + '/set')
83
        except ValueError:
84
            self.logger.error("ERROR: invalid scope given. server deactivated")
85
            self.server.deactivate()
86
            sys.exit(errno.EINVAL)
87
88
    #######################################################################
89
    def publish_emotion(self, em_type, emotion, blocking):
90
        """publish an emotion via mw
91
        :param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
92
        :param emotion: emotion to set
93
        :param blocking: True if this call should block until execution finished on robot
94
        """
95
96 e3691d2d fl
        # create emotion & fill it with values:
97 78cc756c Simon Schulz
        rsb_em = EmotionExpression()
98 bbd41216 fl
        rsb_em.emotion = self.convert_emotiontype_to_rsbval(emotion.value)
99 70c54617 Simon Schulz
        rsb_em.duration = int(emotion.time_ms)
100
101 041c3638 fl
        # with rsb.createRemoteServer(self.base_scope + '/HeadAnimationset') 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 = self.server.emotion(rsb_em)
110
            self.logger.debug("server reply: '%s'" % result)
111
        else:
112
            #if (em_type == EmotionExpression.TYPE_DEFAULT):
113
            #    future = server.defaultEmotion.async(rsb_em)
114
            #else:
115
            future = self.server.emotion.async(rsb_em)
116
            self.logger.debug("server reply: '%s'" % future)
117
            #we could block here for a incoming result with a timeout in s
118
            #print '> server reply: "%s"' % future.get(timeout = 10);
119
        self.logger.debug("emotion rpc done")
120 70c54617 Simon Schulz
121 11b8a2b5 fl
    def publish_head_animation(self, animation, blocking):
122 78cc756c Simon Schulz
        """publish an head HeadAnimation via mw
123
        :param HeadAnimation: HeadAnimation to set
124 70c54617 Simon Schulz
        :param blocking: True if this call should block until execution finished on robot
125
        """
126
127 78cc756c Simon Schulz
        self.logger.debug("calling the HeadAnimation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
128 70c54617 Simon Schulz
129 e3691d2d fl
        # create HeadAnimation & fill it with values:
130 78cc756c Simon Schulz
        rsb_ani = HeadAnimation()
131 70c54617 Simon Schulz
132
        #select ani
133 11b8a2b5 fl
        rsb_ani.animation = self.convert_HeadAnimationtype_to_rsbval(animation.value)
134
        rsb_ani.repetitions = animation.repetitions
135
        rsb_ani.duration_each = animation.time_ms
136
        rsb_ani.emphasis_scale = animation.scale
137 70c54617 Simon Schulz
138 73f57e84 fl
        if blocking:
139 70c54617 Simon Schulz
            #blocking:
140 c00b9328 fl
            result = self.server.animation(rsb_ani)
141 70c54617 Simon Schulz
            self.logger.debug("server reply: '%s'" % result)
142
        else:
143 c00b9328 fl
            future = self.server.animation.async(rsb_ani)
144 70c54617 Simon Schulz
            #we can block here for a incoming result with a timeout in s
145
            #print '> server reply: "%s"' % future.get(timeout = 10);
146
147 78cc756c Simon Schulz
        self.logger.debug("HeadAnimation rpc done")
148 70c54617 Simon Schulz
149
    def publish_default_emotion(self, emotion, blocking):
150
        self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
151
152
    def publish_current_emotion(self, emotion, blocking):
153
        self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
154
155
    def publish_gaze_target(self, gaze, blocking):
156
        """publish a gaze target via mw
157
        :param gaze: gaze to set
158
        :param blocking: True if this call should block until execution finished on robot
159
        """
160
        self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
161
162 e3691d2d fl
        # create gaze target & fill it with values:
163 73f57e84 fl
        hg = BinocularHeadGaze()
164 70c54617 Simon Schulz
165 d076942c fl
        hg.target.elevation = gaze.tilt
166
        hg.target.azimuth = gaze.pan
167 73f57e84 fl
        hg.eye_vergence = gaze.vergence
168 78cc756c Simon Schulz
169 d076942c fl
        hg.offset.elevation = gaze.tilt_offset
170
        hg.offset.azimuth = gaze.pan_offset
171 0b6329fb fl
172 73f57e84 fl
        if blocking:
173 e3691d2d fl
            # blocking:
174 73f57e84 fl
            result = self.server.gaze(hg)
175 ee1e3286 fl
            self.logger.debug("server reply blocking: '%s'" % result)
176 70c54617 Simon Schulz
        else:
177 73f57e84 fl
            future = self.server.gaze.async(hg)
178 ee1e3286 fl
            self.logger.debug("server reply non-blocking: '%s'" % future)
179 e3691d2d fl
            # we can block here for a incoming result with a timeout in s
180 70c54617 Simon Schulz
            #print '> server reply: "%s"' % future.get(timeout = 10);
181
182
        self.logger.debug("gaze rpc done")
183
184 5eb9025f fl
    def publish_speech(self, text, blocking):
185
        """publish a tts request via mw
186
        :param text: text to synthesize and speak
187
        :param blocking: True if this call should block until execution finished on robot
188
        """
189 c00b9328 fl
        """
190 5eb9025f fl
        self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
191 70c54617 Simon Schulz

192 5eb9025f fl
        if (blocking):
193
            # blocking:
194
            result = self.server.speech(text)
195
            self.logger.debug("server reply: '%s'" % result)
196
        else:
197
            future = self.server.speech.async(text)
198
            # we can block here for a incoming result with a timeout in s
199
            #print '> server reply: "%s"' % future.get(timeout = 10);
200 70c54617 Simon Schulz

201 5eb9025f fl
        self.logger.debug("speech rpc done")
202 c00b9328 fl
        """
203
        print "WARNING: Not IMPLEMENTED."
204 70c54617 Simon Schulz
205
206 5eb9025f fl
    #######################################################################
207
    def is_running(self):
208
        return True
209 70c54617 Simon Schulz
210
211 5eb9025f fl
    #######################################################################
212
    # some helpers
213
    def convert_HeadAnimationtype_to_rsbval(self, value):
214
        """convert RobotHeadAnimation.value to RSB HeadAnimation value
215
        :param value: RobotHeadAnimation.* id to convert to rsb id
216
        """
217
        # NOTE: this convertion is important as the actual integer values of
218
        #      thy python api and the protobuf might be different
219
220 11b8a2b5 fl
        if value == HeadAnimation.IDLE:
221 5eb9025f fl
            return HeadAnimation().IDLE
222 11b8a2b5 fl
        elif value == HeadAnimation.HEAD_NOD:
223 5eb9025f fl
            return HeadAnimation().HEAD_NOD
224 11b8a2b5 fl
        elif (value == HeadAnimation.HEAD_SHAKE):
225 5eb9025f fl
            return HeadAnimation().HEAD_SHAKE
226 11b8a2b5 fl
        elif (value == HeadAnimation.EYEBLINK_LEFT):
227
            return HeadAnimation().EYEBLINK_LEFT
228
        elif (value == HeadAnimation.EYEBLINK_RIGHT):
229
            return HeadAnimation().EYEBLINK_RIGHT
230
        elif (value == HeadAnimation.EYEBLINK_BOTH):
231 5eb9025f fl
            return HeadAnimation().EYEBLINK_BOTH
232 11b8a2b5 fl
        elif (value == HeadAnimation.EYEBROWS_RAISE):
233 5eb9025f fl
            return HeadAnimation().EYEBROWS_RAISE
234 11b8a2b5 fl
        elif (value == HeadAnimation.EYEBROWS_LOWER):
235 5eb9025f fl
            return HeadAnimation().EYEBROWS_LOWER
236
        else:
237
            self.logger.error("invalid HeadAnimation type %d\n" % (value))
238
            return HeadAnimation().NEUTRAL
239 70c54617 Simon Schulz
240 5eb9025f fl
    def convert_emotiontype_to_rsbval(self, value):
241
        """convert RobotEmotion.value to RSB HeadAnimation value
242
        :param value: RobotEmotion.* id to convert to rsb id
243
        """
244
        # NOTE: this convertion is important as the actual integer values of
245
        #      thy python api and the protobuf might be different
246
247
        if (value == RobotEmotion.NEUTRAL):
248
            return EmotionExpression().NEUTRAL
249
        elif (value == RobotEmotion.HAPPY):
250
            return EmotionExpression().HAPPY
251
        elif (value == RobotEmotion.SAD):
252
            return EmotionExpression().SAD
253
        elif (value == RobotEmotion.ANGRY):
254
            return EmotionExpression().ANGRY
255
        elif (value == RobotEmotion.SURPRISED):
256
            return EmotionExpression().SURPRISED
257
        elif (value == RobotEmotion.FEAR):
258
            return EmotionExpression().FEAR
259
        else:
260
            self.logger.error("invalid emotion type %d\n" % (value))
261
            return EmotionExpression().NEUTRAL
262 e3691d2d fl
263
    """
264 5eb9025f fl
    def publish_mouth_target(self, mouth, blocking):
265
        publish a mouth target via mw
266
        :param mouth: mouth value to set
267
        :param blocking: True if this call should block until execution finished on robot
268

269
        self.logger.debug("calling the mouth rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
270

271
        #create mouth state & fill it with values:
272
        rsb_mouth = MouthTarget()
273

274
        #fill proto
275
        rsb_mouth.opening_left   = mouth.opening_left
276
        rsb_mouth.opening_center = mouth.opening_center
277
        rsb_mouth.opening_right  = mouth.opening_right
278
        rsb_mouth.position_left  = mouth.position_left
279
        rsb_mouth.position_center = mouth.position_center
280
        rsb_mouth.position_right = mouth.position_right
281

282
        if (blocking):
283
            #blocking:
284
            result = self.server.mouth(rsb_mouth)
285
            self.logger.debug("server reply: '%s'" % result)
286
        else:
287
            future = self.server.mouth.async(rsb_mouth)
288
            #we can block here for a incoming result with a timeout in s
289
            #print '> server reply: "%s"' % future.get(timeout = 10);
290

291
        self.logger.debug("mouth rpc done")
292
    """