Revision 70c54617

View differences:

client/python/hlrc_client/Middleware.py
35 35
class Middleware:
36 36
    #######################################################################
37 37
    def __init__(self, scope, loglevel=logging.WARNING):
38
	"""initialise
39
	:param scope: base scope we want to listen on
40
	"""
41
	self.base_scope = scope
42

  
43
	self.logger = logging.getLogger(__name__)
44

  
45
	# create nice and actually usable formatter and add it to the handler
46
	self.config_logger(loglevel)
47

  
48
	#initialise defaults
49
	self.default_emotion = RobotEmotion()
50
	self.current_emotion = RobotEmotion()
51
	self.gaze_target = RobotGaze()
52
	self.mouth_target = RobotMouth()
53
	self.robot_animation = RobotAnimation()
54

  
55
	def __del__(self):
56
	    """destructor
57
	    """
58
	    self.logger.debug("destructor of Middleware called")
59

  
60
	    def config_logger(self, level):
61
		formatter = logging.Formatter('%(asctime)s %(name)-30s %(levelname)-8s > %(message)s')
62
		ch = logging.StreamHandler()
63
		#ch.setLevel(level)
64
		ch.setFormatter(formatter)
65
		self.logger.setLevel(level)
66
		self.logger.addHandler(ch)
67

  
68
		#######################################################################
69
		#abstract/virtual functions
70
		def die_virtual(self, funcname):
71
		    raise NotImplementedError(funcname + "() is virtual, must be overwritten")
72

  
73
		def init_middleware(self):
74
		    self.die_virtual(sys._getframe().f_code.co_name)
75

  
76
		    def publish_default_emotion(self, emotion, blocking):
77
			self.die_virtual(sys._getframe().f_code.co_name)
78

  
79
			def publish_current_emotion(self, emotion, blocking):
80
			    self.die_virtual(sys._getframe().f_code.co_name)
81

  
82
			    def publish_gaze_target(self, gaze, blocking):
83
				self.die_virtual(sys._getframe().f_code.co_name)
84

  
85
				def publish_mouth_target(self, mouth, blocking):
86
				    self.die_virtual(sys._getframe().f_code.co_name)
87

  
88
				    def publish_head_animation(self, animation, blocking):
89
					self.die_virtual(sys._getframe().f_code.co_name)
90

  
91
					def publish_speech(self, text, blocking):
92
					    self.die_virtual(sys._getframe().f_code.co_name)
93

  
94
					    #######################################################################
95
					    #finally some implemented functions
96
					    def set_default_emotion(self, emotion, blocking):
97
						"""set the default emotion
98
						:param emotion: RobotEmotion to set
99
						:param blocking: True if this call should block until execution finished on robot
100
						"""
101
						self.default_emotion = emotion
102
						self.publish_default_emotion(emotion, blocking)
103

  
104
						def set_current_emotion(self, emotion, blocking):
105
						    """set a temporary emotion (duration: see emotion.time_ms)
106
						    :param emotion: RobotEmotion to set temporarily
107
						    :param blocking: True if this call should block until execution finished on robot
108
						    """
109
						    self.current_emotion = emotion
110
						    self.publish_current_emotion(emotion, blocking)
111

  
112
						    def set_head_animation(self, animation, blocking):
113
							"""trigger a head animation
114
							:param animation: RobotAnimation to set
115
							:param blocking: True if this call should block until execution finished on robot
116
							"""
117
							self.animation = animation
118
							self.publish_head_animation(animation, blocking)
119

  
120
							def set_mouth_target(self, mouth, blocking):
121
							    """set a mouth target
122
							    :param mouth: RobotMouth to set
123
							    :param blocking: True if this call should block until execution finished on robot
124
							    """
125
							    self.mouth_target = mouth
126
							    self.publish_mouth_target(mouth, blocking)
127

  
128
							    def set_speak(self, text, blocking):
129
								"""trigger a tts speech output
130
								:param text: text to synthesize and speak
131
								:param blocking: True if this call should block until execution finished on robot
132
								"""
133
								self.publish_speech(text, blocking)
134

  
135
								def set_gaze_target(self, gaze, blocking):
