color_camera.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 //##################
00019 //#### includes ####
00020 
00021 // standard includes
00022 //--
00023 
00024 // ROS includes
00025 #include <ros/ros.h>
00026 #include <polled_camera/publication_server.h>
00027 #include <cv_bridge/CvBridge.h>
00028 
00029 // ROS message includes
00030 #include <sensor_msgs/Image.h>
00031 #include <sensor_msgs/CameraInfo.h>
00032 #include <sensor_msgs/fill_image.h>
00033 #include <sensor_msgs/SetCameraInfo.h>
00034 
00035 // external includes
00036 #include <cob_camera_sensors/AbstractColorCamera.h>
00037 #include <cob_vision_utils/CameraSensorToolbox.h>
00038 #include <cob_vision_utils/GlobalDefines.h>
00039 
00040 using namespace ipa_CameraSensors;
00041 
00044 class CobColorCameraNode
00045 {
00046 private:
00047         ros::NodeHandle node_handle_;
00048         polled_camera::PublicationServer image_poll_server_;
00049 
00050         AbstractColorCameraPtr color_camera_;   
00051 
00052         std::string config_directory_; 
00053         int camera_index_;      
00054         int color_camera_intrinsic_id_; 
00055         ipa_CameraSensors::t_cameraType color_camera_intrinsic_type_;   
00056 
00057         sensor_msgs::CameraInfo camera_info_msg_;       
00058 
00059         ros::ServiceServer camera_info_service_;
00060 
00061         cv::Mat color_image_8U3_;
00062 
00063 public:
00064         CobColorCameraNode(const ros::NodeHandle& node_handle)
00065         : node_handle_(node_handle),
00066           color_camera_(AbstractColorCameraPtr()),
00067           color_image_8U3_(cv::Mat())
00068         {
00070         }
00071 
00072         ~CobColorCameraNode()
00073         {
00074                 image_poll_server_.shutdown();
00075                 color_camera_->Close();
00076         }
00077 
00079         bool init()
00080         {
00081                 if (loadParameters() == false)
00082                 {
00083                         ROS_ERROR("[color_camera] Could not read all parameters from launch file");
00084                         return false;
00085                 }
00086 
00087                 if (color_camera_->Init(config_directory_, camera_index_) & ipa_CameraSensors::RET_FAILED)
00088                 {
00089                         std::stringstream ss;
00090                         ss << "initialization of color camera ";
00091                         ss << camera_index_;
00092                         ss << " failed";
00093                         ROS_ERROR("[color_camera] %s", ss.str().c_str());
00094                         color_camera_->Close();
00095                         color_camera_ = AbstractColorCameraPtr();
00096                         return false;
00097                 }
00098 
00099                 if (color_camera_ && (color_camera_->Open() & ipa_CameraSensors::RET_FAILED))
00100                 {
00101                         std::stringstream ss;
00102                         ss << "Could not open color camera ";
00103                         ss << camera_index_;
00104                         ROS_ERROR("[color_camera] %s", ss.str().c_str());
00105                         color_camera_->Close();
00106                         color_camera_ = AbstractColorCameraPtr();
00107                         return false;
00108                 }
00109 
00111                 ipa_CameraSensors::t_cameraProperty cameraProperty;
00112                 cameraProperty.propertyID = ipa_CameraSensors::PROP_CAMERA_RESOLUTION;
00113                 color_camera_->GetProperty(&cameraProperty);
00114                 int color_sensor_width = cameraProperty.cameraResolution.xResolution;
00115                 int color_sensor_height = cameraProperty.cameraResolution.yResolution;
00116                 cv::Size color_image_size(color_sensor_width, color_sensor_height);
00117 
00119                 ipa_CameraSensors::CameraSensorToolboxPtr color_sensor_toolbox = ipa_CameraSensors::CreateCameraSensorToolbox();
00120                 color_sensor_toolbox->Init(config_directory_, color_camera_->GetCameraType(), camera_index_, color_image_size);
00121 
00122                 cv::Mat d = color_sensor_toolbox->GetDistortionParameters(color_camera_intrinsic_type_, color_camera_intrinsic_id_);
00123                 camera_info_msg_.header.stamp = ros::Time::now();
00124                 if (camera_index_ == 0)
00125                         camera_info_msg_.header.frame_id = "head_color_camera_r_link";
00126                 else
00127                         camera_info_msg_.header.frame_id = "head_color_camera_l_link";
00128                 camera_info_msg_.D[0] = d.at<double>(0, 0);
00129                 camera_info_msg_.D[1] = d.at<double>(0, 1);
00130                 camera_info_msg_.D[2] = d.at<double>(0, 2);
00131                 camera_info_msg_.D[3] = d.at<double>(0, 3);
00132                 camera_info_msg_.D[4] = 0;
00133 
00134                 cv::Mat k = color_sensor_toolbox->GetIntrinsicMatrix(color_camera_intrinsic_type_, color_camera_intrinsic_id_);
00135                 camera_info_msg_.K[0] = k.at<double>(0, 0);
00136                 camera_info_msg_.K[1] = k.at<double>(0, 1);
00137                 camera_info_msg_.K[2] = k.at<double>(0, 2);
00138                 camera_info_msg_.K[3] = k.at<double>(1, 0);
00139                 camera_info_msg_.K[4] = k.at<double>(1, 1);
00140                 camera_info_msg_.K[5] = k.at<double>(1, 2);
00141                 camera_info_msg_.K[6] = k.at<double>(2, 0);
00142                 camera_info_msg_.K[7] = k.at<double>(2, 1);
00143                 camera_info_msg_.K[8] = k.at<double>(2, 2);
00144 
00145                 camera_info_msg_.width = color_sensor_width;
00146                 camera_info_msg_.height = color_sensor_height;
00147 
00149                 camera_info_service_ = node_handle_.advertiseService("set_camera_info", &CobColorCameraNode::setCameraInfo, this);
00150 
00152                 image_poll_server_ = polled_camera::advertise(node_handle_, "request_image", &CobColorCameraNode::pollCallback, this);
00153 
00154                 return true;
00155         }
00156 
00161         bool setCameraInfo(sensor_msgs::SetCameraInfo::Request& req,
00162                         sensor_msgs::SetCameraInfo::Response& rsp)
00163         {
00165                 camera_info_msg_ = req.camera_info;
00166 
00167                 rsp.success = false;
00168                 rsp.status_message = "[color_camera] Setting camera parameters through ROS not implemented";
00169 
00170                 return true;
00171         }
00172 
00174         void pollCallback(polled_camera::GetPolledImage::Request& req,
00175                         polled_camera::GetPolledImage::Response& res,
00176                         sensor_msgs::Image& image_msg, sensor_msgs::CameraInfo& info)
00177         {
00179                 if (color_camera_->GetColorImage(&color_image_8U3_) & ipa_Utils::RET_FAILED)
00180                 {
00181                         ROS_ERROR("[color_camera] Color image acquisition failed");
00182                         res.success = false;
00183                         return;
00184                 }
00185 
00186                 try
00187                 {
00188                         IplImage img = color_image_8U3_;
00189                         image_msg = *(sensor_msgs::CvBridge::cvToImgMsg(&img, "bgr8"));
00190                 }
00191                 catch (sensor_msgs::CvBridgeException error)
00192                 {
00193                         ROS_ERROR("[color_camera] Could not convert IplImage to ROS message");
00194                 }
00195 
00197                 ros::Time now = ros::Time::now();
00198                 image_msg.header.stamp = now;
00199                 if (camera_index_ == 0)
00200                         image_msg.header.frame_id = "head_color_camera_r_link";
00201                 else
00202                         image_msg.header.frame_id = "head_color_camera_l_link";
00203                 image_msg.encoding = "bgr8";
00204 
00205                 info = camera_info_msg_;
00206                 info.width = color_image_8U3_.cols;
00207                 info.height = color_image_8U3_.rows;
00208                 info.header.stamp = now;
00209                 if (camera_index_ == 0)
00210                         info.header.frame_id = "head_color_camera_r_link";
00211                 else
00212                         info.header.frame_id = "head_color_camera_l_link";
00213 
00214                 res.success = true;
00215     return;
00216         }
00217 
00218         bool loadParameters()
00219         {
00220                 std::string tmp_string = "NULL";
00221 
00222                 if (node_handle_.getParam("configuration_files", config_directory_) == false)
00223                 {
00224                         ROS_ERROR("[color_camera] Path to xml configuration for color camera not specified");
00225                         return false;
00226                 }
00227 
00228                 ROS_INFO("Configuration directory: %s", config_directory_.c_str());
00229 
00231                 if (node_handle_.getParam("camera_index", camera_index_) == false)
00232                 {
00233                         ROS_ERROR("[color_camera] Color camera index (0 or 1) not specified");
00234                         return false;
00235                 }
00236 
00238                 if (node_handle_.getParam("color_camera_type", tmp_string) == false)
00239                 {
00240                         ROS_ERROR("[color_camera] Color camera type not specified");
00241                         return false;
00242                 }
00243                 if (tmp_string == "CAM_AVTPIKE") color_camera_ = ipa_CameraSensors::CreateColorCamera_AVTPikeCam();
00244                 else if (tmp_string == "CAM_VIRTUAL") color_camera_ = ipa_CameraSensors::CreateColorCamera_VirtualCam();
00245                 else if (tmp_string == "CAM_PROSILICA") ROS_ERROR("[color_camera] Color camera type not CAM_PROSILICA not yet implemented");
00246                 else
00247                 {
00248                         std::string str = "[color_camera] Camera type '" + tmp_string + "' unknown, try 'CAM_AVTPIKE' or 'CAM_PROSILICA'";
00249                         ROS_ERROR("%s", str.c_str());
00250                         return false;
00251                 }
00252 
00253                 ROS_INFO("Camera type: %s_%d", tmp_string.c_str(), camera_index_);
00254 
00255                 // There are several intrinsic matrices, optimized to different cameras
00256                 // Here, we specified the desired intrinsic matrix for each camera
00257                 if (node_handle_.getParam("color_camera_intrinsic_type", tmp_string) == false)
00258                 {
00259                         ROS_ERROR("[color_camera] Intrinsic camera type for color camera not specified");
00260                         return false;
00261                 }
00262                 if (tmp_string == "CAM_AVTPIKE")
00263                 {
00264                         color_camera_intrinsic_type_ = ipa_CameraSensors::CAM_AVTPIKE;
00265                 }
00266                 else if (tmp_string == "CAM_PROSILICA")
00267                 {
00268                         color_camera_intrinsic_type_ = ipa_CameraSensors::CAM_PROSILICA;
00269                 }
00270                 else if (tmp_string == "CAM_VIRTUALRANGE")
00271                 {
00272                         color_camera_intrinsic_type_ = ipa_CameraSensors::CAM_VIRTUALRANGE;
00273                 }
00274                 else
00275                 {
00276                         std::string str = "[color_camera] Camera type '" + tmp_string + "' for intrinsics  unknown, try 'CAM_AVTPIKE','CAM_PROSILICA' or 'CAM_SWISSRANGER'";
00277                         ROS_ERROR("%s", str.c_str());
00278                         return false;
00279                 }
00280                 if (node_handle_.getParam("color_camera_intrinsic_id", color_camera_intrinsic_id_) == false)
00281                 {
00282                         ROS_ERROR("[color_camera] Intrinsic camera id for color camera not specified");
00283                         return false;
00284                 }
00285 
00286                 ROS_INFO("Intrinsic for color camera: %s_%d", tmp_string.c_str(), color_camera_intrinsic_id_);
00287 
00288                 return true;
00289         }
00290 };
00291 
00292 //#######################
00293 //#### main programm ####
00294 int main(int argc, char** argv)
00295 {
00297         ros::init(argc, argv, "color_camera");
00298 
00300         ros::NodeHandle nh;
00301 
00303         CobColorCameraNode camera_node(nh);
00304 
00306         if (!camera_node.init()) return 0;
00307 
00308         ros::spin();
00309 
00310         return 0;
00311 }


cob_camera_sensors
Author(s): Jan Fischer , Richard Bormann
autogenerated on Sat Jun 8 2019 21:02:02