objectdetection_db.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Store the ObjectDetection message
00004 #
00005 
00006 # parameters:
00007 #    dbname,hostname,port,username,passwd: for DB connection
00008 #    table_name(default "tf"): DB table name
00009 
00010 # fore example, table definition of "tf"
00011 #                                  Table "public.tf"
00012 #         Column          |  Type   |                    Modifiers
00013 #-------------------------+---------+-------------------------------------------------
00014 # id                      | integer | not null default nextval('tf_id_seq'::regclass)
00015 # header_stamp            | bigint  |
00016 # header_frame_id         | text    |
00017 # child_frame_id          | text    |
00018 # transform_translation_x | real    |
00019 # transform_translation_y | real    |
00020 # transform_translation_z | real    |
00021 # transform_rotation_x    | real    |
00022 # transform_rotation_y    | real    |
00023 # transform_rotation_z    | real    |
00024 # transform_rotation_w    | real    |
00025 #Indexes:
00026 #    "tf_stamp_idx" btree (header_stamp)
00027 
00028 import roslib; roslib.load_manifest('jsk_pr2_startup')
00029 import rospy
00030 import pgdb
00031 import tf
00032 
00033 from geometry_msgs.msg import PoseStamped
00034 from posedetection_msgs.msg import ObjectDetection
00035 
00036 class ObjectDetectionDB:
00037     def __init__(self,connection=None,db_lock=None): # TODO
00038         db_name = rospy.get_param('~db_name','pr2db')
00039         host = rospy.get_param('~host_name','c1')
00040         port = rospy.get_param('~port',5432)
00041         username = rospy.get_param('~user_name','pr2admin')
00042         passwd = rospy.get_param('~passwd','')
00043         # args dbname, host, port, opt, tty, user, passwd
00044         if connection == None:
00045             self.con = pgdb.connect(database=db_name, host=host,
00046                                     user=username, password=passwd)
00047         else:
00048             self.con = connection
00049         self.tf_listener = tf.TransformListener()
00050         self.robot_frame = rospy.get_param('~base_frame_id','/base_link')
00051         self.table_name = rospy.get_param('~table_name','tf')
00052         self.subscribers = []
00053         return
00054 
00055     # DB Insertion function
00056     def insert_pose_to_db(self, table, stamp, source, target, pose):
00057         rospy.loginfo("insert ObjectDetection message");
00058         (trans,rot) = (pose.position, pose.orientation)
00059         cursor = self.con.cursor()
00060         cursor.execute("INSERT INTO %s (header_stamp,header_frame_id,child_frame_id,transform_translation_x, transform_translation_y, transform_translation_z, transform_rotation_x, transform_rotation_y, transform_rotation_z, transform_rotation_w) VALUES (%d,'%s','%s',%f,%f,%f,%f,%f,%f,%f);" % (table,stamp.to_nsec(),source,target,trans.x,trans.y,trans.z,rot.x,rot.y,rot.z,rot.w))
00061         cursor.close()
00062         self.con.commit()
00063 
00064     def objectdetection_cb(self, msg):
00065         try:
00066             self.tf_listener.waitForTransform(self.robot_frame, msg.header.frame_id, msg.header.stamp, rospy.Duration(1.0))
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                 pose = tpose.pose
00071 
00072                 trans = (pose.position.x, pose.position.y, pose.position.z)
00073                 rot = (pose.orientation.x,pose.orientation.y,pose.orientation.z,pose.orientation.w)
00074                 self.insert_pose_to_db(self.table_name, msg.header.stamp,
00075                                        msg.header.frame_id, obj.type, pose)
00076         except (tf.Exception, tf.LookupException, tf.ConnectivityException):
00077             return
00078 
00079     def update_subscribers(self):
00080         current_subscribers = rospy.client.get_published_topics()
00081         targets = [x for x in current_subscribers if x[1]=='posedetection_msgs/ObjectDetection' and ('_agg' in x[0])]
00082         for sub in self.subscribers:
00083             if sub.get_num_connections() == 0:
00084                 sub.unregister()
00085                 self.subscribers.remove(sub)
00086                 rospy.loginfo('unsubscribe (%s)',sub.name)
00087         for topic_info in targets:
00088             if topic_info[0] in [x.name for x in self.subscribers]:
00089                 continue
00090             sub = rospy.Subscriber(topic_info[0], ObjectDetection,
00091                                    obj.objectdetection_cb)
00092             self.subscribers += [sub]
00093             rospy.loginfo('start subscribe (%s)',sub.name)
00094 
00095 if __name__ == "__main__":
00096     rospy.init_node('pbjectdetecton_db')
00097     obj = ObjectDetectionDB()
00098     looprate = rospy.Rate(1.0)
00099     while not rospy.is_shutdown():
00100         obj.update_subscribers()
00101         looprate.sleep()
00102 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


jsk_pr2_startup
Author(s): Manabu Saito
autogenerated on Sat Mar 23 2013 17:11:33