136
								    """set a new gaze
137
								    :param gaze: RobotGaze to set
138
								    :param blocking: True if this call should block until execution finished on robot
139
								    """
140
								    self.gaze_target = gaze
141
								    self.publish_gaze_target(gaze, blocking)
142

  
143
								    #######################################################################
144
								    #some get methods
145
								    #def get_current_emotion(self):
146
									#	return self.current_emotion
147
									#
148
									#def get_default_emotion(self):
149
									    #	return self.default_emotion
150
									    #
38
        """initialise
39
        :param scope: base scope we want to listen on
40
        """
41
        self.base_scope = scope
42

  
43
        self.logger = logging.getLogger(__name__)
44

  
45
        # create nice and actually usable formatter and add it to the handler
46
        self.config_logger(loglevel)
47

  
48
        #initialise defaults
49
        self.default_emotion = RobotEmotion()
50
        self.current_emotion = RobotEmotion()
51
        self.gaze_target = RobotGaze()
52
        self.mouth_target = RobotMouth()
53
        self.robot_animation = RobotAnimation()
54

  
55
    def __del__(self):
56
        """destructor
57
        """
58
        self.logger.debug("destructor of Middleware called")
59

  
60
    def config_logger(self, level):
61
        formatter = logging.Formatter('%(asctime)s %(name)-30s %(levelname)-8s > %(message)s')
62
        ch = logging.StreamHandler()
63
        #ch.setLevel(level)
64
        ch.setFormatter(formatter)
65
        self.logger.setLevel(level)
66
        self.logger.addHandler(ch)
67

  
68
    #######################################################################
69
    #abstract/virtual functions
70
    def die_virtual(self, funcname):
71
        raise NotImplementedError(funcname + "() is virtual, must be overwritten")
72

  
73
    def init_middleware(self):
74
        self.die_virtual(sys._getframe().f_code.co_name)
75

  
76
    def publish_default_emotion(self, emotion, blocking):
77
        self.die_virtual(sys._getframe().f_code.co_name)
78

  
79
    def publish_current_emotion(self, emotion, blocking):
80
        self.die_virtual(sys._getframe().f_code.co_name)
81

  
82
    def publish_gaze_target(self, gaze, blocking):
83
        self.die_virtual(sys._getframe().f_code.co_name)
84

  
85
    def publish_mouth_target(self, mouth, blocking):
86
        self.die_virtual(sys._getframe().f_code.co_name)
87

  
88
    def publish_head_animation(self, animation, blocking):
89
        self.die_virtual(sys._getframe().f_code.co_name)
90

  
91
    def publish_speech(self, text, blocking):
92
        self.die_virtual(sys._getframe().f_code.co_name)
93

  
94
    #######################################################################
95
    #finally some implemented functions
96
    def set_default_emotion(self, emotion, blocking):
97
        """set the default emotion
98
        :param emotion: RobotEmotion to set
99
        :param blocking: True if this call should block until execution finished on robot
100
        """
101
        self.default_emotion = emotion
102
        self.publish_default_emotion(emotion, blocking)
103

  
104
    def set_current_emotion(self, emotion, blocking):
105
        """set a temporary emotion (duration: see emotion.time_ms)
106
        :param emotion: RobotEmotion to set temporarily
107
        :param blocking: True if this call should block until execution finished on robot
108
        """
109
        self.current_emotion = emotion
110
        self.publish_current_emotion(emotion, blocking)
111

  
112
    def set_head_animation(self, animation, blocking):
113
        """trigger a head animation
114
        :param animation: RobotAnimation to set
115
        :param blocking: True if this call should block until execution finished on robot
116
        """
117
        self.animation = animation
118
        self.publish_head_animation(animation, blocking)
119

  
120
    def set_mouth_target(self, mouth, blocking):
121
        """set a mouth target
122
        :param mouth: RobotMouth to set
123
        :param blocking: True if this call should block until execution finished on robot
124
        """
125
        self.mouth_target = mouth
126
        self.publish_mouth_target(mouth, blocking)
127

  
128
    def set_speak(self, text, blocking):
129
        """trigger a tts speech output
130
        :param text: text to synthesize and speak
131
        :param blocking: True if this call should block until execution finished on robot
132
        """
