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> 28 #define THE_CAMERA static_cast<dc1394camera_t*>(m_dc1394camera) 29 #define THE_CONTEXT static_cast<dc1394_t*>(m_dc1394_lib_context) 34 CImageGrabber_dc1394::CImageGrabber_dc1394(
39 m_bInitialized (false),
40 m_dc1394_lib_context(NULL),
41 m_dc1394camera (NULL),
46 #if MRPT_HAS_LIBDC1394_2 52 dc1394camera_list_t * list;
56 if (err!=DC1394_SUCCESS)
58 cerr <<
"[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras (Maybe your user has no rights to access IEEE1394?)." << endl;
64 cerr <<
"[CImageGrabber_dc1394] ERROR: No cameras found." << endl;
75 dc1394_camera_free_list(list);
76 cerr <<
"[CImageGrabber_dc1394] ERROR: Failed to initialize camera with GUID "<<list->ids[0].guid<<
"\n";
85 if (list->ids[
i].guid==cameraGUID && list->ids[
i].unit==cameraUnit)
90 dc1394_camera_free_list(list);
91 cerr <<
"[CImageGrabber_dc1394] ERROR: Failed to initialize camera with GUID "<<list->ids[0].guid<<
"\n";
100 dc1394_camera_free_list(list);
101 cerr <<
"[CImageGrabber_dc1394] ERROR: Camera with GUID="<<cameraGUID<<
" and UNIT="<<cameraUnit<<
" not found.\n";
106 dc1394_camera_free_list(list);
115 dc1394video_modes_t modes;
116 err=dc1394_video_get_supported_modes(
THE_CAMERA, &modes);
117 if (err!=DC1394_SUCCESS)
119 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not get list of modes." << endl;
124 if (options.
mode7>=0)
128 cout <<
"[CImageGrabber_dc1394] Mode is mode7: " << options.
mode7 << endl;
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; 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)
160 if (verbose) cout <<
"------ Supported video modes ------" << endl;
161 bool valid_video_mode =
false;
165 switch( modes.modes[
i] )
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;
200 cerr <<
"[CImageGrabber_dc1394] ERROR: Requested video mode is not valid." << endl;
207 else cout <<
mode << endl;
210 if (!valid_video_mode)
230 err=dc1394_video_set_iso_speed(
THE_CAMERA, DC1394_ISO_SPEED_400);
231 if (err!=DC1394_SUCCESS)
233 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not set iso speed." << endl;
239 if (err!=DC1394_SUCCESS)
241 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not set video mode." << endl;
246 dc1394framerate_t the_framerate;
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;
259 cerr <<
"[CImageGrabber_dc1394] ERROR: Requested framerate is not valid." << endl;
263 err=dc1394_video_set_framerate(
THE_CAMERA, the_framerate);
264 if (err!=DC1394_SUCCESS)
266 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not set framerate." << endl;
270 err=dc1394_capture_setup(
THE_CAMERA, SIZE_RING_BUFFER, DC1394_CAPTURE_FLAGS_DEFAULT);
271 if (err!=DC1394_SUCCESS)
273 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera." << endl;
277 cout <<
"------ Other options ------" << endl;
279 if ((err = dc1394_video_get_iso_channel(
THE_CAMERA, &iso_chan)) == DC1394_SUCCESS)
281 cout <<
"ISO Channel: " << iso_chan << endl;
283 dc1394speed_t iso_speed;
284 if ((err = dc1394_video_get_iso_speed(
THE_CAMERA, &iso_speed)) == DC1394_SUCCESS)
286 cout <<
"ISO Speed: " << iso_speed << endl;
289 #define SET_TRIGGER(opt,OPT,TYPE) \ 290 if (options.trigger_##opt>=0) \ 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"); \ 295 SET_TRIGGER(
mode,MODE,MODE)
296 SET_TRIGGER(
source,SOURCE,SOURCE)
297 SET_TRIGGER(polarity,POLARITY,ACTIVE)
301 DC1394_WRN(err,
"[CImageGrabber_dc1394::changeCaptureOptions] Could not set trigger power");
308 err=dc1394_video_set_transmission(
THE_CAMERA, DC1394_ON);
309 if (err!=DC1394_SUCCESS)
311 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not start camera iso transmission." << endl;
323 dc1394featureset_t features;
324 if( (err=dc1394_feature_get_all(
THE_CAMERA,&features)) == DC1394_SUCCESS )
325 dc1394_feature_print_all(&features, stdout);
330 THROW_EXCEPTION(
"[CImageGrabber_dc1394] ERROR: MRPT compiled with MRPT_HAS_LIBDC1394_2=0 !");
340 #if MRPT_HAS_LIBDC1394_2 344 dc1394_video_set_transmission(
THE_CAMERA, DC1394_OFF );
373 #if MRPT_HAS_LIBDC1394_2 374 dc1394video_frame_t *frame=NULL;
377 err=dc1394_video_set_transmission(
THE_CAMERA, DC1394_ON);
378 if (err!=DC1394_SUCCESS)
380 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not start camera iso transmission." << endl;
385 err = dc1394_capture_dequeue(
THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
387 if (err!=DC1394_SUCCESS)
389 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not capture a frame" << endl;
395 const unsigned int width = frame->size[0];
396 const unsigned int height = frame->size[1];
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);
413 free(new_frame->image);
424 if ((err = dc1394_deinterlace_stereo(frame->image, imageBuf,
width, 2*
height)) != DC1394_SUCCESS)
426 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not deinterlace stereo images: " << err << endl;
430 if ((err = dc1394_bayer_decoding_8bit(imageBuf, imageBufRGB,
432 DC1394_COLOR_FILTER_GBRG,
433 DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
435 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not apply Bayer conversion: " << err << endl;
443 delete[] imageBufRGB;
447 err = dc1394_capture_enqueue(
THE_CAMERA, frame);
448 if (err!=DC1394_SUCCESS)
450 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not enqueue the ring buffer frame" << endl;
456 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
470 #if MRPT_HAS_LIBDC1394_2 471 dc1394video_frame_t *frame=NULL;
474 dc1394error_t err=dc1394_capture_dequeue(
THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame);
475 if (err!=DC1394_SUCCESS)
477 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not capture a frame" << endl;
483 const unsigned int width = frame->size[0];
484 const unsigned int height = frame->size[1];
488 THROW_EXCEPTION(
"Call to getObservation(stereo) but the camera was not set as stereo!");
498 if ((err = dc1394_deinterlace_stereo(frame->image, imageBuf,
width, 2*
height)) != DC1394_SUCCESS)
500 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not deinterlace stereo images: " << err << endl;
504 if ((err = dc1394_bayer_decoding_8bit(imageBuf, imageBufRGB,
506 DC1394_COLOR_FILTER_GBRG,
507 DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS)
509 cerr <<
"[CImageGrabber_dc1394] ERROR: Could not apply Bayer conversion: " << err << endl;
517 delete[] imageBufRGB;
525 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
540 #if MRPT_HAS_LIBDC1394_2 543 #define SET_MODE(feat,FEAT) \ 544 if (options.feat##_mode>=0) \ 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"); \ 549 SET_MODE(shutter,SHUTTER)
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)
559 #define SET_VALUE(feat,FEAT) \ 560 if (options.feat>=0) \ 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"); \ 565 SET_VALUE(shutter,SHUTTER)
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)
576 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
590 #if MRPT_HAS_LIBDC1394_2 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");
597 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !");
610 #if MRPT_HAS_LIBDC1394_2 612 dc1394_t * lib_context = NULL;
613 dc1394camera_list_t * list=NULL;
618 lib_context = dc1394_new ();
620 throw std::runtime_error(
"[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras (Maybe your user has no rights to access IEEE1394?).");
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?).");
629 for (
unsigned int i=0;
i< list->num;
i++)
633 info.guid = list->ids[
i].guid;
634 info.unit = list->ids[
i].unit;
637 dc1394camera_t *cam = dc1394_camera_new_unit(lib_context, list->ids[
i].guid, list->ids[
i].unit);
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) ));
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;
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;
670 dc1394_camera_free(cam);
672 out_list.push_back(
info);
676 dc1394_free( lib_context ); lib_context = NULL;
677 dc1394_camera_free_list(list); list = NULL;
679 catch(std::exception &e)
681 if (list) dc1394_camera_free_list(list);
682 if (lib_context) dc1394_free( lib_context );
687 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)
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...
TCaptureOptions_dc1394 m_options
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.
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.
GLsizei const GLchar ** string
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. Where available, this should contain the accurate satellite-based time...
Used in enumerateCameras.
int frame_height
Capture resolution (Default: 640x480)
#define THROW_STACKED_EXCEPTION(e)
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