hlrc / client / python / hlrc_client / MiddlewareROS.py @ 6304bfc1
History | View | Annotate | Download (13.907 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 | |||
30 | 70c54617 | Simon Schulz | import rospy |
31 | 64f5c90e | Simon Schulz | import actionlib |
32 | from hlrc_server.msg import * |
||
33 | 2126781b | Simon Schulz | from actionlib_msgs.msg import GoalStatus |
34 | 0c286af0 | Simon Schulz | |
35 | class MiddlewareROS(Middleware): |
||
36 | 3877047d | Simon Schulz | #default timeout to wait in case of blocking calls
|
37 | ROS_ACTION_CALL_TIMEOUT = 30.0
|
||
38 | |||
39 | #######################################################################
|
||
40 | 6304bfc1 | Robert Haschke | def __init__(self, scope, loglevel=logging.WARNING, timeout=None): |
41 | 70c54617 | Simon Schulz | """initialise
|
42 | :param scope: base scope we want to listen on
|
||
43 | """
|
||
44 | #init base settings
|
||
45 | Middleware.__init__(self,scope,loglevel)
|
||
46 | 6304bfc1 | Robert Haschke | #ros-specific initialization
|
47 | if timeout is None: timeout = rospy.Duration() |
||
48 | self.init_middleware(timeout)
|
||
49 | 70c54617 | Simon Schulz | |
50 | def __del__(self): |
||
51 | """destructor
|
||
52 | """
|
||
53 | self.logger.debug("destructor of MiddlewareROS called") |
||
54 | |||
55 | #######################################################################
|
||
56 | 6304bfc1 | Robert Haschke | def init_middleware(self, timeout): |
57 | 70c54617 | Simon Schulz | """initialise middleware
|
58 | """
|
||
59 | self.logger.info("initialising ROS middleware connection on scope %s" % (self.base_scope)) |
||
60 | |||
61 | 67f533d7 | Robert Haschke | try:
|
62 | rospy.init_node('hlrc_client', anonymous=True) |
||
63 | except rospy.exceptions.ROSException as e: |
||
64 | 6304bfc1 | Robert Haschke | self.logger.info(str(e)) |
65 | 70c54617 | Simon Schulz | |
66 | self.logger.info("creating action clients") |
||
67 | |||
68 | 6304bfc1 | Robert Haschke | start = time.time() |
69 | def create_client(sub_scope, action, name): |
||
70 | if not timeout.is_zero(): |
||
71 | t = rospy.Duration.from_sec(timeout.to_sec() - (time.time() - start)) # time left
|
||
72 | if t <= rospy.Duration():
|
||
73 | raise Exception("timeout while connecting to actionlib servers") |
||
74 | else:
|
||
75 | t = timeout |
||
76 | |||
77 | self.logger.debug("creating %s client" % name) |
||
78 | |||
79 | client = actionlib.SimpleActionClient(self.base_scope + sub_scope, action)
|
||
80 | if not client.wait_for_server(t): |
||
81 | raise Exception("timeout while creating %s client" % name) |
||
82 | return client
|
||
83 | |||
84 | self.speech_client = create_client("/set/speech", speechAction, "speech action") |
||
85 | self.default_emotion_client = create_client("/set/defaultEmotion", emotionstateAction, "default emotion") |
||
86 | self.current_emotion_client = create_client("/set/currentEmotion", emotionstateAction, "current emotion") |
||
87 | self.animation_client = create_client("/set/animation", animationAction, "animation") |
||
88 | self.gazetarget_client = create_client("/set/gaze", gazetargetAction, "gazetarget") |
||
89 | self.lookattarget_client = create_client("/set/lookat", lookattargetAction, "lookattarget") |
||
90 | self.mouthtarget_client = create_client("/set/mouth", mouthtargetAction, "mouthtarget") |
||
91 | 70c54617 | Simon Schulz | |
92 | self.logger.info("MiddlewareROS initialised") |
||
93 | |||
94 | 02fc76ae | Simon Schulz | #######################################################################
|
95 | def is_running(self): |
||
96 | return not rospy.is_shutdown() |
||
97 | 70c54617 | Simon Schulz | |
98 | #######################################################################
|
||
99 | def publish_emotion(self, em_type, emotion, blocking): |
||
100 | """publish an emotion via mw
|
||
101 | :param em_type: type of emotion (RobotEmotion::TYPE_DEFAULT or RobotEmotion::TYPE_CURRENT)
|
||
102 | :param emotion: emotion to set
|
||
103 | :param blocking: True if this call should block until execution finished on robot
|
||
104 | """
|
||
105 | self.logger.debug("calling the emotion rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
106 | |||
107 | #create a goal to send to the action server.
|
||
108 | goal = emotionstateGoal() |
||
109 | goal.value = self.convert_emotiontype_to_rosval(emotion.value)
|
||
110 | goal.duration = int(emotion.time_ms)
|
||
111 | |||
112 | #which client do we use?
|
||
113 | if (em_type == RobotEmotion.TYPE_DEFAULT):
|
||
114 | client = self.default_emotion_client
|
||
115 | else:
|
||
116 | client = self.current_emotion_client
|
||
117 | |||
118 | #send the goal to the server
|
||
119 | if (blocking):
|
||
120 | 2126781b | Simon Schulz | #send and wait:
|
121 | state = client.send_goal_and_wait(goal, execute_timeout = rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT)) |
||
122 | if (state != GoalStatus.SUCCEEDED):
|
||
123 | self.logger.error("%s timed out waiting for result or soemthing else went wrong" % (sys._getframe().f_code.co_name)) |
||
124 | else:
|
||
125 | #non blocking, fire & forget
|
||
126 | client.send_goal(goal) |
||
127 | |||
128 | self.logger.debug("emotion rpc done") |
||
129 | 70c54617 | Simon Schulz | |
130 | def publish_default_emotion(self, emotion, blocking): |
||
131 | self.publish_emotion(RobotEmotion.TYPE_DEFAULT, emotion, blocking)
|
||
132 | |||
133 | def publish_current_emotion(self, emotion, blocking): |
||
134 | self.publish_emotion(RobotEmotion.TYPE_CURRENT, emotion, blocking)
|
||
135 | |||
136 | def publish_head_animation(self, animation, blocking): |
||
137 | """publish an head animation via mw
|
||
138 | :param animation: animation to set
|
||
139 | :param blocking: True if this call should block until execution finished on robot
|
||
140 | """
|
||
141 | self.logger.debug("calling the animation rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
142 | |||
143 | #create a goal to send to the action server.
|
||
144 | goal = animationGoal() |
||
145 | goal.target = self.convert_animationtype_to_rosval(animation.value)
|
||
146 | goal.repetitions = animation.repetitions |
||
147 | goal.duration_each = int(animation.time_ms)
|
||
148 | goal.scale = animation.scale |
||
149 | |||
150 | #send the goal to the server
|
||
151 | if (blocking):
|
||
152 | 2126781b | Simon Schulz | #send and wait:
|
153 | state = self.animation_client.send_goal_and_wait(goal, execute_timeout = rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
|
||
154 | if (state != GoalStatus.SUCCEEDED):
|
||
155 | self.logger.error("%s timed out waiting for result or soemthing else went wrong" % (sys._getframe().f_code.co_name)) |
||
156 | else:
|
||
157 | #non blocking, fire & forget
|
||
158 | self.animation_client.send_goal(goal)
|
||
159 | 70c54617 | Simon Schulz | |
160 | self.logger.debug("animation rpc done") |
||
161 | |||
162 | def publish_gaze_target(self, gaze, blocking): |
||
163 | """publish a gaze target via mw
|
||
164 | :param gaze: gaze to set
|
||
165 | :param blocking: True if this call should block until execution finished on robot
|
||
166 | """
|
||
167 | self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
168 | |||
169 | #create a goal to send to the action server.
|
||
170 | goal = gazetargetGoal() |
||
171 | goal.pan = gaze.pan |
||
172 | goal.tilt = gaze.tilt |
||
173 | goal.roll = gaze.roll |
||
174 | goal.vergence = gaze.vergence |
||
175 | goal.pan_offset = gaze.pan_offset |
||
176 | goal.tilt_offset = gaze.tilt_offset |
||
177 | |||
178 | #type
|
||
179 | if (gaze.gaze_type == RobotGaze.GAZETARGET_ABSOLUTE):
|
||
180 | goal.gaze_type = gazetargetGoal.GAZETARGET_ABSOLUTE |
||
181 | else:
|
||
182 | goal.gaze_type = gazetargetGoal.GAZETARGET_RELATIVE |
||
183 | |||
184 | 04cf2b6f | Sebastian Meyer zu Borgsen | goal.gaze_timestamp = rospy.Time.from_sec(gaze.gaze_timestamp.to_seconds()) |
185 | 70c54617 | Simon Schulz | |
186 | #send the goal to the server
|
||
187 | if (blocking):
|
||
188 | 2126781b | Simon Schulz | #send and wait:
|
189 | state = self.gazetarget_client.send_goal_and_wait(goal, execute_timeout = rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
|
||
190 | if (state != GoalStatus.SUCCEEDED):
|
||
191 | self.logger.error("%s timed out waiting for result or soemthing else went wrong" % (sys._getframe().f_code.co_name)) |
||
192 | else:
|
||
193 | #non blocking, fire & forget
|
||
194 | self.gazetarget_client.send_goal(goal)
|
||
195 | 70c54617 | Simon Schulz | |
196 | self.logger.debug("gaze rpc done") |
||
197 | |||
198 | 465c7ad7 | Florian Lier | def publish_lookat_target(self, x, y, z, blocking, frame_id='', roll=0.0): |
199 | """publish a gaze target via mw
|
||
200 | :param x,y,z: position to look at (in eyes frame or frame_id)
|
||
201 | :param roll: side-ways motion of head
|
||
202 | :param blocking: True if this call should block until execution finished on robot
|
||
203 | """
|
||
204 | self.logger.debug("calling the gaze rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
205 | |||
206 | # create a goal to send to the action server.
|
||
207 | goal = lookattargetGoal() |
||
208 | 60b91de0 | Robert Haschke | p = goal.point |
209 | p.header.frame_id = frame_id |
||
210 | p.header.stamp = rospy.Time.now() |
||
211 | p.point.x = float(x)
|
||
212 | p.point.y = float(y)
|
||
213 | p.point.z = float(z)
|
||
214 | 465c7ad7 | Florian Lier | goal.roll = roll |
215 | |||
216 | # send the goal to the server
|
||
217 | if blocking:
|
||
218 | # send and wait:
|
||
219 | state = self.lookattarget_client.send_goal_and_wait(goal, execute_timeout=rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
|
||
220 | if state != GoalStatus.SUCCEEDED:
|
||
221 | self.logger.error("%s timed out waiting for result or soemthing else went wrong" % (sys._getframe().f_code.co_name)) |
||
222 | else:
|
||
223 | # non blocking, fire & forget
|
||
224 | self.lookattarget_client.send_goal(goal)
|
||
225 | |||
226 | self.logger.debug("lookat rpc done") |
||
227 | |||
228 | 70c54617 | Simon Schulz | def publish_mouth_target(self, mouth, blocking): |
229 | """publish a mouth target via mw
|
||
230 | :param mouth: mouth value to set
|
||
231 | :param blocking: True if this call should block until execution finished on robot
|
||
232 | """
|
||
233 | self.logger.debug("calling the mouth rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
234 | |||
235 | #create a goal to send to the action server.
|
||
236 | goal = mouthtargetGoal() |
||
237 | goal.opening_left = mouth.opening_left |
||
238 | goal.opening_center = mouth.opening_center |
||
239 | goal.opening_right = mouth.opening_right |
||
240 | goal.position_left = mouth.position_left |
||
241 | goal.position_center = mouth.position_center |
||
242 | goal.position_right = mouth.position_right |
||
243 | |||
244 | #send the goal to the server
|
||
245 | if (blocking):
|
||
246 | 2126781b | Simon Schulz | #send and wait:
|
247 | state = self.mouthtarget_client.send_goal_and_wait(goal, execute_timeout = rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
|
||
248 | if (state != GoalStatus.SUCCEEDED):
|
||
249 | self.logger.error("%s timed out waiting for result or soemthing else went wrong" % (sys._getframe().f_code.co_name)) |
||
250 | else:
|
||
251 | #non blocking, fire & forget
|
||
252 | self.mouthtarget_client.send_goal(goal)
|
||
253 | 70c54617 | Simon Schulz | |
254 | self.logger.debug("mouth rpc done") |
||
255 | |||
256 | def publish_speech(self, text_, blocking): |
||
257 | """publish a tts request via mw
|
||
258 | :param text_: text to synthesize and speak
|
||
259 | :param blocking: True if this call should block until execution finished on robot
|
||
260 | """
|
||
261 | self.logger.debug("calling the speech rpc (%s)..." % ("BLOCKING" if blocking else "NON-BLOCKING")) |
||
262 | |||
263 | #create a goal to send to the action server.
|
||
264 | goal = speechGoal(text=text_) |
||
265 | |||
266 | #send the goal to the server
|
||
267 | if (blocking):
|
||
268 | 2126781b | Simon Schulz | #send and wait:
|
269 | state = self.speech_client.send_goal_and_wait(goal, execute_timeout = rospy.Duration.from_sec(MiddlewareROS.ROS_ACTION_CALL_TIMEOUT))
|
||
270 | if (state != GoalStatus.SUCCEEDED):
|
||
271 | self.logger.error("%s timed out waiting for result or soemthing else went wrong" % (sys._getframe().f_code.co_name)) |
||
272 | else:
|
||
273 | #non blocking, fire & forget
|
||
274 | self.speech_client.send_goal(goal)
|
||
275 | 70c54617 | Simon Schulz | |
276 | self.logger.debug("speech rpc done") |
||
277 | |||
278 | #######################################################################
|
||
279 | # some helpers
|
||
280 | def convert_animationtype_to_rosval(self, value): |
||
281 | """convert RobotAnimation.value to ROS animation value
|
||
282 | :param value: RobotAnimation.* id to convert to rsb id
|
||
283 | """
|
||
284 | #NOTE: this convertion is important as the actual integer values of
|
||
285 | # thy python api and the protobuf might be different
|
||
286 | if (value == RobotAnimation.IDLE):
|
||
287 | return animationGoal.IDLE
|
||
288 | elif (value == RobotAnimation.HEAD_NOD):
|
||
289 | return animationGoal.HEAD_NOD
|
||
290 | elif (value == RobotAnimation.HEAD_SHAKE):
|
||
291 | return animationGoal.HEAD_SHAKE
|
||
292 | elif (value == RobotAnimation.EYEBLINK_L):
|
||
293 | return animationGoal.EYEBLINK_L
|
||
294 | elif (value == RobotAnimation.EYEBLINK_R):
|
||
295 | return animationGoal.EYEBLINK_R
|
||
296 | elif (value == RobotAnimation.EYEBLINK_BOTH):
|
||
297 | return animationGoal.EYEBLINK_BOTH
|
||
298 | elif (value == RobotAnimation.EYEBROWS_RAISE):
|
||
299 | return animationGoal.EYEBROWS_RAISE
|
||
300 | elif (value == RobotAnimation.EYEBROWS_LOWER):
|
||
301 | return animationGoal.EYEBROWS_LOWER
|
||
302 | else:
|
||
303 | self.logger.error("invalid animation type %d\n" % (value)) |
||
304 | return animationGoal.NEUTRAL
|
||
305 | |||
306 | def convert_emotiontype_to_rosval(self, value): |
||
307 | """convert RobotEmotion.value to ROS animation value
|
||
308 | :param value: RobotEmotion.* id to convert to ros id
|
||
309 | """
|
||
310 | #NOTE: this convertion is important as the actual integer values of
|
||
311 | # thy python api and the protobuf might be different
|
||
312 | |||
313 | if (value == RobotEmotion.NEUTRAL):
|
||
314 | return emotionstateGoal.NEUTRAL
|
||
315 | elif (value == RobotEmotion.HAPPY):
|
||
316 | return emotionstateGoal.HAPPY
|
||
317 | elif (value == RobotEmotion.SAD):
|
||
318 | return emotionstateGoal.SAD
|
||
319 | elif (value == RobotEmotion.ANGRY):
|
||
320 | return emotionstateGoal.ANGRY
|
||
321 | elif (value == RobotEmotion.SURPRISED):
|
||
322 | return emotionstateGoal.SURPRISED
|
||
323 | elif (value == RobotEmotion.FEAR):
|
||
324 | return emotionstateGoal.FEAR
|
||
325 | else:
|
||
326 | self.logger.error("invalid emotion type %d\n" % (value)) |
||
327 | return emotionstateGoal.NEUTRAL |