133
        self.publish_speech(text, blocking)
134

  
135
    def set_gaze_target(self, gaze, blocking):
136
        """set a new gaze
137
        :param gaze: RobotGaze to set
138
        :param blocking: True if this call should block until execution finished on robot
139
        """
140
        self.gaze_target = gaze
141
        self.publish_gaze_target(gaze, blocking)
142

  
143
    #######################################################################
144
    #some get methods
145
    #def get_current_emotion(self):
146
    #       return self.current_emotion
147
    #
148
    #def get_default_emotion(self):
149
    #       return self.default_emotion
150
    #
client/python/hlrc_client/MiddlewareROS.py
28 28
from Middleware import *
29 29
import errno
30 30

  
31
import roslib; 
32
import rospy 
31
import roslib;
32
import rospy
33 33
import actionlib
34 34
from hlrc_server.msg import *
35 35

  
......
39 39

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

  
50
	def __del__(self):
51
	    """destructor
52
	    """
53
	    self.logger.debug("destructor of MiddlewareROS called")
54

  
55
	    #######################################################################
56
	    def init_middleware(self):
57
		"""initialise middleware
58
		"""
59
		self.logger.info("initialising ROS middleware connection on scope %s" % (self.base_scope))
60
		
61
		rospy.init_node('hlrc_client', anonymous=True)
62
		
63
		self.logger.info("creating action clients")
64
		
65
		self.logger.debug("creating speech action client")
66
		self.speech_client = actionlib.SimpleActionClient(self.base_scope + "/set/speech", speechAction)
67
		self.speech_client.wait_for_server()
68
		
69
		self.logger.debug("creating default emotion action client")
70
		self.default_emotion_client = actionlib.SimpleActionClient(self.base_scope + "/set/defaultEmotion", emotionstateAction)
71
		self.default_emotion_client.wait_for_server()
72
		
73
		self.logger.debug("creating current emotion action client")
74
		self.current_emotion_client = actionlib.SimpleActionClient(self.base_scope + "/set/currentEmotion", emotionstateAction)
75
		self.current_emotion_client.wait_for_server()
76
		
77
		self.logger.debug("creating animation action client")
78
		self.animation_client = actionlib.SimpleActionClient(self.base_scope + "/set/animation", animationAction)
79
		self.animation_client.wait_for_server()
80
		
81
		self.logger.debug("creating gazetarget action client")
82
		self.gazetarget_client = actionlib.SimpleActionClient(self.base_scope + "/set/gaze", gazetargetAction)
83
		self.gazetarget_client.wait_for_server()
84
		
85
		self.logger.debug("creating mouthtarget action client")
86
		self.mouthtarget_client = actionlib.SimpleActionClient(self.base_scope + "/set/mouth", mouthtargetAction)
87
		self.mouthtarget_client.wait_for_server()
88
		
89
		self.logger.info("MiddlewareROS initialised")
90
		
91

  
92
		#######################################################################
93
		def publish_emotion(self, em_type, emotion, blocking):
94
		    """publish an emotion via mw
95
		    :param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
96
		    :param emotion: emotion to set
97
		    :param blocking: True if this call should block until execution finished on robot
98
		    """
99
		    self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
100

  
101
		    #create a goal to send to the action server.
102
		    goal = emotionstateGoal()
103
		    goal.value    = self.convert_emotiontype_to_rosval(emotion.value)
104
		    goal.duration =  int(emotion.time_ms)
105

  
106
		    #which client do we use?
107
		    if (em_type == RobotEmotion.TYPE_DEFAULT):
108
			client = self.default_emotion_client
109
			else:
110
			    client = self.current_emotion_client
111

  
112
			    #send the goal to the server
113
			    client.send_goal(goal)
114

  
115
			    #wait for the server to finish
116
			    if (blocking):
