Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / MiddlewareRSB.py @ ee1e3286

History | View | Annotate | Download (11.644 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 70c54617 Simon Schulz
99
        #set value
100
        rsb_em.value = self.convert_emotiontype_to_rsbval(emotion.value)
101
102
        #set duration
103
        rsb_em.duration = int(emotion.time_ms)
104
105
        with rsb.createRemoteServer(self.base_scope + '/set') as server:
106
            self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
107
108
            if (blocking):
109
                #blocking rpc call:
110 78cc756c Simon Schulz
                if (em_type == EmotionExpression.TYPE_DEFAULT):
111 70c54617 Simon Schulz
                    result = server.defaultEmotion(rsb_em)
112
                else:
113
                    result = server.currentEmotion(rsb_em)
114
115
                self.logger.debug("server reply: '%s'" % result)
116
            else:
117 78cc756c Simon Schulz
                if (em_type == EmotionExpression.TYPE_DEFAULT):
118 70c54617 Simon Schulz
                    future = server.defaultEmotion.async(rsb_em)
119
                else:
120
                    future = server.currentEmotion.async(rsb_em)
121
122 e3691d2d fl
                    #we could block here for a incoming result with a timeout in s
123
                    #print '> server reply: "%s"' % future.get(timeout = 10);
124 70c54617 Simon Schulz
            self.logger.debug("emotion rpc done")
125
126 78cc756c Simon Schulz
    def publish_head_HeadAnimation(self, HeadAnimation, blocking):
127
        """publish an head HeadAnimation via mw
128
        :param HeadAnimation: HeadAnimation to set
129 70c54617 Simon Schulz
        :param blocking: True if this call should block until execution finished on robot
130
        """
131
132 78cc756c Simon Schulz
        self.logger.debug("calling the HeadAnimation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
133 70c54617 Simon Schulz
134 e3691d2d fl
        # create HeadAnimation & fill it with values:
135 78cc756c Simon Schulz
        rsb_ani = HeadAnimation()
136 70c54617 Simon Schulz
137
        #select ani
138 78cc756c Simon Schulz
        rsb_ani.target = self.convert_HeadAnimationtype_to_rsbval(HeadAnimation.value)
139
        rsb_ani.repetitions = HeadAnimation.repetitions
140
        rsb_ani.duration_each = HeadAnimation.time_ms
141 e3691d2d fl
        rsb_ani.scale = HeadAnimation.scale
142 70c54617 Simon Schulz
143 73f57e84 fl
        if blocking:
144 70c54617 Simon Schulz
            #blocking:
145 78cc756c Simon Schulz
            result = self.server.HeadAnimation(rsb_ani)
146 70c54617 Simon Schulz
            self.logger.debug("server reply: '%s'" % result)
147
        else:
148 78cc756c Simon Schulz
            future = self.server.HeadAnimation.async(rsb_ani)
149 70c54617 Simon Schulz
            #we can block here for a incoming result with a timeout in s
150
            #print '> server reply: "%s"' % future.get(timeout = 10);
151
152 78cc756c Simon Schulz
        self.logger.debug("HeadAnimation rpc done")
153 70c54617 Simon Schulz
154
    def publish_default_emotion(self, emotion, blocking):
155
        self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
156
157
    def publish_current_emotion(self, emotion, blocking):
158
        self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
159
160
    def publish_gaze_target(self, gaze, blocking):
161
        """publish a gaze target via mw
162
        :param gaze: gaze to set
163
        :param blocking: True if this call should block until execution finished on robot
164
        """
165
        self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
166
167 e3691d2d fl
        # create gaze target & fill it with values:
168 73f57e84 fl
        hg = BinocularHeadGaze()
169 70c54617 Simon Schulz
170 d076942c fl
        hg.target.elevation = gaze.tilt
171
        hg.target.azimuth = gaze.pan
172 73f57e84 fl
        hg.eye_vergence = gaze.vergence
173 78cc756c Simon Schulz
174 d076942c fl
        hg.offset.elevation = gaze.tilt_offset
175
        hg.offset.azimuth = gaze.pan_offset
176 0b6329fb fl
177 73f57e84 fl
        if blocking:
178 e3691d2d fl
            # blocking:
179 73f57e84 fl
            result = self.server.gaze(hg)
180 ee1e3286 fl
            self.logger.debug("server reply blocking: '%s'" % result)
181 70c54617 Simon Schulz
        else:
182 73f57e84 fl
            future = self.server.gaze.async(hg)
183 ee1e3286 fl
            self.logger.debug("server reply non-blocking: '%s'" % future)
184 e3691d2d fl
            # we can block here for a incoming result with a timeout in s
185 70c54617 Simon Schulz
            #print '> server reply: "%s"' % future.get(timeout = 10);
186
187
        self.logger.debug("gaze rpc done")
188
189 5eb9025f fl
    def publish_speech(self, text, blocking):
190
        """publish a tts request via mw
191
        :param text: text to synthesize and speak
192
        :param blocking: True if this call should block until execution finished on robot
193
        """
194
        self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
195 70c54617 Simon Schulz
196 5eb9025f fl
        if (blocking):
197
            # blocking:
198
            result = self.server.speech(text)
199
            self.logger.debug("server reply: '%s'" % result)
200
        else:
201
            future = self.server.speech.async(text)
202
            # we can block here for a incoming result with a timeout in s
203
            #print '> server reply: "%s"' % future.get(timeout = 10);
204 70c54617 Simon Schulz
205 5eb9025f fl
        self.logger.debug("speech rpc done")
206 70c54617 Simon Schulz
207
208 5eb9025f fl
    #######################################################################
209
    def is_running(self):
210
        return True
211 70c54617 Simon Schulz
212
213 5eb9025f fl
    #######################################################################
214
    # some helpers
215
    def convert_HeadAnimationtype_to_rsbval(self, value):
216
        """convert RobotHeadAnimation.value to RSB HeadAnimation value
217
        :param value: RobotHeadAnimation.* id to convert to rsb id
218
        """
219
        # NOTE: this convertion is important as the actual integer values of
220
        #      thy python api and the protobuf might be different
221
222
        if (value == RobotHeadAnimation.IDLE):
223
            return HeadAnimation().IDLE
224
        elif (value == RobotHeadAnimation.HEAD_NOD):
225
            return HeadAnimation().HEAD_NOD
226
        elif (value == RobotHeadAnimation.HEAD_SHAKE):
227
            return HeadAnimation().HEAD_SHAKE
228
        elif (value == RobotHeadAnimation.EYEBLINK_L):
229
            return HeadAnimation().EYEBLINK_L
230
        elif (value == RobotHeadAnimation.EYEBLINK_R):
231
            return HeadAnimation().EYEBLINK_R
232
        elif (value == RobotHeadAnimation.EYEBLINK_BOTH):
233
            return HeadAnimation().EYEBLINK_BOTH
234
        elif (value == RobotHeadAnimation.EYEBROWS_RAISE):
235
            return HeadAnimation().EYEBROWS_RAISE
236
        elif (value == RobotHeadAnimation.EYEBROWS_LOWER):
237
            return HeadAnimation().EYEBROWS_LOWER
238
        else:
239
            self.logger.error("invalid HeadAnimation type %d\n" % (value))
240
            return HeadAnimation().NEUTRAL
241 70c54617 Simon Schulz
242 5eb9025f fl
    def convert_emotiontype_to_rsbval(self, value):
243
        """convert RobotEmotion.value to RSB HeadAnimation value
244
        :param value: RobotEmotion.* id to convert to rsb id
245
        """
246
        # NOTE: this convertion is important as the actual integer values of
247
        #      thy python api and the protobuf might be different
248
249
        if (value == RobotEmotion.NEUTRAL):
250
            return EmotionExpression().NEUTRAL
251
        elif (value == RobotEmotion.HAPPY):
252
            return EmotionExpression().HAPPY
253
        elif (value == RobotEmotion.SAD):
254
            return EmotionExpression().SAD
255
        elif (value == RobotEmotion.ANGRY):
256
            return EmotionExpression().ANGRY
257
        elif (value == RobotEmotion.SURPRISED):
258
            return EmotionExpression().SURPRISED
259
        elif (value == RobotEmotion.FEAR):
260
            return EmotionExpression().FEAR
261
        else:
262
            self.logger.error("invalid emotion type %d\n" % (value))
263
            return EmotionExpression().NEUTRAL
264 e3691d2d fl
265
    """
266 5eb9025f fl
    def publish_mouth_target(self, mouth, blocking):
267
        publish a mouth target via mw
268
        :param mouth: mouth value to set
269
        :param blocking: True if this call should block until execution finished on robot
270

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

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

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

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

293
        self.logger.debug("mouth rpc done")
294
    """