xb3.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 #ifndef _XB3_CPP_
34 #define _XB3_CPP_
35 
36 #include "xb3.h"
37 
42 void clean_dc1394(void)
43 {
44  dc1394_capture_stop(t_dc1394.camera);
45 
46  ROS_WARN("stopping camera capture\n");
47 
48  dc1394_video_set_transmission(t_dc1394.camera, DC1394_OFF );
49 
50  dc1394_camera_free( t_dc1394.camera );
51 }
52 
53 
59 void signal_handler(int sig)
60 {
61 // printf("%d %d %d\n",sig,SIGSEGV,SIGINT);
62 
63  if(sig==SIGSEGV)
64  {
65 
66  signal(SIGSEGV, SIG_DFL);
67 
68  ROS_WARN("System segfaulted");
69 
70  clean_dc1394();
71  ros::shutdown();
72 
73 // exit(0);
74  }
75  else if(sig==SIGINT)
76  {
77  ROS_WARN("Ctrl-c pressed");
78 
79  clean_dc1394();
80  ros::shutdown();
81 
82 // exit(0);
83  }
84 }
85 
86 
95 int init_camera(dc1394_t * d, PGRStereoCamera_t* stereoCamera, dc1394camera_t** camera, unsigned int nThisCam)
96 
97 {
98  dc1394camera_list_t * list;
99  dc1394error_t err;
100 
101  // Find cameras on the 1394 buses
102  d = dc1394_new ();
103 
104  // Enumerate cameras connected to the PC
105  err = dc1394_camera_enumerate (d, &list);
106 
107  if ( err != DC1394_SUCCESS )
108  {
109  fprintf( stderr, "Unable to look for cameras\n\n"
110  "Please check \n"
111  " - if the kernel modules `ieee1394',`raw1394' and `ohci1394' are loaded \n"
112  " - if you have read/write access to /dev/raw1394\n\n");
113  return 1;
114  }
115 
116  // get the camera nodes and describe them as we find them
117  if (list->num == 0)
118  {
119  fprintf( stderr, "No cameras found!\n"); clean_dc1394();
120  }
121 
122  printf( "There were %d camera(s) found attached to your PC\n", list->num );
123 
124  // Identify cameras. Use the first stereo camera that is found
125  for ( nThisCam = 0; nThisCam < list->num; nThisCam++ )
126  {
127  *camera = dc1394_camera_new(d, list->ids[nThisCam].guid);
128 
129  if(!*camera)
130  {
131  printf("Failed to initialize camera with guid %llx", list->ids[nThisCam].guid);
132  continue;
133  }
134 
135  printf( "Camera %d model = '%s'\n", nThisCam, (*camera)->model );
136 
137  if ( isStereoCamera(*camera))
138  {
139 
140  printf( "Using this camera\n" );
141  break;
142  }
143 
144  dc1394_camera_free(*camera);
145  }
146 
147  if ( nThisCam == list->num )
148  {
149  printf( "No stereo cameras were detected\n" );
150  return 0;
151  }
152 
153 
154  // query information about this stereo camera
155  err = queryStereoCamera( *camera, stereoCamera );
156  if ( err != DC1394_SUCCESS )
157  {
158  fprintf( stderr, "Cannot query all information from camera\n" );
159  clean_dc1394();
160  }
161 
162  if ( stereoCamera->model != BUMBLEBEEXB3 )
163  {
164  fprintf( stderr, "Stereo camera found was not a BB XB3\n" );
165  clean_dc1394();
166  }
167 
168  // override the default nBytesPerPixel to be 3, because we really do
169  // want to run at 3 camera mode
170  stereoCamera->nBytesPerPixel = 3;
171  stereoCamera->nRows = 960; //this should be untouched. This is the actual sensor resolution
172  stereoCamera->nCols = 1280;//this should be untouched. This is the actual sensor resolution
173 
174  // set the capture mode
175  printf( "Setting stereo video capture mode\n" );
176 
177  err = setStereoVideoCapture( stereoCamera );
178 
179  if ( err != DC1394_SUCCESS )
180  {
181  fprintf( stderr, "Could not set up video capture mode\n" );
182  clean_dc1394();
183  }
184 
185  // have the camera start sending us data
186  printf( "Start transmission\n" );
187  err = startTransmission( stereoCamera );
188  if ( err != DC1394_SUCCESS )
189  {
190  fprintf( stderr, "Unable to start camera iso transmission\n" );
191  clean_dc1394();
192  }
193 return 1;}
194 
195 
203 int copy_img_buffer_to_cvMat(unsigned char* Buffer, cv::Mat left, cv::Mat center, cv::Mat right)
204 {
205  //Get pointers to the three image buffers
206  unsigned char* ptr_left_buffers = Buffer;
207  unsigned char* ptr_center_buffer = Buffer + 3 * 1280*960;
208  unsigned char* ptr_right_buffers = Buffer + 6 * 1280*960;
209 
210  //Get pointers to the three cv::Mat
211  unsigned char* ptr_left_cvMat = left.ptr<unsigned char>(0);
212  unsigned char* ptr_center_cvMat = center.ptr<unsigned char>(0);
213  unsigned char* ptr_right_cvMat = right.ptr<unsigned char>(0);
214 
215 
216  int count=0;
217  for(int c=0; c<1280;c++)
218  for (int l=0; l<960; l++)
219  {
220  //from RGB to BGR
221  ptr_left_cvMat[count] = ptr_left_buffers[count+2];
222  ptr_left_cvMat[count+1] = ptr_left_buffers[count+1];
223  ptr_left_cvMat[count+2] = ptr_left_buffers[count];
224 
225  ptr_center_cvMat[count] = ptr_center_buffer[count+2];
226  ptr_center_cvMat[count+1] = ptr_center_buffer[count+1];
227  ptr_center_cvMat[count+2] = ptr_center_buffer[count];
228 
229  ptr_right_cvMat[count] = ptr_right_buffers[count+2];
230  ptr_right_cvMat[count+1] = ptr_right_buffers[count+1];
231  ptr_right_cvMat[count+2] = ptr_right_buffers[count];
232 
233  count+=3;
234  }
235 return 1;}
236 
246 void set_fixed_fields_image_msg(sensor_msgs::Image* msg, int height, int width, char* encoding, int is_bigendian, char* frame_id)
247 {
248  msg->height = height; //set the height.
249  msg->width = width; //set the width
250  msg->encoding = sensor_msgs::image_encodings::RGB8; //Set the encoding
251  msg->is_bigendian = 0;
252  msg->step = width*3;
253  msg->data.resize(width*height*3);
254  msg->header.frame_id = frame_id;
255  //ROS_INFO("Resized image msg to %d",width*height*3);
256 }
257 
265 void set_fixed_camera_info(sensor_msgs::CameraInfo *info, int height, int width, char* frame_id)
266 {
267  info->height = height;
268  info->width = width;
269  info->header.frame_id = frame_id;
270  info->roi.do_rectify = true;
271 }
272 
279 void copy_pixels_to_image_msg_data(unsigned char *in, sensor_msgs::Image *image, int size)
280 {
281  //printf("image->width=%d height=%d\n",image->width, image->height);
282  //int t=0;
283  for(int i=0; i<size; i+=3)
284  //for (int l=0; l<960; l++)
285  //for(int c=0; c<1280; c++)
286  {
287  //printf("t=%d i=%d\n",t,i);
288  //char a = in[i+2];
289  //int t = c + l*1280;
290  image->data[i] = in[i+2];
291  image->data[i+1] = in[i+1];
292  image->data[i+2] = in[i];
293  //t+=3;
294  }
295 }
296 
302 void reconfig(xb3::xb3Config &config, uint32_t level)
303 {
304  ROS_INFO("dynamic reconfigure1n");
305 }
306 
307 using namespace std;
308 
309 int main(int argc, char **argv)
310 {
312  t_flags.debug=false;
313  t_buffers.pucLeftRGB=NULL;
314  t_buffers.pucCenterRGB=NULL;
315  t_buffers.pucRightRGB=NULL;
316 
318  ros::init(argc, argv, "xb3", ros::init_options::NoSigintHandler | ros::init_options::AnonymousName);
319  ros::NodeHandle nh;
320 
322  signal(SIGINT, signal_handler);
323  signal(SIGSEGV, signal_handler);
324 
326 
327  int width;
328  if (nh.hasParam("width"))
329  {
330  nh.getParam("width", width);
331  }
332  else
333  {
334  ROS_ERROR("Set properly an image width value. (param= 'width') ");
335  return -1;
336  }
337 
338  int height;
339  if (nh.hasParam("height"))
340  {
341  nh.getParam("height", height);
342  }
343  else
344  {
345  ROS_ERROR("Set properly an image height value. (param= 'height') ");
346  return -1;
347  }
348 
350  set_fixed_camera_info(&t_msgs.short_left_info, height,width, (char*)(ros::names::remap("/xb3_optical_frame")).c_str());
351  set_fixed_camera_info(&t_msgs.short_right_info, height,width, (char*)(ros::names::remap("/xb3_optical_frame")).c_str());
352  set_fixed_camera_info(&t_msgs.wide_left_info, height,width, (char*)(ros::names::remap("/xb3_optical_frame")).c_str());
353  set_fixed_camera_info(&t_msgs.wide_right_info, height,width, (char*)(ros::names::remap("/xb3_optical_frame")).c_str());
354 
356  set_fixed_fields_image_msg(&t_msgs.short_left, height,width, (char*)"RGB8", 0, (char*)(ros::names::remap( "/xb3_optical_frame")).c_str());
357  set_fixed_fields_image_msg(&t_msgs.short_right, height,width, (char*)"RGB8", 0, (char*)(ros::names::remap( "/xb3_optical_frame")).c_str());
358  set_fixed_fields_image_msg(&t_msgs.wide_left, height,width, (char*)"RGB8", 0, (char*)(ros::names::remap( "/xb3_optical_frame")).c_str());
359  set_fixed_fields_image_msg(&t_msgs.wide_right, height,width, (char*)"RGB8", 0, (char*)(ros::names::remap("/xb3_optical_frame")).c_str());
360 
362  image_transport::ImageTransport it(nh);
363  t_msgs.short_left_pub = it.advertiseCamera("short/left/image_raw", 1);
364  t_msgs.short_right_pub = it.advertiseCamera("short/right/image_raw", 1);
365  t_msgs.wide_left_pub = it.advertiseCamera("wide/left/image_raw", 1);
366  t_msgs.wide_right_pub = it.advertiseCamera("wide/right/image_raw", 1);
367 
368 
369  t_msgs.short_left_info_manager = new camera_info_manager::CameraInfoManager(ros::NodeHandle(nh, "short/left"),"short/left","package://xb3/calibrations/short_left.yaml");
370  t_msgs.short_right_info_manager = new camera_info_manager::CameraInfoManager(ros::NodeHandle(nh, "short/right"),"short/right", "package://xb3/calibrations/short_right.yaml");
371  t_msgs.wide_left_info_manager = new camera_info_manager::CameraInfoManager(ros::NodeHandle(nh, "wide/left"),"wide/left","package://xb3/calibrations/wide_left.yaml");
372  t_msgs.wide_right_info_manager = new camera_info_manager::CameraInfoManager(ros::NodeHandle(nh, "wide/right"),"wide/right","package://xb3/calibrations/wide_right.yaml");
373 
374 
375 //while(nh.ok())
376 //{
377  //ros::spinOnce();
378 //}
379 
380  cout<<"TT"<<endl;
382  init_camera(t_dc1394.d, &t_dc1394.stereoCamera,&t_dc1394.camera, t_dc1394.nThisCam );
383  PFLN
384  ROS_INFO("Connected to XB3 camera");
385 
386  cout<<"A1"<<endl;
387  dc1394_feature_set_mode(t_dc1394.camera,DC1394_FEATURE_SHUTTER,DC1394_FEATURE_MODE_MANUAL);
388  dc1394_feature_set_value(t_dc1394.camera,DC1394_FEATURE_SHUTTER,100);
389 
390  dc1394featureset_t features;
391  dc1394_feature_get_all(t_dc1394.camera,&features);
392 
393  FILE *fd = fopen("/home/jorge/Desktop/conf.txt","w");
394  dc1394_feature_print_all(&features,fd);
395 
396  fclose(fd);
397 
399  t_buffers.nBufferSize = t_dc1394.stereoCamera.nRows * t_dc1394.stereoCamera.nCols * t_dc1394.stereoCamera.nBytesPerPixel;
400  t_buffers.pucDeInterlacedBuffer = new unsigned char[ t_buffers.nBufferSize ];
401  t_buffers.pucRGBBuffer = NULL;
402  t_buffers.pucGreenBuffer = NULL;
403 
404  t_imgs.left.create(cvSize(1280,960),CV_8UC3);
405  t_imgs.center.create(cvSize(1280,960),CV_8UC3);
406  t_imgs.right.create(cvSize(1280,960),CV_8UC3);
407 
408  t_imgs.left_640_480.create(cvSize(width,height),CV_8UC3);
409  t_imgs.center_640_480.create(cvSize(width,height),CV_8UC3);
410  t_imgs.right_640_480.create(cvSize(width,height),CV_8UC3);
411 
412  //allocation color processing buffers
413  if ( t_dc1394.stereoCamera.bColor )
414  {
415  t_buffers.pucRGBBuffer = new unsigned char[ 3 * t_buffers.nBufferSize ];
416  t_buffers.pucGreenBuffer = new unsigned char[ t_buffers.nBufferSize ];
417  }
418  else
419  {
420  ROS_ERROR("This driver works only with color stereo cameras");
421  signal_handler(SIGINT);
422  }
423 
424  cout<<"Hf"<<endl;
425 
427  if (t_flags.debug)
428  {
429  cvNamedWindow("left",CV_WINDOW_AUTOSIZE);
430  cvNamedWindow("center",CV_WINDOW_AUTOSIZE);
431  cvNamedWindow("right",CV_WINDOW_AUTOSIZE);
432  }
433 
434 
435 
436 
437  dynamic_reconfigure::Server<xb3::xb3Config> srv;
438  dynamic_reconfigure::Server<xb3::xb3Config>::CallbackType f;
439  f = boost::bind(&reconfig, _1, _2);
440  srv.setCallback(f);
441 
442  static tf::TransformBroadcaster br;
443  tf::Transform transform;
444  transform.setOrigin( tf::Vector3(0,-1, 0.0) );
445  transform.setRotation( tf::Quaternion(0, 0, 0) );
446 
447  cout<<"tf broadcast"<<endl;
448 
449  while(1)
450  {
451  cout<<"get images"<<endl;
452  // get the images from the capture buffer and do all required processing
453  extractImagesColorXB3( &t_dc1394.stereoCamera, DC1394_BAYER_METHOD_HQLINEAR, t_buffers.pucDeInterlacedBuffer, t_buffers.pucRGBBuffer, t_buffers.pucGreenBuffer, &t_buffers.pucRightRGB, &t_buffers.pucLeftRGB, &t_buffers.pucCenterRGB);
454 
455  cout<<"get current time"<<endl;
456  //Get current time
457  ros::Time stamp=ros::Time::now();
458 
459  copy_img_buffer_to_cvMat(t_buffers.pucRGBBuffer, t_imgs.left, t_imgs.center, t_imgs.right);
460 
461 
462  if (width==640 && height==480)
463  {
464  cv::pyrDown(t_imgs.left, t_imgs.left_640_480);
465  cv::pyrDown(t_imgs.right, t_imgs.right_640_480);
466  cv::pyrDown(t_imgs.center, t_imgs.center_640_480);
467  }
468  else if (width==1280 && height==960)
469  {
470  t_imgs.left_640_480=t_imgs.left;
471  t_imgs.right_640_480=t_imgs.right;
472  t_imgs.center_640_480=t_imgs.center;
473  }
474 
475  cout<<"copy and publish"<<endl;
476 
478  //http://www.ros.org/wiki/stereo_image_proc
479 
480  //short_left is actually xb3 left camera
481  copy_pixels_to_image_msg_data(t_imgs.left_640_480.ptr<unsigned char>(0), &t_msgs.short_left, height*width*3);
482 
483  //short_right is actually xb3 center camera
484  copy_pixels_to_image_msg_data(t_imgs.center_640_480.ptr<unsigned char>(0), &t_msgs.short_right, height*width*3);
485 
486  //wide_left is actually xb3 left camera
487  copy_pixels_to_image_msg_data(t_imgs.left_640_480.ptr<unsigned char>(0), &t_msgs.wide_left, height*width*3);
488 
489  //wide_right is actually xb3 right camera
490  copy_pixels_to_image_msg_data(t_imgs.right_640_480.ptr<unsigned char>(0), &t_msgs.wide_right, height*width*3);
491 
492 
493  t_msgs.short_left_info = t_msgs.short_left_info_manager->getCameraInfo();
494  t_msgs.short_right_info = t_msgs.short_right_info_manager->getCameraInfo();
495 
496  t_msgs.wide_left_info = t_msgs.wide_left_info_manager->getCameraInfo();
497  t_msgs.wide_right_info = t_msgs.wide_right_info_manager->getCameraInfo();
498 
499  t_msgs.short_left_info.header.frame_id = ros::names::remap("/xb3_optical_frame");
500  t_msgs.short_right_info.header.frame_id = ros::names::remap("/xb3_optical_frame");
501  t_msgs.wide_left_info.header.frame_id = ros::names::remap("/xb3_optical_frame");
502  t_msgs.wide_right_info.header.frame_id = ros::names::remap("/xb3_optical_frame");
503 
504  t_msgs.short_left_pub.publish(t_msgs.short_left, t_msgs.short_left_info, stamp);
505  t_msgs.short_right_pub.publish(t_msgs.short_right, t_msgs.short_right_info, stamp);
506  t_msgs.wide_left_pub.publish(t_msgs.wide_left, t_msgs.wide_left_info, stamp);
507  t_msgs.wide_right_pub.publish(t_msgs.wide_right, t_msgs.wide_right_info, stamp);
508 
509  cout<<"published"<<endl;
510  if (t_flags.debug) //copy to cvimage and display in highgui
511  {
512  cv::imshow("left",t_imgs.left_640_480);
513  cv::imshow("center",t_imgs.center_640_480);
514  cv::imshow("right",t_imgs.right_640_480);
515  }
516 
517  //br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "/world", "/xb3_optical_frame"));
518 
519  ros::spinOnce(); //spin ros
520 
521  if (t_flags.debug)
522  {
523  char k=cvWaitKey(40);
524  if(k=='q') break;
525  }
526  }
527 
528  clean_dc1394();
529 
530  return 0;
531 }
532 
533 #endif
534 
bool isStereoCamera(dc1394camera_t *camera)
void copy_pixels_to_image_msg_data(unsigned char *in, sensor_msgs::Image *image, int size)
Copy image pixel data to ros sensor image message.
Definition: xb3.cpp:279
PGRStereoCameraModel_t model
Definition: pgr_stereocam.h:68
void signal_handler(int sig)
signal handler function This handles with SIGINT and SIGSEGV signals
Definition: xb3.cpp:59
void reconfig(xb3::xb3Config &config, uint32_t level)
necessary function for dynamic_reconfigure (I guess :))
Definition: xb3.cpp:302
dc1394error_t startTransmission(PGRStereoCamera_t *stereoCamera)
int copy_img_buffer_to_cvMat(unsigned char *Buffer, cv::Mat left, cv::Mat center, cv::Mat right)
copy an unsigned char buffer to cv::Mat
Definition: xb3.cpp:203
int init_camera(dc1394_t *d, PGRStereoCamera_t *stereoCamera, dc1394camera_t **camera, unsigned int nThisCam)
initialize camera initializes camera with dc1394
Definition: xb3.cpp:95
dc1394error_t queryStereoCamera(dc1394camera_t *camera, PGRStereoCamera_t *stereoCamera)
Main header file for the xb3 module.
unsigned int nBytesPerPixel
Definition: pgr_stereocam.h:73
void set_fixed_camera_info(sensor_msgs::CameraInfo *info, int height, int width, char *frame_id)
This function will set all the fixed parameters on image camera info messages.
Definition: xb3.cpp:265
void set_fixed_fields_image_msg(sensor_msgs::Image *msg, int height, int width, char *encoding, int is_bigendian, char *frame_id)
This function will set all the fixed parameters on image sensor messages.
Definition: xb3.cpp:246
unsigned int nCols
Definition: pgr_stereocam.h:72
#define PFLN
void extractImagesColorXB3(PGRStereoCamera_t *stereoCamera, dc1394bayer_method_t bayerMethod, unsigned char *pucDeInterleaved, unsigned char *pucRGB, unsigned char *pucGreen, unsigned char **ppucRightRGB, unsigned char **ppucLeftRGB, unsigned char **ppucCenterRGB, TriclopsInput *pShortInput, TriclopsInput *pWideInput)
unsigned int nRows
Definition: pgr_stereocam.h:71
dc1394error_t setStereoVideoCapture(PGRStereoCamera_t *stereoCamera)
void clean_dc1394(void)
stop camera captur stops camera capture using dc1394
Definition: xb3.cpp:42
int main(int argc, char **argv)
Definition: xb3.cpp:309


xb3
Author(s): Miguel Oliveira, Tiago Talhada
autogenerated on Mon Mar 2 2015 01:33:02