117
				timed_out = client.wait_for_result(rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
118
				if (timed_out):
119
				    self.logger.error("%s timed out waiting for result" % (sys._getframe().f_code.co_name))
120

  
121
				    def publish_default_emotion(self, emotion, blocking):
122
					self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
123

  
124
					def publish_current_emotion(self, emotion, blocking):
125
					    self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
126

  
127
					    def publish_head_animation(self, animation, blocking):
128
						"""publish an head animation via mw
129
						:param animation: animation to set
130
						:param blocking: True if this call should block until execution finished on robot
131
						"""
132
						self.logger.debug("calling the animation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
133

  
134
						#create a goal to send to the action server.
135
						goal = animationGoal()
136
						goal.target = self.convert_animationtype_to_rosval(animation.value)
137
						goal.repetitions = animation.repetitions
138
						goal.duration_each =  int(animation.time_ms)
139
						goal.scale = animation.scale
140

  
141
						#send the goal to the server
142
						self.animation_client.send_goal(goal)
143

  
144
						#wait for the server to finish
145
						if (blocking):
146
						    timed_out = self.animation_client.wait_for_result(rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
147
						    if (timed_out):
148
							self.logger.error("%s timed out waiting for result" % (sys._getframe().f_code.co_name))
149

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

  
152
							def publish_gaze_target(self, gaze, blocking):
153
							    """publish a gaze target via mw
154
							    :param gaze: gaze to set
155
							    :param blocking: True if this call should block until execution finished on robot
156
							    """
157
							    self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING"))
158

  
159
							    #create a goal to send to the action server.
160
							    goal = gazetargetGoal()
161
							    goal.pan  = gaze.pan
162
							    goal.tilt = gaze.tilt
163
							    goal.roll = gaze.roll
164
							    goal.vergence = gaze.vergence
165
							    goal.pan_offset  = gaze.pan_offset
166
							    goal.tilt_offset = gaze.tilt_offset
167

  
168
							    #type
169
							    if (gaze.gaze_type == RobotGaze.GAZETARGET_ABSOLUTE):
170
								goal.gaze_type = gazetargetGoal.GAZETARGET_ABSOLUTE
171
								else:
172
								    goal.gaze_type = gazetargetGoal.GAZETARGET_RELATIVE
173

  
174
								    goal.timestamp = rospy.Time.from_sec(gaze.timestamp)
175

  
176
								    #send the goal to the server
177
								    self.gazetarget_client.send_goal(goal)
178

  
179
								    #wait for the server to finish
180
								    if (blocking):
181
									timed_out = self.gazetarget_client.wait_for_result(rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
182
									if (timed_out):
183
									    self.logger.error("%s timed out waiting for result" % (sys._getframe().f_code.co_name))
184

  
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 a goal to send to the action server.
196
										goal = mouthtargetGoal()
197
										goal.opening_left  = mouth.opening_left
198
										goal.opening_center = mouth.opening_center
199
										goal.opening_right = mouth.opening_right
200
										goal.position_left = mouth.position_left
201
										goal.position_center  = mouth.position_center
202
										goal.position_right = mouth.position_right
203

  
204
										#send the goal to the server
205
										self.mouthtarget_client.send_goal(goal)
206

  
207
										#wait for the server to finish
208
										if (blocking):
209
										    timed_out = self.mouthtarget_client.wait_for_result(rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
210
										    if (timed_out):
211
											self.logger.error("%s timed out waiting for result" % (sys._getframe().f_code.co_name))
212

  
213
											self.logger.debug("mouth rpc done")
214

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

  
222
											    #create a goal to send to the action server.
223
											    goal = speechGoal(text=text_)
224

  
225
											    #send the goal to the server
226
											    self.speech_client.send_goal(goal)
227

  
228
											    #wait for the server to finish
229
											    if (blocking):
230
												timed_out = self.speech_client.wait_for_result(rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
231
												if (timed_out):
232
												    self.logger.error("%s timed out waiting for result" % (sys._getframe().f_code.co_name))
233

  
234
												    self.logger.debug("speech rpc done")
235

  
236
												    #######################################################################
237
												    # some helpers
238
												    def convert_animationtype_to_rosval(self, value):
239
													"""convert RobotAnimation.value to ROS animation value
240
													:param value: RobotAnimation.* id to convert to rsb id
241
													"""
242
													#NOTE: this convertion is important as the actual integer values of
243
													#      thy python api and the protobuf might be different
244
													if (value == RobotAnimation.IDLE):
245
													    return animationGoal.IDLE
246