Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / MiddlewareRSB.py @ 2bbc570d

History | View | Annotate | Download (11.745 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
        self.server = None
53
54 70c54617 Simon Schulz
        #call mw init
55
        self.init_middleware()
56
57
    def __del__(self):
58
        """destructor
59
        """
60
        self.logger.debug("destructor of MiddlewareROS called")
61
62
    #######################################################################
63
    def init_middleware(self):
64
        """initialise middleware
65
        """
66 e3691d2d fl
        # mute rsb logging:
67 70c54617 Simon Schulz
        logging.getLogger("rsb").setLevel(logging.ERROR)
68
69
        #initialise RSB stuff
70 e3691d2d fl
        self.logger.info(
71
            "initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope))
72 70c54617 Simon Schulz
73 e3691d2d fl
        self.EmotionExpression_converter = rsb.converter.ProtocolBufferConverter(messageClass=EmotionExpression)
74 78cc756c Simon Schulz
        rsb.converter.registerGlobalConverter(self.EmotionExpression_converter)
75 70c54617 Simon Schulz
76 e3691d2d fl
        self.HeadAnimation_converter = rsb.converter.ProtocolBufferConverter(messageClass=HeadAnimation)
77 78cc756c Simon Schulz
        rsb.converter.registerGlobalConverter(self.HeadAnimation_converter)
78 70c54617 Simon Schulz
79 e3691d2d fl
        self.BinocularHeadGaze_converter = rsb.converter.ProtocolBufferConverter(messageClass=BinocularHeadGaze)
80 78cc756c Simon Schulz
        rsb.converter.registerGlobalConverter(self.BinocularHeadGaze_converter)
81 70c54617 Simon Schulz
82
        try:
83
            self.server = rsb.createRemoteServer(self.base_scope + '/set')
84
        except ValueError:
85
            self.logger.error("ERROR: invalid scope given. server deactivated")
86
            self.server.deactivate()
87
            sys.exit(errno.EINVAL)
88
89
    #######################################################################
90
    def publish_emotion(self, em_type, emotion, blocking):
91
        """publish an emotion via mw
92
        :param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
93
        :param emotion: emotion to set
94
        :param blocking: True if this call should block until execution finished on robot
95
        """
96
97 e3691d2d fl
        # create emotion & fill it with values:
98 78cc756c Simon Schulz
        rsb_em = EmotionExpression()
99 70c54617 Simon Schulz
100
        #set value
101
        rsb_em.value = self.convert_emotiontype_to_rsbval(emotion.value)
102
103
        #set duration
104
        rsb_em.duration = int(emotion.time_ms)
105
106
        with rsb.createRemoteServer(self.base_scope + '/set') as server:
107
            self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
108
109
            if (blocking):
110
                #blocking rpc call:
111 78cc756c Simon Schulz
                if (em_type == EmotionExpression.TYPE_DEFAULT):
112 70c54617 Simon Schulz
                    result = server.defaultEmotion(rsb_em)
113
                else:
114
                    result = server.currentEmotion(rsb_em)
115
116
                self.logger.debug("server reply: '%s'" % result)
117
            else:
118 78cc756c Simon Schulz
                if (em_type == EmotionExpression.TYPE_DEFAULT):
119 70c54617 Simon Schulz
                    future = server.defaultEmotion.async(rsb_em)
120
                else:
121
                    future = server.currentEmotion.async(rsb_em)
122
123 e3691d2d fl
                    #we could block here for a incoming result with a timeout in s
124
                    #print '> server reply: "%s"' % future.get(timeout = 10);
125 70c54617 Simon Schulz
            self.logger.debug("emotion rpc done")
126
127 78cc756c Simon Schulz
    def publish_head_HeadAnimation(self, HeadAnimation, blocking):
128
        """publish an head HeadAnimation via mw
129
        :param HeadAnimation: HeadAnimation to set
130 70c54617 Simon Schulz
        :param blocking: True if this call should block until execution finished on robot
131
        """
132
133 78cc756c Simon Schulz
        self.logger.debug("calling the HeadAnimation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
134 70c54617 Simon Schulz
135 e3691d2d fl
        # create HeadAnimation & fill it with values:
136 78cc756c Simon Schulz
        rsb_ani = HeadAnimation()
137 70c54617 Simon Schulz
138
        #select ani
139 78cc756c Simon Schulz
        rsb_ani.target = self.convert_HeadAnimationtype_to_rsbval(HeadAnimation.value)
140
        rsb_ani.repetitions = HeadAnimation.repetitions
141
        rsb_ani.duration_each = HeadAnimation.time_ms
142 e3691d2d fl
        rsb_ani.scale = HeadAnimation.scale
143 70c54617 Simon Schulz
144 73f57e84 fl
        if blocking:
145 70c54617 Simon Schulz
            #blocking:
146 78cc756c Simon Schulz
            result = self.server.HeadAnimation(rsb_ani)
147 70c54617 Simon Schulz
            self.logger.debug("server reply: '%s'" % result)
148
        else:
149 78cc756c Simon Schulz
            future = self.server.HeadAnimation.async(rsb_ani)
150 70c54617 Simon Schulz
            #we can block here for a incoming result with a timeout in s
151
            #print '> server reply: "%s"' % future.get(timeout = 10);
152
153 78cc756c Simon Schulz
        self.logger.debug("HeadAnimation rpc done")
154 70c54617 Simon Schulz
155
    def publish_default_emotion(self, emotion, blocking):
156
        self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
157
158
    def publish_current_emotion(self, emotion, blocking):
159
        self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
160
161
    def publish_gaze_target(self, gaze, blocking):
162
        """publish a gaze target via mw
163
        :param gaze: gaze to set
164
        :param blocking: True if this call should block until execution finished on robot
165
        """
166
        self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
167
168 e3691d2d fl
        # create gaze target & fill it with values:
169 73f57e84 fl
        hg = BinocularHeadGaze()
170 70c54617 Simon Schulz
171 0b6329fb fl
        t = SphericalDirectionFloat()
172
        t.elevation = gaze.tilt
173
        t.azimuth = gaze.pan
174 5c8ed74d fl
        hg.target.CopyFrom(t)
175 78cc756c Simon Schulz
176 73f57e84 fl
        hg.eye_vergence = gaze.vergence
177 78cc756c Simon Schulz
178 0b6329fb fl
        o = SphericalDirectionFloat()
179
        o.elevation = gaze.tilt_offset
180
        o.azimuth = gaze.pan_offset
181 5c8ed74d fl
        hg.offset.CopyFrom(o)
182 0b6329fb fl
183 73f57e84 fl
        if blocking:
184 e3691d2d fl
            # blocking:
185 73f57e84 fl
            result = self.server.gaze(hg)
186 2bbc570d fl
            self.logger.info("server reply blocking: '%s'" % result)
187 70c54617 Simon Schulz
        else:
188 73f57e84 fl
            future = self.server.gaze.async(hg)
189 2bbc570d fl
            self.logger.info("server reply non-blocking: '%s'" % future)
190 e3691d2d fl
            # we can block here for a incoming result with a timeout in s
191 70c54617 Simon Schulz
            #print '> server reply: "%s"' % future.get(timeout = 10);
192
193
        self.logger.debug("gaze rpc done")
194
195 5eb9025f fl
    def publish_speech(self, text, blocking):
196
        """publish a tts request via mw
197
        :param text: text to synthesize and speak
198
        :param blocking: True if this call should block until execution finished on robot
199
        """
200
        self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
201 70c54617 Simon Schulz
202 5eb9025f fl
        if (blocking):
203
            # blocking:
204
            result = self.server.speech(text)
205
            self.logger.debug("server reply: '%s'" % result)
206
        else:
207
            future = self.server.speech.async(text)
208
            # we can block here for a incoming result with a timeout in s
209
            #print '> server reply: "%s"' % future.get(timeout = 10);
210 70c54617 Simon Schulz
211 5eb9025f fl
        self.logger.debug("speech rpc done")
212 70c54617 Simon Schulz
213
214 5eb9025f fl
    #######################################################################
215
    def is_running(self):
216
        return True
217 70c54617 Simon Schulz
218
219 5eb9025f fl
    #######################################################################
220
    # some helpers
221
    def convert_HeadAnimationtype_to_rsbval(self, value):
222
        """convert RobotHeadAnimation.value to RSB HeadAnimation value
223
        :param value: RobotHeadAnimation.* id to convert to rsb id
224
        """
225
        # NOTE: this convertion is important as the actual integer values of
226
        #      thy python api and the protobuf might be different
227
228
        if (value == RobotHeadAnimation.IDLE):
229
            return HeadAnimation().IDLE
230
        elif (value == RobotHeadAnimation.HEAD_NOD):
231
            return HeadAnimation().HEAD_NOD
232
        elif (value == RobotHeadAnimation.HEAD_SHAKE):
233
            return HeadAnimation().HEAD_SHAKE
234
        elif (value == RobotHeadAnimation.EYEBLINK_L):
235
            return HeadAnimation().EYEBLINK_L
236
        elif (value == RobotHeadAnimation.EYEBLINK_R):
237
            return HeadAnimation().EYEBLINK_R
238
        elif (value == RobotHeadAnimation.EYEBLINK_BOTH):
239
            return HeadAnimation().EYEBLINK_BOTH
240
        elif (value == RobotHeadAnimation.EYEBROWS_RAISE):
241
            return HeadAnimation().EYEBROWS_RAISE
242
        elif (value == RobotHeadAnimation.EYEBROWS_LOWER):
243
            return HeadAnimation().EYEBROWS_LOWER
244
        else:
245
            self.logger.error("invalid HeadAnimation type %d\n" % (value))
246
            return HeadAnimation().NEUTRAL
247 70c54617 Simon Schulz
248 5eb9025f fl
    def convert_emotiontype_to_rsbval(self, value):
249
        """convert RobotEmotion.value to RSB HeadAnimation value
250
        :param value: RobotEmotion.* id to convert to rsb id
251
        """
252
        # NOTE: this convertion is important as the actual integer values of
253
        #      thy python api and the protobuf might be different
254
255
        if (value == RobotEmotion.NEUTRAL):
256
            return EmotionExpression().NEUTRAL
257
        elif (value == RobotEmotion.HAPPY):
258
            return EmotionExpression().HAPPY
259
        elif (value == RobotEmotion.SAD):
260
            return EmotionExpression().SAD
261
        elif (value == RobotEmotion.ANGRY):
262
            return EmotionExpression().ANGRY
263
        elif (value == RobotEmotion.SURPRISED):
264
            return EmotionExpression().SURPRISED
265
        elif (value == RobotEmotion.FEAR):
266
            return EmotionExpression().FEAR
267
        else:
268
            self.logger.error("invalid emotion type %d\n" % (value))
269
            return EmotionExpression().NEUTRAL
270 e3691d2d fl
271
    """
272 5eb9025f fl
    def publish_mouth_target(self, mouth, blocking):
273
        publish a mouth target via mw
274
        :param mouth: mouth value to set
275
        :param blocking: True if this call should block until execution finished on robot
276

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

279
        #create mouth state & fill it with values:
280
        rsb_mouth = MouthTarget()
281

282
        #fill proto
283
        rsb_mouth.opening_left   = mouth.opening_left
284
        rsb_mouth.opening_center = mouth.opening_center
285
        rsb_mouth.opening_right  = mouth.opening_right
286
        rsb_mouth.position_left  = mouth.position_left
287
        rsb_mouth.position_center = mouth.position_center
288
        rsb_mouth.position_right = mouth.position_right
289

290
        if (blocking):
291
            #blocking:
292
            result = self.server.mouth(rsb_mouth)
293
            self.logger.debug("server reply: '%s'" % result)
294
        else:
295
            future = self.server.mouth.async(rsb_mouth)
296
            #we can block here for a incoming result with a timeout in s
297
            #print '> server reply: "%s"' % future.get(timeout = 10);
298

299
        self.logger.debug("mouth rpc done")
300
    """