Ejemplo n.º 1
0
int CameraDcam::setupCaptureFormat7()
{
    switch(mode)
    {
        case CAMERA_MODE_RAW8:
            format7image.colorCodingId = COLOR_FORMAT7_RAW8;
            bytesPerPixel = 1;
            break;
        case CAMERA_MODE_RAW12:
            format7image.colorCodingId = COLOR_FORMAT7_RAW16;
            bytesPerPixel = 2;
            break;
        case CAMERA_MODE_MONO8:
            format7image.colorCodingId = COLOR_FORMAT7_MONO8;
            bytesPerPixel = 1;
            break;
        default:
            GDOS_WARNING("Unable to setup capture format 7.\n");
            return DC1394_FAILURE;

    }
   /*-----------------------------------------------------------------------
   *  setup capture for format 7
   *-----------------------------------------------------------------------*/
   if (dc1394_dma_setup_format7_capture( porthandle[dc1394CameraPortNo],
                     camera_node,
                     channel,
                     format7image.mode,
                     SPEED_400,
                     format7image.bytesPerPacket,
                     format7image.leftImagePosition,
                     format7image.topImagePosition,
                     format7image.width,
                     format7image.height,
                     8,    // number of buffers
                     1,    // drop frames
                     device,
                     &dc1394Camera ) != DC1394_SUCCESS)
   {
      GDOS_ERROR("Unable to setup camera format 7.\n");
      return DC1394_FAILURE;
   }

  if( dc1394_set_format7_color_coding_id(
                     porthandle[dc1394CameraPortNo],
                     camera_node,
                     format7image.mode,
                     format7image.colorCodingId) != DC1394_SUCCESS)
  {
      GDOS_ERROR("Unable to setup camera color mode 7.\n");
      return DC1394_FAILURE;
  }
   GDOS_DBG_INFO("Parameter setting successful.");

  if( dc1394_query_format7_color_filter_id(
                     porthandle[dc1394CameraPortNo],
                     camera_node,
                     format7image.mode,
                     &format7image.colorFilterId) != DC1394_SUCCESS)
  {
      GDOS_ERROR("Unable to query camera color filter id 7.\n");
      return DC1394_FAILURE;
  }
  GDOS_DBG_INFO("Found color filter id:%i .\n",&format7image.colorFilterId);


  /* set trigger mode */
  /* not used with shot function!) */
  if( dc1394_set_trigger_mode(
                     porthandle[dc1394CameraPortNo],
                     camera_node,
                     TRIGGER_MODE_0)
      != DC1394_SUCCESS) //falling edge of extTrig shutter time set in register (no parameter)
  {
      GDOS_WARNING("Unable to setup format 7 trigger mode.\n");
  }

/*  if (dc1394_query_format7_total_bytes(handle, camera_nodes[0], MODE_FORMAT7_0, &total_bytes) != DC1394_SUCCESS)
  {
    printf("dc1394_query_format7_total_bytes error\n");
    return DC1394_FAILURE;
  }
  printf( "camera reports total bytes per frame = %lld bytes\n",
          total_bytes);
          */

  return DC1394_SUCCESS;
}
Ejemplo n.º 2
0
int dc1394Init(int cam, int width, int height, int max_frames, int trigger_mode, int shutter, int gain, int trig_on, int iso_speed, int frame_rate)
{
  int numNodes;
  nodeid_t * camera_nodes;
  int numCameras;

  if (first) {
    bzero(Cams, sizeof(Cams));
    first = 0;
  }
    printf("now is the time\n");
  if ((cam < 0) || (cam >= MAX_CAMERAS)) {
    fprintf(stderr, "Camera out of range, cam = %d must be between 0 and %d\n", cam, MAX_CAMERAS);
    return 0;
  }

  /* clean up any active daq */
  if (Cams[cam].thread_id != 0) {
    void *status;
    pthread_cancel(Cams[cam].thread_id);
    pthread_join(Cams[cam].thread_id, &status);
    Cams[cam].thread_id = 0;
  }

  /* now find the camera choke if not there or not correct */
  Cams[cam].handle = dc1394_create_handle(0);
  if (Cams[cam].handle==NULL)
  {
    fprintf( stderr, "Unable to aquire a raw1394 handle\n\n"
             "Please check \n"
	     "  - if the kernel modules `ieee1394',`raw1394' and `ohci1394' are loaded \n"
	     "  - if you have read/write access to /dev/raw1394\n\n");
    return(0);
  }

  /*-----------------------------------------------------------------------
   *  get the camera nodes and describe them as we find them
   *-----------------------------------------------------------------------*/
  numNodes = raw1394_get_nodecount(Cams[cam].handle);
  camera_nodes = dc1394_get_camera_nodes(Cams[cam].handle,&numCameras,1);
  if (numCameras<1)
  {
    fprintf( stderr, "no cameras found :(\n");
    dc1394_destroy_handle(Cams[cam].handle);
    Cams[cam].handle = 0;
    return(0);
  }

  /*-----------------------------------------------------------------------
   *  to prevent the iso-transfer bug from raw1394 system, check if
   *  camera is highest node. For details see 
   *  http://linux1394.sourceforge.net/faq.html#DCbusmgmt
   *  and
   *  http://sourceforge.net/tracker/index.php?func=detail&aid=435107&group_id=8157&atid=108157
   *-----------------------------------------------------------------------*/
  if( camera_nodes[0] == numNodes-1)
  {
    fprintf( stderr, "\n"
             "Sorry, your camera is the highest numbered node\n"
             "of the bus, and has therefore become the root node.\n"
             "The root node is responsible for maintaining \n"
             "the timing of isochronous transactions on the IEEE \n"
             "1394 bus.  However, if the root node is not cycle master \n"
             "capable (it doesn't have to be), then isochronous \n"
             "transactions will not work.  The host controller card is \n"
             "cycle master capable, however, most cameras are not.\n"
             "\n"
             "The quick solution is to add the parameter \n"
             "attempt_root=1 when loading the OHCI driver as a \n"
             "module.  So please do (as root):\n"
             "\n"
             "   rmmod ohci1394\n"
             "   insmod ohci1394 attempt_root=1\n"
             "\n"
             "for more information see the FAQ at \n"
             "http://linux1394.sourceforge.net/faq.html#DCbusmgmt\n"
             "\n");
    dc1394_destroy_handle(Cams[cam].handle);
    dc1394_free_camera_nodes(camera_nodes);
    return(0);
  }
  
  /*-----------------------------------------------------------------------
   *  setup capture
   *-----------------------------------------------------------------------*/
   if (dc1394_dma_setup_capture(Cams[cam].handle,camera_nodes[0],
                           0, /* channel */ 
                           FORMAT_VGA_NONCOMPRESSED,
                           MODE_640x480_MONO,
                           SPEED_400,
                           FRAMERATE_30,
				3, /* frames */
			    1,
			    "/dev/video1394/0", 
                           &Cams[cam].camera)!=DC1394_SUCCESS) 
 {
    fprintf( stderr,"unable to setup camera-\n"
             "check line %d of %s to make sure\n"
             "that the video mode,framerate and format are\n"
             "supported by your camera\n",
             __LINE__,__FILE__);
    dc1394_dma_release_camera(Cams[cam].handle,&Cams[cam].camera);
    Cams[cam].dma_active = 0;
    dc1394_destroy_handle(Cams[cam].handle);
    Cams[cam].handle = 0;
    dc1394_free_camera_nodes(camera_nodes);
    return(0);
  }
  dc1394_free_camera_nodes(camera_nodes);
  
  /* set trigger mode */
  if( dc1394_set_trigger_mode(Cams[cam].handle, Cams[cam].camera.node, trigger_mode)
      != DC1394_SUCCESS)
  {
    fprintf( stderr, "unable to set camera trigger mode\n");
    /*    
    dc1394_dma_release_camera(Cams[cam].handle,&Cams[cam].camera);
    Cams[cam].dma_active = 0;
    dc1394_destroy_handle(Cams[cam].handle);
    Cams[cam].handle = 0;
    return(0);
    */
  }

  /* eventually the same for the shutter and gain */

  if (dc1394_set_trigger_on_off(Cams[cam].handle, Cams[cam].camera.node,
				trig_on) != DC1394_SUCCESS)
    {
      fprintf(stderr, "unable to set trigger on to %d\n", trig_on);
    }


  /*-----------------------------------------------------------------------
   *  have the camera start sending us data
   *-----------------------------------------------------------------------*/
  if (dc1394_start_iso_transmission(Cams[cam].handle,Cams[cam].camera.node) !=DC1394_SUCCESS) 
  {
    fprintf( stderr, "unable to start camera iso transmission\n");
    dc1394_dma_release_camera(Cams[cam].handle,&Cams[cam].camera);
    Cams[cam].dma_active = 0;
    dc1394_destroy_handle(Cams[cam].handle);
    Cams[cam].handle = 0;
    return(0);
  }
  Cams[cam].running = 1;

  /* make room for the answers and write down all of the knobs for later use */
  if ( (width != Cams[cam].width) ||
       (height != Cams[cam].height) ||
       (max_frames != Cams[cam].max_frames) ){
    if (Cams[cam].frames != NULL)
      free(Cams[cam].frames);
    Cams[cam].frames = (unsigned char *)malloc(width*height*max_frames);
    if (Cams[cam].frames == NULL) {
      fprintf(stderr, "Could not allocate memory for %d frames (%d x %d)\n", max_frames, width, height);
      dc1394_dma_release_camera(Cams[cam].handle,&Cams[cam].camera);
      Cams[cam].dma_active = 0;
      dc1394_destroy_handle(Cams[cam].handle);
      Cams[cam].handle = 0;
      Cams[cam].max_frames = 0;
      return(0);
    }
    Cams[cam].times = (double *)malloc(max_frames * sizeof(double));
    if (Cams[cam].times == NULL) {
      fprintf(stderr, "Could not allocate memory for %d times\n", max_frames);
      dc1394_dma_release_camera(Cams[cam].handle,&Cams[cam].camera);
      free( Cams[cam].frames );
      Cams[cam].dma_active = 0;
      dc1394_destroy_handle(Cams[cam].handle);
      Cams[cam].handle = 0;
      Cams[cam].max_frames = 0;
     return(0);
    }

    Cams[cam].width = width;
    Cams[cam].height = height;
    Cams[cam].max_frames = max_frames;
  }
  Cams[cam].next_frame = 0;
  Cams[cam].trigger_mode = trigger_mode;
  Cams[cam].shutter = shutter;
  Cams[cam].gain = gain;
  if (pthread_create(&Cams[cam].thread_id,  NULL, AcquireFrames, (void *)cam) != 0) {
      fprintf(stderr, "Could not create thread to handle camera daq\n");
      dc1394_stop_iso_transmission(Cams[cam].handle,Cams[cam].camera.node);
      Cams[cam].running = 0;
      dc1394_dma_release_camera(Cams[cam].handle,&Cams[cam].camera);
      Cams[cam].dma_active = 0;
      dc1394_destroy_handle(Cams[cam].handle);
      Cams[cam].handle = 0;
      return(0);
  }
  return(1);
}