26#include <core/exception.h>
27#include <core/exceptions/software.h>
28#include <dc1394/utils.h>
29#include <fvcams/cam_exceptions.h>
30#include <fvutils/system/camargp.h>
31#include <utils/system/console_colors.h>
57 dc1394video_mode_t mode,
85 if ((mode == DC1394_VIDEO_MODE_640x480_YUV422) && (framerate == DC1394_FRAMERATE_30)) {
108 dc1394camera_list_t *list;
110 if (dc1394_camera_enumerate(
_dc1394, &list) != DC1394_SUCCESS) {
111 throw Exception(
"Could not enumerate cameras");
115 if (strcmp(
_model,
"any") == 0) {
121 throw Exception(
"Could not create camera for first foiund camera");
125 for (
unsigned int i = 0; i < list->num; ++i) {
126 dc1394camera_t *tmpcam = dc1394_camera_new(
_dc1394, list->ids[i].guid);
127 if (strcmp(
_model, tmpcam->model) == 0) {
132 dc1394_camera_free(tmpcam);
159 dc1394_video_set_transmission(
_camera, DC1394_OFF);
163 dc1394_iso_release_bandwidth(
_camera, INT_MAX);
164 for (
int channel = 0; channel < 64; ++channel) {
165 dc1394_iso_release_channel(
_camera, channel);
170 if (
_camera->bmode_capable > 0) {
171 dc1394_video_set_operation_mode(
_camera, DC1394_OPERATION_MODE_1394B);
174 ((err = dc1394_video_set_iso_speed(
_camera,
_speed)) != DC1394_SUCCESS)
175 || ((err = dc1394_video_set_mode(
_camera,
_mode)) != DC1394_SUCCESS)
176 || ((err = dc1394_video_set_framerate(
_camera,
_framerate)) != DC1394_SUCCESS)) {
177 throw Exception(
"Setting up the camera failed: %s", dc1394_error_get_string(err));
183 dc1394_format7_get_recommended_packet_size(
_camera,
_mode, &rps);
195 != DC1394_SUCCESS)) {
196 throw Exception(
"Could not setup Format7 parameters: %s", dc1394_error_get_string(err));
230 throw Exception(
"FirewireCamera: Cannot start closed camera");
237 throw Exception(
"FirewireCamera: Could not setup capture (%s)", dc1394_error_get_string(err));
240 if ((err = dc1394_video_set_transmission(
_camera, DC1394_ON)) != DC1394_SUCCESS) {
243 throw Exception(
"FirewireCamera: Could not start ISO transmission (%s)",
244 dc1394_error_get_string(err));
257 dc1394_video_set_transmission(
_camera, DC1394_OFF);
270 dc1394switch_t status;
271 if (dc1394_video_get_transmission(
_camera, &status) != DC1394_SUCCESS) {
272 throw Exception(
"Could not get transmission status");
274 return (status == DC1394_ON);
282 dc1394_camera_print_info(
_camera, stdout);
285 printf(
"Parameters:\n"
286 "valid frame received: %i\n"
288 "auto shutter: %i (shutter value: %u)\n"
289 "auto white balance: %i (white balance value %u/%u)\n"
290 "do set shutter: %i do set white balance: %i\n",
344 != (err = dc1394_capture_dequeue(
_camera, DC1394_CAPTURE_POLICY_WAIT, &
_frame))) {
348 dc1394_error_get_string(err));
377 return _frame->total_bytes;
414 unsigned int width, height;
416 if ((err = dc1394_get_image_size_from_video_mode(
_camera,
_mode, &width, &height))
418 throw Exception(
"FirewireCamera(%s): cannot get width (%s)",
420 dc1394_error_get_string(err));
436 unsigned int width, height;
438 if ((err = dc1394_get_image_size_from_video_mode(
_camera,
_mode, &width, &height))
440 throw Exception(
"FirewireCamera(%s): cannot get width (%s)",
442 dc1394_error_get_string(err));
456 case DC1394_VIDEO_MODE_320x240_YUV422:
457 case DC1394_VIDEO_MODE_640x480_YUV422:
458 case DC1394_VIDEO_MODE_800x600_YUV422:
459 case DC1394_VIDEO_MODE_1024x768_YUV422:
460 case DC1394_VIDEO_MODE_1280x960_YUV422:
461 case DC1394_VIDEO_MODE_1600x1200_YUV422:
return YUV422_PACKED;
463 case DC1394_VIDEO_MODE_640x480_YUV411:
return YUV411_PACKED;
465 case DC1394_VIDEO_MODE_640x480_RGB8:
466 case DC1394_VIDEO_MODE_800x600_RGB8:
467 case DC1394_VIDEO_MODE_1024x768_RGB8:
468 case DC1394_VIDEO_MODE_1280x960_RGB8:
469 case DC1394_VIDEO_MODE_1600x1200_RGB8:
return RGB;
471 case DC1394_VIDEO_MODE_640x480_MONO8:
472 case DC1394_VIDEO_MODE_800x600_MONO8:
473 case DC1394_VIDEO_MODE_1024x768_MONO8:
474 case DC1394_VIDEO_MODE_1280x960_MONO8:
475 case DC1394_VIDEO_MODE_1600x1200_MONO8:
return MONO8;
477 case DC1394_VIDEO_MODE_640x480_MONO16:
478 case DC1394_VIDEO_MODE_800x600_MONO16:
479 case DC1394_VIDEO_MODE_1024x768_MONO16:
480 case DC1394_VIDEO_MODE_1280x960_MONO16:
481 case DC1394_VIDEO_MODE_1600x1200_MONO16:
return MONO16;
483 case DC1394_VIDEO_MODE_FORMAT7_0:
484 case DC1394_VIDEO_MODE_FORMAT7_1:
485 case DC1394_VIDEO_MODE_FORMAT7_2:
486 case DC1394_VIDEO_MODE_FORMAT7_3:
487 case DC1394_VIDEO_MODE_FORMAT7_4:
488 case DC1394_VIDEO_MODE_FORMAT7_5:
489 case DC1394_VIDEO_MODE_FORMAT7_6:
490 case DC1394_VIDEO_MODE_FORMAT7_7:
492 case DC1394_COLOR_CODING_MONO8:
return MONO8;
493 case DC1394_COLOR_CODING_YUV411:
return YUV411_PACKED;
494 case DC1394_COLOR_CODING_YUV422:
return YUV422_PACKED;
495 case DC1394_COLOR_CODING_RGB8:
return RGB;
496 case DC1394_COLOR_CODING_MONO16:
return MONO16;
497 case DC1394_COLOR_CODING_RAW8:
return RAW8;
498 case DC1394_COLOR_CODING_RAW16:
return RAW16;
499 default:
return CS_UNKNOWN;
503 default:
return CS_UNKNOWN;
525 dc1394_feature_set_mode(
_camera,
526 DC1394_FEATURE_FOCUS,
527 enabled ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL))
531 throw Exception(
"FirewireCamera(%s): Setting auto focus failed (%s)",
533 dc1394_error_get_string(err));
546 unsigned int focus = 0;
547 if (dc1394_feature_get_value(
_camera, DC1394_FEATURE_FOCUS, &
focus) == DC1394_SUCCESS) {
557 dc1394_feature_set_value(
_camera, DC1394_FEATURE_FOCUS,
focus);
563 unsigned int min = 0;
564 unsigned int max = 0;
565 if (dc1394_feature_get_boundaries(
_camera, DC1394_FEATURE_FOCUS, &min, &max) == DC1394_SUCCESS) {
575 unsigned int max = 0;
576 unsigned int min = 0;
577 if (dc1394_feature_get_boundaries(
_camera, DC1394_FEATURE_FOCUS, &min, &max) == DC1394_SUCCESS) {
590 if (dc1394_feature_set_mode(
_camera,
591 DC1394_FEATURE_SHUTTER,
592 enabled ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL)
613 if (dc1394_feature_set_value(
_camera, DC1394_FEATURE_SHUTTER,
shutter) != DC1394_SUCCESS) {
624 if (dc1394_feature_get_value(
_camera, DC1394_FEATURE_SHUTTER, &
_shutter) != DC1394_SUCCESS) {
625 throw Exception(
"Failed to retrieve shutter value");
637 if (dc1394_feature_set_mode(
_camera,
638 DC1394_FEATURE_WHITE_BALANCE,
639 enabled ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL)
663 throw Exception(
"Failed to retrieve white balance values");
677 if (dc1394_feature_whitebalance_set_value(
_camera, ub, vr) != DC1394_SUCCESS) {
678 throw Exception(
"Failed to set white balance to ub=%d vr=%d", ub, vr);
690 if (dc1394_feature_get_boundaries(
_camera, DC1394_FEATURE_GAIN, &min, &max) != DC1394_SUCCESS) {
691 throw Exception(
"Failed to get boundaries for feature gain");
699 if (dc1394_feature_set_mode(
_camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL)
701 throw Exception(
"Failed to set manual mode for feature gain");
703 if (dc1394_feature_set_value(
_camera, DC1394_FEATURE_GAIN, gain) != DC1394_SUCCESS) {
704 throw Exception(
"Failed to set value for feature gain");
718 }
else if (f ==
"manual") {
721 char * endptr = NULL;
722 long int focus_value = strtol(f.c_str(), &endptr, 10);
723 if (endptr[0] != 0) {
725 }
else if (focus_value < 0) {
746 string::size_type commapos = w.find(
",", 0);
747 if (commapos == string::npos) {
748 throw Exception(
"Illegal white balance value, neither auto and no comma found");
750 string ub = w.substr(0, commapos);
751 string vr = w.substr(commapos + 1);
753 long int ub_i = strtol(ub.c_str(), &endptr, 10);
754 if (endptr[0] != 0) {
756 "String to int conversion failed");
757 }
else if (ub_i < 0) {
760 long int vr_i = strtol(vr.c_str(), &endptr, 10);
761 if (endptr[0] != 0) {
763 "String to int conversion failed");
764 }
else if (vr_i < 0) {
787 long int tmp = strtol(s.c_str(), &endptr, 10);
788 if (endptr[0] !=
'\0') {
790 "String to int conversion failed");
791 }
else if (tmp < 0) {
855 _mode = DC1394_VIDEO_MODE_640x480_YUV422;
856 _speed = DC1394_ISO_SPEED_400;
869 if (cap->
has(
"mode")) {
870 string m = cap->
get(
"mode");
871 if (m ==
"640x480_MONO16") {
872 _mode = DC1394_VIDEO_MODE_640x480_MONO16;
873 }
else if (m ==
"FORMAT7_0") {
874 _mode = DC1394_VIDEO_MODE_FORMAT7_0;
876 }
else if (m ==
"FORMAT7_1") {
877 _mode = DC1394_VIDEO_MODE_FORMAT7_1;
879 }
else if (m ==
"FORMAT7_2") {
880 _mode = DC1394_VIDEO_MODE_FORMAT7_2;
882 }
else if (m ==
"FORMAT7_3") {
883 _mode = DC1394_VIDEO_MODE_FORMAT7_3;
885 }
else if (m ==
"FORMAT7_4") {
886 _mode = DC1394_VIDEO_MODE_FORMAT7_4;
888 }
else if (m ==
"FORMAT7_5") {
889 _mode = DC1394_VIDEO_MODE_FORMAT7_5;
891 }
else if (m ==
"FORMAT7_6") {
892 _mode = DC1394_VIDEO_MODE_FORMAT7_6;
894 }
else if (m ==
"FORMAT7_7") {
895 _mode = DC1394_VIDEO_MODE_FORMAT7_7;
899 if (cap->
has(
"coding")) {
900 string c = cap->
get(
"coding");
903 }
else if (c ==
"MONO8") {
905 }
else if (c ==
"MONO16") {
907 }
else if (c ==
"RAW16") {
911 if (cap->
has(
"isospeed")) {
912 string s = cap->
get(
"isospeed");
914 _speed = DC1394_ISO_SPEED_400;
915 }
else if (s ==
"800") {
916 _speed = DC1394_ISO_SPEED_800;
919 if (cap->
has(
"framerate")) {
920 string f = cap->
get(
"framerate");
923 }
else if (f ==
"3.75") {
925 }
else if (f ==
"7.5") {
927 }
else if (f ==
"15") {
929 }
else if (f ==
"30") {
931 }
else if (f ==
"60") {
933 }
else if (f ==
"120") {
935 }
else if (f ==
"240") {
939 if (cap->
has(
"focus")) {
942 if (cap->
has(
"nbufs")) {
945 if (cap->
has(
"width")) {
948 if (cap->
has(
"height")) {
951 if (cap->
has(
"startx")) {
954 if (cap->
has(
"starty")) {
957 if (cap->
has(
"packetsize")) {
958 string p = cap->
get(
"packetsize");
959 if (p ==
"recommended") {
965 if (cap->
has(
"gain")) {
966 string g = cap->
get(
"gain");
968 _gain = atoi(g.c_str());
972 if (cap->
has(
"white_balance")) {
975 if (cap->
has(
"shutter")) {
986 dc1394_t * dc1394 = dc1394_new();
987 dc1394camera_list_t *list;
989 if ((err = dc1394_camera_enumerate(dc1394, &list)) != DC1394_SUCCESS) {
990 throw Exception(
"Could not enumerate cameras: %s", dc1394_error_get_string(err));
994 for (
unsigned int i = 0; i < list->num; ++i) {
995 dc1394camera_t *tmpcam = dc1394_camera_new(dc1394, list->ids[i].guid);
996 dc1394_camera_print_info(tmpcam, stdout);
997 dc1394_camera_free(tmpcam);
1000 printf(
"Could not find any cameras\n");
Base class for exceptions in Fawkes.
std::string cam_id() const
Get camera ID.
bool has(std::string s) const
Check if an parameter was given.
std::string get(std::string s) const
Get the value of the given parameter.
Capturing a frame failed.
bool _do_set_shutter
true if the shutter should actually be set, false otherwise
int _format7_starty
Format7 ROI Start Y coordinate.
virtual void white_balance(unsigned int *ub, unsigned int *vr)
Get white balance values.
bool _device_opened
true if device has been opened, false otherwise
virtual void set_auto_focus(bool enabled)
Enable or disable auto focus.
virtual void print_info()
Print out camera information.
virtual unsigned int focus_max()
Get maximum focus value.
virtual const char * model() const
Get camera model.
virtual colorspace_t colorspace()
Colorspace of returned image.
bool _started
true if camera has been started, false otherwise
virtual unsigned int focus()
Get current focus value.
dc1394framerate_t _framerate
DC1394 framerate.
dc1394speed_t _speed
DC1394 speed.
virtual void close()
Close camera.
FirewireCamera(dc1394framerate_t framerate=DC1394_FRAMERATE_30, dc1394video_mode_t mode=DC1394_VIDEO_MODE_640x480_YUV422, dc1394speed_t speed=DC1394_ISO_SPEED_400, int num_buffers=8)
Constructor.
virtual void parse_set_white_balance(const char *white_balance)
Parse white balance and set value.
static void print_available_fwcams()
Print list of cameras.
bool _auto_focus
true if auto focus is enabled, false if disabled
unsigned int _white_balance_ub
White balance U/B value.
virtual unsigned int pixel_width()
Width of image in pixels.
virtual void set_auto_shutter(bool enabled)
Set status of auto shutter.
virtual void set_focus(unsigned int focus)
Set new focus value.
virtual void set_gain(unsigned int gain)
Set the gain.
bool _auto_gain
True, if gain is set automatically.
virtual void open_device()
Open the desired device.
int _format7_width
Format7 width.
int _format7_startx
Format7 ROI Start X coordinate.
virtual void set_image_number(unsigned int n)
Set image number to retrieve.
virtual void set_white_balance(unsigned int ub, unsigned int vr)
Set white balance values.
virtual void set_auto_white_balance(bool enabled)
Set status of auto white balance.
virtual void capture()
Capture an image.
dc1394camera_t * _camera
DC1394 camera handle.
virtual void parse_set_shutter(const char *shutter)
Parse shutter and set value.
virtual unsigned char * buffer()
Get access to current image buffer.
virtual unsigned int shutter()
Get shutter value.
virtual unsigned int pixel_height()
Height of image in pixels.
unsigned int _shutter
Shutter value.
virtual void open()
Open the camera.
virtual unsigned int buffer_size()
Size of buffer.
char * _model
Camera model, used in open to identify the camera, if empty first found camera is used.
virtual bool auto_focus()
Check if auto focus is enabled.
dc1394_t * _dc1394
DC1394 main context.
bool _do_set_focus
true if the focus should actually be set, false otherwise
bool _valid_frame_received
true, if a valid frame has been received, false otherwise
bool _opened
true if camera has been opened, false otherwise
dc1394color_coding_t _format7_coding
Format7 color coding.
int _format7_bpp
Format7 bytes per packet.
bool _format7_mode_enabled
Indicator of Format7 status.
unsigned int _gain
Gain value.
bool iso_mode_enabled()
Check if ISO mode is enabled.
virtual bool ready()
Camera is ready for taking pictures.
virtual bool auto_white_balance()
Get status of auto white balance.
unsigned int _white_balance_vr
White balance V/R value.
virtual void start()
Start image transfer from the camera.
virtual ~FirewireCamera()
Empty destructor.
bool _do_set_white_balance
true if the white balance should actually be set, false otherwise
virtual uint64_t guid() const
Get Firewire GUID of camera.
int _format7_height
Format7 height.
dc1394video_mode_t _mode
DC1394 video mode.
virtual void stop()
Stop image transfer from the camera.
unsigned int _focus
Focus value.
virtual void dispose_buffer()
Dispose current buffer.
virtual void set_shutter(unsigned int shutter)
Set shutter value.
virtual unsigned int focus_min()
Get minimum focus value.
virtual bool auto_shutter()
Get status of auto shutter.
int _num_buffers
Number of DMA buffers.
virtual void parse_set_focus(const char *focus)
Parse focus and set value.
virtual void flush()
Flush image queue.
dc1394video_frame_t * _frame
Last captured DC1394 video frame.
bool _auto_white_balance
true if auto white balance is enabled, false if disabled
bool _auto_shutter
true if auto shutter is enabled, false if disabled
Fawkes library namespace.