5 from pydoc
import locate
10 from opcua
import ua, common
11 from opcua
import uamethod
13 from roslib
import message
21 def __init__(self, server, parent, idx, name, action_type, feedback_type):
25 self.
type = action_type.split(
"/")[0]
28 goal_name =
"_" + action_type.split(
"/")[-1]
29 msg_name = goal_name.replace(
"Goal",
"")
30 class_name = msg_name.replace(
"_",
"", 1)
31 rospy.logdebug(
"Trying to find module with name: " + self.
type +
".msg." + goal_name.replace(
"Goal",
""))
32 actionspec = locate(self.
type +
".msg." + msg_name)
33 rospy.logdebug(
"We are creating action: " + self.
name)
34 rospy.logdebug(
"We have type: " + self.
type)
35 rospy.logdebug(
"We have msg name: " + msg_name)
36 rospy.logdebug(
"We have class name: " + class_name)
37 rospy.logdebug(
"We have goal name: " + goal_name)
38 rospy.logdebug(
"We have goal class name: " + goal_name.replace(
"_",
"", 1))
40 goalspec = locate(self.
type +
".msg." + goal_name)
41 rospy.logdebug(
"found goalspec")
42 self.
goal_class = getattr(goalspec, goal_name.replace(
"_",
"", 1))
44 if 'move_base_simple' in self.
name:
48 rospy.logdebug(
"found goal_instance " + str(self.
goal_instance))
50 self.
client = actionlib.SimpleActionClient(self.
get_ns_name(), getattr(actionspec, class_name))
51 rospy.logdebug(
"We have created a SimpleActionClient for action " + self.
name)
52 except actionlib.ActionException
as e:
53 rospy.logerr(
"Error when creating ActionClient for action " + self.
name, e)
54 rospy.logdebug(
"Creating parent objects for action " + str(self.
name))
56 rospy.logdebug(
"Found parent for action: " + str(self.
parent))
57 rospy.logdebug(
"Creating main node with name " + self.name.split(
"/")[-1])
59 if self.name.split(
"/")[-1] == self.parent.nodeid.Identifier:
63 ua.NodeId(self.name.split(
"/")[-1], self.parent.nodeid.NamespaceIndex, ua.NodeIdType.String),
64 ua.QualifiedName(self.name.split(
"/")[-1], self.parent.nodeid.NamespaceIndex))
65 rospy.logdebug(
"Created Main Node under parent!")
66 self.
result = self.main_node.add_object(
67 ua.NodeId(self.
name +
"_result", self.main_node.nodeid.NamespaceIndex, ua.NodeIdType.String),
68 ua.QualifiedName(
"result", self.main_node.nodeid.NamespaceIndex))
70 self.
name +
"_result_value",
73 self.result_node.set_value(
"No goal completed yet")
74 rospy.logdebug(
"Created result node")
75 self.
goal = self.main_node.add_object(
76 ua.NodeId(self.
name +
"_goal", self.main_node.nodeid.NamespaceIndex, ua.NodeIdType.String),
77 ua.QualifiedName(
"goal", parent.nodeid.NamespaceIndex))
83 rospy.logdebug(
"Created goal node")
86 ua.NodeId(self.
name +
"feedback", self.main_node.nodeid.NamespaceIndex, ua.NodeIdType.String),
87 ua.QualifiedName(
"feedback", self.main_node.nodeid.NamespaceIndex))
88 rospy.logdebug(
"Created feedback node")
91 rospy.logdebug(
"We are trying to create Feedback for feedback type: " + self.
feedback_type)
94 rospy.logdebug(
"Created feedback message instance")
96 except rospy.ROSException:
98 rospy.logerror(
"Didn't find feedback message class for type " + self.
feedback_type)
104 ua.NodeId(self.
name +
"status", self.main_node.nodeid.NamespaceIndex, ua.NodeIdType.String),
105 ua.QualifiedName(
"status", self.main_node.nodeid.NamespaceIndex))
107 self.
name +
"_status",
109 self.status_node.set_value(
"No goal sent yet")
110 rospy.loginfo(
"Created ROS Action with name: %s", self.
name)
116 if hasattr(message,
'__slots__')
and hasattr(message,
'_slot_types'):
117 for slot_name
in message.__slots__:
118 self.
update_value(topic_name +
'/' + slot_name, getattr(message, slot_name))
120 elif type(message)
in (list, tuple):
121 if (len(message) > 0)
and hasattr(message[0],
'__slots__'):
122 for index, slot
in enumerate(message):
129 self._recursive_create_items(self.
_feedback_nodes[topic_name], topic_name +
'[%d]' % index,
134 if len(message) < len(self.
_feedback_nodes[topic_name].get_children()):
135 for i
in range(len(message), self.
_feedback_nodes[topic_name].childCount()):
136 item_topic_name = topic_name +
'[%d]' % i
147 self.client.cancel_all_goals()
149 except (rospy.ROSException, UaError)
as e:
150 rospy.logerr(
"Error when cancelling a goal for " + self.
name, e)
153 rospy.logdebug(
"reached parent object creation! current parent: " + str(parent))
154 hierachy = topic_name.split(
'/')
155 rospy.logdebug(
"Current hierachy: " + str(hierachy))
156 if len(hierachy) == 0
or len(hierachy) == 1:
158 for name
in hierachy:
159 rospy.logdebug(
"current name: " + str(name))
162 nodewithsamename = self.server.find_action_node_with_same_name(name, idx)
163 if nodewithsamename
is not None:
164 rospy.logdebug(
"Found node with same name, is now new parent")
170 newparent = parent.add_object(
171 ua.NodeId(name, parent.nodeid.NamespaceIndex, ua.NodeIdType.String),
172 ua.QualifiedName(name, parent.nodeid.NamespaceIndex))
176 except IndexError, UaError:
177 newparent = parent.add_object(
178 ua.NodeId(name + str(random.randint(0, 10000)), parent.nodeid.NamespaceIndex,
179 ua.NodeIdType.String),
180 ua.QualifiedName(name, parent.nodeid.NamespaceIndex))
188 topic_text = feedback_topic_name.split(
'/')[-1]
189 if '[' in topic_text:
190 topic_text = topic_text[topic_text.index(
'['):]
193 if hasattr(feedback_message,
'__slots__')
and hasattr(feedback_message,
'_slot_types'):
195 new_node = parent.add_object(
196 ua.NodeId(feedback_topic_name, parent.nodeid.NamespaceIndex, ua.NodeIdType.String),
197 ua.QualifiedName(feedback_topic_name, parent.nodeid.NamespaceIndex))
199 for slot_name, type_name_child
in zip(feedback_message.__slots__, feedback_message._slot_types):
201 getattr(feedback_message, slot_name))
208 base_instance = roslib.message.get_message_class(base_type_str)()
209 except (ValueError, TypeError):
212 if array_size
is not None and hasattr(base_instance,
'__slots__'):
213 for index
in range(array_size):
219 new_node.set_writable(
True)
225 splits = self.name.split(
"/")
230 rospy.logdebug(
"Created ns name: " + res[:-1])
235 rospy.loginfo(
"Sending Goal for " + self.
name)
238 if 'move_base' in self.
name:
239 rospy.loginfo(
"setting frame_id for move_base malformation")
241 target_pose = getattr(goal_msg,
"target_pose")
242 header = getattr(target_pose,
"header")
243 setattr(header,
"frame_id",
"map")
244 setattr(target_pose,
"header", header)
245 except AttributeError
as e:
246 rospy.logerr(
"Error occured when setting frame_id", e)
247 rospy.loginfo(
"Created Message Instance for goal-send: " + str(goal_msg))
251 except Exception
as e:
252 rospy.logerr(
"Error occured during goal sending for Action " + str(self.
name))
256 rospy.logdebug(
"Creating message for goal call")
258 if isinstance(inputs, tuple):
261 while arg_counter < len(inputs)
and object_counter < len(sample.__slots__):
262 cur_arg = inputs[arg_counter]
263 cur_slot = sample.__slots__[object_counter]
265 while cur_slot ==
'header':
266 rospy.logdebug(
"ignoring header")
268 if object_counter < len(sample.__slots__):
269 cur_slot = sample.__slots__[object_counter]
270 real_slot = getattr(sample, cur_slot)
272 "cur_arg: " + str(cur_arg) +
" cur_slot_name: " + str(cur_slot) +
" real slot content: " + str(
274 if hasattr(real_slot,
'_type'):
275 rospy.logdebug(
"We found an object with name " + str(cur_slot) +
", creating it recursively")
276 arg_counter_before = arg_counter
278 arg_counter, inputs, sample)
279 if arg_counter != arg_counter_before:
281 rospy.logdebug(
"completed object, object counter: " + str(object_counter) +
" len(object): " + str(
282 len(sample.__slots__)))
284 already_set.append(cur_slot)
286 setattr(sample, cur_slot, cur_arg)
293 rospy.logdebug(
"Create Object Instance Notify")
295 while object_counter < len(object.__slots__)
and counter < len(inputs):
296 cur_arg = inputs[counter]
297 cur_slot = object.__slots__[object_counter]
299 while cur_slot ==
'header':
300 rospy.logdebug(
"ignoring header")
302 if object_counter < len(object.__slots__):
303 cur_slot = object.__slots__[object_counter]
305 return already_set, counter
306 real_slot = getattr(object, cur_slot)
308 "cur_arg: " + str(cur_arg) +
" cur_slot_name: " + str(cur_slot) +
" real slot content: " + str(
310 if hasattr(real_slot,
'_type'):
311 rospy.logdebug(
"Recursive Object found in request/response of service call")
316 already_set.append(cur_slot)
317 setattr(object, cur_slot, cur_arg)
321 setattr(parent, name, object)
322 return already_set, counter
325 self.client.cancel_all_goals()
326 for child
in item.get_children():
328 self.server.server.delete_nodes([child])
333 rospy.logdebug(
"updated result cb reached")
335 self.result_node.set_value(repr(result))
338 rospy.logdebug(
"updated state cb reached")
339 self.status_node.set_value(repr(self.client.get_goal_status_text()))
342 rospy.logdebug(
"updated feedback cb reached")
347 rospy.logdebug(
"getting correct name for: " + str(topic_name))
348 splits = topic_name.split(
'/')
349 return string.join(splits[0:-1],
'/')
354 for slot_name
in goal_class.__slots__:
355 if slot_name !=
'header':
356 slot = getattr(goal_class, slot_name)
357 if hasattr(slot,
'_type'):
359 array.extend(array_to_merge)
361 if isinstance(slot, list):
362 rospy.logdebug(
"Found an Array Argument!")
367 arg.ArrayDimensions = []
368 arg.Description = ua.LocalizedText(
"Array")
371 if hasattr(goal_class,
"__name__"):
372 arg.Name = goal_class.__name__ + slot_name
377 arg.ArrayDimensions = []
378 arg.Description = ua.LocalizedText(slot_name)
388 return "Goal RECALLED" 390 return "Goal RECALLING" 392 return "Goal PREEMPTING" 394 return "Goal REJECTED" 396 return "Goal ABORTED" 398 return "Goal SUCEEDED" 400 return "Goal PREEMPTED" 404 return "Goal PENDING" 407 def refresh_dict(namespace_ros, actionsdict, topicsdict, server, idx_actions):
408 topics = rospy.get_published_topics(namespace_ros)
410 for actionNameOPC
in actionsdict:
412 for topicROS, topic_type
in topics:
414 if actionNameOPC
in topicROS:
417 actionsdict[actionNameOPC].recursive_delete_items(actionsdict[actionNameOPC].parent)
418 tobedeleted.append(actionNameOPC)
419 rospy.logdebug(
"deleting action: " + actionNameOPC)
421 for name
in tobedeleted:
422 del actionsdict[name]
def map_status_to_string(param)
def recursive_delete_items(self, item)
def create_message_instance(self, inputs, sample)
def update_value(self, topic_name, message)
def get_correct_name(topic_name)
def _extract_array_info(type_str)
def nextname(hierachy, index_of_last_processed)
feedback_message_instance
def _recursive_create_feedback_items(self, parent, feedback_topic_name, type_name, feedback_message)
def recursive_create_objects(self, topic_name, idx, parent)
def getobjectidfromtype(type_name)
def update_feedback(self, feedback)
def refresh_dict(namespace_ros, actionsdict, topicsdict, server, idx_actions)
def create_object_instance(self, already_set, object, name, counter, inputs, parent)
def update_result(self, state, result)
def send_goal(self, parent, inputs)
def __init__(self, server, parent, idx, name, action_type, feedback_type)
def cancel_goal(self, parent, inputs)
def getargarray(goal_class)
def own_rosnode_cleanup()
def message_callback(self, message)
def _create_node_with_type(parent, idx, topic_name, topic_text, type_name, array_size)