converters/camera.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
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 * LOCAL includes
20 */
21 #include "camera.hpp"
23 #include "../tools/alvisiondefinitions.h" // for kTop...
24 #include "../tools/from_any_value.hpp"
25 
26 /*
27 * ROS includes
28 */
29 #include <cv_bridge/cv_bridge.h>
30 #include <ros/console.h>
31 
32 /*
33 * CV includes
34 */
35 #include <opencv2/imgproc/imgproc.hpp>
36 
37 /*
38 * BOOST includes
39 */
40 #include <boost/foreach.hpp>
41 #define for_each BOOST_FOREACH
42 
43 namespace naoqi
44 {
45 namespace converter
46 {
47 
48 namespace camera_info_definitions
49 {
50 
51 const sensor_msgs::CameraInfo& getEmptyInfo()
52 {
53  static const sensor_msgs::CameraInfo cam_info_msg;
54  return cam_info_msg;
55 }
56 
57 const sensor_msgs::CameraInfo& getCameraInfo( int camera_source, int resolution )
58 {
62  if ( camera_source == AL::kTopCamera)
63  {
64  if ( resolution == AL::kVGA )
65  {
66  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoTOPVGA();
67  return cam_info_msg;
68  }
69  else if( resolution == AL::kQVGA )
70  {
71  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoTOPQVGA();
72  return cam_info_msg;
73  }
74  else if( resolution == AL::kQQVGA )
75  {
76  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoTOPQQVGA();
77  return cam_info_msg;
78  }
79  }
80  else if ( camera_source == AL::kBottomCamera )
81  {
82  if ( resolution == AL::kVGA )
83  {
84  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoBOTTOMVGA();
85  return cam_info_msg;
86  }
87  else if( resolution == AL::kQVGA )
88  {
89  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoBOTTOMQVGA();
90  return cam_info_msg;
91  }
92  else if( resolution == AL::kQQVGA )
93  {
94  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoBOTTOMQQVGA();
95  return cam_info_msg;
96  }
97  }
98  else if ( camera_source == AL::kDepthCamera )
99  {
100  if ( resolution == AL::kVGA )
101  {
102  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHVGA();
103  ROS_WARN("VGA resolution is not supported for the depth camera, use QVGA or lower");
104  return cam_info_msg;
105  }
106  else if( resolution == AL::kQVGA )
107  {
108  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQVGA();
109  return cam_info_msg;
110  }
111  else if( resolution == AL::kQQVGA )
112  {
113  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQQVGA();
114  return cam_info_msg;
115  }
116  else if (resolution == AL::k720p){
117  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTH720P();
118  return cam_info_msg;
119  }
120  else if (resolution == AL::kQ720p){
121  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQ720P();
122  return cam_info_msg;
123  }
124  else if (resolution == AL::kQQ720p){
125  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQQ720P();
126  return cam_info_msg;
127  }
128  else if (resolution == AL::kQQQ720p){
129  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQQQ720P();
130  return cam_info_msg;
131  }
132  else if (resolution == AL::kQQQQ720p){
133  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQQQQ720P();
134  return cam_info_msg;
135  }
136  }
137  else if (camera_source == AL::kInfraredOrStereoCamera) {
138  if ( resolution == AL::kVGA )
139  {
140  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHVGA();
141  ROS_WARN("VGA resolution is not supported for the depth camera, use QVGA or lower");
142  return cam_info_msg;
143  }
144  else if( resolution == AL::kQVGA )
145  {
146  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQVGA();
147  return cam_info_msg;
148  }
149  else if( resolution == AL::kQQVGA )
150  {
151  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoDEPTHQQVGA();
152  return cam_info_msg;
153  }
154  else if (resolution == AL::k720px2){
155  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoStereo720PX2();
156  return cam_info_msg;
157  }
158  else if (resolution == AL::kQ720px2){
159  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoStereoQ720PX2();
160  return cam_info_msg;
161  }
162  else if (resolution == AL::kQQ720px2){
163  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoStereoQQ720PX2();
164  return cam_info_msg;
165  }
166  else if (resolution == AL::kQQQ720px2){
167  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoStereoQQQ720PX2();
168  return cam_info_msg;
169  }
170  else if (resolution == AL::kQQQQ720px2){
171  static const sensor_msgs::CameraInfo cam_info_msg = createCameraInfoStereoQQQQ720PX2();
172  return cam_info_msg;
173  }
174  }
175  else{
176  std::cout << "no camera information found for camera_source " << camera_source << " and res: " << resolution << std::endl;
177  return getEmptyInfo();
178  }
179 }
180 
181 } // camera_info_definitions
182 
184  const std::string& name,
185  const float& frequency,
186  const qi::SessionPtr& session,
187  const int& camera_source,
188  const int& resolution,
189  const bool& has_stereo) : BaseConverter( name, frequency, session ),
190  p_video_( session->service("ALVideoDevice") ),
191  camera_source_(camera_source),
192  resolution_(resolution),
193  // change in case of depth camera
194  colorspace_( (camera_source_!=AL::kDepthCamera)?AL::kRGBColorSpace:AL::kRawDepthColorSpace ),
195  msg_colorspace_( (camera_source_!=AL::kDepthCamera)?"rgb8":"16UC1" ),
196  cv_mat_type_( (camera_source_!=AL::kDepthCamera)?CV_8UC3:CV_16U ),
197  camera_info_( camera_info_definitions::getCameraInfo(camera_source, resolution) )
198 {
199  switch (camera_source) {
200  case AL::kTopCamera:
201  msg_frameid_ = "CameraTop_optical_frame";
202  break;
203 
204  case AL::kBottomCamera:
205  msg_frameid_ = "CameraBottom_optical_frame";
206  break;
207 
208  case AL::kDepthCamera:
209  msg_frameid_ = "CameraDepth_optical_frame";
210 
211  if (has_stereo)
213 
214  break;
215 
217  msg_frameid_ = "CameraDepth_optical_frame";
218 
219  if (!has_stereo) {
222  msg_colorspace_ = "16UC1";
223  cv_mat_type_ = CV_16U;
224  }
225 
227  break;
228  }
229 }
230 
232 {
233  if (!handle_.empty())
234  {
235  std::cout << "Unsubscribe camera handle " << handle_ << std::endl;
236  p_video_.call<qi::AnyValue>("unsubscribe", handle_);
237  handle_.clear();
238  }
239 }
240 
242 {
243  if (!handle_.empty())
244  {
245  p_video_.call<qi::AnyValue>("unsubscribe", handle_);
246  handle_.clear();
247  }
248 
249  handle_ = p_video_.call<std::string>(
250  "subscribeCamera",
251  name_,
253  resolution_,
254  colorspace_,
255  frequency_
256  );
257 }
258 
260 {
261  callbacks_[action] = cb;
262 }
263 
264 void CameraConverter::callAll( const std::vector<message_actions::MessageAction>& actions )
265 {
266 
267  if (handle_.empty() )
268  {
269  std::cerr << name_ << "Camera Handle is empty - cannot retrieve image" << std::endl;
270  std::cerr << name_ << "Might be a NAOqi problem. Try to restart the ALVideoDevice." << std::endl;
271  return;
272  }
273 
274  qi::AnyValue image_anyvalue = p_video_.call<qi::AnyValue>("getImageRemote", handle_);
275  tools::NaoqiImage image;
276  try{
277  image = tools::fromAnyValueToNaoqiImage(image_anyvalue);
278  }
279  catch(std::runtime_error& e)
280  {
281  std::cout << "Cannot retrieve image" << std::endl;
282  return;
283  }
284 
285  // Create a cv::Mat of the right dimensions
286  cv::Mat cv_img(image.height, image.width, cv_mat_type_, image.buffer);
287  msg_ = cv_bridge::CvImage(std_msgs::Header(), msg_colorspace_, cv_img).toImageMsg();
288  msg_->header.frame_id = msg_frameid_;
289 
290  msg_->header.stamp = ros::Time::now();
291  //msg_->header.stamp.sec = image.timestamp_s;
292  //msg_->header.stamp.nsec = image.timestamp_us*1000;
293  camera_info_.header.stamp = msg_->header.stamp;
294 
296  {
297  callbacks_[action]( msg_, camera_info_ );
298  }
299 }
300 
301 } // publisher
302 } //naoqi
const int kQQQ720px2
const int kBottomCamera
const int kRGBColorSpace
const int kQ720p
const int kRawDepthColorSpace
const sensor_msgs::CameraInfo & getEmptyInfo()
void callAll(const std::vector< message_actions::MessageAction > &actions)
const int kQQQQ720px2
const int k720p
NaoqiImage fromAnyValueToNaoqiImage(qi::AnyValue &value)
sensor_msgs::CameraInfo camera_info_
const int kDepthColorSpace
const int kQVGA
#define ROS_WARN(...)
CameraConverter(const std::string &name, const float &frequency, const qi::SessionPtr &session, const int &camera_source, const int &resolution, const bool &has_stereo=false)
const int kVGA
const int kDepthCamera
const int k720px2
const int kQ720px2
const int kQQQ720p
const int kTopCamera
const int kInfraredOrStereoCamera
action
const int kQQ720px2
void registerCallback(const message_actions::MessageAction action, Callback_t cb)
const int kQQQQ720p
boost::function< void(sensor_msgs::ImagePtr, sensor_msgs::CameraInfo)> Callback_t
const sensor_msgs::CameraInfo & getCameraInfo(int camera_source, int resolution)
The struct describing an image retrieved by ALVideoDevice. This specification can be found here: http...
Definition: naoqi_image.hpp:30
const int kInfraredColorSpace
static Time now()
#define for_each
const int kQQVGA
std::map< message_actions::MessageAction, Callback_t > callbacks_
sensor_msgs::ImagePtr toImageMsg() const
const int kQQ720p


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26