12 #include <mrpt/config.h> 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> 27 #define THE_CAMERA static_cast<dc1394camera_t*>(m_dc1394camera) 28 #define THE_CONTEXT static_cast<dc1394_t*>(m_dc1394_lib_context) 33 CImageGrabber_dc1394::CImageGrabber_dc1394(
36 : m_bInitialized(false),
37 m_dc1394_lib_context(nullptr),
38 m_dc1394camera(nullptr),
43 #if MRPT_HAS_LIBDC1394_2 49 dc1394camera_list_t* list;
53 if (err != DC1394_SUCCESS)
55 cerr <<
"[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras " 56 "(Maybe your user has no rights to access IEEE1394?)." 63 cerr <<
"[CImageGrabber_dc1394] ERROR: No cameras found." << endl;
74 dc1394_camera_free_list(list);
75 cerr <<
"[CImageGrabber_dc1394] ERROR: Failed to initialize camera " 77 << list->ids[0].guid <<
"\n";
86 if (list->ids[
i].guid == cameraGUID &&
87 list->ids[
i].unit == cameraUnit)
90 dc1394_camera_new_unit(
THE_CONTEXT, cameraGUID, cameraUnit);
93 dc1394_camera_free_list(list);
94 cerr <<
"[CImageGrabber_dc1394] ERROR: Failed to " 95 "initialize camera with GUID " 96 << list->ids[0].guid <<
"\n";
105 dc1394_camera_free_list(list);
106 cerr <<
"[CImageGrabber_dc1394] ERROR: Camera with GUID=" 107 << cameraGUID <<
" and UNIT=" << cameraUnit <<
" not found.\n";
111 dc1394_camera_free_list(list);
120 dc1394video_modes_t modes;
121 err = dc1394_video_get_supported_modes(
THE_CAMERA, &modes);
122 if (err != DC1394_SUCCESS)
124 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not get list of modes." 130 if (options.
mode7 >= 0)
134 cout <<
"[CImageGrabber_dc1394] Mode is mode7: " << options.
mode7 139 #define TEST_MODE(W, H, COLORMODEL) \ 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; 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)
173 if (verbose) cout <<
"------ Supported video modes ------" << endl;
174 bool valid_video_mode =
false;
178 switch (modes.modes[
i])
180 case DC1394_VIDEO_MODE_160x120_YUV444:
181 mode =
"160x120_YUV444";
183 case DC1394_VIDEO_MODE_320x240_YUV422:
184 mode =
"320x240_YUV422";
186 case DC1394_VIDEO_MODE_640x480_YUV411:
187 mode =
"640x480_YUV411";
189 case DC1394_VIDEO_MODE_640x480_YUV422:
190 mode =
"640x480_YUV422";
192 case DC1394_VIDEO_MODE_640x480_RGB8:
193 mode =
"640x480_RGB8";
195 case DC1394_VIDEO_MODE_640x480_MONO8:
196 mode =
"640x480_MONO8";
198 case DC1394_VIDEO_MODE_640x480_MONO16:
199 mode =
"640x480_MONO16";
201 case DC1394_VIDEO_MODE_800x600_YUV422:
202 mode =
"800x600_YUV422";
204 case DC1394_VIDEO_MODE_800x600_RGB8:
205 mode =
"800x600_RGB8";
207 case DC1394_VIDEO_MODE_800x600_MONO8:
208 mode =
"800x600_MONO8";
210 case DC1394_VIDEO_MODE_1024x768_YUV422:
211 mode =
"1024x768_YUV422";
213 case DC1394_VIDEO_MODE_1024x768_RGB8:
214 mode =
"1024x768_RGB8";
216 case DC1394_VIDEO_MODE_1024x768_MONO8:
217 mode =
"1024x768_MONO8";
219 case DC1394_VIDEO_MODE_800x600_MONO16:
220 mode =
"800x600_MONO16";
222 case DC1394_VIDEO_MODE_1024x768_MONO16:
223 mode =
"1024x768_MONO16";
225 case DC1394_VIDEO_MODE_1280x960_YUV422:
226 mode =
"1280x960_YUV422";
228 case DC1394_VIDEO_MODE_1280x960_RGB8:
229 mode =
"1280x960_RGB8";
231 case DC1394_VIDEO_MODE_1280x960_MONO8:
232 mode =
"1280x960_MONO8";
234 case DC1394_VIDEO_MODE_1600x1200_YUV422:
235 mode =
"1600x1200_YUV422";
237 case DC1394_VIDEO_MODE_1600x1200_RGB8:
238 mode =
"1600x1200_RGB8";
240 case DC1394_VIDEO_MODE_1600x1200_MONO8:
241 mode =
"1600x1200_MONO8";
243 case DC1394_VIDEO_MODE_1280x960_MONO16:
244 mode =
"1280x960_MONO16";
246 case DC1394_VIDEO_MODE_1600x1200_MONO16:
247 mode =
"1600x1200_MONO16";
249 case DC1394_VIDEO_MODE_EXIF:
252 case DC1394_VIDEO_MODE_FORMAT7_0:
255 case DC1394_VIDEO_MODE_FORMAT7_1:
258 case DC1394_VIDEO_MODE_FORMAT7_2:
261 case DC1394_VIDEO_MODE_FORMAT7_3:
264 case DC1394_VIDEO_MODE_FORMAT7_4:
267 case DC1394_VIDEO_MODE_FORMAT7_5:
270 case DC1394_VIDEO_MODE_FORMAT7_6:
273 case DC1394_VIDEO_MODE_FORMAT7_7:
277 cerr <<
"[CImageGrabber_dc1394] ERROR: Requested video mode is " 286 cout <<
mode <<
" (*)" << endl;
288 cout <<
mode << endl;
291 if (!valid_video_mode)
294 "[CImageGrabber_dc1394] ERROR: Requested mode %ix%i " 295 "color_model:%i is not available for this camera.",
316 err = dc1394_video_set_iso_speed(
THE_CAMERA, DC1394_ISO_SPEED_400);
317 if (err != DC1394_SUCCESS)
319 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not set iso speed." 327 if (err != DC1394_SUCCESS)
329 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not set video mode." 334 dc1394framerate_t the_framerate;
338 the_framerate = DC1394_FRAMERATE_1_875;
341 the_framerate = DC1394_FRAMERATE_3_75;
344 the_framerate = DC1394_FRAMERATE_7_5;
347 the_framerate = DC1394_FRAMERATE_15;
350 the_framerate = DC1394_FRAMERATE_30;
353 the_framerate = DC1394_FRAMERATE_60;
356 the_framerate = DC1394_FRAMERATE_120;
359 the_framerate = DC1394_FRAMERATE_240;
363 cerr <<
"[CImageGrabber_dc1394] ERROR: Requested framerate is not " 369 err = dc1394_video_set_framerate(
THE_CAMERA, the_framerate);
370 if (err != DC1394_SUCCESS)
372 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not set framerate." 377 err = dc1394_capture_setup(
378 THE_CAMERA, SIZE_RING_BUFFER, DC1394_CAPTURE_FLAGS_DEFAULT);
379 if (err != DC1394_SUCCESS)
381 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not setup camera-\nmake " 382 "sure that the video mode and framerate are\nsupported by your " 388 cout <<
"------ Other options ------" << endl;
390 if ((err = dc1394_video_get_iso_channel(
THE_CAMERA, &iso_chan)) ==
392 if (verbose) cout <<
"ISO Channel: " << iso_chan << endl;
394 dc1394speed_t iso_speed;
395 if ((err = dc1394_video_get_iso_speed(
THE_CAMERA, &iso_speed)) ==
397 if (verbose) cout <<
"ISO Speed: " << iso_speed << endl;
400 #define SET_TRIGGER(opt, OPT, TYPE) \ 401 if (options.trigger_##opt >= 0) \ 403 err = dc1394_external_trigger_set_##opt( \ 405 static_cast<dc1394trigger_##opt##_t>( \ 406 DC1394_TRIGGER_##TYPE##_MIN + options.trigger_##opt)); \ 409 "[CImageGrabber_dc1394::changeCaptureOptions] Could not set " \ 412 SET_TRIGGER(
mode, MODE, MODE)
413 SET_TRIGGER(
source, SOURCE, SOURCE)
414 SET_TRIGGER(polarity, POLARITY, ACTIVE)
417 err = dc1394_external_trigger_set_power(
421 "[CImageGrabber_dc1394::changeCaptureOptions] Could not set " 429 err = dc1394_video_set_transmission(
THE_CAMERA, DC1394_ON);
430 if (err != DC1394_SUCCESS)
432 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not start camera iso " 446 dc1394featureset_t features;
447 if ((err = dc1394_feature_get_all(
THE_CAMERA, &features)) ==
449 dc1394_feature_print_all(&features, stdout);
454 "[CImageGrabber_dc1394] ERROR: MRPT compiled with " 455 "MRPT_HAS_LIBDC1394_2=0 !");
465 #if MRPT_HAS_LIBDC1394_2 469 dc1394_video_set_transmission(
THE_CAMERA, DC1394_OFF);
500 #if MRPT_HAS_LIBDC1394_2 501 dc1394video_frame_t* frame =
nullptr;
504 err = dc1394_video_set_transmission(
THE_CAMERA, DC1394_ON);
505 if (err != DC1394_SUCCESS)
507 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not start camera iso " 515 dc1394_capture_dequeue(
THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
518 if (err != DC1394_SUCCESS)
520 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not capture a frame" 527 const unsigned int width = frame->size[0];
528 const unsigned int height = frame->size[1];
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);
548 free(new_frame->image);
559 if ((err = dc1394_deinterlace_stereo(
560 frame->image, imageBuf,
width, 2 *
height)) != DC1394_SUCCESS)
562 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not deinterlace " 568 if ((err = dc1394_bayer_decoding_8bit(
570 DC1394_COLOR_FILTER_GBRG,
572 DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
574 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not apply Bayer " 586 delete[] imageBufRGB;
590 err = dc1394_capture_enqueue(
THE_CAMERA, frame);
591 if (err != DC1394_SUCCESS)
593 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not enqueue the ring " 601 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
616 #if MRPT_HAS_LIBDC1394_2 617 dc1394video_frame_t* frame =
nullptr;
621 dc1394_capture_dequeue(
THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
622 if (err != DC1394_SUCCESS)
624 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not capture a frame" 631 const unsigned int width = frame->size[0];
632 const unsigned int height = frame->size[1];
637 "Call to getObservation(stereo) but the camera was not set as " 648 if ((err = dc1394_deinterlace_stereo(
649 frame->image, imageBuf,
width, 2 *
height)) != DC1394_SUCCESS)
651 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not deinterlace " 657 if ((err = dc1394_bayer_decoding_8bit(
659 DC1394_COLOR_FILTER_GBRG,
661 DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
663 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not apply Bayer " 676 delete[] imageBufRGB;
684 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
699 #if MRPT_HAS_LIBDC1394_2 702 #define SET_MODE(feat, FEAT) \ 703 if (options.feat##_mode >= 0) \ 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)); \ 711 "[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat " \ 714 SET_MODE(shutter, SHUTTER)
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)
724 #define SET_VALUE(feat, FEAT) \ 725 if (options.feat >= 0) \ 727 err = dc1394_feature_set_value( \ 728 THE_CAMERA, DC1394_FEATURE_##FEAT, options.feat); \ 731 "[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat " \ 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)
745 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
759 #if MRPT_HAS_LIBDC1394_2 761 err = dc1394_software_trigger_set_power(
THE_CAMERA, (dc1394switch_t)
level);
764 "[CImageGrabber_dc1394::setSoftwareTriggerLevel] Could not set " 765 "software trigger level");
769 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
781 #if MRPT_HAS_LIBDC1394_2 783 dc1394_t* lib_context =
nullptr;
784 dc1394camera_list_t* list =
nullptr;
789 lib_context = dc1394_new();
791 throw std::runtime_error(
792 "[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras " 793 "(Maybe your user has no rights to access IEEE1394?).");
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?).");
804 for (
unsigned int i = 0;
i < list->num;
i++)
808 info.
guid = list->ids[
i].guid;
809 info.
unit = list->ids[
i].unit;
812 dc1394camera_t* cam = dc1394_camera_new_unit(
813 lib_context, list->ids[
i].guid, list->ids[
i].unit);
815 throw std::runtime_error(
817 "[CImageGrabber_dc1394] ERROR: Failed to query camera " 819 static_cast<unsigned int>(list->ids[
i].guid)));
831 for (
int j = 0; j < DC1394_VIDEO_MODE_FORMAT7_NUM; j++)
848 dc1394_camera_free(cam);
850 out_list.push_back(info);
854 dc1394_free(lib_context);
855 lib_context =
nullptr;
856 dc1394_camera_free_list(list);
859 catch (std::exception& e)
861 if (list) dc1394_camera_free_list(list);
862 if (lib_context) dc1394_free(lib_context);
867 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !")
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.
virtual ~CImageGrabber_dc1394()
Destructor.
unsigned __int16 uint16_t
mrpt::utils::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
uint64_t strobe_control_csr
static void enumerateCameras(TCameraInfoList &out_list)
Generates a list with the information on all the existing (Firewire) cameras in the system...
std::list< TCameraInfo > TCameraInfoList
#define THROW_EXCEPTION(msg)
bool deinterlace_stereo
For stereo cameras (eg PR Bumblebee)
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Contains classes for various device interfaces.
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...
uint64_t advanced_features_csr
TCaptureOptions_dc1394 m_options
uint32_t unit_sub_sw_version
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.
uint32_t unit_dependent_directory
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.
int frame_width
Capture resolution (Default: 640x480)
GLsizei const GLchar ** string
bool has_feature_error_status
mrpt::utils::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
void * m_dc1394_lib_context
Internal use:
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.
grabber_dc1394_framerate_t framerate
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Used in enumerateCameras.
bool has_vmode_error_status
#define THROW_STACKED_EXCEPTION(e)
uint32_t command_registers_base
int ring_buffer_size
Size of the libdc1394 ring buffer.
GLsizei GLsizei GLchar * source
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
unsigned __int32 uint32_t