Statistics
| Branch: | Tag: | Revision:

hlrc / client / python / hlrc_client / MiddlewareRSB.py @ 78cc756c

History | View | Annotate | Download (11.515 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
        #init base settings
48
        Middleware.__init__(self,scope,loglevel)
49
        #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
        #mute rsb logging:
62
        logging.getLogger("rsb").setLevel(logging.ERROR)
63
64
        #initialise RSB stuff
65
        self.logger.info("initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope))
66
67 78cc756c Simon Schulz
        self.EmotionExpression_converter = rsb.converter.ProtocolBufferConverter(messageClass = EmotionExpression)
68
        rsb.converter.registerGlobalConverter(self.EmotionExpression_converter)
69 70c54617 Simon Schulz
70 78cc756c Simon Schulz
        self.HeadAnimation_converter = rsb.converter.ProtocolBufferConverter(messageClass = HeadAnimation)
71
        rsb.converter.registerGlobalConverter(self.HeadAnimation_converter)
72 70c54617 Simon Schulz
73 78cc756c Simon Schulz
        self.BinocularHeadGaze_converter = rsb.converter.ProtocolBufferConverter(messageClass = BinocularHeadGaze)
74
        rsb.converter.registerGlobalConverter(self.BinocularHeadGaze_converter)
75 70c54617 Simon Schulz
76
        try:
77
            self.server = rsb.createRemoteServer(self.base_scope + '/set')
78
        except ValueError:
79
            self.logger.error("ERROR: invalid scope given. server deactivated")
80
            self.server.deactivate()
81
            sys.exit(errno.EINVAL)
82
83
    #######################################################################
84
    def publish_emotion(self, em_type, emotion, blocking):
85
        """publish an emotion via mw
86
        :param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
87
        :param emotion: emotion to set
88
        :param blocking: True if this call should block until execution finished on robot
89
        """
90
91
        #create emotion & fill it with values:
92 78cc756c Simon Schulz
        rsb_em = EmotionExpression()
93 70c54617 Simon Schulz
94
        #set value
95
        rsb_em.value = self.convert_emotiontype_to_rsbval(emotion.value)
96
97
        #set duration
98
        rsb_em.duration = int(emotion.time_ms)
99
100
        with rsb.createRemoteServer(self.base_scope + '/set') as server:
101
            self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
102
103
            if (blocking):
104
                #blocking rpc call:
105 78cc756c Simon Schulz
                if (em_type == EmotionExpression.TYPE_DEFAULT):
106 70c54617 Simon Schulz
                    result = server.defaultEmotion(rsb_em)
107
                else:
108
                    result = server.currentEmotion(rsb_em)
109
110
                self.logger.debug("server reply: '%s'" % result)
111
            else:
112 78cc756c Simon Schulz
                if (em_type == EmotionExpression.TYPE_DEFAULT):
113 70c54617 Simon Schulz
                    future = server.defaultEmotion.async(rsb_em)
114
                else:
115
                    future = server.currentEmotion.async(rsb_em)
116
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 78cc756c Simon Schulz
    def publish_head_HeadAnimation(self, HeadAnimation, blocking):
122
        """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 78cc756c Simon Schulz
        #create HeadAnimation & fill it with values:
130
        rsb_ani = HeadAnimation()
131 70c54617 Simon Schulz
132
        #select ani
133 78cc756c Simon Schulz
        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 70c54617 Simon Schulz
138
        if (blocking):
139
            #blocking:
140 78cc756c Simon Schulz
            result = self.server.HeadAnimation(rsb_ani)
141 70c54617 Simon Schulz
            self.logger.debug("server reply: '%s'" % result)
142
        else:
143 78cc756c Simon Schulz
            future = self.server.HeadAnimation.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
        #create gaze target & fill it with values:
163 78cc756c Simon Schulz
        rsb_gaze = BinocularHeadGaze()
164 70c54617 Simon Schulz
165
        #fill proto
166 78cc756c Simon Schulz
        rsb_gaze.target = SphericalDirectionFloat()
167
        rsb_gaze.target.elevation = gaze.tilt
168
        rsb_gaze.target.azimuth   = gaze.pan
169
170 70c54617 Simon Schulz
        rsb_gaze.vergence = gaze.vergence
171 78cc756c Simon Schulz
172
        
173
        rsb_gaze.offset = SphericalDirectionFloat()
174
        rsb_gaze.offset.elevation = gaze.tilt_offset
175
        rsb_gaze.offset.azimuth   = gaze.pan_offset
176 70c54617 Simon Schulz
177
        if (blocking):
178
            #blocking:
179
            result = self.server.gaze(rsb_gaze)
180
            self.logger.debug("server reply: '%s'" % result)
181
        else:
182
            future = self.server.gaze.async(rsb_gaze)
183
            #we can block here for a incoming result with a timeout in s
184
            #print '> server reply: "%s"' % future.get(timeout = 10);
185
186
        self.logger.debug("gaze rpc done")
187
188 78cc756c Simon Schulz
"""
189 70c54617 Simon Schulz
    def publish_mouth_target(self, mouth, blocking):
190 78cc756c Simon Schulz
        publish a mouth target via mw
191 70c54617 Simon Schulz
        :param mouth: mouth value to set
192
        :param blocking: True if this call should block until execution finished on robot
193 78cc756c Simon Schulz
        
194 70c54617 Simon Schulz
        self.logger.debug("calling the mouth rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
195

196
        #create mouth state & fill it with values:
197
        rsb_mouth = MouthTarget()
198

199
        #fill proto
200
        rsb_mouth.opening_left   = mouth.opening_left
201
        rsb_mouth.opening_center = mouth.opening_center
202
        rsb_mouth.opening_right  = mouth.opening_right
203
        rsb_mouth.position_left  = mouth.position_left
204
        rsb_mouth.position_center = mouth.position_center
205
        rsb_mouth.position_right = mouth.position_right
206

207
        if (blocking):
208
            #blocking:
209
            result = self.server.mouth(rsb_mouth)
210
            self.logger.debug("server reply: '%s'" % result)
211
        else:
212
            future = self.server.mouth.async(rsb_mouth)
213
            #we can block here for a incoming result with a timeout in s
214
            #print '> server reply: "%s"' % future.get(timeout = 10);
215

216
        self.logger.debug("mouth rpc done")
217 78cc756c Simon Schulz
"""
218 70c54617 Simon Schulz
219
    def publish_speech(self, text, blocking):
220
        """publish a tts request via mw
221
        :param text: text to synthesize and speak
222
        :param blocking: True if this call should block until execution finished on robot
223
        """
224
        self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
225
226
        if (blocking):
227
            #blocking:
228
            result = self.server.speech(text)
229
            self.logger.debug("server reply: '%s'" % result)
230
        else:
231
            future = self.server.speech.async(text)
232
            #we can block here for a incoming result with a timeout in s
233
            #print '> server reply: "%s"' % future.get(timeout = 10);
234
235
        self.logger.debug("speech rpc done")
236 02fc76ae Simon Schulz
    #######################################################################
237
    def is_running(self):
238
        return True
239 70c54617 Simon Schulz
240
    #######################################################################
241
    # some helpers
242 78cc756c Simon Schulz
    def convert_HeadAnimationtype_to_rsbval(self, value):
243
        """convert RobotHeadAnimation.value to RSB HeadAnimation value
244
        :param value: RobotHeadAnimation.* id to convert to rsb id
245 70c54617 Simon Schulz
        """
246
        #NOTE: this convertion is important as the actual integer values of
247
        #      thy python api and the protobuf might be different
248
249 78cc756c Simon Schulz
        if (value == RobotHeadAnimation.IDLE):
250
            return HeadAnimation().IDLE
251
        elif (value == RobotHeadAnimation.HEAD_NOD):
252
            return HeadAnimation().HEAD_NOD
253
        elif (value == RobotHeadAnimation.HEAD_SHAKE):
254
            return HeadAnimation().HEAD_SHAKE
255
        elif (value == RobotHeadAnimation.EYEBLINK_L):
256
            return HeadAnimation().EYEBLINK_L
257
        elif (value == RobotHeadAnimation.EYEBLINK_R):
258
            return  HeadAnimation().EYEBLINK_R
259
        elif (value == RobotHeadAnimation.EYEBLINK_BOTH):
260
            return  HeadAnimation().EYEBLINK_BOTH
261
        elif (value == RobotHeadAnimation.EYEBROWS_RAISE):
262
            return  HeadAnimation().EYEBROWS_RAISE
263
        elif (value == RobotHeadAnimation.EYEBROWS_LOWER):
264
            return  HeadAnimation().EYEBROWS_LOWER
265 70c54617 Simon Schulz
        else:
266 78cc756c Simon Schulz
            self.logger.error("invalid HeadAnimation type %d\n" % (value))
267
            return  HeadAnimation().NEUTRAL
268 70c54617 Simon Schulz
269
    def convert_emotiontype_to_rsbval(self, value):
270 78cc756c Simon Schulz
        """convert RobotEmotion.value to RSB HeadAnimation value
271 70c54617 Simon Schulz
        :param value: RobotEmotion.* id to convert to rsb id
272
        """
273
        #NOTE: this convertion is important as the actual integer values of
274
        #      thy python api and the protobuf might be different
275
276
        if (value == RobotEmotion.NEUTRAL):
277 78cc756c Simon Schulz
            return EmotionExpression().NEUTRAL
278 70c54617 Simon Schulz
        elif (value == RobotEmotion.HAPPY):
279 78cc756c Simon Schulz
            return EmotionExpression().HAPPY
280 70c54617 Simon Schulz
        elif (value == RobotEmotion.SAD):
281 78cc756c Simon Schulz
            return EmotionExpression().SAD
282 70c54617 Simon Schulz
        elif (value == RobotEmotion.ANGRY):
283 78cc756c Simon Schulz
            return EmotionExpression().ANGRY
284 70c54617 Simon Schulz
        elif (value == RobotEmotion.SURPRISED):
285 78cc756c Simon Schulz
            return  EmotionExpression().SURPRISED
286 70c54617 Simon Schulz
        elif (value == RobotEmotion.FEAR):
287 78cc756c Simon Schulz
            return EmotionExpression().FEAR
288 70c54617 Simon Schulz
        else:
289
            self.logger.error("invalid emotion type %d\n" % (value))
290 78cc756c Simon Schulz
            return  EmotionExpression().NEUTRAL