split_bb2_nodelet.cpp
Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2012-2013, Laboratorio de Robotica Movel - ICMC/USP
00003  *  Rafael Luiz Klaser <rlklaser@gmail.com>
00004  *  http://lrm.icmc.usp.br
00005  *
00006  *  Apoio FAPESP: 2012/04555-4
00007  *
00008  *  This program is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00030 #include <ros/ros.h>
00031 #include <nodelet/nodelet.h>
00032 #include <image_transport/image_transport.h>
00033 #include <sensor_msgs/image_encodings.h>
00034 #include <camera_info_manager/camera_info_manager.h>
00035 
00036 #include <lrm_camera_drivers/split_node.h>
00037 
00038 #include <dc1394/dc1394.h>
00039 #include <cstring>
00040 
00041 namespace lrm_camera_drivers {
00042 
00043 class SplitBB2Nodelet: public nodelet::Nodelet {
00044 
00045         struct st_stereo_camera ctx_;
00046 
00047         image_transport::Subscriber sub_;
00048         boost::shared_ptr<image_transport::ImageTransport> it_;
00049 
00050         void publishCam(const sensor_msgs::Image& image);
00051 
00052         bool fillImages(sensor_msgs::Image& left_image, sensor_msgs::Image& right_image, std::string encoding_arg, uint32_t rows_arg, uint32_t cols_arg, const void* data_arg);
00053 
00054         virtual void onInit();
00055         void connectCb();
00056         void imageCb(const sensor_msgs::ImageConstPtr& raw_msg);
00057 };
00058 
00059 void SplitBB2Nodelet::onInit() {
00060         ros::NodeHandle &nh = getNodeHandle();
00061         ros::NodeHandle &nh_priv = getPrivateNodeHandle();
00062 
00063         it_.reset(new image_transport::ImageTransport(nh));
00064 
00065         nh_priv.param("name", ctx_.name, std::string("camera"));
00066         nh_priv.param("frame_id", ctx_.frame_id, std::string(""));
00067 
00068         ros::NodeHandle nh_left(nh.getNamespace() + "/left");
00069         ros::NodeHandle nh_right(nh.getNamespace() + "/right");
00070 
00071         nh_priv.param("left/camera_info_url", ctx_.left.url, std::string(""));
00072         nh_priv.param("right/camera_info_url", ctx_.right.url, std::string(""));
00073 
00074         ctx_.left.publisher = it_->advertiseCamera(nh_left.getNamespace() + "/image_raw", 1);
00075         ctx_.right.publisher = it_->advertiseCamera(nh_right.getNamespace() + "/image_raw", 1);
00076 
00077         ctx_.left.manager.reset(new camera_info_manager::CameraInfoManager(
00078                         nh_left, ctx_.name + "_left", ctx_.left.url));
00079         ctx_.right.manager.reset(new camera_info_manager::CameraInfoManager(
00080                         nh_right, ctx_.name + "_right", ctx_.right.url));
00081 
00082         image_transport::TransportHints hints("raw", ros::TransportHints(), nh);
00083         sub_ = it_->subscribe("camera/image_raw", 1, &SplitBB2Nodelet::imageCb, this, hints);
00084 }
00085 
00086 void SplitBB2Nodelet::connectCb() {
00087 }
00088 
00089 void SplitBB2Nodelet::imageCb(const sensor_msgs::ImageConstPtr& raw_msg) {
00090         publishCam(*raw_msg);
00091 }
00092 
00093 bool SplitBB2Nodelet::fillImages(sensor_msgs::Image& left_image, sensor_msgs::Image&
00094                 right_image, std::string encoding_arg, uint32_t rows_arg, uint32_t cols_arg, const void* data_arg) {
00095 
00096         uint32_t step_arg = cols_arg * sensor_msgs::image_encodings::numChannels(encoding_arg);
00097 
00098         size_t st0 = (step_arg * rows_arg);
00099         left_image.encoding = encoding_arg;
00100         left_image.height = rows_arg;
00101         left_image.width = cols_arg;
00102         left_image.step = cols_arg;
00103         left_image.data.resize(st0); //*2
00104 
00105         right_image.encoding = encoding_arg;
00106         right_image.height = rows_arg;
00107         right_image.width = cols_arg;
00108         right_image.step = cols_arg;
00109         right_image.data.resize(st0);
00110 
00111         uint8_t* crt = (uint8_t*) data_arg;
00112         uint8_t* left = (uint8_t*) &left_image.data[0];
00113         uint8_t* right = (uint8_t*) &right_image.data[0];
00114 
00115         int k = (cols_arg * rows_arg);
00116 
00117         //dc1394_deinterlace_stereo(crt, left, cols_arg, rows_arg);
00118         //memcpy(right, left+k, k);
00119 
00120         //deinterlace stereo
00121         while (k > 0) {
00122                 *(left++) = *(crt++);
00123                 *(right++) = *(crt++);
00124                 k--;
00125         }
00126 
00127         left_image.is_bigendian = 0;
00128         right_image.is_bigendian = 0;
00129         return true;
00130 }
00131 
00132 void SplitBB2Nodelet::publishCam(const sensor_msgs::Image& image) {
00133         uint32_t imHeight = image.height;
00134         uint32_t imWidth = image.width;
00135         const void *imRaw = image.data.data();
00136 
00137         std::string encoding = sensor_msgs::image_encodings::BAYER_BGGR8;
00138 
00139         fillImages(ctx_.left.image, ctx_.right.image, encoding, imHeight, imWidth, imRaw);
00140 
00141         // Update diagnostics and publish
00142         ros::Time stamp = image.header.stamp;
00143 
00144         ctx_.frame_id = ctx_.frame_id == "" ? image.header.frame_id : ctx_.frame_id;
00145 
00146         ctx_.left.image.header.frame_id = ctx_.frame_id;
00147         ctx_.right.image.header.frame_id = ctx_.frame_id;
00148 
00149         ctx_.left.info = ctx_.left.manager->getCameraInfo();
00150         ctx_.left.info.header.frame_id = ctx_.frame_id;
00151         //ctx_.left.info.header.stamp = stamp;
00152         ctx_.left.info.height = imHeight;
00153         ctx_.left.info.width = imWidth;
00154 
00155         ctx_.right.info = ctx_.right.manager->getCameraInfo();
00156         ctx_.right.info.header.frame_id = ctx_.frame_id;
00157         //ctx_.right.info.header.stamp = stamp;
00158         ctx_.right.info.height = imHeight;
00159         ctx_.right.info.width = imWidth;
00160 
00161         ctx_.left.publisher.publish(ctx_.left.image, ctx_.left.info, stamp);
00162         ctx_.right.publisher.publish(ctx_.right.image, ctx_.right.info, stamp);
00163 }
00164 
00165 };
00166 
00167 #include <pluginlib/class_list_macros.h>
00168 PLUGINLIB_EXPORT_CLASS(lrm_camera_drivers::SplitBB2Nodelet, nodelet::Nodelet)


lrm_camera_drivers
Author(s): Rafael Luiz Klaser
autogenerated on Mon Oct 6 2014 01:52:55