articulation_collector.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('articulation_structure')
00004 import rospy
00005 import sys
00006 from visualization_msgs.msg import Marker, MarkerArray
00007 from geometry_msgs.msg import Vector3, Point, Pose, PoseArray
00008 from std_msgs.msg import ColorRGBA
00009 from sensor_msgs.msg import ChannelFloat32
00010 from articulation_msgs.msg import *
00011 from articulation_msgs.srv import *
00012 from articulation_models.track_utils import *
00013 import logging
00014 import getopt
00015 import colorsys
00016 import numpy
00017 import copy
00018 
00019 class articulation_collector:
00020   def __init__(self):
00021     try:
00022       rospy.wait_for_service('fit_models', 5)
00023       rospy.wait_for_service('get_spanning_tree', 5)
00024       rospy.wait_for_service('get_fast_graph', 5)
00025       rospy.wait_for_service('visualize_graph', 5)
00026       print "services OK"
00027     except rospy.ROSException:
00028       print "Services not found"
00029       rospy.signal_shutdown('Quit')
00030 
00031     self.pose_pub = rospy.Publisher('track', TrackMsg)
00032     self.object_pub = rospy.Publisher('object', ArticulatedObjectMsg)
00033     rospy.Subscriber("/camera/pose_array", PoseArray, self.callback,queue_size=1)
00034     self.object_parts = []
00035     self.fit_models = rospy.ServiceProxy('fit_models', ArticulatedObjectSrv)
00036     self.get_spanning_tree = rospy.ServiceProxy('get_spanning_tree', ArticulatedObjectSrv)
00037     self.get_fast_graph = rospy.ServiceProxy('get_fast_graph', ArticulatedObjectSrv)
00038     self.visualize_graph = rospy.ServiceProxy('visualize_graph', ArticulatedObjectSrv)
00039     
00040     self.sigma_position = rospy.get_param('~sigma_position',0.01)
00041     self.sigma_orientation = rospy.get_param('~sigma_orientation',0.3)
00042     self.reduce_dofs = rospy.get_param('~reduce_dofs',1)
00043     self.samples = rospy.get_param('~samples',50) #keep this number of samples
00044     self.downsample = rospy.get_param('~downsample',False)#downsample or latest obs?
00045     self.object_msg = ArticulatedObjectMsg()
00046     set_param(self.object_msg, "sigma_position", self.sigma_position, ParamMsg.PRIOR)
00047     set_param(self.object_msg, "sigma_orientation", self.sigma_orientation, ParamMsg.PRIOR)
00048     set_param(self.object_msg, "reduce_dofs", self.reduce_dofs, ParamMsg.PRIOR)
00049   
00050   def callback(self, pose_array):
00051     #rospy.loginfo("received pose array of length %d" % len(pose_array.poses))
00052     marker_array = MarkerArray()
00053     
00054     all_detected = True
00055     for p in pose_array.poses:
00056       r = [p.orientation.x,p.orientation.y,p.orientation.z,p.orientation.w]
00057       if( numpy.dot(r,r) == 0 ):
00058         all_detected = False
00059 
00060     if not all_detected:
00061       #rospy.loginfo('not all markers detected')
00062       return
00063     
00064     if(len(self.object_parts)==0): #generate new TrackMsgs for all Object Parts if there aren't any yet
00065       self.object_parts = [ 
00066           TrackMsg(
00067                    id=i,
00068 
00069           ) 
00070           for (i,p) in enumerate(pose_array.poses) ]
00071       
00072     for (track,pose) in zip(self.object_parts,pose_array.poses):
00073       track.pose.append( pose )
00074       track.pose_headers.append(pose_array.header)
00075 #      if(len(track.pose)>self.samples):
00076 #        track.pose = track.pose[len(track.pose)-self.samples:]
00077       
00078     rospy.loginfo('sending tracks with '+('/'.join(["%d"%len(track.pose) for track in self.object_parts]))+' poses')
00079     for track in self.object_parts:
00080       track.header = pose_array.header
00081       self.pose_pub.publish(track)
00082       
00083                 # downsample or cut-off?
00084 #    self.object_msg.parts = self.object_parts
00085     self.object_msg.header = pose_array.header
00086     self.object_msg.parts = copy.deepcopy(self.object_parts)
00087     if self.downsample:
00088       print "downsampling to %d observations"%(self.samples)
00089       for part in self.object_msg.parts:
00090         if len(part.pose)>self.samples:
00091           part.pose = [p for (i,p) in enumerate(part.pose) if i % (len(part.pose) / self.samples + 1) == 0 or i==len(part.pose)-1] 
00092     else:
00093       print "selecting latest %d observations"%(self.samples)
00094       for part in self.object_msg.parts:
00095         if len(part.pose)>self.samples:
00096           part.pose = part.pose[len(part.pose) - self.samples:]
00097 
00098     self.object_msg.header = pose_array.header
00099     
00100     request = ArticulatedObjectSrvRequest()
00101     request.object = self.object_msg
00102     
00103     parts = len(self.object_parts) 
00104     response = self.fit_models(request)
00105     print '\n'.join(
00106        ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f sigma_pos=%f sigma_orient=%f'%(
00107             model.name[0:2],
00108             model.id/parts,
00109             model.id%parts,
00110             get_param(model,'bic'),
00111             get_param(model,'avg_error_position'),
00112             get_param(model,'avg_error_orientation'),
00113             get_param(model,'sigma_position'),
00114             get_param(model,'sigma_orientation')
00115         ) for model in response.object.models])
00116     request.object = copy.deepcopy(response.object)
00117 
00118     response = self.get_fast_graph(request)
00119     self.object_pub.publish(response.object)
00120     self.object_msg.models = response.object.models
00121       
00122     request.object = copy.deepcopy(response.object)
00123     response = self.visualize_graph(request)
00124     
00125     rospy.loginfo('received articulation model: '+(' '.join(
00126        ['(%s:%d,%d)'%(model.name[0:2],model.id/parts,model.id%parts) for model in response.object.models])))
00127   
00128     print '\n'.join(
00129        ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f'%(
00130             model.name[0:2],
00131             model.id/parts,
00132             model.id%parts,
00133             get_param(model,'bic'),
00134             get_param(model,'avg_error_position'),
00135             get_param(model,'avg_error_orientation')
00136         ) for model in response.object.models])
00137 #    print "dofs=%d, nominal dofs=%d, bic=%f pos_err=%f rot_err=%f"%(
00138 #            %get_param(response.object,'dof'),
00139 #            get_param(response.object,'dof.nominal'),
00140 #            get_param(response.object,'bic'),
00141 #            get_param(response.object,'avg_error_position'),
00142 #            get_param(response.object,'avg_error_orientation')
00143 #            )      
00144 def main():
00145   try:
00146     rospy.init_node('articulation_collector')
00147     articulation_collector()
00148     rospy.spin()
00149   except rospy.ROSInterruptException: pass
00150     
00151 if __name__ == '__main__':
00152   main()
00153 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


articulation_structure
Author(s): sturm
autogenerated on Wed Dec 26 2012 15:37:59