ximea_ros_driver.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 
00003 Copyright 2015  Abdelhamid El-Bably (University of Waterloo)
00004                       [ahelbably@uwaterloo.ca]
00005                 Arun Das (University of Waterloo)
00006                       [adas@uwaterloo.ca]
00007                 Michael Tribou (University of Waterloo)
00008                       [mjtribou@uwaterloo.ca]
00009 
00010 All rights reserved.
00011 
00012 ********************************************************************************/
00013 #include <ximea_camera/ximea_ros_driver.h>
00014 #include <string>
00015 #include <algorithm>
00016 #include <boost/make_shared.hpp>
00017 
00018 ximea_ros_driver::ximea_ros_driver(const ros::NodeHandle &nh, std::string cam_name, int serial_no, std::string yaml_url): ximea_driver(serial_no, cam_name)
00019 {
00020   pnh_ = nh;
00021   cam_info_manager_ = boost::make_shared<camera_info_manager::CameraInfoManager>(pnh_, cam_name_);
00022   cam_info_manager_->loadCameraInfo(yaml_url);
00023   it_ = boost::make_shared<image_transport::ImageTransport>(nh);
00024   ros_cam_pub_ = it_->advertise(std::string("image_raw"), 1);
00025   cam_info_pub_ = pnh_.advertise<sensor_msgs::CameraInfo>(std::string("camera_info"), 1);
00026 }
00027 
00028 ximea_ros_driver::ximea_ros_driver(const ros::NodeHandle &nh, std::string file_name) : ximea_driver(file_name)
00029 {
00030   pnh_ = nh;
00031   cam_info_manager_ = boost::make_shared<camera_info_manager::CameraInfoManager>(pnh_, cam_name_);
00032   cam_info_manager_->loadCameraInfo(yaml_url_);
00033   it_ = boost::make_shared<image_transport::ImageTransport>(nh);
00034   ros_cam_pub_ = it_->advertise(std::string("image_raw"), 1);
00035   cam_info_pub_ = pnh_.advertise<sensor_msgs::CameraInfo>(std::string("camera_info"), 1);
00036 }
00037 
00038 void ximea_ros_driver::common_initialize(const ros::NodeHandle &nh)
00039 {
00040   pnh_ = nh;
00041   cam_info_manager_ = boost::make_shared<camera_info_manager::CameraInfoManager>(pnh_, cam_name_);
00042   cam_info_manager_->loadCameraInfo("");  // TODO: yaml_url
00043   it_ = boost::make_shared<image_transport::ImageTransport>(nh);
00044   ros_cam_pub_ = it_->advertise(cam_name_ + std::string("/image_raw"), 1);
00045   cam_info_pub_ = pnh_.advertise<sensor_msgs::CameraInfo>(cam_name_ + std::string("/camera_info"), 1);
00046 }
00047 
00048 void ximea_ros_driver::publishImage(const ros::Time & now)
00049 {
00050   cam_buffer_ = reinterpret_cast<char *>(image_.bp);
00051   cam_buffer_size_ = image_.width * image_.height * bpp_;
00052   ros_image_.data.resize(cam_buffer_size_);
00053   ros_image_.encoding = encoding_;
00054   ros_image_.width = image_.width;
00055   ros_image_.height = image_.height;
00056   ros_image_.step = image_.width * bpp_;
00057 
00058   copy(reinterpret_cast<char *>(cam_buffer_),
00059        (reinterpret_cast<char *>(cam_buffer_)) + cam_buffer_size_,
00060        ros_image_.data.begin());
00061 
00062   ros_cam_pub_.publish(ros_image_);
00063 }
00064 
00065 void ximea_ros_driver::publishCamInfo(const ros::Time &now)
00066 {
00067   ros_image_.header.stamp = now;
00068   cam_info_ = cam_info_manager_->getCameraInfo();
00069   cam_info_.header.frame_id = frame_id_;
00070   cam_info_pub_.publish(cam_info_);
00071 }
00072 
00073 void ximea_ros_driver::publishImageAndCamInfo()
00074 {
00075   ros::Time now = ros::Time::now();
00076   publishImage(now);
00077   publishCamInfo(now);
00078 }
00079 
00080 void ximea_ros_driver::setImageDataFormat(std::string image_format)
00081 {
00082   XI_RETURN stat;
00083   int image_data_format;
00084 
00085   if (!hasValidHandle())
00086   {
00087     return;
00088   }
00089   if (image_format == std::string("XI_MONO16"))
00090   {
00091     image_data_format = XI_MONO16;
00092     encoding_ = std::string("mono16");
00093     bpp_ = 2;
00094   }
00095 
00096   else if (image_format == std::string("XI_RGB24"))
00097   {
00098     image_data_format = XI_RGB24;
00099     encoding_ = std::string("bgr8");
00100     bpp_ = 3;
00101   }
00102 
00103   else if (image_format == std::string("XI_RGB32"))
00104   {
00105     image_data_format = XI_RGB32;
00106     encoding_ = std::string("bgr16");
00107     bpp_ = 3;
00108   }
00109 
00110   else if (image_format == std::string("XI_RGB_PLANAR"))
00111   {
00112     image_data_format = XI_MONO8;
00113     std::cout << "This is unsupported in ROS default to XI_MONO8" << std::endl;
00114     bpp_ = 1;
00115   }
00116 
00117   else if (image_format == std::string("XI_RAW8"))
00118   {
00119     image_data_format = XI_RAW8;
00120     encoding_ = std::string("mono8");
00121     bpp_ = 1;
00122   }
00123 
00124   else if (image_format == std::string("XI_RAW16"))
00125   {
00126     image_data_format = XI_RAW16;
00127     encoding_ = std::string("mono16");
00128     bpp_ = 2;
00129   }
00130 
00131   else
00132   {
00133     image_data_format = XI_MONO8;
00134     encoding_ = std::string("mono8");
00135     bpp_ = 1;
00136   }
00137 
00138   stat = xiSetParamInt(xiH_, XI_PRM_IMAGE_DATA_FORMAT, image_data_format);
00139   errorHandling(stat, "image_format");    // if we cannot set the format then there is something wrong we should probably quit then
00140   image_data_format_ = image_data_format;
00141 }


ximea_camera
Author(s): Abdelhamid El-Bably, Arun Das, Michael Tribou
autogenerated on Thu Jun 6 2019 21:17:12