00001 #include "arm_3d_cb_calib/calib_3d_cbs.h"
00002
00003 #include <pcl/point_types.h>
00004 #include <pcl_ros/point_cloud.h>
00005 #include <pcl/point_cloud.h>
00006
00007 #include "geometry_msgs/PoseStamped.h"
00008 #include <rosbag/bag.h>
00009 #include <rosbag/view.h>
00010 #include <sensor_msgs/PointCloud2.h>
00011
00012 using namespace arm_3d_cb_calib;
00013
00014 typedef pcl::PointCloud<pcl::PointXYZ> PCXYZ;
00015 typedef pcl::PointXYZ PXYZ;
00016
00017 void readCBPoseBag(char* filename, vector<PCXYZ::Ptr> &pcs,
00018 vector<gtsam::Pose3> &poses, int num)
00019 {
00020 printf("Reading bag...");
00021 rosbag::Bag bag_in(filename, rosbag::bagmode::Read);
00022 vector<string> topics;
00023 string pc_topic = "/pc";
00024 string pose_topic = "/pose";
00025 topics.push_back(pc_topic);
00026 topics.push_back(pose_topic);
00027 rosbag::View view(bag_in, rosbag::TopicQuery(topics));
00028 num *= 2;
00029 BOOST_FOREACH(rosbag::MessageInstance const m, view) {
00030 if(m.getTopic() == pc_topic) {
00031 PCXYZ::Ptr new_pc(new PCXYZ());
00032 pcl::fromROSMsg<PXYZ>(*m.instantiate<sensor_msgs::PointCloud2>(),*new_pc);
00033 pcs.push_back(new_pc);
00034 }
00035 if(m.getTopic() == pose_topic) {
00036 geometry_msgs::PoseStamped::ConstPtr pose = m.instantiate<geometry_msgs::PoseStamped>();
00037 poses.push_back(geomPoseToGtsamPose3(pose->pose));
00038 }
00039 if(num >= 0)
00040 if(--num == 0)
00041 break;
00042 }
00043 bag_in.close();
00044 printf("done.\n");
00045 }
00046
00047 int myrandom (int i) { return std::rand()%i;}
00048 int main(int argc, char* argv[])
00049 {
00050 ros::init(argc, argv, "find_cb_calib");
00051 int num_kinects = argc-1;
00052
00053 double dim_cb;
00054 int num_rows, num_cols;
00055 ros::param::param<double>("~dim_cb", dim_cb, 0.0);
00056 ros::param::param<int>("~num_rows", num_rows, 7);
00057 ros::param::param<int>("~num_cols", num_cols, 6);
00058 CBCalibProblem prob(num_kinects, dim_cb, num_rows, num_cols);
00059 cout << "dim_cb " << dim_cb << ", num_rows " << num_rows << ", num_cols " << num_cols << endl;
00060
00061 for(int k=0;k<num_kinects;k++) {
00062 vector<PCXYZ::Ptr> pcs;
00063 readCBPoseBag(argv[k+1], pcs, prob.base_T_ee_poses, -1);
00064
00065 for(size_t i=0;i<pcs.size();i++) {
00066 prob.kinect_p_points[k].push_back(vector<Point3>());
00067 for(size_t j=0;j<pcs[i]->size();j++) {
00068 PXYZ pt = pcs[i]->at(j);
00069 prob.kinect_p_points[k][i].push_back(Point3(pt.x,pt.y,pt.z));
00070 }
00071 }
00072 }
00073
00074 CBCalibSolution sol;
00075
00076 solveCBCalibProblem(prob, sol);
00077
00078
00079
00080 sol.cb_T_ee_pose.inverse().print("Checkerboard offset\n");
00081 Vector3 p = sol.cb_T_ee_pose.translation().vector();
00082 Quaternion q = sol.cb_T_ee_pose.rotation().toQuaternion();
00083 printf("Offset: %4f %4f %4f %4f %4f %4f %4f\n", p[0], p[1], p[2], q.x(), q.y(), q.z(), q.w());
00084 printf("Kinect poses in base frame as Trans-xyz, Quaternion-xyzw\n");
00085
00086 for(int k=0;k<num_kinects;k++) {
00087 p = sol.kinect_T_base_poses[k].inverse().translation().vector();
00088 q = sol.kinect_T_base_poses[k].inverse().rotation().toQuaternion();
00089 printf("Kinect %d: %4f %4f %4f %4f %4f %4f %4f\n", k+1, p[0], p[1], p[2], q.x(), q.y(), q.z(), q.w());
00090 }
00091
00092 return 0;
00093 }