Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / MiddlewareRSB.py @ 8fd53883

History | View | Annotate | Download (11.655 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
        self.EmotionExpression_converter = None
50
        self.HeadAnimation_converter = None
51
        self.BinocularHeadGaze_converter = None
52
        #call mw init
53
        self.server = None
54
        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
        # mute rsb logging:
66
        logging.getLogger("rsb").setLevel(logging.ERROR)
67

    
68
        #initialise RSB stuff
69
        self.logger.info(
70
            "initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope))
71

    
72
        self.EmotionExpression_converter = rsb.converter.ProtocolBufferConverter(messageClass=EmotionExpression)
73
        rsb.converter.registerGlobalConverter(self.EmotionExpression_converter)
74

    
75
        self.HeadAnimation_converter = rsb.converter.ProtocolBufferConverter(messageClass=HeadAnimation)
76
        rsb.converter.registerGlobalConverter(self.HeadAnimation_converter)
77

    
78
        self.BinocularHeadGaze_converter = rsb.converter.ProtocolBufferConverter(messageClass=BinocularHeadGaze)
79
        rsb.converter.registerGlobalConverter(self.BinocularHeadGaze_converter)
80

    
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
        # create emotion & fill it with values:
97
        rsb_em = EmotionExpression()
98
        rsb_em.emotion = self.convert_emotiontype_to_rsbval(emotion.value)
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
                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 = server.currentEmotion.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

    
121
    def publish_head_HeadAnimation(self, HeadAnimation, blocking):
122
        """publish an head HeadAnimation via mw
123
        :param HeadAnimation: HeadAnimation to set
124
        :param blocking: True if this call should block until execution finished on robot
125
        """
126

    
127
        self.logger.debug("calling the HeadAnimation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
128

    
129
        # create HeadAnimation & fill it with values:
130
        rsb_ani = HeadAnimation()
131

    
132
        #select ani
133
        rsb_ani.target = self.convert_HeadAnimationtype_to_rsbval(HeadAnimation.value)
134
        rsb_ani.repetitions = HeadAnimation.repetitions
135
        rsb_ani.duration_each = HeadAnimation.time_ms
136
        rsb_ani.scale = HeadAnimation.scale
137

    
138
        if blocking:
139
            #blocking:
140
            result = self.server.HeadAnimation(rsb_ani)
141
            self.logger.debug("server reply: '%s'" % result)
142
        else:
143
            future = self.server.HeadAnimation.async(rsb_ani)
144
            #we can block here for a incoming result with a timeout in s
145
            #print '> server reply: "%s"' % future.get(timeout = 10);
146

    
147
        self.logger.debug("HeadAnimation rpc done")
148

    
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
        # create gaze target & fill it with values:
163
        hg = BinocularHeadGaze()
164

    
165
        hg.target.elevation = gaze.tilt
166
        hg.target.azimuth = gaze.pan
167
        hg.eye_vergence = gaze.vergence
168

    
169
        hg.offset.elevation = gaze.tilt_offset
170
        hg.offset.azimuth = gaze.pan_offset
171

    
172
        if blocking:
173
            # blocking:
174
            result = self.server.gaze(hg)
175
            self.logger.debug("server reply blocking: '%s'" % result)
176
        else:
177
            future = self.server.gaze.async(hg)
178
            self.logger.debug("server reply non-blocking: '%s'" % future)
179
            # we can block here for a incoming result with a timeout in s
180
            #print '> server reply: "%s"' % future.get(timeout = 10);
181

    
182
        self.logger.debug("gaze rpc done")
183

    
184
    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
        self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
190

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

    
200
        self.logger.debug("speech rpc done")
201

    
202

    
203
    #######################################################################
204
    def is_running(self):
205
        return True
206

    
207

    
208
    #######################################################################
209
    # some helpers
210
    def convert_HeadAnimationtype_to_rsbval(self, value):
211
        """convert RobotHeadAnimation.value to RSB HeadAnimation value
212
        :param value: RobotHeadAnimation.* id to convert to rsb id
213
        """
214
        # NOTE: this convertion is important as the actual integer values of
215
        #      thy python api and the protobuf might be different
216

    
217
        if (value == RobotHeadAnimation.IDLE):
218
            return HeadAnimation().IDLE
219
        elif (value == RobotHeadAnimation.HEAD_NOD):
220
            return HeadAnimation().HEAD_NOD
221
        elif (value == RobotHeadAnimation.HEAD_SHAKE):
222
            return HeadAnimation().HEAD_SHAKE
223
        elif (value == RobotHeadAnimation.EYEBLINK_L):
224
            return HeadAnimation().EYEBLINK_L
225
        elif (value == RobotHeadAnimation.EYEBLINK_R):
226
            return HeadAnimation().EYEBLINK_R
227
        elif (value == RobotHeadAnimation.EYEBLINK_BOTH):
228
            return HeadAnimation().EYEBLINK_BOTH
229
        elif (value == RobotHeadAnimation.EYEBROWS_RAISE):
230
            return HeadAnimation().EYEBROWS_RAISE
231
        elif (value == RobotHeadAnimation.EYEBROWS_LOWER):
232
            return HeadAnimation().EYEBROWS_LOWER
233
        else:
234
            self.logger.error("invalid HeadAnimation type %d\n" % (value))
235
            return HeadAnimation().NEUTRAL
236

    
237
    def convert_emotiontype_to_rsbval(self, value):
238
        """convert RobotEmotion.value to RSB HeadAnimation value
239
        :param value: RobotEmotion.* id to convert to rsb id
240
        """
241
        # NOTE: this convertion is important as the actual integer values of
242
        #      thy python api and the protobuf might be different
243

    
244
        if (value == RobotEmotion.NEUTRAL):
245
            return EmotionExpression().NEUTRAL
246
        elif (value == RobotEmotion.HAPPY):
247
            return EmotionExpression().HAPPY
248
        elif (value == RobotEmotion.SAD):
249
            return EmotionExpression().SAD
250
        elif (value == RobotEmotion.ANGRY):
251
            return EmotionExpression().ANGRY
252
        elif (value == RobotEmotion.SURPRISED):
253
            return EmotionExpression().SURPRISED
254
        elif (value == RobotEmotion.FEAR):
255
            return EmotionExpression().FEAR
256
        else:
257
            self.logger.error("invalid emotion type %d\n" % (value))
258
            return EmotionExpression().NEUTRAL
259

    
260
    """
261
    def publish_mouth_target(self, mouth, blocking):
262
        publish a mouth target via mw
263
        :param mouth: mouth value to set
264
        :param blocking: True if this call should block until execution finished on robot
265

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

268
        #create mouth state & fill it with values:
269
        rsb_mouth = MouthTarget()
270

271
        #fill proto
272
        rsb_mouth.opening_left   = mouth.opening_left
273
        rsb_mouth.opening_center = mouth.opening_center
274
        rsb_mouth.opening_right  = mouth.opening_right
275
        rsb_mouth.position_left  = mouth.position_left
276
        rsb_mouth.position_center = mouth.position_center
277
        rsb_mouth.position_right = mouth.position_right
278

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

288
        self.logger.debug("mouth rpc done")
289
    """