void setSetting(dc1394camera_t *cam, dc1394feature_t setting, float value){ dc1394error_t err; float min, max; dc1394_feature_get_absolute_boundaries(cam, setting, &min, &max); if(value < min || value > max){ std::cerr << "CameraIIDC: cannot set value. Out of permissable range." << std::endl; } else { err = dc1394_feature_set_absolute_control(cam, setting, DC1394_ON); if(err != DC1394_SUCCESS) cerr << "Could not enable absolute control!" << endl; err = dc1394_feature_set_absolute_value(cam, setting, value); if(err != DC1394_SUCCESS) cerr << "Could not set absolute value!" << endl; } }
void FirewireVideo::SetShutterTime(float val){ dc1394error_t err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); if (err < 0) { throw VideoException("Could not set manual shutter mode"); } err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_ON); if (err < 0) { throw VideoException("Could not set absolute control for shutter"); } err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, val); if (err < 0) { throw VideoException("Could not set shutter value"); } }
void FirewireVideo::SetGain(float val){ dc1394error_t err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); if (err < 0) { throw VideoException("Could not set manual gain mode"); } err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_GAIN, DC1394_ON); if (err < 0) { throw VideoException("Could not set absolute control for gain"); } err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_GAIN, val); if (err < 0) { throw VideoException("Could not set gain value"); } }
int main(int argc, char *argv[]) { fitsfile *fptr; long fpixel=1, nelements, naxes[3]; dc1394camera_t *camera; int grab_n_frames; struct timeval start_time, end_time; time_t start_sec, end_sec; suseconds_t start_usec, end_usec; float elapsed_time, fps; int i, status; unsigned int min_bytes, max_bytes, max_height, max_width; unsigned int actual_bytes; uint64_t total_bytes = 0; unsigned int width, height; dc1394video_frame_t *frame=NULL; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; char *filename; grab_n_frames = atoi(argv[1]); filename = argv[2]; width = 320; height = 240; naxes[0] = width; naxes[1] = height; naxes[2] = grab_n_frames; nelements = naxes[0]*naxes[1]*naxes[2]; stderr = freopen("grab_cube.log", "w", stderr); d = dc1394_new (); if (!d) return 1; err=dc1394_camera_enumerate (d, &list); DC1394_ERR_RTN(err,"Failed to enumerate cameras"); if (list->num == 0) { dc1394_log_error("No cameras found"); return 1; } camera = dc1394_camera_new (d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %"PRIx64, list->ids[0].guid); return 1; } dc1394_camera_free_list (list); printf("Using camera with GUID %"PRIx64"\n", camera->guid); /*----------------------------------------------------------------------- * setup capture for format 7 *-----------------------------------------------------------------------*/ // err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); // DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot operate at 1394B"); // libdc1394 doesn't work well with firewire800 yet so set to legacy 400 mode dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); // configure camera for format7 err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot choose format7_0"); printf ("I: video mode is format7_0\n"); err = dc1394_format7_get_max_image_size (camera, DC1394_VIDEO_MODE_FORMAT7_1, &max_width, &max_height); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get max image size for format7_0"); printf ("I: max image size is: height = %d, width = %d\n", max_height, max_width); printf ("I: current image size is: height = %d, width = %d\n", height, width); err = dc1394_format7_set_roi (camera, DC1394_VIDEO_MODE_FORMAT7_1, DC1394_COLOR_CODING_MONO16, // not sure why RAW8/16 don't work DC1394_USE_MAX_AVAIL, 0, 0, // left, top width, height); // width, height DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set roi"); printf ("I: roi is (0, 0) - (%d, %d)\n", width, height); // set the frame rate to absolute value in frames/sec err = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_FRAME_RATE, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, 330.0); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate"); printf("I: framerate is %f fps\n", 330.0); // set the shutter speed to absolute value in seconds err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, 3.0e-3); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter"); printf("I: exptime is %f s\n", 3.0e-3); // set gain manually. use relative value here in range 48 to 730. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 400); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain"); printf ("I: gain is %d\n", 400); // set brightness manually. use relative value in range 0 to 1023. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 100); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness"); printf ("I: brightness is %d\n", 100); err = dc1394_format7_get_total_bytes (camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get total bytes"); printf ("I: total bytes is %"PRIu64" before SFF enabled\n", total_bytes); err=dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing"); /*----------------------------------------------------------------------- * print allowed and used packet size *-----------------------------------------------------------------------*/ err=dc1394_format7_get_packet_parameters(camera, DC1394_VIDEO_MODE_FORMAT7_1, &min_bytes, &max_bytes); DC1394_ERR_RTN(err,"Packet para inq error"); printf( "camera reports allowed packet size from %d - %d bytes\n", min_bytes, max_bytes); err=dc1394_format7_get_packet_size(camera, DC1394_VIDEO_MODE_FORMAT7_1, &actual_bytes); DC1394_ERR_RTN(err,"dc1394_format7_get_packet_size error"); printf( "camera reports actual packet size = %d bytes\n", actual_bytes); err=dc1394_format7_get_total_bytes(camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes); DC1394_ERR_RTN(err,"dc1394_query_format7_total_bytes error"); printf( "camera reports total bytes per frame = %"PRId64" bytes\n", total_bytes); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err=dc1394_video_set_transmission(camera,DC1394_ON); if (err!=DC1394_SUCCESS) { dc1394_log_error("unable to start camera iso transmission"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } // set up FITS image and capture fits_create_file(&fptr, filename, &status); dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1, &width, &height); fits_create_img(fptr, USHORT_IMG, 3, naxes, &status); /*----------------------------------------------------------------------- * capture frames and measure the time for this operation *-----------------------------------------------------------------------*/ gettimeofday(&start_time, NULL); printf("Start capture:\n"); for( i = 0; i < grab_n_frames; ++i) { /*----------------------------------------------------------------------- * capture one frame *-----------------------------------------------------------------------*/ err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); if (err!=DC1394_SUCCESS) { dc1394_log_error("unable to capture"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } // attempt to preallocate image array and write to memory before dumping to disk. // turns out to be slow due to large size of images. cfitsio buffering is far // more efficient. //memcpy(im_buffer+2*i*naxes[0]*naxes[1], //frame->image-1, //naxes[0]*naxes[1]*sizeof(short)); // just writing each frame to the FITS file goes pretty fast fits_write_img(fptr, TUSHORT, fpixel+i*naxes[0]*naxes[1], naxes[0]*naxes[1], frame->image-1, &status); // release buffer dc1394_capture_enqueue(camera,frame); } gettimeofday(&end_time, NULL); printf("End capture.\n"); /*----------------------------------------------------------------------- * stop data transmission *-----------------------------------------------------------------------*/ start_sec = start_time.tv_sec; start_usec = start_time.tv_usec; end_sec = end_time.tv_sec; end_usec = end_time.tv_usec; elapsed_time = (float)((end_sec + 1.0e-6*end_usec) - (start_sec + 1.0e-6*start_usec)); fps = grab_n_frames/elapsed_time; printf("Elapsed time = %g seconds.\n", elapsed_time); printf("Framerate = %g fps.\n", fps); err=dc1394_video_set_transmission(camera,DC1394_OFF); DC1394_ERR_RTN(err,"couldn't stop the camera?"); /*----------------------------------------------------------------------- * save FITS image to disk *-----------------------------------------------------------------------*/ //fits_write_img(fptr, TUSHORT, fpixel, naxes[0]*naxes[1]*naxes[2], im_buffer, &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); //free(im_buffer); printf("wrote: %s\n", filename); printf("Image is %d bits/pixel.\n", frame->data_depth); /*----------------------------------------------------------------------- * close camera, cleanup *-----------------------------------------------------------------------*/ dc1394_capture_stop(camera); dc1394_video_set_transmission(camera, DC1394_OFF); dc1394_camera_free(camera); dc1394_free (d); return 0; }
bool CvCaptureCAM_DC1394_v2_CPP::setProperty(int propId, double value) { switch (propId) { case CV_CAP_PROP_FRAME_WIDTH: if(started) return false; frameWidth = cvRound(value); frameHeight = 0; break; case CV_CAP_PROP_FRAME_HEIGHT: if(started) return false; frameWidth = 0; frameHeight = cvRound(value); break; case CV_CAP_PROP_FPS: if(started) return false; fps = value; break; case CV_CAP_PROP_RECTIFICATION: if( cameraId != VIDERE ) return false; rectify = fabs(value) > FLT_EPSILON; break; case CV_CAP_PROP_MODE: if(started) return false; userMode = cvRound(value); break; case CV_CAP_PROP_ISO_SPEED: if(started) return false; isoSpeed = cvRound(value); break; //The code below is based on coriander, callbacks.c:795, refer to case RANGE_MENU_MAN : default: if (propId<CV_CAP_PROP_MAX_DC1394 && dc1394properties[propId]!=-1 && dcCam) { //get the corresponding feature from property-id dc1394feature_info_t *act_feature = &feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN]; if (cvRound(value) == CV_CAP_PROP_DC1394_OFF) { if ( (act_feature->on_off_capable) && (dc1394_feature_set_power(dcCam, act_feature->id, DC1394_OFF) == DC1394_SUCCESS)) { act_feature->is_on=DC1394_OFF; return true; } return false; } //try to turn the feature ON, feature can be ON and at the same time it can be not capable to change state to OFF if ( (act_feature->is_on == DC1394_OFF) && (act_feature->on_off_capable == DC1394_TRUE)) { if (dc1394_feature_set_power(dcCam, act_feature->id, DC1394_ON) == DC1394_SUCCESS) feature_set.feature[dc1394properties[propId]-DC1394_FEATURE_MIN].is_on=DC1394_ON; } //turn off absolute mode - the actual value will be stored in the value field, //otherwise it would be stored into CSR (control and status register) absolute value if (act_feature->absolute_capable && dc1394_feature_set_absolute_control(dcCam, act_feature->id, DC1394_OFF) !=DC1394_SUCCESS) return false; else act_feature->abs_control=DC1394_OFF; //set AUTO if (cvRound(value) == CV_CAP_PROP_DC1394_MODE_AUTO) { if (dc1394_feature_set_mode(dcCam, act_feature->id, DC1394_FEATURE_MODE_AUTO)!=DC1394_SUCCESS) return false; act_feature->current_mode=DC1394_FEATURE_MODE_AUTO; return true; } //set ONE PUSH if (cvRound(value) == CV_CAP_PROP_DC1394_MODE_ONE_PUSH_AUTO) { //have to set to manual first, otherwise one push will be ignored (AVT manual 4.3.0 p. 115) if (dc1394_feature_set_mode(dcCam, act_feature->id, DC1394_FEATURE_MODE_ONE_PUSH_AUTO)!=DC1394_SUCCESS) return false; //will change to act_feature->current_mode=DC1394_FEATURE_MODE_ONE_PUSH_AUTO; return true; } //set the feature to MANUAL mode, if (dc1394_feature_set_mode(dcCam, act_feature->id, DC1394_FEATURE_MODE_MANUAL)!=DC1394_SUCCESS) return false; else act_feature->current_mode=DC1394_FEATURE_MODE_MANUAL; // if property is one of the white balance features treat it in different way if (propId == CV_CAP_PROP_WHITE_BALANCE_BLUE_U) { if (dc1394_feature_whitebalance_set_value(dcCam,cvRound(value), act_feature->RV_value)!=DC1394_SUCCESS) return false; else { act_feature->BU_value = cvRound(value); return true; } } if (propId == CV_CAP_PROP_WHITE_BALANCE_RED_V) { if (dc1394_feature_whitebalance_set_value(dcCam, act_feature->BU_value, cvRound(value))!=DC1394_SUCCESS) return false; else { act_feature->RV_value = cvRound(value); return true; } } //first: check boundaries if (value < act_feature->min) { value = act_feature->min; } else if (value > act_feature->max) { value = act_feature->max; } if (dc1394_feature_set_value(dcCam, act_feature->id, cvRound(value)) == DC1394_SUCCESS) { act_feature->value = value; return true; } } return false; } return true; }
/************************************************************************************** ** Client is asking us to start an exposure ***************************************************************************************/ bool FFMVCCD::StartExposure(float duration) { FILE *imagefile; dc1394error_t err; dc1394video_frame_t *frame; int i; int ms; unsigned int val; float gain = 1.0; uint32_t uwidth, uheight; float sub_length; float fval; ms = duration* 1000; //IDMessage(getDeviceName(), "Doing %d sub exposures at %f %s each", sub_count, absShutter, prop_info.pUnits); ExposureRequest=duration; // Since we have only have one CCD with one chip, we set the exposure duration of the primary CCD PrimaryCCD.setBPP(16); PrimaryCCD.setExposureDuration(duration); gettimeofday(&ExpStart,NULL); InExposure=true; IDMessage(getDeviceName(), "Exposure has begun."); // Let's get a pointer to the frame buffer uint8_t * image = PrimaryCCD.getFrameBuffer(); // Get width and height int width = PrimaryCCD.getSubW() / PrimaryCCD.getBinX(); int height = PrimaryCCD.getSubH() / PrimaryCCD.getBinY(); memset(image, 0, PrimaryCCD.getFrameBufferSize()); if (duration != last_exposure_length) { /* Calculate the number of exposures needed */ sub_count = duration / max_exposure; if (ms % ((int) (max_exposure * 1000))) { ++sub_count; } sub_length = duration / sub_count; IDMessage(getDeviceName(), "Triggering a %f second exposure using %d subs of %f seconds", duration, sub_count, sub_length); /* Set sub length */ #if 0 err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_SHUTTER, DC1394_ON); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Failed to enable ansolute shutter control."); } #endif err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_SHUTTER, sub_length); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to set shutter value."); } err = dc1394_feature_get_absolute_value(dcam, DC1394_FEATURE_SHUTTER, &fval); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to get shutter value."); } IDMessage(getDeviceName(), "Shutter value is %f.", fval); } /* Flush the DMA buffer */ while (1) { err=dc1394_capture_dequeue(dcam, DC1394_CAPTURE_POLICY_POLL, &frame); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Flushing DMA buffer failed!"); break; } if (!frame) { break; } dc1394_capture_enqueue(dcam, frame); } /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ IDMessage(getDeviceName(), "start transmission"); err=dc1394_video_set_transmission(dcam, DC1394_ON); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to start transmission"); return false; } // We're done return true; }
/************************************************************************************** ** Client is asking us to establish connection to the device ***************************************************************************************/ bool FFMVCCD::Connect() { dc1394camera_list_t *list; dc1394error_t err; bool supported; bool settings_valid; uint32_t val; dc1394format7mode_t fm7; dc1394feature_info_t feature; float min, max; dc1394 = dc1394_new(); if (!dc1394) { return false; } err = dc1394_camera_enumerate(dc1394, &list); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Could not find DC1394 cameras!"); return false; } if (!list->num) { IDMessage(getDeviceName(), "No DC1394 cameras found!"); return false; } dcam = dc1394_camera_new(dc1394, list->ids[0].guid); if (!dcam) { IDMessage(getDeviceName(), "Unable to connect to camera!"); return false; } /* Reset camera */ err = dc1394_camera_reset(dcam); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to reset camera!"); return false; } /* Set mode */ err = dc1394_video_set_mode(dcam, DC1394_VIDEO_MODE_640x480_MONO16); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to connect to set videomode!"); return false; } /* Disable Auto exposure control */ err = dc1394_feature_set_power(dcam, DC1394_FEATURE_EXPOSURE, DC1394_OFF); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to disable auto exposure control"); return false; } /* Set frame rate to the lowest possible */ err = dc1394_video_set_framerate(dcam, DC1394_FRAMERATE_7_5); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to connect to set framerate!"); return false; } /* Turn frame rate control off to enable extended exposure (subs of 512ms) */ err = dc1394_feature_set_power(dcam, DC1394_FEATURE_FRAME_RATE, DC1394_OFF); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to disable framerate!"); return false; } /* Get the longest possible exposure length */ err = dc1394_feature_set_mode(dcam, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Failed to enable manual shutter control."); } err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_SHUTTER, DC1394_ON); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Failed to enable absolute shutter control."); } err = dc1394_feature_get_absolute_boundaries(dcam, DC1394_FEATURE_SHUTTER, &min, &max); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Could not get max shutter length"); } else { max_exposure = max; } /* Set gain to max. By setting the register directly, we can achieve a * gain of 24 dB...compared to a gain of 12 dB which is reported as the * max */ err = dc1394_set_control_register(dcam, 0x820, 0x40); //err = dc1394_set_control_register(dcam, 0x820, 0x7f); if (err != DC1394_SUCCESS) { return err; } #if 0 /* Set absolute gain to max */ err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_GAIN, DC1394_ON); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Failed to enable ansolute gain control."); } err = dc1394_feature_get_absolute_boundaries(dcam, DC1394_FEATURE_GAIN, &min, &max); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Could not get max gain value"); } else { err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_GAIN, max); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Could not set max gain value"); } } #endif /* Set brightness */ err = dc1394_feature_set_mode(dcam, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Failed to enable manual brightness control."); } err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_BRIGHTNESS, DC1394_ON); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Failed to enable ansolute brightness control."); } err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_BRIGHTNESS, 1); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Could not set max brightness value"); } /* Turn gamma control off */ err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_GAMMA, 1); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Could not set gamma value"); } err = dc1394_feature_set_power(dcam, DC1394_FEATURE_GAMMA, DC1394_OFF); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to disable gamma!"); return false; } /* Turn off white balance */ err = dc1394_feature_set_power(dcam, DC1394_FEATURE_WHITE_BALANCE, DC1394_OFF); if (err != DC1394_SUCCESS) { IDMessage(getDeviceName(), "Unable to disable white balance!"); return false; } err=dc1394_capture_setup(dcam,10, DC1394_CAPTURE_FLAGS_DEFAULT); return true; }
int main(int argc, char *argv[]) { fitsfile *fptr; long fpixel=1, nelements, naxes[2]; dc1394camera_t *camera; int grab_n_frames; struct timeval start_time, end_time; int i, j, status; unsigned int max_height, max_width; uint64_t total_bytes = 0; unsigned int width, height; dc1394video_frame_t *frame=NULL; dc1394_t * d; dc1394camera_list_t * list; dc1394error_t err; char *filename; unsigned char *buffer; float *average; grab_n_frames = atoi(argv[1]); filename = argv[2]; status = 0; width = 320; height = 240; naxes[0] = width; naxes[1] = height; nelements = naxes[0]*naxes[1]; stderr = freopen("grab_cube.log", "w", stderr); d = dc1394_new(); if (!d) return 1; err = dc1394_camera_enumerate(d, &list); DC1394_ERR_RTN(err, "Failed to enumerate cameras."); if (list->num == 0) { dc1394_log_error("No cameras found."); return 1; } camera = dc1394_camera_new (d, list->ids[0].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %"PRIx64, list->ids[0].guid); return 1; } dc1394_camera_free_list(list); printf("Using camera with GUID %"PRIx64"\n", camera->guid); /*----------------------------------------------------------------------- * setup capture for format 7 *-----------------------------------------------------------------------*/ // err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); // DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot operate at 1394B"); // libdc1394 doesn't work well with firewire800 yet so set to legacy 400 mode dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); // configure camera for format7 err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot choose format7_0"); err = dc1394_format7_get_max_image_size (camera, DC1394_VIDEO_MODE_FORMAT7_1, &max_width, &max_height); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get max image size for format7_0"); err = dc1394_format7_set_roi (camera, DC1394_VIDEO_MODE_FORMAT7_1, DC1394_COLOR_CODING_MONO8, // not sure why RAW8/16 don't work DC1394_USE_MAX_AVAIL, 0, 0, // left, top width, height); // width, height DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set roi"); // set the frame rate to absolute value in frames/sec err = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_FRAME_RATE, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, 100.0); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate"); // set the shutter speed to absolute value in seconds err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to manual"); err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_TRUE); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to absolute mode"); err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, 1.0e-2); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter"); // set gain manually. use relative value here in range 48 to 730. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 200); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain"); // set brightness manually. use relative value in range 0 to 1023. err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness to manual"); err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 50); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness"); err = dc1394_format7_get_total_bytes (camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes); DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get total bytes"); // err = dc1394_feature_set_value (camera, DC1394_FEATURE_GAIN, 24); //DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error setting gain"); err = dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT); DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing"); /*----------------------------------------------------------------------- * have the camera start sending us data *-----------------------------------------------------------------------*/ err = dc1394_video_set_transmission(camera, DC1394_ON); if (err != DC1394_SUCCESS) { dc1394_log_error("unable to start camera iso transmission"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } /* allocate the buffers */ if (!(buffer = malloc(nelements*sizeof(char)))) { printf("Couldn't Allocate Image Buffer\n"); exit(-1); } if (!(average = calloc(nelements, sizeof(float)))) { printf("Couldn't Allocate Average Image Buffer\n"); exit(-1); } // set up FITS image and capture fits_create_file(&fptr, filename, &status); dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1, &width, &height); fits_create_img(fptr, FLOAT_IMG, 2, naxes, &status); /*----------------------------------------------------------------------- * capture frames and measure the time for this operation *-----------------------------------------------------------------------*/ gettimeofday(&start_time, NULL); printf("Start capture:\n"); for (i=0; i<grab_n_frames; ++i) { /*----------------------------------------------------------------------- * capture one frame *-----------------------------------------------------------------------*/ err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); if (err != DC1394_SUCCESS) { dc1394_log_error("unable to capture"); dc1394_capture_stop(camera); dc1394_camera_free(camera); exit(1); } memcpy(buffer, frame->image, nelements*sizeof(char)); // release buffer dc1394_capture_enqueue(camera,frame); for (j=0; j<nelements; j++) { average[j] += (1.0/grab_n_frames)*(buffer[j]); } } gettimeofday(&end_time, NULL); printf("End capture.\n"); /*----------------------------------------------------------------------- * stop data transmission *-----------------------------------------------------------------------*/ err = dc1394_video_set_transmission(camera, DC1394_OFF); DC1394_ERR_RTN(err,"couldn't stop the camera?"); /*----------------------------------------------------------------------- * save FITS image to disk *-----------------------------------------------------------------------*/ fits_write_img(fptr, TFLOAT, fpixel, naxes[0]*naxes[1], average, &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); free(buffer); free(average); printf("wrote: %s\n", filename); printf("Readout is %d bits/pixel.\n", frame->data_depth); /*----------------------------------------------------------------------- * close camera, cleanup *-----------------------------------------------------------------------*/ dc1394_capture_stop(camera); dc1394_video_set_transmission(camera, DC1394_OFF); dc1394_camera_free(camera); dc1394_free(d); return 0; }
// Note: // the following was tested on a IIDC camera over USB therefore might not work as // well on a camera over proper firewire transport void FirewireVideo::init_format7_camera( uint64_t guid, int dma_frames, dc1394speed_t iso_speed, dc1394video_mode_t video_mode, float framerate, uint32_t width, uint32_t height, uint32_t left, uint32_t top, bool reset_at_boot ) { if(video_mode< DC1394_VIDEO_MODE_FORMAT7_0) throw VideoException("roi can be specified only for format7 modes"); camera = dc1394_camera_new (d, guid); if (!camera) throw VideoException("Failed to initialize camera"); // Attempt to stop camera if it is already running dc1394switch_t is_iso_on = DC1394_OFF; dc1394_video_get_transmission(camera, &is_iso_on); if (is_iso_on==DC1394_ON) { dc1394_video_set_transmission(camera, DC1394_OFF); } cout << "Using camera with GUID " << camera->guid << endl; if(reset_at_boot){ dc1394_camera_reset(camera); } //----------------------------------------------------------------------- // setup mode and roi //----------------------------------------------------------------------- if(iso_speed >= DC1394_ISO_SPEED_800) { err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B); if( err != DC1394_SUCCESS ) throw VideoException("Could not set DC1394_OPERATION_MODE_1394B"); } err=dc1394_video_set_iso_speed(camera, iso_speed); if( err != DC1394_SUCCESS ) throw VideoException("Could not set iso speed"); // check that the required mode is actually supported dc1394format7mode_t format7_info; err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info); if( err != DC1394_SUCCESS ) throw VideoException("Could not get format7 mode info"); // safely set the video mode err=dc1394_video_set_mode(camera, video_mode); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 video mode"); // set position to 0,0 so that setting any size within min and max is a valid command err = dc1394_format7_set_image_position(camera, video_mode,0,0); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 image position"); // work out the desired image size width = nearest_value(width, format7_info.unit_pos_x, 0, format7_info.max_size_x - left); height = nearest_value(height, format7_info.unit_pos_y, 0, format7_info.max_size_y - top); // set size err = dc1394_format7_set_image_size(camera,video_mode,width,height); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 size"); // get the info again since many parameters depend on image size err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info); if( err != DC1394_SUCCESS ) throw VideoException("Could not get format7 mode info"); // work out position of roi left = nearest_value(left, format7_info.unit_size_x, format7_info.unit_size_x, format7_info.max_size_x - width); top = nearest_value(top, format7_info.unit_size_y, format7_info.unit_size_y, format7_info.max_size_y - height); // set roi position err = dc1394_format7_set_image_position(camera,video_mode,left,top); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 size"); this->width = width; this->height = height; this->top = top; this->left = left; cout<<"roi: "<<left<<" "<<top<<" "<<width<<" "<<height<<" "; //----------------------------------------------------------------------- // setup frame rate //----------------------------------------------------------------------- err=dc1394_format7_set_packet_size(camera,video_mode, format7_info.max_packet_size); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 packet size"); if((framerate != MAX_FR) && (framerate != EXT_TRIG)){ //set the framerate by using the absolute feature as suggested by the //folks at PointGrey err = dc1394_feature_set_absolute_control(camera,DC1394_FEATURE_FRAME_RATE,DC1394_ON); if( err != DC1394_SUCCESS ) throw VideoException("Could not turn on absolute frame rate control"); err = dc1394_feature_set_mode(camera,DC1394_FEATURE_FRAME_RATE,DC1394_FEATURE_MODE_MANUAL); if( err != DC1394_SUCCESS ) throw VideoException("Could not make frame rate manual "); err=dc1394_feature_set_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,framerate); if( err != DC1394_SUCCESS ) throw VideoException("Could not set format7 framerate "); } // ask the camera what is the resulting framerate (this assume that such a rate is actually // allowed by the shutter time) float value; err=dc1394_feature_get_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,&value); if( err != DC1394_SUCCESS ) throw VideoException("Could not get framerate"); cout<<" framerate(shutter permitting):"<<value<<endl; //----------------------------------------------------------------------- // setup capture //----------------------------------------------------------------------- err=dc1394_capture_setup(camera,dma_frames, DC1394_CAPTURE_FLAGS_DEFAULT); if( err != DC1394_SUCCESS ) throw VideoException("Could not setup camera - check settings"); Start(); }
static int capture_loop(dc1394camera_t *camera, const char *basename, float delay, bool testonly) { uint16_t average; uint32_t num_saturated, num_half_saturated; float shutter = SHUTTER_GOOD; float gain = GAIN_GOOD; CHECK(dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400)); CHECK(dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_1280x960_MONO16)); CHECK(dc1394_video_set_framerate(camera, DC1394_FRAMERATE_3_75)); CHECK(dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT)); CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_EXPOSURE, DC1394_OFF)); CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_ON)); CHECK(dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL)); CHECK(dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 0)); CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_GAIN, DC1394_ON)); CHECK(dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL)); CHECK(dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 500)); CHECK(dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_GAIN, DC1394_ON)); CHECK(dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_GAIN, GAIN_GOOD)); CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_SHUTTER, DC1394_ON)); CHECK(dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL)); CHECK(dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, 500)); CHECK(dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_ON)); CHECK(dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, SHUTTER_GOOD)); CHECK(dc1394_video_set_transmission(camera, DC1394_ON)); while (true) { alarm(10+(unsigned)delay); if (capture_image(camera, basename, shutter, gain, &average, &num_saturated, &num_half_saturated, testonly) == -1) { return -1; } if (num_saturated > SATURATED_HIGH) { /* too much saturation */ if (gain > GAIN_MIN) { gain = new_gain(average, average*0.5, gain); } else if (shutter > SHUTTER_MIN) { shutter = new_shutter(average, average*0.5, shutter, SHUTTER_MAX); } } else if (average < AVERAGE_LOW && num_saturated == 0 && num_half_saturated < SATURATED_HIGH) { /* too dark */ if (shutter < SHUTTER_GOOD) { float shutter2 = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_GOOD); average = (shutter2/shutter)*average; shutter = shutter2; } if (average < AVERAGE_LOW) { if (gain < GAIN_MAX) { gain = new_gain(average, AVERAGE_TARGET, gain); } else if (shutter < SHUTTER_MAX) { shutter = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_MAX); } } } else if (average > AVERAGE_HIGH) { /* too light */ if (shutter > SHUTTER_GOOD) { float shutter2 = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_GOOD); average = (shutter2/shutter)*average; shutter = shutter2; } if (average > AVERAGE_HIGH) { if (gain > GAIN_MIN) { gain = new_gain(average, AVERAGE_TARGET, gain); } else if (shutter > SHUTTER_MIN) { shutter = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_MAX); } } } fflush(stdout); usleep(delay*1.0e6); } return 0; }