objectdetection_db.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Store the ObjectDetection message
00004 #
00005 
00006 import rospy
00007 import tf
00008 import rosgraph
00009 from geometry_msgs.msg import PoseStamped, TransformStamped
00010 from posedetection_msgs.msg import ObjectDetection
00011 
00012 from mongodb_store.message_store import MessageStoreProxy
00013 
00014 class ObjectDetectionDB(object):
00015     def __init__(self):
00016         self.db_name = rospy.get_param('robot/database','jsk_robot_lifelog')
00017         try:
00018             self.col_name = rospy.get_param('robot/name')
00019         except KeyError as e:
00020             rospy.logerr("please specify param \"robot/name\" (e.g. pr1012, olive)")
00021             exit(1)
00022         self.update_cycle = rospy.get_param('update_cycle', 1)
00023         self.map_frame = rospy.get_param('~map_frame', 'map')
00024         self.robot_frame = rospy.get_param('~robot_frame','base_footprint')
00025         self.tf_listener = tf.TransformListener()
00026         self.msg_store = MessageStoreProxy(database=self.db_name, collection=self.col_name)
00027         rospy.loginfo("connected to %s.%s" % (self.db_name, self.col_name))
00028         rospy.loginfo("map->robot: %s -> %s" % (self.map_frame, self.robot_frame))
00029 
00030         self.master = rosgraph.Master("/rostopic")
00031         self.subscribers = []
00032 
00033     # DB Insertion function
00034     def _insert_pose_to_db(self, map_to_robot, robot_to_obj):
00035         try:
00036             self.msg_store.insert(map_to_robot)
00037             self.msg_store.insert(robot_to_obj)
00038             rospy.loginfo('inserted map2robot: %s, robot2obj: %s' % (map_to_robot, robot_to_obj))
00039         except Exception as e:
00040             rospy.logwarn('failed to insert to db' + e)
00041 
00042     def _lookup_transform(self, target_frame, source_frame, time=rospy.Time(0), timeout=rospy.Duration(0.0)):
00043         self.tf_listener.waitForTransform(target_frame, source_frame, time, timeout)
00044         res = self.tf_listener.lookupTransform(target_frame, source_frame, time)
00045         ret = TransformStamped()
00046         ret.header.frame_id = target_frame
00047         ret.header.stamp = time
00048         ret.child_frame_id = source_frame
00049         ret.transform.translation.x = res[0][0]
00050         ret.transform.translation.y = res[0][1]
00051         ret.transform.translation.z = res[0][2]
00052         ret.transform.rotation.x = res[1][0]
00053         ret.transform.rotation.y = res[1][1]
00054         ret.transform.rotation.z = res[1][2]
00055         ret.transform.rotation.w = res[1][3]
00056         return ret
00057 
00058     def _objectdetection_cb(self, msg):
00059         try:
00060             self.tf_listener.waitForTransform(self.robot_frame, self.map_frame, msg.header.stamp, rospy.Duration(1.0))
00061             map_to_robot = self._lookup_transform(self.robot_frame, self.map_frame, msg.header.stamp)
00062         except Exception as e:
00063             rospy.logwarn("failed to lookup tf: %s", e)
00064             return
00065 
00066         try:
00067             for obj in msg.objects:
00068                 spose = PoseStamped(header=msg.header,pose=obj.pose)
00069                 tpose = self.tf_listener.transformPose(self.robot_frame, spose)
00070                 obj.pose = tpose.pose
00071                 self._insert_pose_to_db(map_to_robot, obj)
00072         except Exception as e:
00073             rospy.logwarn("failed to object pose transform: %s", e)
00074 
00075     def _update_subscribers(self):
00076         object_detection_topics = [x[0] for x in rospy.client.get_published_topics()
00077                                    if x[1]=='posedetection_msgs/ObjectDetection' and (not ('_agg' in x[0]))]
00078         _, subs, _ = self.master.getSystemState()
00079         targets = [x[0] for x in subs if x[0] in object_detection_topics and not rospy.get_name() in x[1]]
00080         for sub in self.subscribers:
00081             sub_nodes = [x[1] for x in subs if x[0] == sub.name]
00082             if (not sub_nodes) or (len(sub_nodes[0]) == 1 and (rospy.get_name() in sub_nodes[0])):
00083                 sub.unregister()
00084                 self.subscribers.remove(sub)
00085                 rospy.loginfo('unsubscribed (%s)',sub.name)
00086         for topic_name in targets:
00087             if topic_name in [x.name for x in self.subscribers]:
00088                 continue
00089             sub = rospy.Subscriber(topic_name, ObjectDetection,
00090                                    self._objectdetection_cb)
00091             self.subscribers += [sub]
00092             rospy.loginfo('start subscribe (%s)',sub.name)
00093 
00094     def sleep_one_cycle(self):
00095         self._update_subscribers()
00096         rospy.sleep(self.update_cycle)
00097 
00098 if __name__ == "__main__":
00099     rospy.init_node('objectdetecton_db')
00100     obj = ObjectDetectionDB()
00101     while not rospy.is_shutdown():
00102         obj.sleep_one_cycle()


jsk_robot_startup
Author(s):
autogenerated on Sat Jul 1 2017 02:42:18