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 #include <ros/ros.h>
00031 #include <image_transport/image_transport.h>
00032
00033 #include <cv_bridge/cv_bridge.h>
00034 #include <image_geometry/pinhole_camera_model.h>
00035 #include <tf/transform_listener.h>
00036 #include <tf/transform_broadcaster.h>
00037
00038 #include <pcl/point_types.h>
00039 #include <pcl_ros/point_cloud.h>
00040 #include <pcl_ros/transforms.h>
00041 #include <pcl/registration/registration.h>
00042 #include <pcl/registration/transformation_estimation_svd.h>
00043
00044 #include <sensor_msgs/Image.h>
00045 #include <sensor_msgs/CameraInfo.h>
00046 #include <geometry_msgs/PointStamped.h>
00047
00048 #include <turtlebot_kinect_arm_calibration/detect_calibration_pattern.h>
00049
00050 using namespace std;
00051 using namespace Eigen;
00052
00053 tf::Transform tfFromEigen(Eigen::Matrix4f trans)
00054 {
00055 tf::Matrix3x3 btm;
00056 btm.setValue(trans(0,0),trans(0,1),trans(0,2),
00057 trans(1,0),trans(1,1),trans(1,2),
00058 trans(2,0),trans(2,1),trans(2,2));
00059 tf::Transform ret;
00060 ret.setOrigin(tf::Vector3(trans(0,3),trans(1,3),trans(2,3)));
00061 ret.setBasis(btm);
00062 return ret;
00063 }
00064
00065 Eigen::Matrix4f EigenFromTF(tf::Transform trans)
00066 {
00067 Eigen::Matrix4f out;
00068 tf::Quaternion quat = trans.getRotation();
00069 tf::Vector3 origin = trans.getOrigin();
00070
00071 Eigen::Quaternionf quat_out(quat.w(), quat.x(), quat.y(), quat.z());
00072 Eigen::Vector3f origin_out(origin.x(), origin.y(), origin.z());
00073
00074 out.topLeftCorner<3,3>() = quat_out.toRotationMatrix();
00075 out.topRightCorner<3,1>() = origin_out;
00076 out(3,3) = 1;
00077
00078 return out;
00079 }
00080
00081
00082 class CalibrateKinectCheckerboard
00083 {
00084
00085 ros::NodeHandle nh_;
00086 image_transport::ImageTransport it_;
00087 image_transport::Publisher pub_;
00088 ros::Publisher detector_pub_;
00089 ros::Publisher physical_pub_;
00090
00091
00092 ros::Subscriber image_sub_;
00093 ros::Subscriber info_sub_;
00094
00095
00096 cv_bridge::CvImagePtr input_bridge_;
00097 cv_bridge::CvImagePtr output_bridge_;
00098 tf::TransformListener tf_listener_;
00099 tf::TransformBroadcaster tf_broadcaster_;
00100 image_geometry::PinholeCameraModel cam_model_;
00101
00102
00103 PatternDetector pattern_detector_;
00104
00105
00106 Eigen::Transform<float, 3, Eigen::Affine> transform_;
00107
00108
00109 pcl::PointCloud<pcl::PointXYZ> detector_points_;
00110 pcl::PointCloud<pcl::PointXYZ> ideal_points_;
00111 pcl::PointCloud<pcl::PointXYZ> image_points_;
00112 pcl::PointCloud<pcl::PointXYZ> physical_points_;
00113
00114
00115 bool calibrated;
00116
00117 ros::Timer timer_;
00118
00119
00120 std::string fixed_frame;
00121 std::string camera_frame;
00122 std::string target_frame;
00123 std::string tip_frame;
00124 std::string touch_frame;
00125
00126 int checkerboard_width;
00127 int checkerboard_height;
00128 double checkerboard_grid;
00129
00130
00131 geometry_msgs::PointStamped gripper_tip;
00132
00133 public:
00134 CalibrateKinectCheckerboard()
00135 : nh_("~"), it_(nh_), calibrated(false)
00136 {
00137
00138 nh_.param<std::string>("fixed_frame", fixed_frame, "/base_link");
00139 nh_.param<std::string>("camera_frame", camera_frame, "/camera_link");
00140 nh_.param<std::string>("target_frame", target_frame, "/calibration_pattern");
00141 nh_.param<std::string>("tip_frame", tip_frame, "/gripper_link");
00142
00143 nh_.param<int>("checkerboard_width", checkerboard_width, 6);
00144 nh_.param<int>("checkerboard_height", checkerboard_height, 7);
00145 nh_.param<double>("checkerboard_grid", checkerboard_grid, 0.027);
00146
00147
00148 pattern_detector_.setPattern(cv::Size(checkerboard_width, checkerboard_height), checkerboard_grid, CHESSBOARD);
00149
00150 transform_.translation().setZero();
00151 transform_.matrix().topLeftCorner<3, 3>() = Quaternionf().setIdentity().toRotationMatrix();
00152
00153
00154 info_sub_ = nh_.subscribe("/camera/rgb/camera_info", 1, &CalibrateKinectCheckerboard::infoCallback, this);
00155
00156
00157 pub_ = it_.advertise("calibration_pattern_out", 1);
00158 detector_pub_ = nh_.advertise<pcl::PointCloud<pcl::PointXYZ> >("detector_cloud", 1);
00159 physical_pub_ = nh_.advertise<pcl::PointCloud<pcl::PointXYZ> >("physical_points_cloud", 1);
00160
00161
00162 ideal_points_.push_back( pcl::PointXYZ(0, 0, 0) );
00163 ideal_points_.push_back( pcl::PointXYZ((checkerboard_width-1)*checkerboard_grid, 0, 0) );
00164 ideal_points_.push_back( pcl::PointXYZ(0, (checkerboard_height-1)*checkerboard_grid, 0) );
00165 ideal_points_.push_back( pcl::PointXYZ((checkerboard_width-1)*checkerboard_grid, (checkerboard_height-1)*checkerboard_grid, 0) );
00166
00167
00168 nh_.param<double>("gripper_tip_x", gripper_tip.point.x, 0.0);
00169 nh_.param<double>("gripper_tip_y", gripper_tip.point.y, 0.0);
00170 nh_.param<double>("gripper_tip_z", gripper_tip.point.z, 0.0);
00171 gripper_tip.header.frame_id = tip_frame;
00172
00173 ROS_INFO("[calibrate] Initialized.");
00174 }
00175
00176 void infoCallback(const sensor_msgs::CameraInfoConstPtr& info_msg)
00177 {
00178 if (calibrated)
00179 return;
00180 cam_model_.fromCameraInfo(info_msg);
00181 pattern_detector_.setCameraMatrices(cam_model_.intrinsicMatrix(), cam_model_.distortionCoeffs());
00182
00183 calibrated = true;
00184 image_sub_ = nh_.subscribe("/camera/rgb/image_mono", 1, &CalibrateKinectCheckerboard::imageCallback, this);
00185
00186 ROS_INFO("[calibrate] Got image info!");
00187 }
00188
00189 void pointcloudCallback(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& msg)
00190 {
00191 sensor_msgs::ImagePtr image_msg(new sensor_msgs::Image);
00192 pcl::toROSMsg (*msg, *image_msg);
00193
00194 imageCallback(image_msg);
00195 }
00196
00197 void imageCallback(const sensor_msgs::ImageConstPtr& image_msg)
00198 {
00199 try
00200 {
00201 input_bridge_ = cv_bridge::toCvCopy(image_msg, "mono8");
00202 output_bridge_ = cv_bridge::toCvCopy(image_msg, "bgr8");
00203 }
00204 catch (cv_bridge::Exception& ex)
00205 {
00206 ROS_ERROR("[calibrate] Failed to convert image");
00207 return;
00208 }
00209
00210 Eigen::Vector3f translation;
00211 Eigen::Quaternionf orientation;
00212
00213 if (!pattern_detector_.detectPattern(input_bridge_->image, translation, orientation, output_bridge_->image))
00214 {
00215 ROS_INFO("[calibrate] Couldn't detect checkerboard, make sure it's visible in the image.");
00216 return;
00217 }
00218
00219 tf::Transform target_transform;
00220 tf::StampedTransform base_transform;
00221 try
00222 {
00223 ros::Time acquisition_time = image_msg->header.stamp;
00224 ros::Duration timeout(1.0 / 30.0);
00225
00226 target_transform.setOrigin( tf::Vector3(translation.x(), translation.y(), translation.z()) );
00227 target_transform.setRotation( tf::Quaternion(orientation.x(), orientation.y(), orientation.z(), orientation.w()) );
00228 tf_broadcaster_.sendTransform(tf::StampedTransform(target_transform, image_msg->header.stamp, image_msg->header.frame_id, target_frame));
00229 }
00230 catch (tf::TransformException& ex)
00231 {
00232 ROS_WARN("[calibrate] TF exception:\n%s", ex.what());
00233 return;
00234 }
00235 publishCloud(ideal_points_, target_transform, image_msg->header.frame_id);
00236
00237 overlayPoints(ideal_points_, target_transform, output_bridge_);
00238
00239
00240 pub_.publish(output_bridge_->toImageMsg());
00241
00242 pcl_ros::transformPointCloud(ideal_points_, image_points_, target_transform);
00243
00244 cout << "Got an image callback!" << endl;
00245
00246 calibrate(image_msg->header.frame_id);
00247
00248 ros::shutdown();
00249 }
00250
00251
00252 void publishCloud(pcl::PointCloud<pcl::PointXYZ> detector_points, tf::Transform &transform, const std::string frame_id)
00253 {
00254
00255 pcl::PointCloud<pcl::PointXYZ> transformed_detector_points;
00256
00257 pcl_ros::transformPointCloud(detector_points, transformed_detector_points, transform);
00258
00259 transformed_detector_points.header.frame_id = frame_id;
00260 detector_pub_.publish(transformed_detector_points);
00261 }
00262
00263 void overlayPoints(pcl::PointCloud<pcl::PointXYZ> detector_points, tf::Transform &transform, cv_bridge::CvImagePtr& image)
00264 {
00265
00266 pcl::PointCloud<pcl::PointXYZ> transformed_detector_points;
00267
00268 pcl_ros::transformPointCloud(detector_points, transformed_detector_points, transform);
00269
00270 int font_face = cv::FONT_HERSHEY_SCRIPT_SIMPLEX;
00271 double font_scale = 1;
00272 int thickness = 2;
00273 int radius = 5;
00274
00275 for (unsigned int i=0; i < transformed_detector_points.size(); i++)
00276 {
00277 pcl::PointXYZ pt = transformed_detector_points[i];
00278 cv::Point3d pt_cv(pt.x, pt.y, pt.z);
00279 cv::Point2d uv;
00280 uv = cam_model_.project3dToPixel(pt_cv);
00281
00282 cv::circle(image->image, uv, radius, CV_RGB(255,0,0), -1);
00283 cv::Size text_size;
00284 int baseline = 0;
00285 std::stringstream out;
00286 out << i+1;
00287
00288 text_size = cv::getTextSize(out.str(), font_face, font_scale, thickness, &baseline);
00289
00290 cv::Point origin = cvPoint(uv.x - text_size.width / 2,
00291 uv.y - radius - baseline);
00292 cv::putText(image->image, out.str(), origin, font_face, font_scale, CV_RGB(255,0,0), thickness);
00293 }
00294 }
00295
00296 bool calibrate(const std::string frame_id)
00297 {
00298 physical_points_.empty();
00299 physical_points_.header.frame_id = fixed_frame;
00300 cout << "Is the checkerboard correct? " << endl;
00301 cout << "Move edge of gripper to point 1 in image and press Enter. " << endl;
00302 cin.ignore();
00303 addPhysicalPoint();
00304 cout << "Move edge of gripper to point 2 in image and press Enter. " << endl;
00305 cin.ignore();
00306 addPhysicalPoint();
00307 cout << "Move edge of gripper to point 3 in image and press Enter. " << endl;
00308 cin.ignore();
00309 addPhysicalPoint();
00310 cout << "Move edge of gripper to point 4 in image and press Enter. " << endl;
00311 cin.ignore();
00312 addPhysicalPoint();
00313
00314 Eigen::Matrix4f t;
00315
00316 physical_pub_.publish(physical_points_);
00317
00318 pcl::registration::TransformationEstimationSVD<pcl::PointXYZ, pcl::PointXYZ> svd_estimator;
00319 svd_estimator.estimateRigidTransformation( physical_points_, image_points_, t );
00320
00321
00322 tf::Transform transform = tfFromEigen(t), trans_full, camera_transform_unstamped;
00323 tf::StampedTransform camera_transform;
00324
00325 cout << "Resulting transform (camera frame -> fixed frame): " << endl << t << endl << endl;
00326
00327 try
00328 {
00329 tf_listener_.lookupTransform(frame_id, camera_frame, ros::Time(0), camera_transform);
00330 }
00331 catch (tf::TransformException& ex)
00332 {
00333 ROS_WARN("[calibrate] TF exception:\n%s", ex.what());
00334 return false;
00335 }
00336
00337 camera_transform_unstamped = camera_transform;
00338 trans_full = camera_transform_unstamped.inverse()*transform;
00339
00340 Eigen::Matrix4f t_full = EigenFromTF(trans_full);
00341 Eigen::Matrix4f t_full_inv = (Eigen::Transform<float,3,Affine>(t_full).inverse()).matrix();
00342
00343 cout << "Resulting transform (fixed frame -> camera frame): " << endl << t_full << endl << endl;
00344 printStaticTransform(t_full_inv, fixed_frame, camera_frame);
00345
00346 return true;
00347 }
00348
00349 void printStaticTransform(Eigen::Matrix4f& transform, const std::string frame1, const std::string frame2)
00350 {
00351 Eigen::Quaternionf quat(transform.topLeftCorner<3,3>() );
00352 Eigen::Vector3f translation(transform.topRightCorner<3,1>() );
00353
00354 cout << "Static transform publisher (use for external kinect): " << endl;
00355
00356 cout << "rosrun tf static_transform_publisher x y z qx qy qz qw frame_id child_frame_id period_in_ms" << endl;
00357 cout << "rosrun tf static_transform_publisher " << translation.x() << " "
00358 << translation.y() << " " << translation.z() << " "
00359 << quat.x() << " " << quat.y() << " " << quat.z() << " " << quat.w() << " "
00360 << frame1 << " " << frame2 << " 100" << endl << endl;
00361
00362 tf::Transform temp_tf_trans = tfFromEigen(transform);
00363
00364 double yaw, pitch, roll;
00365
00366 std::string fixed_frame_urdf(fixed_frame);
00367
00368
00369
00370 if (fixed_frame_urdf.size() > 0 && fixed_frame_urdf[0] == '/')
00371 fixed_frame_urdf.erase(0, 1);
00372
00373 temp_tf_trans.getBasis().getEulerYPR(yaw, pitch, roll);
00374
00375 cout << "URDF output (use for kinect on robot): " << endl;
00376
00377 cout << "<?xml version=\"1.0\"?>\n<robot>\n" <<
00378 "\t<property name=\"turtlebot_calib_cam_x\" value=\"" << translation.x() << "\" />\n" <<
00379 "\t<property name=\"turtlebot_calib_cam_y\" value=\"" << translation.y() << "\" />\n" <<
00380 "\t<property name=\"turtlebot_calib_cam_z\" value=\"" << translation.z() << "\" />\n" <<
00381 "\t<property name=\"turtlebot_calib_cam_rr\" value=\"" << roll << "\" />\n" <<
00382 "\t<property name=\"turtlebot_calib_cam_rp\" value=\"" << pitch << "\" />\n" <<
00383 "\t<property name=\"turtlebot_calib_cam_ry\" value=\"" << yaw << "\" />\n" <<
00384 "\t<property name=\"turtlebot_kinect_frame_name\" value=\"" << fixed_frame_urdf << "\" />\n" <<
00385 "</robot>" << endl << endl;
00386 }
00387
00388 void addPhysicalPoint()
00389 {
00390 geometry_msgs::PointStamped pt_out;
00391
00392 try
00393 {
00394 tf_listener_.transformPoint(fixed_frame, gripper_tip, pt_out);
00395 }
00396 catch (tf::TransformException& ex)
00397 {
00398 ROS_WARN("[calibrate] TF exception:\n%s", ex.what());
00399 return;
00400 }
00401
00402 physical_points_.push_back(pcl::PointXYZ(pt_out.point.x, pt_out.point.y, pt_out.point.z));
00403 }
00404
00405 void convertIdealPointstoPointcloud()
00406 {
00407 detector_points_.points.resize(pattern_detector_.ideal_points.size());
00408 for (unsigned int i=0; i < pattern_detector_.ideal_points.size(); i++)
00409 {
00410 cv::Point3f pt = pattern_detector_.ideal_points[i];
00411 detector_points_[i].x = pt.x; detector_points_[i].y = pt.y; detector_points_[i].z = pt.z;
00412 }
00413 }
00414
00415 };
00416
00417
00418 int main(int argc, char** argv)
00419 {
00420 ros::init(argc, argv, "calibrate_kinect_arm");
00421
00422 CalibrateKinectCheckerboard cal;
00423 ros::spin();
00424 }
00425