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 *
00008 from std_msgs.msg import ColorRGBA,String
00009 from sensor_msgs.msg import ChannelFloat32, CameraInfo, CompressedImage
00010 from tf.msg import tfMessage
00011 from articulation_msgs.msg import *
00012 from articulation_msgs.srv import *
00013 from articulation_models.track_utils import *
00014 from imagerender.srv import *
00015 import tf
00016 import process_bags
00017 import rosbag
00018 import time
00019
00020 def all_detected(pose_array):
00021 for p in pose_array.poses:
00022 r = [p.orientation.x, p.orientation.y, p.orientation.z, p.orientation.w]
00023 if(numpy.dot(r, r) == 0):
00024 return False
00025 return True
00026
00027 class ProcessRobotBag:
00028 def __init__(self):
00029 rospy.wait_for_service('fit_models', 5)
00030 rospy.wait_for_service('get_spanning_tree', 5)
00031 rospy.wait_for_service('get_fast_graph', 5)
00032 rospy.wait_for_service('visualize_graph', 5)
00033 rospy.wait_for_service('render_image', 5)
00034 self.tf = tf.TransformListener()
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.get_graph = rospy.ServiceProxy('get_graph', ArticulatedObjectSrv)
00039 self.visualize_graph = rospy.ServiceProxy('visualize_graph', ArticulatedObjectSrv)
00040 self.render_image = rospy.ServiceProxy('render_image', RenderImageSrv)
00041
00042 self.infile = rospy.get_param('~infile', 'in.bag')
00043 self.outfile = rospy.get_param('~outfile', 'out.bag')
00044
00045 self.start_t = rospy.get_param('~start', 0.0)
00046 self.stop_t = rospy.get_param('~stop', 0.0)
00047
00048 self.sigma_position = rospy.get_param('~sigma_position', 0.01)
00049 self.sigma_orientation = rospy.get_param('~sigma_orientation', 0.3)
00050 self.sigmax_position = rospy.get_param('~sigmax_position', 0.01)
00051 self.sigmax_orientation = rospy.get_param('~sigmax_orientation', 0.3)
00052 self.samples = rospy.get_param('~samples', 50)
00053 self.every = rospy.get_param('~every', 1)
00054 self.downsample = rospy.get_param('~downsample', False)
00055 self.object_msg = ArticulatedObjectMsg()
00056 set_param(self.object_msg, "sigma_position", self.sigma_position, ParamMsg.PRIOR)
00057 set_param(self.object_msg, "sigma_orientation", self.sigma_orientation, ParamMsg.PRIOR)
00058 set_param(self.object_msg, "sigmax_position", self.sigmax_position, ParamMsg.PRIOR)
00059 set_param(self.object_msg, "sigmax_orientation", self.sigmax_orientation, ParamMsg.PRIOR)
00060 reduce_dofs = rospy.get_param("~reduce_dofs", 1)
00061 set_param(self.object_msg, "reduce_dofs", reduce_dofs, ParamMsg.PRIOR)
00062
00063 self.object_parts = []
00064
00065 self.run()
00066
00067 def run(self):
00068 startn = 0
00069 outbag = rosbag.Bag(self.outfile, "w")
00070 count = 0
00071 out_publishers = {}
00072 first_t = None
00073 graph = "0-DOF "
00074 object_pub = rospy.Publisher("/object", ArticulatedObjectMsg)
00075 graph_pub = rospy.Publisher("/graph", String)
00076 for topic, msg, t in rosbag.Bag(self.infile).read_messages():
00077 if topic == "/camera/image_raw/compressed" and count >= startn:
00078 print "rendering image.."
00079 self.render_image(RenderImageSrvRequest())
00080 graph_pub.publish(graph)
00081 print "done rendering image.."
00082
00083 if first_t == None:
00084 first_t = t
00085 if topic not in out_publishers:
00086 print "adding publisher for topic '%s' with type '%s'" % (topic, msg.__class__.__name__)
00087 out_publishers[topic] = rospy.Publisher(topic, msg.__class__)
00088
00089 out_publishers[topic].publish(msg)
00090 outbag.write(topic, msg, t)
00091 if t < rospy.Duration(self.start_t) + first_t:
00092 continue
00093 if self.stop_t > 0 and t > rospy.Duration(self.stop_t) + first_t:
00094 continue
00095 if topic == "/camera/pose_array":
00096 count += 1
00097 if(count % self.every == 0):
00098 if not all_detected(msg):
00099 continue
00100 if len(self.object_parts) == 0:
00101 self.object_parts = \
00102 [
00103 TrackMsg(
00104 id=i,
00105
00106 )
00107 for i in range(len(msg.poses))
00108 ]
00109 for i, p in enumerate(msg.poses):
00110 self.object_parts[i].pose.append(p)
00111
00112 if(count < startn):
00113 continue
00114
00115
00116
00117 rospy.loginfo('sending tracks with ' + ('/'.join(["%d" % len(track.pose) for track in self.object_parts])) + ' poses')
00118 for track in self.object_parts:
00119 track.header = msg.header
00120 outbag.write('/track', track, t)
00121
00122 self.object_msg.header = msg.header
00123 self.object_msg.parts = copy.deepcopy(self.object_parts)
00124 if self.downsample:
00125 for part in self.object_msg.parts:
00126 if len(part.pose) > self.samples:
00127 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]
00128 else:
00129 for part in self.object_msg.parts:
00130 if len(part.pose) > self.samples:
00131 part.pose = part.pose[len(part.pose) - self.samples:]
00132
00133 rospy.loginfo('fitting tracks with ' + ('/'.join(["%d" % len(track.pose) for track in self.object_msg.parts])) + ' poses')
00134
00135 request = ArticulatedObjectSrvRequest()
00136 request.object = self.object_msg
00137 parts = len(self.object_parts)
00138 print "fitting models.."
00139 response = self.fit_models(request)
00140 print '\n'.join(
00141 ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f sigma_pos=%f sigma_orient=%f' % (
00142 model.name[0:2],
00143 model.id / parts,
00144 model.id % parts,
00145 get_param(model, 'bic'),
00146 get_param(model, 'avg_error_position'),
00147 get_param(model, 'avg_error_orientation'),
00148 get_param(model, 'sigma_position'),
00149 get_param(model, 'sigma_orientation')
00150 ) for model in response.object.models])
00151 request.object = copy.deepcopy(response.object)
00152
00153 print "selecting models and structure.."
00154 response = self.get_fast_graph(request)
00155
00156 outbag.write('/object', response.object, t)
00157 object_pub.publish(response.object)
00158 graph = "%d-DOF "%(get_param(response.object, 'dof')) \
00159 + ','.join(
00160 ['(%s:%d,%d)' % (
00161 model.name[0:2],
00162 (model.id / parts) + 1,
00163 (model.id % parts) + 1
00164 ) for model in response.object.models])
00165 self.object_msg.models = response.object.models
00166
00167 request.object = copy.deepcopy(response.object)
00168 print "visualizing models.."
00169 response = self.visualize_graph(request)
00170 outbag.write('/structure_array', response.object.markers, t)
00171
00172 rospy.loginfo('received articulation model: ' + (' '.join(
00173 ['(%s:%d,%d)' % (model.name[0:2], model.id / parts, model.id % parts) for model in response.object.models])))
00174
00175 print '\n'.join(
00176 ['(%s:%d,%d) bic=%f pos_err=%f rot_err=%f' % (
00177 model.name[0:2],
00178 model.id / parts,
00179 model.id % parts,
00180 get_param(model, 'bic'),
00181 get_param(model, 'avg_error_position'),
00182 get_param(model, 'avg_error_orientation')
00183 ) for model in response.object.models])
00184 print "dofs=%d, nominal dofs=%d, bic=%f pos_err=%f rot_err=%f" % (
00185 get_param(response.object, 'dof'),
00186 get_param(response.object, 'dof.nominal'),
00187 get_param(response.object, 'bic'),
00188 get_param(response.object, 'avg_error_position'),
00189 get_param(response.object, 'avg_error_orientation')
00190 )
00191 print "done evaluating new pose.."
00192
00193
00194 outbag.close()
00195
00196 if __name__ == '__main__':
00197 rospy.init_node('process_robotbag')
00198
00199 ProcessRobotBag()
00200
00201
00202
00203