Main MRPT website > C++ reference for MRPT 1.5.9
CImageGrabber_dc1394.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 
12 #include <mrpt/config.h>
14 
15 // Include the libdc1394-2 headers:
16 #if MRPT_HAS_LIBDC1394_2
17 # include <dc1394/control.h>
18 # include <dc1394/conversions.h>
19 # include <dc1394/utils.h>
20 # include <dc1394/register.h>
21 #endif
22 
23 using namespace std;
24 using namespace mrpt;
25 using namespace mrpt::hwdrivers;
26 
27 
28 #define THE_CAMERA static_cast<dc1394camera_t*>(m_dc1394camera)
29 #define THE_CONTEXT static_cast<dc1394_t*>(m_dc1394_lib_context)
30 
31 /*-------------------------------------------------------------
32  Constructor
33  -------------------------------------------------------------*/
34 CImageGrabber_dc1394::CImageGrabber_dc1394(
35  uint64_t cameraGUID,
36  uint16_t cameraUnit,
37  const TCaptureOptions_dc1394 &options,
38  bool verbose ) :
39  m_bInitialized (false),
40  m_dc1394_lib_context(NULL),
41  m_dc1394camera (NULL),
42  m_options (options)
43 {
45 
46 #if MRPT_HAS_LIBDC1394_2
47  // Open lib:
48  m_dc1394_lib_context = dc1394_new ();
50 
51  // Enumerate cameras:
52  dc1394camera_list_t * list;
53  dc1394error_t err;
54 
55  err=dc1394_camera_enumerate(THE_CONTEXT, &list);
56  if (err!=DC1394_SUCCESS)
57  {
58  cerr << "[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras (Maybe your user has no rights to access IEEE1394?)." << endl;
59  return;
60  }
61 
62  if (!list->num)
63  {
64  cerr << "[CImageGrabber_dc1394] ERROR: No cameras found." << endl;
65  return;
66  }
67 
68  // Open the first camera or the one given by the user:
69  if (!cameraGUID)
70  {
71  // Default: first one:
72  m_dc1394camera = dc1394_camera_new(THE_CONTEXT, list->ids[0].guid);
73  if (!THE_CAMERA)
74  {
75  dc1394_camera_free_list(list);
76  cerr << "[CImageGrabber_dc1394] ERROR: Failed to initialize camera with GUID "<<list->ids[0].guid<<"\n";
77  return;
78  }
79  }
80  else
81  {
82  // Look for user-given IDs:
83  for (uint32_t i=0;i<list->num;i++)
84  {
85  if (list->ids[i].guid==cameraGUID && list->ids[i].unit==cameraUnit)
86  {
87  m_dc1394camera = dc1394_camera_new_unit(THE_CONTEXT, cameraGUID, cameraUnit);
88  if (!THE_CAMERA)
89  {
90  dc1394_camera_free_list(list);
91  cerr << "[CImageGrabber_dc1394] ERROR: Failed to initialize camera with GUID "<<list->ids[0].guid<<"\n";
92  return;
93  }
94  break;
95  }
96  }
97 
98  if (!m_dc1394camera)
99  {
100  dc1394_camera_free_list(list);
101  cerr << "[CImageGrabber_dc1394] ERROR: Camera with GUID="<<cameraGUID<<" and UNIT="<<cameraUnit<<" not found.\n";
102  return;
103  }
104 
105  }
106  dc1394_camera_free_list(list);
107 
108  // Camera open OK:
109  if (verbose)
110  {
111  dc1394_camera_print_info(THE_CAMERA,stdout);
112  }
113 
114  // get all supported modes:
115  dc1394video_modes_t modes;
116  err=dc1394_video_get_supported_modes(THE_CAMERA, &modes);
117  if (err!=DC1394_SUCCESS)
118  {
119  cerr << "[CImageGrabber_dc1394] ERROR: Could not get list of modes." << endl;
120  return;
121  }
122 
123  // Is mode7? treat differently:
124  if (options.mode7>=0)
125  {
126  m_desired_mode = DC1394_VIDEO_MODE_FORMAT7_MIN + options.mode7;
127  if (verbose)
128  cout << "[CImageGrabber_dc1394] Mode is mode7: " << options.mode7 << endl;
129  }
130  else
131  {
132  #define TEST_MODE(W,H,COLORMODEL) else if (options.frame_width==W && options.frame_height==H && options.color_coding==COLOR_CODING_##COLORMODEL) m_desired_mode=DC1394_VIDEO_MODE_##W##x##H##_##COLORMODEL;
133 
134  if (0) { }
135  TEST_MODE(160,120,YUV444)
136  TEST_MODE(320,240,YUV422)
137  TEST_MODE(640,480,YUV411)
138  TEST_MODE(640,480,YUV422)
139  TEST_MODE(640,480,RGB8)
140  TEST_MODE(640,480,MONO8)
141  TEST_MODE(640,480,MONO16)
142  TEST_MODE(800,600,YUV422)
143  TEST_MODE(800,600,RGB8)
144  TEST_MODE(800,600,MONO8)
145  TEST_MODE(800,600,MONO16)
146  TEST_MODE(1024,768,YUV422)
147  TEST_MODE(1024,768,RGB8)
148  TEST_MODE(1024,768,MONO8)
149  TEST_MODE(1024,768,MONO16)
150  TEST_MODE(1280,960,YUV422)
151  TEST_MODE(1280,960,RGB8)
152  TEST_MODE(1280,960,MONO8)
153  TEST_MODE(1280,960,MONO16)
154  TEST_MODE(1600,1200,YUV422)
155  TEST_MODE(1600,1200,RGB8)
156  TEST_MODE(1600,1200,MONO8)
157  TEST_MODE(1600,1200,MONO16)
158  }
159  // Display all supported modes and chosen:
160  if (verbose) cout << "------ Supported video modes ------" << endl;
161  bool valid_video_mode = false;
162  for(uint32_t i=0; i<modes.num; i++)
163  {
164  string mode;
165  switch( modes.modes[i] )
166  {
167  case DC1394_VIDEO_MODE_160x120_YUV444: mode = "160x120_YUV444"; break;
168  case DC1394_VIDEO_MODE_320x240_YUV422: mode = "320x240_YUV422"; break;
169  case DC1394_VIDEO_MODE_640x480_YUV411: mode = "640x480_YUV411"; break;
170  case DC1394_VIDEO_MODE_640x480_YUV422: mode = "640x480_YUV422"; break;
171  case DC1394_VIDEO_MODE_640x480_RGB8: mode = "640x480_RGB8"; break;
172  case DC1394_VIDEO_MODE_640x480_MONO8: mode = "640x480_MONO8"; break;
173  case DC1394_VIDEO_MODE_640x480_MONO16: mode = "640x480_MONO16"; break;
174  case DC1394_VIDEO_MODE_800x600_YUV422: mode = "800x600_YUV422"; break;
175  case DC1394_VIDEO_MODE_800x600_RGB8: mode = "800x600_RGB8"; break;
176  case DC1394_VIDEO_MODE_800x600_MONO8: mode = "800x600_MONO8"; break;
177  case DC1394_VIDEO_MODE_1024x768_YUV422: mode = "1024x768_YUV422"; break;
178  case DC1394_VIDEO_MODE_1024x768_RGB8: mode = "1024x768_RGB8"; break;
179  case DC1394_VIDEO_MODE_1024x768_MONO8: mode = "1024x768_MONO8"; break;
180  case DC1394_VIDEO_MODE_800x600_MONO16: mode = "800x600_MONO16"; break;
181  case DC1394_VIDEO_MODE_1024x768_MONO16: mode = "1024x768_MONO16"; break;
182  case DC1394_VIDEO_MODE_1280x960_YUV422: mode = "1280x960_YUV422"; break;
183  case DC1394_VIDEO_MODE_1280x960_RGB8: mode = "1280x960_RGB8"; break;
184  case DC1394_VIDEO_MODE_1280x960_MONO8: mode = "1280x960_MONO8"; break;
185  case DC1394_VIDEO_MODE_1600x1200_YUV422: mode = "1600x1200_YUV422"; break;
186  case DC1394_VIDEO_MODE_1600x1200_RGB8: mode = "1600x1200_RGB8"; break;
187  case DC1394_VIDEO_MODE_1600x1200_MONO8: mode = "1600x1200_MONO8"; break;
188  case DC1394_VIDEO_MODE_1280x960_MONO16: mode = "1280x960_MONO16"; break;
189  case DC1394_VIDEO_MODE_1600x1200_MONO16: mode = "1600x1200_MONO16"; break;
190  case DC1394_VIDEO_MODE_EXIF: mode = "EXIF"; break;
191  case DC1394_VIDEO_MODE_FORMAT7_0: mode = "FORMAT7_0"; break;
192  case DC1394_VIDEO_MODE_FORMAT7_1: mode = "FORMAT7_1"; break;
193  case DC1394_VIDEO_MODE_FORMAT7_2: mode = "FORMAT7_2"; break;
194  case DC1394_VIDEO_MODE_FORMAT7_3: mode = "FORMAT7_3"; break;
195  case DC1394_VIDEO_MODE_FORMAT7_4: mode = "FORMAT7_4"; break;
196  case DC1394_VIDEO_MODE_FORMAT7_5: mode = "FORMAT7_5"; break;
197  case DC1394_VIDEO_MODE_FORMAT7_6: mode = "FORMAT7_6"; break;
198  case DC1394_VIDEO_MODE_FORMAT7_7: mode = "FORMAT7_7"; break;
199  default:
200  cerr << "[CImageGrabber_dc1394] ERROR: Requested video mode is not valid." << endl;
201  return;
202  }
203  if (modes.modes[i] == m_desired_mode) valid_video_mode = true;
204  if (verbose)
205  {
206  if (modes.modes[i] == m_desired_mode) cout << mode << " (*)" << endl;
207  else cout << mode << endl;
208  }
209  }
210  if (!valid_video_mode)
211  {
212  cerr << format("[CImageGrabber_dc1394] ERROR: Requested mode %ix%i color_model:%i is not available for this camera.", options.frame_width,options.frame_height, int(options.color_coding) ) << endl;
213  return;
214  }
215 
216  // Reset to bus just in case:
217  // And only once in a program, at start up:
218 // static bool reset_1394bus = true;
219 // if (reset_1394bus)
220  {
221 // reset_1394bus = false;
222 // dc1394_reset_bus(THE_CAMERA);
223  }
224 
225  /*-----------------------------------------------------------------------
226  * setup capture
227  *-----------------------------------------------------------------------*/
228  const int SIZE_RING_BUFFER = options.ring_buffer_size;
229 
230  err=dc1394_video_set_iso_speed(THE_CAMERA, DC1394_ISO_SPEED_400);
231  if (err!=DC1394_SUCCESS)
232  {
233  cerr << "[CImageGrabber_dc1394] ERROR: Could not set iso speed." << endl;
234  return;
235  }
236 
237  err=dc1394_video_set_mode(THE_CAMERA, dc1394video_mode_t(m_desired_mode));
238  // This checking only assures that m_desired_mode is inside dc1394video_mode_t enum range
239  if (err!=DC1394_SUCCESS)
240  {
241  cerr << "[CImageGrabber_dc1394] ERROR: Could not set video mode." << endl;
242  return;
243  }
244 
245 
246  dc1394framerate_t the_framerate;
247  switch( m_options.framerate )
248  {
249  case FRAMERATE_1_875: the_framerate=DC1394_FRAMERATE_1_875; break;
250  case FRAMERATE_3_75: the_framerate=DC1394_FRAMERATE_3_75; break;
251  case FRAMERATE_7_5: the_framerate=DC1394_FRAMERATE_7_5; break;
252  case FRAMERATE_15: the_framerate=DC1394_FRAMERATE_15; break;
253  case FRAMERATE_30: the_framerate=DC1394_FRAMERATE_30; break;
254  case FRAMERATE_60: the_framerate=DC1394_FRAMERATE_60; break;
255  case FRAMERATE_120: the_framerate=DC1394_FRAMERATE_120; break;
256  case FRAMERATE_240: the_framerate=DC1394_FRAMERATE_240; break;
257 
258  default:
259  cerr << "[CImageGrabber_dc1394] ERROR: Requested framerate is not valid." << endl;
260  return;
261  }
262 
263  err=dc1394_video_set_framerate(THE_CAMERA, the_framerate);
264  if (err!=DC1394_SUCCESS)
265  {
266  cerr << "[CImageGrabber_dc1394] ERROR: Could not set framerate." << endl;
267  return;
268  }
269 
270  err=dc1394_capture_setup(THE_CAMERA, SIZE_RING_BUFFER, DC1394_CAPTURE_FLAGS_DEFAULT);
271  if (err!=DC1394_SUCCESS)
272  {
273  cerr << "[CImageGrabber_dc1394] ERROR: Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera." << endl;
274  return;
275  }
276 
277  cout << "------ Other options ------" << endl;
278  uint32_t iso_chan;
279  if ((err = dc1394_video_get_iso_channel(THE_CAMERA, &iso_chan)) == DC1394_SUCCESS)
280  if (verbose)
281  cout << "ISO Channel: " << iso_chan << endl;
282 
283  dc1394speed_t iso_speed;
284  if ((err = dc1394_video_get_iso_speed(THE_CAMERA, &iso_speed)) == DC1394_SUCCESS)
285  if (verbose)
286  cout << "ISO Speed: " << iso_speed << endl;
287 
288  // set trigger options:
289  #define SET_TRIGGER(opt,OPT,TYPE) \
290  if (options.trigger_##opt>=0) \
291  { \
292  err=dc1394_external_trigger_set_##opt(THE_CAMERA, static_cast<dc1394trigger_##opt##_t>(DC1394_TRIGGER_##TYPE##_MIN + options.trigger_##opt)); \
293  DC1394_WRN(err, "[CImageGrabber_dc1394::changeCaptureOptions] Could not set trigger opt"); \
294  }
295  SET_TRIGGER(mode,MODE,MODE)
296  SET_TRIGGER(source,SOURCE,SOURCE)
297  SET_TRIGGER(polarity,POLARITY,ACTIVE)
298  if (options.trigger_power>=0)
299  {
300  err=dc1394_external_trigger_set_power(THE_CAMERA, dc1394switch_t(options.trigger_power));
301  DC1394_WRN(err, "[CImageGrabber_dc1394::changeCaptureOptions] Could not set trigger power");
302  }
303  #undef SET_TRIGGER
304 
305  /*-----------------------------------------------------------------------
306  * have the camera start sending us data
307  *-----------------------------------------------------------------------*/
308  err=dc1394_video_set_transmission(THE_CAMERA, DC1394_ON);
309  if (err!=DC1394_SUCCESS)
310  {
311  cerr << "[CImageGrabber_dc1394] ERROR: Could not start camera iso transmission." << endl;
312  return;
313  }
314 
315  // remember that we successfully initialized everything
316  m_bInitialized = true;
317 
319 
320  // Camera current features:
321  if (verbose)
322  {
323  dc1394featureset_t features;
324  if( (err=dc1394_feature_get_all(THE_CAMERA,&features)) == DC1394_SUCCESS )
325  dc1394_feature_print_all(&features, stdout);
326  }
327 
328 
329 #else
330  THROW_EXCEPTION("[CImageGrabber_dc1394] ERROR: MRPT compiled with MRPT_HAS_LIBDC1394_2=0 !");
331 #endif
332  MRPT_END
333 }
334 
335 /*-------------------------------------------------------------
336  Destructor
337  -------------------------------------------------------------*/
339 {
340 #if MRPT_HAS_LIBDC1394_2
341  m_bInitialized = false;
342  if (THE_CAMERA)
343  {
344  dc1394_video_set_transmission( THE_CAMERA, DC1394_OFF );
345  dc1394_capture_stop( THE_CAMERA );
346 
347  // Release BW:
348 // uint32_t val;
349 // if (dc1394_video_get_bandwidth_usage(THE_CAMERA, &val) == DC1394_SUCCESS)
350 // dc1394_iso_release_bandwidth(THE_CAMERA, val);
351 // if ( dc1394_video_get_iso_channel(THE_CAMERA, &val) == DC1394_SUCCESS)
352 // dc1394_iso_release_channel(THE_CAMERA, val);
353 
354  dc1394_camera_free( THE_CAMERA );
355  }
356  if (THE_CONTEXT)
357  {
358  dc1394_free( THE_CONTEXT );
359  }
360 #endif
361 }
362 
363 
364 /*-------------------------------------------------------------
365  get the image - MONO
366  -------------------------------------------------------------*/
368 {
369  MRPT_START
370 
371  if (!m_bInitialized) return false;
372 
373 #if MRPT_HAS_LIBDC1394_2
374  dc1394video_frame_t *frame=NULL;
375  dc1394error_t err;
376 
377  err=dc1394_video_set_transmission(THE_CAMERA, DC1394_ON);
378  if (err!=DC1394_SUCCESS)
379  {
380  cerr << "[CImageGrabber_dc1394] ERROR: Could not start camera iso transmission." << endl;
381  return false;
382  }
383 
384  // get frame from ring buffer:
385  err = dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
386  //dc1394error_t err=dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_POLL, &frame);
387  if (err!=DC1394_SUCCESS)
388  {
389  cerr << "[CImageGrabber_dc1394] ERROR: Could not capture a frame" << endl;
390  return false;
391  }
392 
393  out_observation.timestamp = mrpt::system::now();
394 
395  const unsigned int width = frame->size[0];
396  const unsigned int height = frame->size[1];
397 
399  {
400  /*-----------------------------------------------------------------------
401  * convert the image from what ever format it is to its RGB8
402  *-----------------------------------------------------------------------*/
403  //dc1394_get_image_size_from_video_mode(THE_CAMERA, m_desired_mode, &width, &height);
404 
405  dc1394video_frame_t *new_frame= static_cast<dc1394video_frame_t*>( calloc(1,sizeof(dc1394video_frame_t)) );
406  new_frame->color_coding=DC1394_COLOR_CODING_RGB8;
407  dc1394_convert_frames(frame, new_frame);
408 
409  // Fill the output class:
410  out_observation.image.loadFromMemoryBuffer(width,height,true, new_frame->image, true /* BGR -> RGB */ );
411 
412  // Free temporary frame:
413  free(new_frame->image);
414  free(new_frame);
415  }
416  else
417  {
418  // Stereo images:
419  dc1394error_t err;
420 
421  uint8_t *imageBuf = new uint8_t[width*height*2];
422  uint8_t *imageBufRGB = new uint8_t[width*height*2*3];
423 
424  if ((err = dc1394_deinterlace_stereo(frame->image, imageBuf, width, 2*height)) != DC1394_SUCCESS)
425  {
426  cerr << "[CImageGrabber_dc1394] ERROR: Could not deinterlace stereo images: " << err << endl;
427  return false;
428  }
429 
430  if ((err = dc1394_bayer_decoding_8bit(imageBuf, imageBufRGB,
431  width, 2*height,
432  DC1394_COLOR_FILTER_GBRG, // Has to be this value for Bumblebee!
433  DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
434  {
435  cerr << "[CImageGrabber_dc1394] ERROR: Could not apply Bayer conversion: " << err << endl;
436  return false;
437  }
438 
439  out_observation.image.loadFromMemoryBuffer(width,height,true, imageBufRGB ); // Left cam.
440  //out_observation.image.loadFromMemoryBuffer(width,height,true, imageBufRGB+ width*height*3 ); // Right cam.
441 
442  delete[] imageBuf;
443  delete[] imageBufRGB;
444  }
445 
446  // Now we can return the frame to the ring buffer:
447  err = dc1394_capture_enqueue(THE_CAMERA, frame);
448  if (err!=DC1394_SUCCESS)
449  {
450  cerr << "[CImageGrabber_dc1394] ERROR: Could not enqueue the ring buffer frame" << endl;
451  return false;
452  }
453 
454  return true;
455 #else
456  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
457 #endif
458  MRPT_END
459 }
460 
461 /*-------------------------------------------------------------
462  get the image - STEREO
463  -------------------------------------------------------------*/
465 {
466  MRPT_START
467 
468  if (!m_bInitialized) return false;
469 
470 #if MRPT_HAS_LIBDC1394_2
471  dc1394video_frame_t *frame=NULL;
472 
473  // get frame from ring buffer:
474  dc1394error_t err=dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
475  if (err!=DC1394_SUCCESS)
476  {
477  cerr << "[CImageGrabber_dc1394] ERROR: Could not capture a frame" << endl;
478  return false;
479  }
480 
481  out_observation.timestamp = mrpt::system::now();
482 
483  const unsigned int width = frame->size[0];
484  const unsigned int height = frame->size[1];
485 
487  {
488  THROW_EXCEPTION("Call to getObservation(stereo) but the camera was not set as stereo!");
489  }
490  else
491  {
492  // Stereo images:
493  dc1394error_t err;
494 
495  uint8_t *imageBuf = new uint8_t[width*height*2];
496  uint8_t *imageBufRGB = new uint8_t[width*height*2*3];
497 
498  if ((err = dc1394_deinterlace_stereo(frame->image, imageBuf, width, 2*height)) != DC1394_SUCCESS)
499  {
500  cerr << "[CImageGrabber_dc1394] ERROR: Could not deinterlace stereo images: " << err << endl;
501  return false;
502  }
503 
504  if ((err = dc1394_bayer_decoding_8bit(imageBuf, imageBufRGB,
505  width, 2*height,
506  DC1394_COLOR_FILTER_GBRG, // Has to be this value for Bumblebee!
507  DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
508  {
509  cerr << "[CImageGrabber_dc1394] ERROR: Could not apply Bayer conversion: " << err << endl;
510  return false;
511  }
512 
513  out_observation.imageLeft.loadFromMemoryBuffer(width,height,true, imageBufRGB ); // Left cam.
514  out_observation.imageRight.loadFromMemoryBuffer(width,height,true, imageBufRGB+ width*height*3 ); // Right cam.
515 
516  delete[] imageBuf;
517  delete[] imageBufRGB;
518  }
519 
520  // Now we can return the frame to the ring buffer:
521  dc1394_capture_enqueue(THE_CAMERA, frame);
522 
523  return true;
524 #else
525  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
526 #endif
527  MRPT_END
528 }
529 
530 
531 /*-------------------------------------------------------------
532  changeCaptureOptions
533  -------------------------------------------------------------*/
535 {
536  MRPT_START
537 
538  if (!m_bInitialized) return false;
539 
540 #if MRPT_HAS_LIBDC1394_2
541  dc1394error_t err;
542  // set features modes:
543  #define SET_MODE(feat,FEAT) \
544  if (options.feat##_mode>=0) \
545  { \
546  err=dc1394_feature_set_mode(THE_CAMERA, DC1394_FEATURE_##FEAT, static_cast<dc1394feature_mode_t>(DC1394_FEATURE_MODE_MIN + options.feat##_mode)); \
547  DC1394_WRN(err, "[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat mode"); \
548  }
549  SET_MODE(shutter,SHUTTER)
550  SET_MODE(gain,GAIN)
551  SET_MODE(gamma,GAMMA)
552  SET_MODE(brightness,BRIGHTNESS)
553  SET_MODE(exposure,EXPOSURE)
554  SET_MODE(sharpness,SHARPNESS)
555  SET_MODE(white_balance,WHITE_BALANCE)
556  #undef SET_MODE
557 
558  // Set features values:
559  #define SET_VALUE(feat,FEAT) \
560  if (options.feat>=0) \
561  { \
562  err=dc1394_feature_set_value(THE_CAMERA, DC1394_FEATURE_##FEAT, options.feat); \
563  DC1394_WRN(err, "[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat value"); \
564  }
565  SET_VALUE(shutter,SHUTTER)
566  SET_VALUE(gain,GAIN)
567  SET_VALUE(gamma,GAMMA)
568  SET_VALUE(brightness,BRIGHTNESS)
569  SET_VALUE(exposure,EXPOSURE)
570  SET_VALUE(sharpness,SHARPNESS)
571  SET_VALUE(white_balance,WHITE_BALANCE)
572  #undef SET_VALUE
573 
574  return true;
575 #else
576  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
577 #endif
578  MRPT_END
579 }
580 
581 /*-------------------------------------------------------------
582  setSoftwareTriggerLevel
583  -------------------------------------------------------------*/
585 {
586  MRPT_START
587 
588  if (!m_bInitialized) return false;
589 
590 #if MRPT_HAS_LIBDC1394_2
591  dc1394error_t err;
592  err = dc1394_software_trigger_set_power(THE_CAMERA, (dc1394switch_t)level);
593  DC1394_WRN(err, "[CImageGrabber_dc1394::setSoftwareTriggerLevel] Could not set software trigger level");
594 
595  return true;
596 #else
597  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
598 #endif
599  MRPT_END
600 }
601 
602 
603 
604 /** Generates a list with the information on all the existing (Firewire) cameras in the system.
605  * \exception std::runtime_error On any error calling libdc1394.
606  */
608 {
609  MRPT_START
610 #if MRPT_HAS_LIBDC1394_2
611 
612  dc1394_t * lib_context = NULL;
613  dc1394camera_list_t * list=NULL;
614  out_list.clear();
615 
616  try
617  {
618  lib_context = dc1394_new ();
619  if (!lib_context)
620  throw std::runtime_error("[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras (Maybe your user has no rights to access IEEE1394?).");
621 
622  // Enumerate cameras:
623  dc1394error_t err;
624 
625  err=dc1394_camera_enumerate(lib_context, &list);
626  if (err!=DC1394_SUCCESS)
627  throw std::runtime_error("[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras (Maybe your user has no rights to access IEEE1394?).");
628 
629  for (unsigned int i=0;i< list->num; i++)
630  {
632 
633  info.guid = list->ids[i].guid;
634  info.unit = list->ids[i].unit;
635 
636  // Try to open it:
637  dc1394camera_t *cam = dc1394_camera_new_unit(lib_context, list->ids[i].guid, list->ids[i].unit);
638  if (!cam)
639  throw std::runtime_error(format("[CImageGrabber_dc1394] ERROR: Failed to query camera with GUID %u\n", static_cast<unsigned int>(list->ids[i].guid) ));
640 
641 
642 
643  info.unit_spec_ID = cam->unit_spec_ID;
644  info.unit_sw_version = cam->unit_sw_version;
645  info.unit_sub_sw_version = cam->unit_sub_sw_version;
646  info.command_registers_base = cam->command_registers_base;
647  info.unit_directory = cam->unit_directory;
648  info.unit_dependent_directory = cam->unit_dependent_directory;
649  info.advanced_features_csr = cam->advanced_features_csr;
650  info.PIO_control_csr = cam->PIO_control_csr;
651  info.SIO_control_csr = cam->SIO_control_csr;
652  info.strobe_control_csr = cam->strobe_control_csr;
653  for (int j=0;j<DC1394_VIDEO_MODE_FORMAT7_NUM;j++)
654  info.format7_csr[j] = cam->format7_csr[j];
655  info.iidc_version = cam->iidc_version;
656  info.vendor = std::string(cam->vendor ? cam->vendor : "");
657  info.model = std::string(cam->model ? cam->model : "");
658  info.vendor_id = cam->vendor_id;
659  info.model_id = cam->model_id;
660  info.bmode_capable = cam->bmode_capable;
661  info.one_shot_capable = cam->one_shot_capable;
662  info.multi_shot_capable = cam->multi_shot_capable;
663  info.can_switch_on_off = cam->can_switch_on_off;
664  info.has_vmode_error_status = cam->has_vmode_error_status;
665  info.has_feature_error_status = cam->has_feature_error_status;
666  info.max_mem_channel = cam->max_mem_channel;
667 
668  //dc1394_camera_print_info(cam,stdout);
669 
670  dc1394_camera_free(cam); // Close camera
671 
672  out_list.push_back(info);
673  }
674 
675  // Free context:
676  dc1394_free( lib_context ); lib_context = NULL;
677  dc1394_camera_free_list(list); list = NULL;
678  }
679  catch(std::exception &e)
680  {
681  if (list) dc1394_camera_free_list(list);
682  if (lib_context) dc1394_free( lib_context );
683 
685  }
686 #else
687  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !")
688 #endif
689  MRPT_END
690 }
691 
692 
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
Options used when creating an dc1394 capture object All but the frame size, framerate, and color_coding can be changed dynamically by CImageGrabber_dc1394::changeCaptureOptions.
unsigned __int16 uint16_t
Definition: rptypes.h:46
mrpt::utils::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
#define THE_CAMERA
static void enumerateCameras(TCameraInfoList &out_list)
Generates a list with the information on all the existing (Firewire) cameras in the system...
#define THROW_EXCEPTION(msg)
bool deinterlace_stereo
For stereo cameras (eg PR Bumblebee)
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
Contains classes for various device interfaces.
#define THE_CONTEXT
STL namespace.
bool getObservation(mrpt::obs::CObservationImage &out_observation)
Grab an image from the opened camera (for monocular cameras).
bool setSoftwareTriggerLevel(bool level)
Changes the boolean level associated to Software Trigger (ON/OFF) Can be used to control camera trigg...
GLenum GLsizei width
Definition: glext.h:3513
unsigned char uint8_t
Definition: rptypes.h:43
void loadFromMemoryBuffer(unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue=false)
Reads the image from raw pixels buffer in memory.
Definition: CImage.cpp:387
#define MRPT_END
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLsizei const GLchar ** string
Definition: glext.h:3919
mrpt::utils::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
grabber_dc1394_color_coding_t color_coding
bool changeCaptureOptions(const TCaptureOptions_dc1394 &options)
Changes the capture properties (brightness, gain, shutter, etc) The frame size, framerate, and color_coding fields in options are ignored since they can be only set at construction time.
#define MRPT_START
GLint mode
Definition: glext.h:5078
unsigned __int64 uint64_t
Definition: rptypes.h:52
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp. Where available, this should contain the accurate satellite-based time...
int frame_height
Capture resolution (Default: 640x480)
backing_store_ptr info
Definition: jmemsys.h:170
#define THROW_STACKED_EXCEPTION(e)
int ring_buffer_size
Size of the libdc1394 ring buffer.
GLint level
Definition: glext.h:3545
GLsizei GLsizei GLchar * source
Definition: glext.h:3908
#define ASSERT_(f)
mrpt::utils::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
bool m_bInitialized
Set to false if we could not initialize the camera.
int mode7
-1: Normal mode, i>=0: use MODE7_i, then frame_width/height and color_coding are ignored.
GLenum GLsizei GLsizei height
Definition: glext.h:3523
unsigned __int32 uint32_t
Definition: rptypes.h:49



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020