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; } }
int ar2VideoSetAbsValue1394(AR2VideoParam1394T *vid, int paramName, ARdouble value) { switch (paramName) { case AR_VIDEO_1394_GAMMA: if (dc1394_feature_set_absolute_value(vid->camera, DC1394_FEATURE_BRIGHTNESS, (float)value) != DC1394_SUCCESS) { ARLOGe("unable to set brightness to %f.\n", value); return -1; } return 0; } return -1; }
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; }
/************************************************************************************** ** 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; }
bool Camera::open(int deviceIdx) { if (numCams < 1) { std::cerr << "Camera not found or could not be opened." << std::endl; return false; } camera = dc1394_camera_new(camDict, camList->ids[deviceIdx].guid); if (!camera) { dc1394_log_error("Failed to initialize camera with guid %.", camList->ids[deviceIdx].guid); return false; } /* reset camera to initial state with default values */ resetCameraRegister(); /* prepare camera for ringlight leds */ configureOutputPins(); configureClockDelay(); configureClockDuration(); configureResetDelay(); configureResetDuration(); /* set video mode */ error = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_640x480_MONO8); if (error != DC1394_SUCCESS) { std::cout << "Could not set video mode" << std::endl; cleanup(camera); return false; } /* camera parameter calibrated with FlyCapture2 */ /* brightness */ error = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL); error = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 0); /* exposure */ error = dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL); error = dc1394_feature_set_value(camera, DC1394_FEATURE_EXPOSURE, 149); /* gamma */ error = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAMMA, DC1394_FEATURE_MODE_MANUAL); error = dc1394_feature_set_value(camera, DC1394_FEATURE_GAMMA, 1024); /* shutter */ error = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL); error = dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, 61); /* gain */ error = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL); error = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 740); /* frame rate */ error = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL); error = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, FRAME_RATE); /* setup capture */ error = dc1394_capture_setup(camera, numDMABuffers, DC1394_CAPTURE_FLAGS_DEFAULT); if (error != DC1394_SUCCESS) { std::cerr << "Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera" << std::endl; cleanup(camera); return false; } /* starting camera sending data */ error = dc1394_video_set_transmission(camera, DC1394_ON); if (error != DC1394_SUCCESS) { std::cerr << "Could not start camera iso transmission" << std::endl; cleanup(camera); return false; } /* capture image with no LEDs to subtract ambient light */ captureAmbientImage(); /* set average image intensity used by ps process for adjustment */ avgImgIntensity = cv::mean(ambientImage)[0]; return true; }
/** Configure a feature for the currently open device. * * @pre feature_set_ initialized * * @param feature desired feature number * @param control [in,out] pointer to control parameter (may change) * @param value [in,out] pointer to requested parameter value (may * change depending on device restrictions) * @param value2 [in,out] optional pointer to second parameter value * for white balance (may change depending on device * restrictions). No second value if NULL pointer. * * The parameter values are represented as double despite the fact * that most features on most cameras only allow unsigned 12-bit * values. The exception is the rare feature that supports * "absolute" values in 32-bit IEEE float format. Double can * represent all possible option values accurately. */ void Features::configure(dc1394feature_t feature, int *control, double *value, double *value2) { // device-relevant information for this feature dc1394feature_info_t *finfo = &feature_set_.feature[feature - DC1394_FEATURE_MIN]; if (!finfo->available) // feature not available? { *control = camera1394::Camera1394_None; return; } switch (*control) { case camera1394::Camera1394_Off: setPower(finfo, DC1394_OFF); break; case camera1394::Camera1394_Query: getValues(finfo, value, value2); break; case camera1394::Camera1394_Auto: if (!setMode(finfo, DC1394_FEATURE_MODE_AUTO)) { setPower(finfo, DC1394_OFF); } break; case camera1394::Camera1394_Manual: if (!setMode(finfo, DC1394_FEATURE_MODE_MANUAL)) { setPower(finfo, DC1394_OFF); break; } // TODO: break this into some internal methods if (finfo->absolute_capable && finfo->abs_control) { // supports reading and setting float value float fmin, fmax; if (DC1394_SUCCESS == dc1394_feature_get_absolute_boundaries(camera_, feature, &fmin, &fmax)) { // clamp *value between minimum and maximum if (*value < fmin) *value = (double) fmin; else if (*value > fmax) *value = (double) fmax; } else { ROS_WARN_STREAM("failed to get feature " << featureName(feature) << " boundaries "); } // @todo handle absolute White Balance values float fval = *value; if (DC1394_SUCCESS != dc1394_feature_set_absolute_value(camera_, feature, fval)) { ROS_WARN_STREAM("failed to set feature " << featureName(feature) << " to " << fval); } } else // no float representation { // round requested value to nearest integer *value = rint(*value); // clamp *value between minimum and maximum if (*value < finfo->min) *value = (double) finfo->min; else if (*value > finfo->max) *value = (double) finfo->max; dc1394error_t rc; uint32_t ival = (uint32_t) *value; // handle White Balance, which has two components if (feature == DC1394_FEATURE_WHITE_BALANCE) { *value2 = rint(*value2); // clamp *value2 between same minimum and maximum if (*value2 < finfo->min) *value2 = (double) finfo->min; else if (*value2 > finfo->max) *value2 = (double) finfo->max; uint32_t ival2 = (uint32_t) *value2; rc = dc1394_feature_whitebalance_set_value(camera_, ival, ival2); } else { // other features only have one component rc = dc1394_feature_set_value(camera_, feature, ival); } if (rc != DC1394_SUCCESS) { ROS_WARN_STREAM("failed to set feature " << featureName(feature) << " to " << ival); } } break; case camera1394::Camera1394_OnePush: // Try to set OnePush mode setMode(finfo, DC1394_FEATURE_MODE_ONE_PUSH_AUTO); // Now turn the control off, so camera does not continue adjusting setPower(finfo, DC1394_OFF); break; case camera1394::Camera1394_None: // Invalid user input, because this feature actually does exist. ROS_INFO_STREAM("feature " << featureName(feature) << " exists, cannot set to None"); break; default: ROS_WARN_STREAM("unknown state (" << *control <<") for feature " << featureName(feature)); } // return actual state reported by the device *control = getState(finfo); ROS_DEBUG_STREAM("feature " << featureName(feature) << " now in state " << *control); }
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; }
static int capture_image(dc1394camera_t *camera, const char *basename, float shutter, float gain, uint16_t *average, uint32_t *num_saturated, uint32_t *num_half_saturated, bool testonly) { int fd; dc1394video_frame_t *frame; uint64_t timestamp; static uint16_t buf[IMAGE_HEIGHT*IMAGE_WIDTH]; struct tm *tm; time_t t; char tstring[50]; char *fname; struct timeval tv; int led; led = open(led_path, O_WRONLY); CHECK(dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_GAIN, gain)); CHECK(dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, shutter)); CHECK(dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame)); timestamp = frame->timestamp; memcpy(buf, frame->image, sizeof(buf)); CHECK(dc1394_capture_enqueue(camera,frame)); if (led != -1) { dprintf(led, "255\n"); } gettimeofday(&tv, NULL); get_averages(buf, average, num_saturated, num_half_saturated); if (*average == 0) { /* bad frame */ return -1; } t = tv.tv_sec; tm = localtime(&t); strftime(tstring, sizeof(tstring), "%Y%m%d%H%M%S", tm); if (asprintf(&fname, "%s-%s-%02u.pgm", basename, tstring, (unsigned)(tv.tv_usec/10000)) == -1) { return -1; } printf("%s shutter=%f gain=%f average=%u saturated=%u hsaturated=%u\n", fname, shutter, gain, *average, *num_saturated, *num_half_saturated); if (!testonly) { fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd == -1) { fprintf(stderr, "Can't create imagefile '%s' - %s", fname, strerror(errno)); free(fname); return -1; } dprintf(fd,"P5\n%u %u\n#PARAM: t=%llu shutter=%f gain=%f average=%u saturated=%u\n65535\n", IMAGE_WIDTH, IMAGE_HEIGHT, (unsigned long long)timestamp, shutter, gain, *average, *num_saturated); if (write(fd, buf, sizeof(buf)) != sizeof(buf)) { fprintf(stderr, "Write failed for %s\n", fname); } close(fd); } free(fname); if (led != -1) { dprintf(led, "0\n"); close(led); } return 0; }