00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 import roslib; roslib.load_manifest('pr2_calibration_estimation')
00037
00038 import sys
00039 import rospy
00040 import time
00041 import numpy
00042 import rosbag
00043 import yaml
00044 import os.path
00045 import numpy
00046 from numpy import kron, ones, eye, array, matrix, diag
00047 import multi_step_cov_estimator as est_helpers
00048 import opt_runner
00049 from sensors.multi_sensor import MultiSensor
00050 from robot_params import RobotParams
00051 from single_transform import SingleTransform
00052 from visualization_msgs.msg import Marker, MarkerArray
00053 import geometry_msgs.msg
00054
00055 def usage():
00056 rospy.logerr("Not enough arguments")
00057 print "Usage:"
00058 print " ./proto1.py [bagfile] [output_dir] [loop_list.yaml]"
00059 sys.exit(0)
00060
00061 if __name__ == '__main__':
00062 rospy.init_node("scatterplot_viewer")
00063
00064 marker_guess_pub = rospy.Publisher("cb_guess", Marker)
00065 marker_fk_pub = rospy.Publisher("cal_markers", Marker)
00066
00067 print "Starting The Post Processing App\n"
00068
00069 if (len(rospy.myargv()) < 3):
00070 usage()
00071 else:
00072 bag_filename = rospy.myargv()[1]
00073 output_dir = rospy.myargv()[2]
00074 loop_list_filename = rospy.myargv()[3]
00075
00076 print "Using Bagfile: %s\n" % bag_filename
00077 if not os.path.isfile(bag_filename):
00078 rospy.logerr("Bagfile does not exist. Exiting...")
00079 sys.exit(1)
00080
00081 loop_list = yaml.load(open(loop_list_filename))
00082
00083 config_param_name = "calibration_config"
00084 if not rospy.has_param(config_param_name):
00085 rospy.logerr("Could not find parameter [%s]. Please populate this namespace with the estimation configuration.", config_param_name)
00086 sys.exit(1)
00087 config = rospy.get_param(config_param_name)
00088
00089
00090 sensors_name = "sensors"
00091 if sensors_name not in config.keys():
00092 rospy.logerr("Could not find namespace [%s/%s]. Please populate this namespace with sensors.", (config_param_name, sensors_name))
00093 sys.exit(1)
00094 sensors_dump = [yaml.load(x) for x in config[sensors_name].values()]
00095 all_sensors_dict = est_helpers.build_sensor_defs(sensors_dump)
00096 all_sensor_types = list(set([x['sensor_type'] for x in all_sensors_dict.values()]))
00097
00098
00099 step_list = est_helpers.load_calibration_steps(config["cal_steps"])
00100
00101 cur_step = step_list[-1]
00102
00103
00104 system_def_dict = yaml.load(open(output_dir + "/" + cur_step["output_filename"] + ".yaml"))
00105 cb_poses = yaml.load(open(output_dir + "/" + cur_step["output_filename"] + "_poses.yaml"))
00106 free_dict = yaml.load(cur_step["free_params"])
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 scatter_list = []
00146
00147 marker_count = 0
00148 for cur_loop in loop_list:
00149 marker_count += 1
00150 sensor_defs = est_helpers.load_requested_sensors(all_sensors_dict, [cur_loop['cam'], cur_loop['3d']])
00151
00152
00153
00154 multisensors = []
00155 bag = rosbag.Bag(bag_filename)
00156 for topic, msg, t in bag.read_messages(topics=['robot_measurement']):
00157 if topic == "robot_measurement":
00158
00159 for cur_laser in msg.M_laser:
00160
00161 if cur_laser.laser_id in ["tilt_laser_6x8", "tilt_laser_8x6", "tilt_laser_7x6", "tilt_laser_6x7"]:
00162 cur_laser.laser_id = "tilt_laser"
00163
00164
00165 ms = MultiSensor(sensor_defs)
00166 ms.sensors_from_message(msg)
00167 multisensors.append(ms)
00168 bag.close()
00169
00170 if (len([ms for ms in multisensors if len(ms.sensors) == 2]) == 0):
00171 print "********** No Data for [%s] + [%s] pair **********" % (cur_loop['cam'], cur_loop['3d'])
00172 continue
00173
00174
00175 multisensors_pruned, cb_poses_pruned = zip(*[(ms,cb) for ms,cb in zip(multisensors, cb_poses) if len(ms.sensors) == 2])
00176 sample_ind = [k for k,ms in zip(range(len(multisensors)), multisensors) if len(ms.sensors) == 2]
00177
00178 print "Sample Indices:"
00179 print ", ".join(["%u" % i for i in sample_ind])
00180
00181 system_def = RobotParams()
00182 system_def.configure(system_def_dict)
00183 for ms in multisensors:
00184 ms.update_config(system_def)
00185
00186
00187 error_calc = opt_runner.ErrorCalc(system_def, free_dict, multisensors_pruned, False)
00188 opt_all_vec = opt_runner.build_opt_vector(system_def, free_dict, numpy.array(cb_poses_pruned))
00189 e = error_calc.calculate_error(opt_all_vec)
00190
00191
00192
00193 errors_dict = opt_runner.compute_errors_breakdown(error_calc, multisensors_pruned, numpy.array(cb_poses_pruned))
00194
00195 print ""
00196 print "Errors Breakdown:"
00197 for sensor_id, error_list in errors_dict.items():
00198 error_cat = numpy.concatenate(error_list)
00199 rms_error = numpy.sqrt( numpy.mean(error_cat**2) )
00200 print " %s: %.6f" % (sensor_id, rms_error)
00201
00202
00203
00204
00205 chain_sensors = [[s for s in ms.sensors if s.sensor_id == cur_loop['3d']][0] for ms in multisensors_pruned]
00206 cam_sensors = [[s for s in ms.sensors if s.sensor_id == cur_loop['cam']][0] for ms in multisensors_pruned]
00207 fk_points = [s.get_measurement() for s in chain_sensors]
00208 cb_points = [SingleTransform(pose).transform * system_def.checkerboards[ms.checkerboard].generate_points() for pose, ms in zip(cb_poses_pruned,multisensors_pruned)]
00209
00210 points_list_fk = [ geometry_msgs.msg.Point(cur_pt[0, 0], cur_pt[0, 1], cur_pt[0, 2]) for cur_pt in list(numpy.concatenate(fk_points,1).T)]
00211 points_list_guess = [ geometry_msgs.msg.Point(cur_pt[0, 0], cur_pt[0, 1], cur_pt[0, 2]) for cur_pt in list(numpy.concatenate(cb_points,1).T)]
00212
00213 m = Marker()
00214 m.header.frame_id = "/torso_lift_link"
00215 m.ns = "uncal_sensor"
00216 m.id = marker_count
00217 m.type = Marker.SPHERE_LIST
00218 m.action = Marker.MODIFY
00219 m.points = points_list_fk
00220 m.color.r = 0.0
00221 m.color.g = 0.0
00222 m.color.b = 1.0
00223 m.color.a = 1.0
00224 m.scale.x = 0.01
00225 m.scale.y = 0.01
00226 m.scale.z = 0.01
00227
00228 marker_fk_pub.publish(m)
00229
00230 m.points = points_list_guess
00231 m.ns = "estimated"
00232 m.color.r = 1.0
00233 m.color.g = 0.0
00234 m.color.b = 0.0
00235 marker_fk_pub.publish(m)
00236
00237
00238
00239
00240
00241 cam_Js = [s.compute_expected_J(fk) for s,fk in zip(cam_sensors, fk_points)]
00242 cam_covs = [matrix(array(s.compute_cov(fk)) * kron(eye(s.get_residual_length()/2),ones([2,2]))) for s,fk in zip(cam_sensors, fk_points)]
00243 fk_covs = [matrix(array(s.compute_cov(None)) * kron(eye(s.get_residual_length()/3),ones([3,3]))) for s in chain_sensors]
00244
00245 full_covs = [matrix(cam_J)*fk_cov*matrix(cam_J).T + cam_cov for cam_J, cam_cov, fk_cov in zip(cam_Js, cam_covs, fk_covs)]
00246
00247
00248
00249 proj_points = [s.compute_expected(pts) for (s,pts) in zip(cam_sensors,fk_points)]
00250 meas_points = [s.get_measurement() for s in cam_sensors]
00251
00252 sample_ind = sum([ [sample_ind[k]]*meas_points[k].shape[0] for k in range(len(proj_points))], [])
00253
00254
00255 r = numpy.concatenate(proj_points) - numpy.concatenate(meas_points)
00256
00257
00258 bearing_list = []
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 import matplotlib.pyplot as plt
00269
00270
00271 cur_scatter = plt.scatter(array(r)[:,0], array(r)[:,1], **cur_loop['plot_ops'])
00272 scatter_list.append(cur_scatter)
00273
00274
00275 for k in range(len(sample_ind)):
00276
00277
00278 pass
00279
00280 for cur_cov in full_covs[0:1]:
00281 circ_angles = numpy.linspace(0,2*numpy.pi, 360, endpoint=True)
00282 circ_pos = numpy.concatenate( [ [numpy.sin(circ_angles)],
00283 [numpy.cos(circ_angles)] ] )
00284 for k in range(cur_cov.shape[0]/2)[0:1]:
00285 l,v = numpy.linalg.eig(cur_cov[(2*k):(2*k+2),(2*k):(2*k+2)])
00286 ellip = numpy.sqrt(4.6052) * matrix(v) * matrix(diag(numpy.sqrt(l))) * matrix(circ_pos)
00287 ellip_shifted = array(ellip + r[k,:].T)
00288
00289
00290
00291 plt.axis('equal')
00292 plt.grid(True)
00293
00294 plt.show()
00295
00296 sys.exit(0)
00297
00298