Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.476 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
#This is untested for now (flier), RST has been refactored during "GGB", so these are the new types (names and packages)
36
from rstsandbox.animation.EmotionExpression_pb2 import EmotionExpression as EmotionState
37
from rstsandbox.animation.HeadAnimation_pb2 import HeadAnimation as Animation
38
from rstsandbox.robot.HumotionGazeTarget_pb2 import HumotionGazeTarget as GazeTarget
39
from rstsandbox.robot.MouthTarget_pb2 import MouthTarget
40

    
41

    
42
class MiddlewareRSB(Middleware):
43
    #######################################################################
44
    def __init__(self, scope, loglevel=logging.WARNING):
45
        """initialise
46
        :param scope: base scope we want to listen on
47
        """
48
        #init base settings
49
        Middleware.__init__(self,scope,loglevel)
50
        #call mw init
51
        self.init_middleware()
52

    
53
    def __del__(self):
54
        """destructor
55
        """
56
        self.logger.debug("destructor of MiddlewareROS called")
57

    
58
    #######################################################################
59
    def init_middleware(self):
60
        """initialise middleware
61
        """
62
        #mute rsb logging:
63
        logging.getLogger("rsb").setLevel(logging.ERROR)
64

    
65
        #initialise RSB stuff
66
        self.logger.info("initialising RSB middleware connection on scope %s, registering rst converters..." % (self.base_scope))
67

    
68
        self.emotionstate_converter = rsb.converter.ProtocolBufferConverter(messageClass = EmotionState)
69
        rsb.converter.registerGlobalConverter(self.emotionstate_converter)
70

    
71
        self.animation_converter = rsb.converter.ProtocolBufferConverter(messageClass = Animation)
72
        rsb.converter.registerGlobalConverter(self.animation_converter)
73

    
74
        self.gaze_converter = rsb.converter.ProtocolBufferConverter(messageClass = GazeTarget)
75
        rsb.converter.registerGlobalConverter(self.gaze_converter)
76

    
77
        self.mouth_converter = rsb.converter.ProtocolBufferConverter(messageClass = MouthTarget)
78
        rsb.converter.registerGlobalConverter(self.mouth_converter)
79

    
80
        try:
81
            self.server = rsb.createRemoteServer(self.base_scope + '/set')
82
        except ValueError:
83
            self.logger.error("ERROR: invalid scope given. server deactivated")
84
            self.server.deactivate()
85
            sys.exit(errno.EINVAL)
86

    
87
    #######################################################################
88
    def publish_emotion(self, em_type, emotion, blocking):
89
        """publish an emotion via mw
90
        :param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
91
        :param emotion: emotion to set
92
        :param blocking: True if this call should block until execution finished on robot
93
        """
94

    
95
        #create emotion & fill it with values:
96
        rsb_em = EmotionState()
97

    
98
        #set value
99
        rsb_em.value = self.convert_emotiontype_to_rsbval(emotion.value)
100

    
101
        #set duration
102
        rsb_em.duration = int(emotion.time_ms)
103

    
104
        with rsb.createRemoteServer(self.base_scope + '/set') as server:
105
            self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
106

    
107
            if (blocking):
108
                #blocking rpc call:
109
                if (em_type == RobotEmotion.TYPE_DEFAULT):
110
                    result = server.defaultEmotion(rsb_em)
111
                else:
112
                    result = server.currentEmotion(rsb_em)
113

    
114
                self.logger.debug("server reply: '%s'" % result)
115
            else:
116
                if (em_type == RobotEmotion.TYPE_DEFAULT):
117
                    future = server.defaultEmotion.async(rsb_em)
118
                else:
119
                    future = server.currentEmotion.async(rsb_em)
120

    
121
                #we could block here for a incoming result with a timeout in s
122
                #print '> server reply: "%s"' % future.get(timeout = 10);
123
            self.logger.debug("emotion rpc done")
124

    
125
    def publish_head_animation(self, animation, blocking):
126
        """publish an head animation via mw
127
        :param animation: animation to set
128
        :param blocking: True if this call should block until execution finished on robot
129
        """
