Main MRPT website > C++ reference for MRPT 1.9.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-2018, 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 #define THE_CAMERA static_cast<dc1394camera_t*>(m_dc1394camera)
28 #define THE_CONTEXT static_cast<dc1394_t*>(m_dc1394_lib_context)
29 
30 /*-------------------------------------------------------------
31  Constructor
32  -------------------------------------------------------------*/
33 CImageGrabber_dc1394::CImageGrabber_dc1394(
34  uint64_t cameraGUID, uint16_t cameraUnit,
35  const TCaptureOptions_dc1394& options, bool verbose)
36  : m_bInitialized(false),
37  m_dc1394_lib_context(nullptr),
38  m_dc1394camera(nullptr),
39  m_options(options)
40 {
42 
43 #if MRPT_HAS_LIBDC1394_2
44  // Open lib:
45  m_dc1394_lib_context = dc1394_new();
47 
48  // Enumerate cameras:
49  dc1394camera_list_t* list;
50  dc1394error_t err;
51 
52  err = dc1394_camera_enumerate(THE_CONTEXT, &list);
53  if (err != DC1394_SUCCESS)
54  {
55  cerr << "[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras "
56  "(Maybe your user has no rights to access IEEE1394?)."
57  << endl;
58  return;
59  }
60 
61  if (!list->num)
62  {
63  cerr << "[CImageGrabber_dc1394] ERROR: No cameras found." << endl;
64  return;
65  }
66 
67  // Open the first camera or the one given by the user:
68  if (!cameraGUID)
69  {
70  // Default: first one:
71  m_dc1394camera = dc1394_camera_new(THE_CONTEXT, list->ids[0].guid);
72  if (!THE_CAMERA)
73  {
74  dc1394_camera_free_list(list);
75  cerr << "[CImageGrabber_dc1394] ERROR: Failed to initialize camera "
76  "with GUID "
77  << list->ids[0].guid << "\n";
78  return;
79  }
80  }
81  else
82  {
83  // Look for user-given IDs:
84  for (uint32_t i = 0; i < list->num; i++)
85  {
86  if (list->ids[i].guid == cameraGUID &&
87  list->ids[i].unit == cameraUnit)
88  {
90  dc1394_camera_new_unit(THE_CONTEXT, cameraGUID, cameraUnit);
91  if (!THE_CAMERA)
92  {
93  dc1394_camera_free_list(list);
94  cerr << "[CImageGrabber_dc1394] ERROR: Failed to "
95  "initialize camera with GUID "
96  << list->ids[0].guid << "\n";
97  return;
98  }
99  break;
100  }
101  }
102 
103  if (!m_dc1394camera)
104  {
105  dc1394_camera_free_list(list);
106  cerr << "[CImageGrabber_dc1394] ERROR: Camera with GUID="
107  << cameraGUID << " and UNIT=" << cameraUnit << " not found.\n";
108  return;
109  }
110  }
111  dc1394_camera_free_list(list);
112 
113  // Camera open OK:
114  if (verbose)
115  {
116  dc1394_camera_print_info(THE_CAMERA, stdout);
117  }
118 
119  // get all supported modes:
120  dc1394video_modes_t modes;
121  err = dc1394_video_get_supported_modes(THE_CAMERA, &modes);
122  if (err != DC1394_SUCCESS)
123  {
124  cerr << "[CImageGrabber_dc1394] ERROR: Could not get list of modes."
125  << endl;
126  return;
127  }
128 
129  // Is mode7? treat differently:
130  if (options.mode7 >= 0)
131  {
132  m_desired_mode = DC1394_VIDEO_MODE_FORMAT7_MIN + options.mode7;
133  if (verbose)
134  cout << "[CImageGrabber_dc1394] Mode is mode7: " << options.mode7
135  << endl;
136  }
137  else
138  {
139 #define TEST_MODE(W, H, COLORMODEL) \
140  else if ( \
141  options.frame_width == W && options.frame_height == H && \
142  options.color_coding == COLOR_CODING_##COLORMODEL) m_desired_mode = \
143  DC1394_VIDEO_MODE_##W##x##H##_##COLORMODEL;
144 
145  if (0)
146  {
147  }
148  TEST_MODE(160, 120, YUV444)
149  TEST_MODE(320, 240, YUV422)
150  TEST_MODE(640, 480, YUV411)
151  TEST_MODE(640, 480, YUV422)
152  TEST_MODE(640, 480, RGB8)
153  TEST_MODE(640, 480, MONO8)
154  TEST_MODE(640, 480, MONO16)
155  TEST_MODE(800, 600, YUV422)
156  TEST_MODE(800, 600, RGB8)
157  TEST_MODE(800, 600, MONO8)
158  TEST_MODE(800, 600, MONO16)
159  TEST_MODE(1024, 768, YUV422)
160  TEST_MODE(1024, 768, RGB8)
161  TEST_MODE(1024, 768, MONO8)
162  TEST_MODE(1024, 768, MONO16)
163  TEST_MODE(1280, 960, YUV422)
164  TEST_MODE(1280, 960, RGB8)
165  TEST_MODE(1280, 960, MONO8)
166  TEST_MODE(1280, 960, MONO16)
167  TEST_MODE(1600, 1200, YUV422)
168  TEST_MODE(1600, 1200, RGB8)
169  TEST_MODE(1600, 1200, MONO8)
170  TEST_MODE(1600, 1200, MONO16)
171  }
172  // Display all supported modes and chosen:
173  if (verbose) cout << "------ Supported video modes ------" << endl;
174  bool valid_video_mode = false;
175  for (uint32_t i = 0; i < modes.num; i++)
176  {
177  string mode;
178  switch (modes.modes[i])
179  {
180  case DC1394_VIDEO_MODE_160x120_YUV444:
181  mode = "160x120_YUV444";
182  break;
183  case DC1394_VIDEO_MODE_320x240_YUV422:
184  mode = "320x240_YUV422";
185  break;
186  case DC1394_VIDEO_MODE_640x480_YUV411:
187  mode = "640x480_YUV411";
188  break;
189  case DC1394_VIDEO_MODE_640x480_YUV422:
190  mode = "640x480_YUV422";
191  break;
192  case DC1394_VIDEO_MODE_640x480_RGB8:
193  mode = "640x480_RGB8";
194  break;
195  case DC1394_VIDEO_MODE_640x480_MONO8:
196  mode = "640x480_MONO8";
197  break;
198  case DC1394_VIDEO_MODE_640x480_MONO16:
199  mode = "640x480_MONO16";
200  break;
201  case DC1394_VIDEO_MODE_800x600_YUV422:
202  mode = "800x600_YUV422";
203  break;
204  case DC1394_VIDEO_MODE_800x600_RGB8:
205  mode = "800x600_RGB8";
206  break;
207  case DC1394_VIDEO_MODE_800x600_MONO8:
208  mode = "800x600_MONO8";
209  break;
210  case DC1394_VIDEO_MODE_1024x768_YUV422:
211  mode = "1024x768_YUV422";
212  break;
213  case DC1394_VIDEO_MODE_1024x768_RGB8:
214  mode = "1024x768_RGB8";
215  break;
216  case DC1394_VIDEO_MODE_1024x768_MONO8:
217  mode = "1024x768_MONO8";
218  break;
219  case DC1394_VIDEO_MODE_800x600_MONO16:
220  mode = "800x600_MONO16";
221  break;
222  case DC1394_VIDEO_MODE_1024x768_MONO16:
223  mode = "1024x768_MONO16";
224  break;
225  case DC1394_VIDEO_MODE_1280x960_YUV422:
226  mode = "1280x960_YUV422";
227  break;
228  case DC1394_VIDEO_MODE_1280x960_RGB8:
229  mode = "1280x960_RGB8";
230  break;
231  case DC1394_VIDEO_MODE_1280x960_MONO8:
232  mode = "1280x960_MONO8";
233  break;
234  case DC1394_VIDEO_MODE_1600x1200_YUV422:
235  mode = "1600x1200_YUV422";
236  break;
237  case DC1394_VIDEO_MODE_1600x1200_RGB8:
238  mode = "1600x1200_RGB8";
239  break;
240  case DC1394_VIDEO_MODE_1600x1200_MONO8:
241  mode = "1600x1200_MONO8";
242  break;
243  case DC1394_VIDEO_MODE_1280x960_MONO16:
244  mode = "1280x960_MONO16";
245  break;
246  case DC1394_VIDEO_MODE_1600x1200_MONO16:
247  mode = "1600x1200_MONO16";
248  break;
249  case DC1394_VIDEO_MODE_EXIF:
250  mode = "EXIF";
251  break;
252  case DC1394_VIDEO_MODE_FORMAT7_0:
253  mode = "FORMAT7_0";
254  break;
255  case DC1394_VIDEO_MODE_FORMAT7_1:
256  mode = "FORMAT7_1";
257  break;
258  case DC1394_VIDEO_MODE_FORMAT7_2:
259  mode = "FORMAT7_2";
260  break;
261  case DC1394_VIDEO_MODE_FORMAT7_3:
262  mode = "FORMAT7_3";
263  break;
264  case DC1394_VIDEO_MODE_FORMAT7_4:
265  mode = "FORMAT7_4";
266  break;
267  case DC1394_VIDEO_MODE_FORMAT7_5:
268  mode = "FORMAT7_5";
269  break;
270  case DC1394_VIDEO_MODE_FORMAT7_6:
271  mode = "FORMAT7_6";
272  break;
273  case DC1394_VIDEO_MODE_FORMAT7_7:
274  mode = "FORMAT7_7";
275  break;
276  default:
277  cerr << "[CImageGrabber_dc1394] ERROR: Requested video mode is "
278  "not valid."
279  << endl;
280  return;
281  }
282  if (modes.modes[i] == m_desired_mode) valid_video_mode = true;
283  if (verbose)
284  {
285  if (modes.modes[i] == m_desired_mode)
286  cout << mode << " (*)" << endl;
287  else
288  cout << mode << endl;
289  }
290  }
291  if (!valid_video_mode)
292  {
293  cerr << format(
294  "[CImageGrabber_dc1394] ERROR: Requested mode %ix%i "
295  "color_model:%i is not available for this camera.",
296  options.frame_width, options.frame_height,
297  int(options.color_coding))
298  << endl;
299  return;
300  }
301 
302  // Reset to bus just in case:
303  // And only once in a program, at start up:
304  // static bool reset_1394bus = true;
305  // if (reset_1394bus)
306  {
307  // reset_1394bus = false;
308  // dc1394_reset_bus(THE_CAMERA);
309  }
310 
311  /*-----------------------------------------------------------------------
312  * setup capture
313  *-----------------------------------------------------------------------*/
314  const int SIZE_RING_BUFFER = options.ring_buffer_size;
315 
316  err = dc1394_video_set_iso_speed(THE_CAMERA, DC1394_ISO_SPEED_400);
317  if (err != DC1394_SUCCESS)
318  {
319  cerr << "[CImageGrabber_dc1394] ERROR: Could not set iso speed."
320  << endl;
321  return;
322  }
323 
324  err = dc1394_video_set_mode(THE_CAMERA, dc1394video_mode_t(m_desired_mode));
325  // This checking only assures that m_desired_mode is inside
326  // dc1394video_mode_t enum range
327  if (err != DC1394_SUCCESS)
328  {
329  cerr << "[CImageGrabber_dc1394] ERROR: Could not set video mode."
330  << endl;
331  return;
332  }
333 
334  dc1394framerate_t the_framerate;
335  switch (m_options.framerate)
336  {
337  case FRAMERATE_1_875:
338  the_framerate = DC1394_FRAMERATE_1_875;
339  break;
340  case FRAMERATE_3_75:
341  the_framerate = DC1394_FRAMERATE_3_75;
342  break;
343  case FRAMERATE_7_5:
344  the_framerate = DC1394_FRAMERATE_7_5;
345  break;
346  case FRAMERATE_15:
347  the_framerate = DC1394_FRAMERATE_15;
348  break;
349  case FRAMERATE_30:
350  the_framerate = DC1394_FRAMERATE_30;
351  break;
352  case FRAMERATE_60:
353  the_framerate = DC1394_FRAMERATE_60;
354  break;
355  case FRAMERATE_120:
356  the_framerate = DC1394_FRAMERATE_120;
357  break;
358  case FRAMERATE_240:
359  the_framerate = DC1394_FRAMERATE_240;
360  break;
361 
362  default:
363  cerr << "[CImageGrabber_dc1394] ERROR: Requested framerate is not "
364  "valid."
365  << endl;
366  return;
367  }
368 
369  err = dc1394_video_set_framerate(THE_CAMERA, the_framerate);
370  if (err != DC1394_SUCCESS)
371  {
372  cerr << "[CImageGrabber_dc1394] ERROR: Could not set framerate."
373  << endl;
374  return;
375  }
376 
377  err = dc1394_capture_setup(
378  THE_CAMERA, SIZE_RING_BUFFER, DC1394_CAPTURE_FLAGS_DEFAULT);
379  if (err != DC1394_SUCCESS)
380  {
381  cerr << "[CImageGrabber_dc1394] ERROR: Could not setup camera-\nmake "
382  "sure that the video mode and framerate are\nsupported by your "
383  "camera."
384  << endl;
385  return;
386  }
387 
388  cout << "------ Other options ------" << endl;
389  uint32_t iso_chan;
390  if ((err = dc1394_video_get_iso_channel(THE_CAMERA, &iso_chan)) ==
391  DC1394_SUCCESS)
392  if (verbose) cout << "ISO Channel: " << iso_chan << endl;
393 
394  dc1394speed_t iso_speed;
395  if ((err = dc1394_video_get_iso_speed(THE_CAMERA, &iso_speed)) ==
396  DC1394_SUCCESS)
397  if (verbose) cout << "ISO Speed: " << iso_speed << endl;
398 
399 // set trigger options:
400 #define SET_TRIGGER(opt, OPT, TYPE) \
401  if (options.trigger_##opt >= 0) \
402  { \
403  err = dc1394_external_trigger_set_##opt( \
404  THE_CAMERA, \
405  static_cast<dc1394trigger_##opt##_t>( \
406  DC1394_TRIGGER_##TYPE##_MIN + options.trigger_##opt)); \
407  DC1394_WRN( \
408  err, \
409  "[CImageGrabber_dc1394::changeCaptureOptions] Could not set " \
410  "trigger opt"); \
411  }
412  SET_TRIGGER(mode, MODE, MODE)
413  SET_TRIGGER(source, SOURCE, SOURCE)
414  SET_TRIGGER(polarity, POLARITY, ACTIVE)
415  if (options.trigger_power >= 0)
416  {
417  err = dc1394_external_trigger_set_power(
418  THE_CAMERA, dc1394switch_t(options.trigger_power));
419  DC1394_WRN(
420  err,
421  "[CImageGrabber_dc1394::changeCaptureOptions] Could not set "
422  "trigger power");
423  }
424 #undef SET_TRIGGER
425 
426  /*-----------------------------------------------------------------------
427  * have the camera start sending us data
428  *-----------------------------------------------------------------------*/
429  err = dc1394_video_set_transmission(THE_CAMERA, DC1394_ON);
430  if (err != DC1394_SUCCESS)
431  {
432  cerr << "[CImageGrabber_dc1394] ERROR: Could not start camera iso "
433  "transmission."
434  << endl;
435  return;
436  }
437 
438  // remember that we successfully initialized everything
439  m_bInitialized = true;
440 
442 
443  // Camera current features:
444  if (verbose)
445  {
446  dc1394featureset_t features;
447  if ((err = dc1394_feature_get_all(THE_CAMERA, &features)) ==
448  DC1394_SUCCESS)
449  dc1394_feature_print_all(&features, stdout);
450  }
451 
452 #else
454  "[CImageGrabber_dc1394] ERROR: MRPT compiled with "
455  "MRPT_HAS_LIBDC1394_2=0 !");
456 #endif
457  MRPT_END
458 }
459 
460 /*-------------------------------------------------------------
461  Destructor
462  -------------------------------------------------------------*/
464 {
465 #if MRPT_HAS_LIBDC1394_2
466  m_bInitialized = false;
467  if (THE_CAMERA)
468  {
469  dc1394_video_set_transmission(THE_CAMERA, DC1394_OFF);
470  dc1394_capture_stop(THE_CAMERA);
471 
472  // Release BW:
473  // uint32_t val;
474  // if (dc1394_video_get_bandwidth_usage(THE_CAMERA, &val) ==
475  // DC1394_SUCCESS)
476  // dc1394_iso_release_bandwidth(THE_CAMERA, val);
477  // if ( dc1394_video_get_iso_channel(THE_CAMERA, &val) ==
478  // DC1394_SUCCESS)
479  // dc1394_iso_release_channel(THE_CAMERA, val);
480 
481  dc1394_camera_free(THE_CAMERA);
482  }
483  if (THE_CONTEXT)
484  {
485  dc1394_free(THE_CONTEXT);
486  }
487 #endif
488 }
489 
490 /*-------------------------------------------------------------
491  get the image - MONO
492  -------------------------------------------------------------*/
494  mrpt::obs::CObservationImage& out_observation)
495 {
496  MRPT_START
497 
498  if (!m_bInitialized) return false;
499 
500 #if MRPT_HAS_LIBDC1394_2
501  dc1394video_frame_t* frame = nullptr;
502  dc1394error_t err;
503 
504  err = dc1394_video_set_transmission(THE_CAMERA, DC1394_ON);
505  if (err != DC1394_SUCCESS)
506  {
507  cerr << "[CImageGrabber_dc1394] ERROR: Could not start camera iso "
508  "transmission."
509  << endl;
510  return false;
511  }
512 
513  // get frame from ring buffer:
514  err =
515  dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
516  // dc1394error_t err=dc1394_capture_dequeue(THE_CAMERA,
517  // DC1394_CAPTURE_POLICY_POLL, &frame);
518  if (err != DC1394_SUCCESS)
519  {
520  cerr << "[CImageGrabber_dc1394] ERROR: Could not capture a frame"
521  << endl;
522  return false;
523  }
524 
525  out_observation.timestamp = mrpt::system::now();
526 
527  const unsigned int width = frame->size[0];
528  const unsigned int height = frame->size[1];
529 
531  {
532  /*-----------------------------------------------------------------------
533  * convert the image from what ever format it is to its RGB8
534  *-----------------------------------------------------------------------*/
535  // dc1394_get_image_size_from_video_mode(THE_CAMERA, m_desired_mode,
536  // &width, &height);
537 
538  dc1394video_frame_t* new_frame = static_cast<dc1394video_frame_t*>(
539  calloc(1, sizeof(dc1394video_frame_t)));
540  new_frame->color_coding = DC1394_COLOR_CODING_RGB8;
541  dc1394_convert_frames(frame, new_frame);
542 
543  // Fill the output class:
544  out_observation.image.loadFromMemoryBuffer(
545  width, height, true, new_frame->image, true /* BGR -> RGB */);
546 
547  // Free temporary frame:
548  free(new_frame->image);
549  free(new_frame);
550  }
551  else
552  {
553  // Stereo images:
554  dc1394error_t err;
555 
556  uint8_t* imageBuf = new uint8_t[width * height * 2];
557  uint8_t* imageBufRGB = new uint8_t[width * height * 2 * 3];
558 
559  if ((err = dc1394_deinterlace_stereo(
560  frame->image, imageBuf, width, 2 * height)) != DC1394_SUCCESS)
561  {
562  cerr << "[CImageGrabber_dc1394] ERROR: Could not deinterlace "
563  "stereo images: "
564  << err << endl;
565  return false;
566  }
567 
568  if ((err = dc1394_bayer_decoding_8bit(
569  imageBuf, imageBufRGB, width, 2 * height,
570  DC1394_COLOR_FILTER_GBRG, // Has to be this value for
571  // Bumblebee!
572  DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
573  {
574  cerr << "[CImageGrabber_dc1394] ERROR: Could not apply Bayer "
575  "conversion: "
576  << err << endl;
577  return false;
578  }
579 
580  out_observation.image.loadFromMemoryBuffer(
581  width, height, true, imageBufRGB); // Left cam.
582  // out_observation.image.loadFromMemoryBuffer(width,height,true,
583  // imageBufRGB+ width*height*3 ); // Right cam.
584 
585  delete[] imageBuf;
586  delete[] imageBufRGB;
587  }
588 
589  // Now we can return the frame to the ring buffer:
590  err = dc1394_capture_enqueue(THE_CAMERA, frame);
591  if (err != DC1394_SUCCESS)
592  {
593  cerr << "[CImageGrabber_dc1394] ERROR: Could not enqueue the ring "
594  "buffer frame"
595  << endl;
596  return false;
597  }
598 
599  return true;
600 #else
601  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
602 #endif
603  MRPT_END
604 }
605 
606 /*-------------------------------------------------------------
607  get the image - STEREO
608  -------------------------------------------------------------*/
610  mrpt::obs::CObservationStereoImages& out_observation)
611 {
612  MRPT_START
613 
614  if (!m_bInitialized) return false;
615 
616 #if MRPT_HAS_LIBDC1394_2
617  dc1394video_frame_t* frame = nullptr;
618 
619  // get frame from ring buffer:
620  dc1394error_t err =
621  dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
622  if (err != DC1394_SUCCESS)
623  {
624  cerr << "[CImageGrabber_dc1394] ERROR: Could not capture a frame"
625  << endl;
626  return false;
627  }
628 
629  out_observation.timestamp = mrpt::system::now();
630 
631  const unsigned int width = frame->size[0];
632  const unsigned int height = frame->size[1];
633 
635  {
637  "Call to getObservation(stereo) but the camera was not set as "
638  "stereo!");
639  }
640  else
641  {
642  // Stereo images:
643  dc1394error_t err;
644 
645  uint8_t* imageBuf = new uint8_t[width * height * 2];
646  uint8_t* imageBufRGB = new uint8_t[width * height * 2 * 3];
647 
648  if ((err = dc1394_deinterlace_stereo(
649  frame->image, imageBuf, width, 2 * height)) != DC1394_SUCCESS)
650  {
651  cerr << "[CImageGrabber_dc1394] ERROR: Could not deinterlace "
652  "stereo images: "
653  << err << endl;
654  return false;
655  }
656 
657  if ((err = dc1394_bayer_decoding_8bit(
658  imageBuf, imageBufRGB, width, 2 * height,
659  DC1394_COLOR_FILTER_GBRG, // Has to be this value for
660  // Bumblebee!
661  DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
662  {
663  cerr << "[CImageGrabber_dc1394] ERROR: Could not apply Bayer "
664  "conversion: "
665  << err << endl;
666  return false;
667  }
668 
669  out_observation.imageLeft.loadFromMemoryBuffer(
670  width, height, true, imageBufRGB); // Left cam.
671  out_observation.imageRight.loadFromMemoryBuffer(
672  width, height, true,
673  imageBufRGB + width * height * 3); // Right cam.
674 
675  delete[] imageBuf;
676  delete[] imageBufRGB;
677  }
678 
679  // Now we can return the frame to the ring buffer:
680  dc1394_capture_enqueue(THE_CAMERA, frame);
681 
682  return true;
683 #else
684  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
685 #endif
686  MRPT_END
687 }
688 
689 /*-------------------------------------------------------------
690  changeCaptureOptions
691  -------------------------------------------------------------*/
693  const TCaptureOptions_dc1394& options)
694 {
695  MRPT_START
696 
697  if (!m_bInitialized) return false;
698 
699 #if MRPT_HAS_LIBDC1394_2
700  dc1394error_t err;
701 // set features modes:
702 #define SET_MODE(feat, FEAT) \
703  if (options.feat##_mode >= 0) \
704  { \
705  err = dc1394_feature_set_mode( \
706  THE_CAMERA, DC1394_FEATURE_##FEAT, \
707  static_cast<dc1394feature_mode_t>( \
708  DC1394_FEATURE_MODE_MIN + options.feat##_mode)); \
709  DC1394_WRN( \
710  err, \
711  "[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat " \
712  "mode"); \
713  }
714  SET_MODE(shutter, SHUTTER)
715  SET_MODE(gain, GAIN)
716  SET_MODE(gamma, GAMMA)
717  SET_MODE(brightness, BRIGHTNESS)
718  SET_MODE(exposure, EXPOSURE)
719  SET_MODE(sharpness, SHARPNESS)
720  SET_MODE(white_balance, WHITE_BALANCE)
721 #undef SET_MODE
722 
723 // Set features values:
724 #define SET_VALUE(feat, FEAT) \
725  if (options.feat >= 0) \
726  { \
727  err = dc1394_feature_set_value( \
728  THE_CAMERA, DC1394_FEATURE_##FEAT, options.feat); \
729  DC1394_WRN( \
730  err, \
731  "[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat " \
732  "value"); \
733  }
734  SET_VALUE(shutter, SHUTTER)
735  SET_VALUE(gain, GAIN)
736  SET_VALUE(gamma, GAMMA)
737  SET_VALUE(brightness, BRIGHTNESS)
738  SET_VALUE(exposure, EXPOSURE)
739  SET_VALUE(sharpness, SHARPNESS)
740  SET_VALUE(white_balance, WHITE_BALANCE)
741 #undef SET_VALUE
742 
743  return true;
744 #else
745  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
746 #endif
747  MRPT_END
748 }
749 
750 /*-------------------------------------------------------------
751  setSoftwareTriggerLevel
752  -------------------------------------------------------------*/
754 {
755  MRPT_START
756 
757  if (!m_bInitialized) return false;
758 
759 #if MRPT_HAS_LIBDC1394_2
760  dc1394error_t err;
761  err = dc1394_software_trigger_set_power(THE_CAMERA, (dc1394switch_t)level);
762  DC1394_WRN(
763  err,
764  "[CImageGrabber_dc1394::setSoftwareTriggerLevel] Could not set "
765  "software trigger level");
766 
767  return true;
768 #else
769  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
770 #endif
771  MRPT_END
772 }
773 
774 /** Generates a list with the information on all the existing (Firewire) cameras
775  * in the system.
776  * \exception std::runtime_error On any error calling libdc1394.
777  */
779 {
780  MRPT_START
781 #if MRPT_HAS_LIBDC1394_2
782 
783  dc1394_t* lib_context = nullptr;
784  dc1394camera_list_t* list = nullptr;
785  out_list.clear();
786 
787  try
788  {
789  lib_context = dc1394_new();
790  if (!lib_context)
791  throw std::runtime_error(
792  "[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras "
793  "(Maybe your user has no rights to access IEEE1394?).");
794 
795  // Enumerate cameras:
796  dc1394error_t err;
797 
798  err = dc1394_camera_enumerate(lib_context, &list);
799  if (err != DC1394_SUCCESS)
800  throw std::runtime_error(
801  "[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras "
802  "(Maybe your user has no rights to access IEEE1394?).");
803 
804  for (unsigned int i = 0; i < list->num; i++)
805  {
806  TCameraInfo info;
807 
808  info.guid = list->ids[i].guid;
809  info.unit = list->ids[i].unit;
810 
811  // Try to open it:
812  dc1394camera_t* cam = dc1394_camera_new_unit(
813  lib_context, list->ids[i].guid, list->ids[i].unit);
814  if (!cam)
815  throw std::runtime_error(
816  format(
817  "[CImageGrabber_dc1394] ERROR: Failed to query camera "
818  "with GUID %u\n",
819  static_cast<unsigned int>(list->ids[i].guid)));
820 
821  info.unit_spec_ID = cam->unit_spec_ID;
822  info.unit_sw_version = cam->unit_sw_version;
823  info.unit_sub_sw_version = cam->unit_sub_sw_version;
824  info.command_registers_base = cam->command_registers_base;
825  info.unit_directory = cam->unit_directory;
826  info.unit_dependent_directory = cam->unit_dependent_directory;
827  info.advanced_features_csr = cam->advanced_features_csr;
828  info.PIO_control_csr = cam->PIO_control_csr;
829  info.SIO_control_csr = cam->SIO_control_csr;
830  info.strobe_control_csr = cam->strobe_control_csr;
831  for (int j = 0; j < DC1394_VIDEO_MODE_FORMAT7_NUM; j++)
832  info.format7_csr[j] = cam->format7_csr[j];
833  info.iidc_version = cam->iidc_version;
834  info.vendor = std::string(cam->vendor ? cam->vendor : "");
835  info.model = std::string(cam->model ? cam->model : "");
836  info.vendor_id = cam->vendor_id;
837  info.model_id = cam->model_id;
838  info.bmode_capable = cam->bmode_capable;
839  info.one_shot_capable = cam->one_shot_capable;
840  info.multi_shot_capable = cam->multi_shot_capable;
841  info.can_switch_on_off = cam->can_switch_on_off;
842  info.has_vmode_error_status = cam->has_vmode_error_status;
843  info.has_feature_error_status = cam->has_feature_error_status;
844  info.max_mem_channel = cam->max_mem_channel;
845 
846  // dc1394_camera_print_info(cam,stdout);
847 
848  dc1394_camera_free(cam); // Close camera
849 
850  out_list.push_back(info);
851  }
852 
853  // Free context:
854  dc1394_free(lib_context);
855  lib_context = nullptr;
856  dc1394_camera_free_list(list);
857  list = nullptr;
858  }
859  catch (std::exception& e)
860  {
861  if (list) dc1394_camera_free_list(list);
862  if (lib_context) dc1394_free(lib_context);
863 
865  }
866 #else
867  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
868 #endif
869  MRPT_END
870 }
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.
#define MRPT_START
Definition: exceptions.h:262
unsigned __int16 uint16_t
Definition: rptypes.h:44
#define THE_CAMERA
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
static void enumerateCameras(TCameraInfoList &out_list)
Generates a list with the information on all the existing (Firewire) cameras in the system...
bool deinterlace_stereo
For stereo cameras (eg PR Bumblebee)
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:75
Contains classes for various device interfaces.
#define THE_CONTEXT
#define THROW_STACKED_EXCEPTION(e)
Definition: exceptions.h:53
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:3531
unsigned char uint8_t
Definition: rptypes.h:41
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::img::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
int frame_width
Capture resolution (Default: 640x480)
GLsizei const GLchar ** string
Definition: glext.h:4101
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.
GLint mode
Definition: glext.h:5669
unsigned __int64 uint64_t
Definition: rptypes.h:50
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Definition: CObservation.h:60
#define MRPT_END
Definition: exceptions.h:266
int ring_buffer_size
Size of the libdc1394 ring buffer.
GLint level
Definition: glext.h:3600
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
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:3554
unsigned __int32 uint32_t
Definition: rptypes.h:47
mrpt::img::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
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:385



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019