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 #include "jsk_libfreenect2/driver.h"
00026 #include <cv_bridge/cv_bridge.h>
00027 #include <std_msgs/Header.h>
00028 #include <sensor_msgs/distortion_models.h>
00029 #include <limits>
00030 #include <boost/tuple/tuple.hpp>
00031 #include <boost/tuple/tuple_io.hpp>
00032
00033 namespace jsk_libfreenect2
00034 {
00035 void Driver::onInit()
00036 {
00037 glfwInit();
00038 timer_ = getNodeHandle().createTimer(
00039 ros::Duration(5.0),
00040 boost::bind(&Driver::run, this, _1), true);
00041 }
00042
00043 void Driver::run(const ros::TimerEvent&) {
00044 freenect2_ = new libfreenect2::Freenect2();
00045 dev_ = freenect2_->openDefaultDevice();
00046 if (dev_ == 0)
00047 {
00048 NODELET_FATAL("[Driver::run] no device connected");
00049 return;
00050 }
00051 listener_ = new libfreenect2::SyncMultiFrameListener(
00052 libfreenect2::Frame::Color |
00053 libfreenect2::Frame::Ir |
00054 libfreenect2::Frame::Depth);
00055 dev_->setColorFrameListener(listener_);
00056 dev_->setIrAndDepthFrameListener(listener_);
00057 dev_->start();
00058 NODELET_INFO_STREAM("[Driver::run] device serial: "
00059 << dev_->getSerialNumber());
00060 NODELET_INFO_STREAM("[Driver::run] device firmware: "
00061 << dev_->getFirmwareVersion());
00062
00063
00064
00065 ros::NodeHandle nh = getNodeHandle();
00066 ros::NodeHandle pnh = getPrivateNodeHandle();
00067 image_transport::ImageTransport it(pnh);
00068 ros::NodeHandle rgb_nh(nh, "rgb");
00069 image_transport::ImageTransport rgb_it(rgb_nh);
00070 ros::NodeHandle ir_nh(nh, "ir");
00071 image_transport::ImageTransport ir_it(ir_nh);
00072 ros::NodeHandle depth_nh(nh, "depth");
00073 image_transport::ImageTransport depth_it(depth_nh);
00074 ros::NodeHandle depth_registered_nh(nh, "depth_registered");
00075 image_transport::ImageTransport depth_registered_it(depth_registered_nh);
00076 ros::NodeHandle projector_nh(nh, "projector");
00077
00078
00079
00080
00081
00082
00083 std::string rgb_frame_id, depth_frame_id;
00084 pnh.param("rgb_frame_id", rgb_frame_id, std::string("/openni_rgb_optical_frame"));
00085 pnh.param("depth_frame_id", depth_frame_id, std::string("/openni_depth_optical_frame"));
00086
00087 default_rgb_params_ = dev_->getColorCameraParams();
00088 default_ir_params_ = dev_->getIrCameraParams();
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 libfreenect2::FrameMap frames;
00099 image_transport::CameraPublisher depth_pub
00100 = depth_it.advertiseCamera("image_raw", 1);
00101 image_transport::CameraPublisher ir_pub
00102 = ir_it.advertiseCamera("image_raw", 1);
00103 image_transport::CameraPublisher color_pub
00104 = rgb_it.advertiseCamera("image_raw", 1);
00105
00106 while(ros::ok())
00107 {
00108 listener_->waitForNewFrame(frames);
00109 {
00110 boost::mutex::scoped_lock lock(mutex_);
00111 libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color];
00112 libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir];
00113 libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth];
00114 cv::Mat cvrgb(rgb->height, rgb->width, CV_8UC3, rgb->data);
00115 cv::Mat cvir(ir->height, ir->width, CV_32FC1, ir->data);
00116 cv::Mat cvdepth(depth->height, depth->width, CV_32FC1, depth->data);
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 ros::Time stamp = ros::Time::now();
00144 std_msgs::Header rgb_header, ir_header;
00145 rgb_header.stamp = stamp;
00146 ir_header.stamp = stamp;
00147 rgb_header.frame_id = rgb_frame_id;
00148 ir_header.frame_id = depth_frame_id;
00149 cv_bridge::CvImage rgb_cvimage(rgb_header, "bgr8", cvrgb);
00150 color_pub.publish(rgb_cvimage.toImageMsg(), getRGBCameraInfo(rgb, stamp, rgb_frame_id));
00151
00152
00153 cv_bridge::CvImage depth_cvimage(ir_header, "32FC1", cvdepth / 1000.0);
00154 depth_pub.publish(depth_cvimage.toImageMsg(), getIRCameraInfo(depth, stamp, depth_frame_id));
00155
00156
00157 cv_bridge::CvImage ir_cvimage(ir_header, "32FC1", cvir / 1000.0);
00158 ir_pub.publish(ir_cvimage.toImageMsg(), getIRCameraInfo(ir, stamp, depth_frame_id));
00159
00160 int key = cv::waitKey(1);
00161 listener_->release(frames);
00162 }
00163 }
00164 dev_->stop();
00165 dev_->close();
00166 }
00167
00168 sensor_msgs::CameraInfo::Ptr Driver::getIRCameraInfo(
00169 libfreenect2::Frame* frame, ros::Time stamp, std::string frame_id)
00170 {
00171 if (ir_caminfo_manager_ && ir_caminfo_manager_->isCalibrated()) {
00172 sensor_msgs::CameraInfo info = ir_caminfo_manager_->getCameraInfo();
00173 info.header.stamp = stamp;
00174 info.header.frame_id = frame_id;
00175 return boost::make_shared<sensor_msgs::CameraInfo>(info);
00176 }
00177 else {
00178 sensor_msgs::CameraInfo info;
00179 info.width = frame->width;
00180 info.height = frame->height;
00181 info.header.stamp = stamp;
00182 info.header.frame_id = frame_id;
00183 info.D.resize(5, 0.0);
00184 info.K.assign(0.0);
00185 info.R.assign(0.0);
00186 info.P.assign(0.0);
00187 info.distortion_model = sensor_msgs::distortion_models::PLUMB_BOB;
00188 info.D[0] = default_ir_params_.k1;
00189 info.D[1] = default_ir_params_.k2;
00190 info.D[2] = default_ir_params_.p1;
00191 info.D[3] = default_ir_params_.p2;
00192 info.D[4] = default_ir_params_.k3;
00193 info.K[0] = info.P[0] = default_ir_params_.fx;
00194 info.K[2] = info.P[2] = default_ir_params_.cx;
00195 info.K[4] = info.P[5] = default_ir_params_.fy;
00196 info.K[5] = info.P[6] = default_ir_params_.cy;
00197 info.K[8] = info.P[10] = 1.0;
00198
00199 info.R[0] = info.R[4] = info.R[8] = 1.0;
00200 return boost::make_shared<sensor_msgs::CameraInfo>(info);
00201 }
00202 }
00203
00204 sensor_msgs::CameraInfo::Ptr Driver::getRGBCameraInfo(
00205 libfreenect2::Frame* frame, ros::Time stamp, std::string frame_id)
00206 {
00207 if (rgb_caminfo_manager_ && rgb_caminfo_manager_->isCalibrated()) {
00208 sensor_msgs::CameraInfo info = rgb_caminfo_manager_->getCameraInfo();
00209 info.header.stamp = stamp;
00210 info.header.frame_id = frame_id;
00211 return boost::make_shared<sensor_msgs::CameraInfo>(info);
00212 }
00213 else {
00214 sensor_msgs::CameraInfo info;
00215 info.width = frame->width;
00216 info.height = frame->height;
00217 info.header.stamp = stamp;
00218 info.header.frame_id = frame_id;
00219 info.D.resize(5, 0.0);
00220 info.K.assign(0.0);
00221 info.R.assign(0.0);
00222 info.P.assign(0.0);
00223 info.distortion_model = sensor_msgs::distortion_models::PLUMB_BOB;
00224
00225 info.K[0] = info.P[0] = default_rgb_params_.fx;
00226 info.K[2] = info.P[2] = default_rgb_params_.cx;
00227 info.K[4] = info.P[5] = default_rgb_params_.fy;
00228 info.K[5] = info.P[6] = default_rgb_params_.cy;
00229 info.K[8] = info.P[10] = 1.0;
00230
00231 info.R[0] = info.R[4] = info.R[8] = 1.0;
00232 return boost::make_shared<sensor_msgs::CameraInfo>(info);
00233 }
00234 }
00235
00236 }
00237
00238 #include <pluginlib/class_list_macros.h>
00239 typedef jsk_libfreenect2::Driver Driver;
00240 PLUGINLIB_DECLARE_CLASS (jsk_libfreenect2, Driver, Driver, nodelet::Nodelet);
00241