color_camera.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 //##################
19 //#### includes ####
20 
21 // standard includes
22 //--
23 
24 // ROS includes
25 #include <ros/ros.h>
27 #include <cv_bridge/CvBridge.h>
28 
29 // ROS message includes
30 #include <sensor_msgs/Image.h>
31 #include <sensor_msgs/CameraInfo.h>
32 #include <sensor_msgs/fill_image.h>
33 #include <sensor_msgs/SetCameraInfo.h>
34 
35 // external includes
39 
40 using namespace ipa_CameraSensors;
41 
45 {
46 private:
49 
51 
52  std::string config_directory_;
56 
57  sensor_msgs::CameraInfo camera_info_msg_;
58 
60 
62 
63 public:
64  CobColorCameraNode(const ros::NodeHandle& node_handle)
65  : node_handle_(node_handle),
66  color_camera_(AbstractColorCameraPtr()),
67  color_image_8U3_(cv::Mat())
68  {
70  }
71 
73  {
74  image_poll_server_.shutdown();
75  color_camera_->Close();
76  }
77 
79  bool init()
80  {
81  if (loadParameters() == false)
82  {
83  ROS_ERROR("[color_camera] Could not read all parameters from launch file");
84  return false;
85  }
86 
87  if (color_camera_->Init(config_directory_, camera_index_) & ipa_CameraSensors::RET_FAILED)
88  {
89  std::stringstream ss;
90  ss << "initialization of color camera ";
91  ss << camera_index_;
92  ss << " failed";
93  ROS_ERROR("[color_camera] %s", ss.str().c_str());
94  color_camera_->Close();
95  color_camera_ = AbstractColorCameraPtr();
96  return false;
97  }
98 
99  if (color_camera_ && (color_camera_->Open() & ipa_CameraSensors::RET_FAILED))
100  {
101  std::stringstream ss;
102  ss << "Could not open color camera ";
103  ss << camera_index_;
104  ROS_ERROR("[color_camera] %s", ss.str().c_str());
105  color_camera_->Close();
106  color_camera_ = AbstractColorCameraPtr();
107  return false;
108  }
109 
113  color_camera_->GetProperty(&cameraProperty);
114  int color_sensor_width = cameraProperty.cameraResolution.xResolution;
115  int color_sensor_height = cameraProperty.cameraResolution.yResolution;
116  cv::Size color_image_size(color_sensor_width, color_sensor_height);
117 
120  color_sensor_toolbox->Init(config_directory_, color_camera_->GetCameraType(), camera_index_, color_image_size);
121 
122  cv::Mat d = color_sensor_toolbox->GetDistortionParameters(color_camera_intrinsic_type_, color_camera_intrinsic_id_);
123  camera_info_msg_.header.stamp = ros::Time::now();
124  if (camera_index_ == 0)
125  camera_info_msg_.header.frame_id = "head_color_camera_r_link";
126  else
127  camera_info_msg_.header.frame_id = "head_color_camera_l_link";
128  camera_info_msg_.D[0] = d.at<double>(0, 0);
129  camera_info_msg_.D[1] = d.at<double>(0, 1);
130  camera_info_msg_.D[2] = d.at<double>(0, 2);
131  camera_info_msg_.D[3] = d.at<double>(0, 3);
132  camera_info_msg_.D[4] = 0;
133 
134  cv::Mat k = color_sensor_toolbox->GetIntrinsicMatrix(color_camera_intrinsic_type_, color_camera_intrinsic_id_);
135  camera_info_msg_.K[0] = k.at<double>(0, 0);
136  camera_info_msg_.K[1] = k.at<double>(0, 1);
137  camera_info_msg_.K[2] = k.at<double>(0, 2);
138  camera_info_msg_.K[3] = k.at<double>(1, 0);
139  camera_info_msg_.K[4] = k.at<double>(1, 1);
140  camera_info_msg_.K[5] = k.at<double>(1, 2);
141  camera_info_msg_.K[6] = k.at<double>(2, 0);
142  camera_info_msg_.K[7] = k.at<double>(2, 1);
143  camera_info_msg_.K[8] = k.at<double>(2, 2);
144 
145  camera_info_msg_.width = color_sensor_width;
146  camera_info_msg_.height = color_sensor_height;
147 
149  camera_info_service_ = node_handle_.advertiseService("set_camera_info", &CobColorCameraNode::setCameraInfo, this);
150 
152  image_poll_server_ = polled_camera::advertise(node_handle_, "request_image", &CobColorCameraNode::pollCallback, this);
153 
154  return true;
155  }
156 
161  bool setCameraInfo(sensor_msgs::SetCameraInfo::Request& req,
162  sensor_msgs::SetCameraInfo::Response& rsp)
163  {
165  camera_info_msg_ = req.camera_info;
166 
167  rsp.success = false;
168  rsp.status_message = "[color_camera] Setting camera parameters through ROS not implemented";
169 
170  return true;
171  }
172 
174  void pollCallback(polled_camera::GetPolledImage::Request& req,
175  polled_camera::GetPolledImage::Response& res,
176  sensor_msgs::Image& image_msg, sensor_msgs::CameraInfo& info)
177  {
179  if (color_camera_->GetColorImage(&color_image_8U3_) & ipa_Utils::RET_FAILED)
180  {
181  ROS_ERROR("[color_camera] Color image acquisition failed");
182  res.success = false;
183  return;
184  }
185 
186  try
187  {
188  IplImage img = color_image_8U3_;
189  image_msg = *(sensor_msgs::CvBridge::cvToImgMsg(&img, "bgr8"));
190  }
191  catch (sensor_msgs::CvBridgeException error)
192  {
193  ROS_ERROR("[color_camera] Could not convert IplImage to ROS message");
194  }
195 
197  ros::Time now = ros::Time::now();
198  image_msg.header.stamp = now;
199  if (camera_index_ == 0)
200  image_msg.header.frame_id = "head_color_camera_r_link";
201  else
202  image_msg.header.frame_id = "head_color_camera_l_link";
203  image_msg.encoding = "bgr8";
204 
205  info = camera_info_msg_;
206  info.width = color_image_8U3_.cols;
207  info.height = color_image_8U3_.rows;
208  info.header.stamp = now;
209  if (camera_index_ == 0)
210  info.header.frame_id = "head_color_camera_r_link";
211  else
212  info.header.frame_id = "head_color_camera_l_link";
213 
214  res.success = true;
215  return;
216  }
217 
219  {
220  std::string tmp_string = "NULL";
221 
222  if (node_handle_.getParam("configuration_files", config_directory_) == false)
223  {
224  ROS_ERROR("[color_camera] Path to xml configuration for color camera not specified");
225  return false;
226  }
227 
228  ROS_INFO("Configuration directory: %s", config_directory_.c_str());
229 
231  if (node_handle_.getParam("camera_index", camera_index_) == false)
232  {
233  ROS_ERROR("[color_camera] Color camera index (0 or 1) not specified");
234  return false;
235  }
236 
238  if (node_handle_.getParam("color_camera_type", tmp_string) == false)
239  {
240  ROS_ERROR("[color_camera] Color camera type not specified");
241  return false;
242  }
243  if (tmp_string == "CAM_AVTPIKE") color_camera_ = ipa_CameraSensors::CreateColorCamera_AVTPikeCam();
244  else if (tmp_string == "CAM_VIRTUAL") color_camera_ = ipa_CameraSensors::CreateColorCamera_VirtualCam();
245  else if (tmp_string == "CAM_PROSILICA") ROS_ERROR("[color_camera] Color camera type not CAM_PROSILICA not yet implemented");
246  else
247  {
248  std::string str = "[color_camera] Camera type '" + tmp_string + "' unknown, try 'CAM_AVTPIKE' or 'CAM_PROSILICA'";
249  ROS_ERROR("%s", str.c_str());
250  return false;
251  }
252 
253  ROS_INFO("Camera type: %s_%d", tmp_string.c_str(), camera_index_);
254 
255  // There are several intrinsic matrices, optimized to different cameras
256  // Here, we specified the desired intrinsic matrix for each camera
257  if (node_handle_.getParam("color_camera_intrinsic_type", tmp_string) == false)
258  {
259  ROS_ERROR("[color_camera] Intrinsic camera type for color camera not specified");
260  return false;
261  }
262  if (tmp_string == "CAM_AVTPIKE")
263  {
264  color_camera_intrinsic_type_ = ipa_CameraSensors::CAM_AVTPIKE;
265  }
266  else if (tmp_string == "CAM_PROSILICA")
267  {
268  color_camera_intrinsic_type_ = ipa_CameraSensors::CAM_PROSILICA;
269  }
270  else if (tmp_string == "CAM_VIRTUALRANGE")
271  {
272  color_camera_intrinsic_type_ = ipa_CameraSensors::CAM_VIRTUALRANGE;
273  }
274  else
275  {
276  std::string str = "[color_camera] Camera type '" + tmp_string + "' for intrinsics unknown, try 'CAM_AVTPIKE','CAM_PROSILICA' or 'CAM_SWISSRANGER'";
277  ROS_ERROR("%s", str.c_str());
278  return false;
279  }
280  if (node_handle_.getParam("color_camera_intrinsic_id", color_camera_intrinsic_id_) == false)
281  {
282  ROS_ERROR("[color_camera] Intrinsic camera id for color camera not specified");
283  return false;
284  }
285 
286  ROS_INFO("Intrinsic for color camera: %s_%d", tmp_string.c_str(), color_camera_intrinsic_id_);
287 
288  return true;
289  }
290 };
291 
292 //#######################
293 //#### main programm ####
294 int main(int argc, char** argv)
295 {
297  ros::init(argc, argv, "color_camera");
298 
300  ros::NodeHandle nh;
301 
303  CobColorCameraNode camera_node(nh);
304 
306  if (!camera_node.init()) return 0;
307 
308  ros::spin();
309 
310  return 0;
311 }
d
AbstractColorCameraPtr color_camera_
Color camera instance.
bool setCameraInfo(sensor_msgs::SetCameraInfo::Request &req, sensor_msgs::SetCameraInfo::Response &rsp)
int main(int argc, char **argv)
ipa_CameraSensors::t_cameraType color_camera_intrinsic_type_
Instrinsic matrix type of left color camera.
boost::shared_ptr< AbstractColorCamera > AbstractColorCameraPtr
ros::NodeHandle node_handle_
std::string config_directory_
Directory of related IPA configuration file.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
__DLL_LIBCAMERASENSORS__ CameraSensorToolboxPtr CreateCameraSensorToolbox()
ros::ServiceServer camera_info_service_
ROSCPP_DECL void spin(Spinner &spinner)
polled_camera::PublicationServer image_poll_server_
PublicationServer advertise(ros::NodeHandle &nh, const std::string &service, const PublicationServer::DriverCallback &cb, const ros::VoidPtr &tracked_object=ros::VoidPtr())
#define ROS_INFO(...)
__DLL_LIBCAMERASENSORS__ AbstractColorCameraPtr CreateColorCamera_VirtualCam()
int color_camera_intrinsic_id_
Instrinsic matrix id of left color camera.
void pollCallback(polled_camera::GetPolledImage::Request &req, polled_camera::GetPolledImage::Response &res, sensor_msgs::Image &image_msg, sensor_msgs::CameraInfo &info)
Callback function for image requests on topic &#39;request_image&#39;.
CobColorCameraNode(const ros::NodeHandle &node_handle)
sensor_msgs::CameraInfo camera_info_msg_
ROS camera information message (e.g. holding intrinsic parameters)
bool getParam(const std::string &key, std::string &s) const
bool init()
Opens the camera sensor.
static Time now()
int camera_index_
Camera index of the color camera for IPA configuration file.
#define ROS_ERROR(...)
__DLL_LIBCAMERASENSORS__ AbstractColorCameraPtr CreateColorCamera_AVTPikeCam()
Definition: AVTPikeCam.cpp:36


cob_camera_sensors
Author(s): Jan Fischer , Richard Bormann
autogenerated on Thu Mar 19 2020 03:23:05