get_any.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import roslib
00005 import rostopic
00006 import importlib
00007 import yaml
00008 import copy
00009 import uuid
00010 import unique_id
00011 import world_canvas_msgs.msg
00012 import world_canvas_msgs.srv
00013 
00014 from geometry_msgs.msg import *
00015 from rospy_message_converter import message_converter
00016 from visualization_msgs.msg import Marker, MarkerArray
00017 from world_canvas_utils.serialization import *
00018 
00019 
00020 def publish(anns, data, topic_name, topic_type, pub_as_list):
00021     # Process retrieved data to build annotations and markers lists
00022     object_list = list()
00023     marker_list = MarkerArray()    
00024 
00025     marker_id = 1
00026     for a, d in zip(anns, data):
00027 
00028         # Objects
00029         type_class = roslib.message.get_message_class(d.type)
00030         if type_class is None:
00031             rospy.logerr("Topic type %s definition not found" % topic_type)
00032             return False
00033         
00034         object = deserialize_msg(d.data, type_class)
00035         object_list.append(object)
00036 
00037         # Markers
00038         marker = Marker()
00039         marker.id = marker_id
00040         marker.header = a.pose.header
00041         marker.type = a.shape
00042         marker.ns = a.type
00043         marker.action = Marker.ADD
00044         marker.lifetime = rospy.Duration.from_sec(0)
00045         marker.pose = copy.deepcopy(a.pose.pose.pose)
00046         marker.scale = a.size
00047         marker.color = a.color
00048 
00049         marker_list.markers.append(marker)
00050 
00051         marker_id = marker_id + 1
00052 
00053     # Advertise topics for retrieved annotations and their visualization markers
00054     # Message type is topic_type if we publish results as a list; a.type otherwise
00055     if not pub_as_list:
00056         topic_type = a.type
00057     
00058     topic_class = roslib.message.get_message_class(topic_type)
00059     if topic_class is None:
00060         # This happens if the topic type is wrong or not known (i.e. absent from ROS_PACKAGE_PATH)
00061         rospy.logerr("Topic type %s definition not found; unable to publish annotations" % topic_type)
00062         return
00063 
00064     marker_pub = rospy.Publisher(topic_name + '_markers', MarkerArray, latch=True, queue_size=1)
00065     object_pub = rospy.Publisher(topic_name + '_client',  topic_class, latch=True, queue_size=1)
00066 
00067     marker_pub.publish(marker_list)
00068 
00069     # Publish resulting lists
00070     if pub_as_list:
00071         object_pub.publish(object_list)
00072     else:
00073         # if pub_as_list is false, publish objects one by one
00074         for object in object_list:
00075             if isinstance(object, topic_class):
00076                 object_pub.publish(object)
00077             else:
00078                 rospy.logwarn("Object is not of type %s but %s" % (topic_type, type(object).__name__))
00079 
00080 # Other ways to do the same: not using ros magic
00081 #     module_name, class_name = topic_type.rsplit(".", 1)
00082 #     module = importlib.import_module(module_name)
00083 #     topic_class = getattr(module, class_name)
00084 #
00085 # or inspecting an already advertised topic
00086 #     input_class, input_topic, input_fn = rostopic.get_topic_class('/annotations')
00087 #     topic_class = roslib.message.get_message_class('yocs_msgs/ColumnList')
00088 
00089     return
00090 
00091 if __name__ == '__main__':
00092     rospy.init_node('objects_loader')
00093     topic_name  = rospy.get_param('~topic_name', 'annotations')
00094     topic_type  = rospy.get_param('~topic_type', None)
00095     pub_as_list = rospy.get_param('~pub_as_list', False)
00096     world    = rospy.get_param('~world')
00097     uuids    = rospy.get_param('~uuids', [])
00098     names    = rospy.get_param('~names', [])
00099     types    = rospy.get_param('~types', [])
00100     keywords = rospy.get_param('~keywords', [])
00101     related  = rospy.get_param('~relationships', [])
00102 
00103     if pub_as_list and topic_type is None:
00104         rospy.logerr("You must specify the topic type if pub_as_list is true")
00105         sys.exit()
00106 
00107     rospy.loginfo("Waiting for get_annotations service...")
00108     rospy.wait_for_service('get_annotations')
00109 
00110     rospy.loginfo("Loading annotations for world '%s'", world)
00111     get_anns_srv = rospy.ServiceProxy('get_annotations', world_canvas_msgs.srv.GetAnnotations)
00112     respAnns = get_anns_srv(world,
00113                            [unique_id.toMsg(uuid.UUID('urn:uuid:' + id)) for id in uuids],
00114                             names, types, keywords,
00115                            [unique_id.toMsg(uuid.UUID('urn:uuid:' + id)) for id in related])
00116 
00117     if len(respAnns.annotations) > 0:
00118         rospy.loginfo("Publishing visualization markers for %d retrieved annotations...",
00119                        len(respAnns.annotations))
00120     else:
00121         rospy.loginfo("No annotations found for world '%s' with the given search criteria", world)
00122         sys.exit()
00123 
00124     rospy.loginfo("Loading data for the %d retrieved annotations", len(respAnns.annotations))
00125     get_data_srv = rospy.ServiceProxy('get_annotations_data', world_canvas_msgs.srv.GetAnnotationsData)
00126     respData = get_data_srv([a.data_id for a in respAnns.annotations])
00127 
00128     if len(respData.data) > 0:
00129         rospy.loginfo("Publishing data for %d retrieved annotations...", len(respData.data))
00130         publish(respAnns.annotations, respData.data, topic_name, topic_type, pub_as_list)
00131     else:
00132         rospy.logwarn("No data found for the %d retrieved annotations", len(respAnns.annotations))
00133 
00134     rospy.loginfo("Requesting server to also publish the same data")
00135     pub_data_srv = rospy.ServiceProxy('pub_annotations_data', world_canvas_msgs.srv.PubAnnotationsData)
00136     respData = pub_data_srv([a.data_id for a in respAnns.annotations], topic_name, topic_type, pub_as_list)
00137 
00138     rospy.loginfo("Done")
00139     rospy.spin()


world_canvas_server
Author(s): Jorge Santos Simón
autogenerated on Thu Jun 6 2019 21:25:06