130

    
131
        self.logger.debug("calling the animation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
132

    
133
        #create animation & fill it with values:
134
        rsb_ani = Animation()
135

    
136
        #select ani
137
        rsb_ani.target = self.convert_animationtype_to_rsbval(animation.value)
138
        rsb_ani.repetitions = animation.repetitions
139
        rsb_ani.duration_each = animation.time_ms
140
        rsb_ani.scale       = animation.scale
141

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

    
151
        self.logger.debug("animation rpc done")
152

    
153
    def publish_default_emotion(self, emotion, blocking):
154
        self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
155

    
156
    def publish_current_emotion(self, emotion, blocking):
157
        self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
158

    
159
    def publish_gaze_target(self, gaze, blocking):
160
        """publish a gaze target via mw
161
        :param gaze: gaze to set
162
        :param blocking: True if this call should block until execution finished on robot
163
        """
164
        self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
165

    
166
        #create gaze target & fill it with values:
167
        rsb_gaze = GazeTarget()
168

    
169
        #fill proto
170
        rsb_gaze.pan  = gaze.pan
171
        rsb_gaze.tilt = gaze.tilt
172
        rsb_gaze.roll = gaze.roll
173
        rsb_gaze.vergence = gaze.vergence
174
        rsb_gaze.pan_offset  = gaze.pan_offset
175
        rsb_gaze.tilt_offset = gaze.tilt_offset
176

    
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
    def publish_mouth_target(self, mouth, blocking):
189
        """publish a mouth target via mw
190
        :param mouth: mouth value to set
191
        :param blocking: True if this call should block until execution finished on robot
192
        """
193
        self.logger.debug("calling the mouth rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
194

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

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

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

    
215
        self.logger.debug("mouth rpc done")
216

    
217
    def publish_speech(self, text, blocking):
218
        """publish a tts request via mw
219
        :param text: text to synthesize and speak
220
        :param blocking: True if this call should block until execution finished on robot
221
        """
222
        self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
223

    
224
        if (blocking):
225
            #blocking:
226
            result = self.server.speech(text)
227
            self.logger.debug("server reply: '%s'" % result)
228
        else:
229
            future = self.server.speech.async(text)
230
            #we can block here for a incoming result with a timeout in s
231
            #print '> server reply: "%s"' % future.get(timeout = 10);
232

    
233
        self.logger.debug("speech rpc done")
234
    #######################################################################
235
    def is_running(self):
236
        return True
237

    
238
    #######################################################################
239
    # some helpers
240
    def convert_animationtype_to_rsbval(self, value):
241
        """convert RobotAnimation.value to RSB animation value
242
        :param value: RobotAnimation.* 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 == RobotAnimation.IDLE):
248
            return Animation().IDLE
249
        elif (value == RobotAnimation.HEAD_NOD):
250
            return Animation().HEAD_NOD
251
        elif (value == RobotAnimation.HEAD_SHAKE):
252
            return Animation().HEAD_SHAKE
253
        elif (value == RobotAnimation.EYEBLINK_L):
254
            return Animation().EYEBLINK_L
255
        elif (value == RobotAnimation.EYEBLINK_R):
256
            return  Animation().EYEBLINK_R
257
        elif (value == RobotAnimation.EYEBLINK_BOTH):
258
            return  Animation().EYEBLINK_BOTH
259
        elif (value == RobotAnimation.EYEBROWS_RAISE):
260
            return  Animation().EYEBROWS_RAISE
261
        elif (value == RobotAnimation.EYEBROWS_LOWER):
262
            return  Animation().EYEBROWS_LOWER
263
        else:
264
            self.logger.error("invalid animation type %d\n" % (value))
265
            return  Animation().NEUTRAL
266

    
267
    def convert_emotiontype_to_rsbval(self, value):
268
        """convert RobotEmotion.value to RSB animation value
269
        :param value: RobotEmotion.* id to convert to rsb id
270
        """
271
        #NOTE: this convertion is important as the actual integer values of
272
        #      thy python api and the protobuf might be different
273

    
274
        if (value == RobotEmotion.NEUTRAL):
275
            return EmotionState().NEUTRAL
276
        elif (value == RobotEmotion.HAPPY):
277
            return EmotionState().HAPPY
278
        elif (value == RobotEmotion.SAD):
279
            return EmotionState().SAD
280
        elif (value == RobotEmotion.ANGRY):
281
            return EmotionState().ANGRY
282
        elif (value == RobotEmotion.SURPRISED):
283
            return  EmotionState().SURPRISED
284
        elif (value == RobotEmotion.FEAR):
285
            return EmotionState().FEAR
286
        else:
287
            self.logger.error("invalid emotion type %d\n" % (value))
288
            return  EmotionState().NEUTRAL