00001
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)
00044 self.downsample = rospy.get_param('~downsample',False)
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
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
00062 return
00063
00064 if(len(self.object_parts)==0):
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
00075
00076
00077 rospy.loginfo('sending tracks with '+('/'.join(["%d"%len(track.pose) for track in self.object_parts]))+' poses')
00078 for track in self.object_parts:
00079 track.header = pose_array.header
00080 self.pose_pub.publish(track)
00081
00082
00083
00084 self.object_msg.header = pose_array.header
00085 self.object_msg.parts = copy.deepcopy(self.object_parts)
00086 if self.downsample:
00087 print "downsampling to %d observations"%(self.samples)
00088 for part in self.object_msg.parts:
00089 if len(part.pose)>self.samples:
00090 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]
00091 else:
00092 print "selecting latest %d observations"%(self.samples)
00093 for part in self.object_msg.parts:
00094 if len(part.pose)>self.samples:
00095 part.pose = part.pose[len(part.pose) - self.samples:]
00096
00097 self.object_msg.header = pose_array.header
00098
00099 request = ArticulatedObjectSrvRequest()
00100 request.object = self.object_msg
00101
00102 parts = len(self.object_parts)
00103 response = self.fit_models(request)
00104 print '\n'.join(
00105 ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f sigma_pos=%f sigma_orient=%f'%(
00106 model.name[0:2],
00107 model.id/parts,
00108 model.id%parts,
00109 get_param(model,'bic'),
00110 get_param(model,'avg_error_position'),
00111 get_param(model,'avg_error_orientation'),
00112 get_param(model,'sigma_position'),
00113 get_param(model,'sigma_orientation')
00114 ) for model in response.object.models])
00115 request.object = copy.deepcopy(response.object)
00116
00117 response = self.get_fast_graph(request)
00118 self.object_pub.publish(response.object)
00119 self.object_msg.models = response.object.models
00120
00121 request.object = copy.deepcopy(response.object)
00122 response = self.visualize_graph(request)
00123
00124 rospy.loginfo('received articulation model: '+(' '.join(
00125 ['(%s:%d,%d)'%(model.name[0:2],model.id/parts,model.id%parts) for model in response.object.models])))
00126
00127 print '\n'.join(
00128 ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f'%(
00129 model.name[0:2],
00130 model.id/parts,
00131 model.id%parts,
00132 get_param(model,'bic'),
00133 get_param(model,'avg_error_position'),
00134 get_param(model,'avg_error_orientation')
00135 ) for model in response.object.models])
00136 print "dofs=%d, nominal dofs=%d, bic=%f pos_err=%f rot_err=%f"%(
00137 get_param(response.object,'dof'),
00138 get_param(response.object,'dof.nominal'),
00139 get_param(response.object,'bic'),
00140 get_param(response.object,'avg_error_position'),
00141 get_param(response.object,'avg_error_orientation')
00142 )
00143 def main():
00144 try:
00145 rospy.init_node('articulation_collector')
00146 articulation_collector()
00147 rospy.spin()
00148 except rospy.ROSInterruptException: pass
00149
00150 if __name__ == '__main__':
00151 main()
00152