예제 #1
0
bool ASICCD::setupParams()
{
    ASI_ERROR_CODE errCode = ASI_SUCCESS;
    int piNumberOfControls = 0;

    errCode = ASIGetNumOfControls(m_camInfo->CameraID, &piNumberOfControls);

    if (errCode != ASI_SUCCESS)
        DEBUGF(INDI::Logger::DBG_DEBUG, "ASIGetNumOfControls error (%d)", errCode);

  if (piNumberOfControls > 0)
  {
      if (ControlNP.nnp > 0)
          free(ControlN);

      if (ControlSP.nsp > 0)
          free(ControlS);

      createControls(piNumberOfControls);
  }

  // Set minimum ASI_BANDWIDTHOVERLOAD on ARM
  #ifdef LOW_USB_BANDWIDTH
  ASI_CONTROL_CAPS pCtrlCaps;
  for(int j = 0; j < piNumberOfControls; j++)
  {
      ASIGetControlCaps(m_camInfo->CameraID, j, &pCtrlCaps);
      if (pCtrlCaps.ControlType == ASI_BANDWIDTHOVERLOAD)
      {
          DEBUGF(INDI::Logger::DBG_DEBUG, "setupParams->set USB %d", pCtrlCaps.MinValue);
          ASISetControlValue(m_camInfo->CameraID, ASI_BANDWIDTHOVERLOAD, pCtrlCaps.MinValue, ASI_FALSE);
          break;
      }
  }
  #endif

  // Get Image Format
  int w=0, h=0, bin=0;
  ASI_IMG_TYPE imgType;
  ASIGetROIFormat(m_camInfo->CameraID, &w, &h, &bin, &imgType);

  DEBUGF(INDI::Logger::DBG_DEBUG, "CCD ID: %d Width: %d Height: %d Binning: %dx%d Image Type: %d", m_camInfo->CameraID, w, h, bin, bin, imgType);

  // Get video format and bit depth
  int bit_depth = 8;

  switch (imgType)
  {
    case ASI_IMG_RAW16:
      bit_depth = 16;

    default:
      break;
  }

  if (VideoFormatSP.nsp > 0)
      free(VideoFormatS);

  VideoFormatS = NULL;
  int nVideoFormats = 0;

  for (int i=0; i < 8; i++)
  {
        if (m_camInfo->SupportedVideoFormat[i] == ASI_IMG_END)
            break;

        nVideoFormats++;
        VideoFormatS = VideoFormatS == NULL ? (ISwitch *) malloc(sizeof(ISwitch)) : (ISwitch *) realloc(VideoFormatS, nVideoFormats * sizeof(ISwitch));

        ISwitch *oneVF = VideoFormatS + (nVideoFormats-1);

        switch (m_camInfo->SupportedVideoFormat[i])
        {
            case ASI_IMG_RAW8:
                IUFillSwitch(oneVF, "ASI_IMG_RAW8", "Raw 8 bit", (imgType == ASI_IMG_RAW8) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_RAW8");
                break;

            case ASI_IMG_RGB24:
                IUFillSwitch(oneVF, "ASI_IMG_RGB24", "RGB 24", (imgType == ASI_IMG_RGB24) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_RGB24");
                break;

            case ASI_IMG_RAW16:
                IUFillSwitch(oneVF, "ASI_IMG_RAW16", "Raw 16 bit", (imgType == ASI_IMG_RAW16) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_RAW16");
            break;

            case ASI_IMG_Y8:
                IUFillSwitch(oneVF, "ASI_IMG_Y8", "Luma", (imgType == ASI_IMG_Y8) ? ISS_ON : ISS_OFF);
                DEBUG(INDI::Logger::DBG_DEBUG, "Supported Video Format: ASI_IMG_Y8");
                break;

            default:
                DEBUGF(INDI::Logger::DBG_DEBUG, "Unknown video format (%d)", m_camInfo->SupportedVideoFormat[i]);
                break;
        }

        oneVF->aux = (void *) &m_camInfo->SupportedVideoFormat[i];
  }

  VideoFormatSP.nsp = nVideoFormats;
  VideoFormatSP.sp  = VideoFormatS;

  float x_pixel_size, y_pixel_size;
  int x_1, y_1, x_2, y_2;

  x_pixel_size = m_camInfo->PixelSize;
  y_pixel_size = m_camInfo->PixelSize;

   x_1 = y_1 = 0;
   x_2 = m_camInfo->MaxWidth;
   y_2 = m_camInfo->MaxHeight;

  SetCCDParams(x_2 - x_1, y_2 - y_1, bit_depth, x_pixel_size, y_pixel_size);

  // Let's calculate required buffer
  int nbuf;
  nbuf = PrimaryCCD.getXRes() * PrimaryCCD.getYRes() * PrimaryCCD.getBPP() / 8;    //  this is pixel cameraCount
  //nbuf += 512;    //  leave a little extra at the end
  PrimaryCCD.setFrameBufferSize(nbuf);

  //if (HasCooler())
  //{
      long pValue = 0;
      ASI_BOOL isAuto= ASI_FALSE;

      if ( (errCode = ASIGetControlValue(m_camInfo->CameraID, ASI_TEMPERATURE, &pValue, &isAuto)) != ASI_SUCCESS)
          DEBUGF(INDI::Logger::DBG_DEBUG, "ASIGetControlValue temperature error (%d)", errCode);

      TemperatureN[0].value = pValue / 10.0;

      DEBUGF(INDI::Logger::DBG_SESSION,  "The CCD Temperature is %f", TemperatureN[0].value);
      IDSetNumber(&TemperatureNP, NULL);
  //}

  ASIStopVideoCapture(m_camInfo->CameraID);

  DEBUGF(INDI::Logger::DBG_DEBUG, "setupParams ASISetROIFormat (%dx%d,  bin %d, type %d)", m_camInfo->MaxWidth, m_camInfo->MaxHeight, 1, imgType);
  ASISetROIFormat(m_camInfo->CameraID, m_camInfo->MaxWidth, m_camInfo->MaxHeight, 1, imgType);

  #if !defined(OSX_EMBEDED_MODE) && !defined(__CYGWIN__)
  updateRecorderFormat();
  streamer->setRecorderSize(w,h);
  #endif

  return true;

}
예제 #2
0
파일: recorder.c 프로젝트: chripell/yaaca
int main(int argc, char *argv[]) {
  unsigned int exposure, gain, mode, nzwo, bin, lm;
  ASI_CAMERA_INFO prop;
  unsigned char *buff;
  int bsize, n=0, max = 0;
  float sum = 0.0;
  int width, height;
  
  if (argc != 9) {
    printf("Usage: %s [camera idx] [mode] [bin] [exposure in ms] [gain] [long mode] [w if >0] [h if>0]\n", argv[0]);
    exit(1);
  }
  cidx = strtoul(argv[1], NULL, 0);
  mode = strtoul(argv[2], NULL, 0);
  bin = strtoul(argv[3], NULL, 0);
  exposure = strtoul(argv[4], NULL, 0);
  gain = strtoul(argv[5], NULL, 0);
  lm = strtoul(argv[6], NULL, 0);
  width = strtol(argv[7], NULL, 0);
  height = strtol(argv[8], NULL, 0);
  nzwo = ASIGetNumOfConnectedCameras();
  if (cidx >= nzwo) {
    fprintf(stderr, "Camera out of range %d/%d\n", cidx, nzwo);
    exit(1);
  }
  CHECK(ASIGetCameraProperty(&prop, cidx));
  CHECK(ASIOpenCamera(cidx));
  CHECK(ASIInitCamera(cidx));
  if (width <= 0)
    width = prop.MaxWidth/bin;
  if (height <= 0)
    height = prop.MaxHeight/bin;
  fprintf(stderr, "%d x %d\n", width, height);
  bsize = width * height;
  if (mode == 1)
    bsize *= 3;
  else if (mode == 2)
    bsize *= 2;
  buff = malloc(bsize);
  CHECK(ASISetROIFormat(cidx, width, height, bin, mode));
  set_cap(ASI_EXPOSURE, exposure * 1000);
  set_cap(ASI_GAIN, gain);
  set_cap(ASI_HARDWARE_BIN, ASI_TRUE);
#ifdef SELF_BULK
  sb_init(bsize);
#endif
  if (lm == 0)
    CHECK(ASIStartVideoCapture(cidx));
  while (1) {
    int r;
    struct timespec start, stop;
    long delta;

    clock_gettime(CLOCK_MONOTONIC, &start);
    if (lm == 1) {
      ASI_EXPOSURE_STATUS s;
	
      CHECK1(ASIGetExpStatus(cidx, &s));
      if (s != ASI_EXP_IDLE && s != ASI_EXP_FAILED) {
	fprintf(stderr, "Not idle or failed: %d\n", s);
	exit(1);
      }
      CHECK(ASIStartExposure(cidx, 0));
      fprintf(stderr, "START\n");
      r = 99;			/* timeout */
      do {
	usleep(10*1000);
	clock_gettime(CLOCK_MONOTONIC, &stop);
	delta = (stop.tv_sec - start.tv_sec) * 1000 +
	  (stop.tv_nsec - start.tv_nsec) / 1000000;
	CHECK1(ASIGetExpStatus(cidx, &s));
	if (s == ASI_EXP_IDLE) {
	  fprintf(stderr, "start\n");
	  CHECK(ASIStartExposure(cidx, 0));
	}
	else if (s == ASI_EXP_SUCCESS) {
	  r = 0;
	  fprintf(stderr, "FINISHED\n");
	  CHECK(ASIGetDataAfterExp(cidx, buff, bsize));
	  break;
	} else if (s == ASI_EXP_FAILED) {
	  r = 98;
	  break;
	}
	else if (s != ASI_EXP_WORKING) {
	  fprintf(stderr, "Unexpected status\n");
	  exit(1);
	}
      } while (delta < 20 * exposure + 500);
    }
    else {
      fprintf(stderr, "ASIGetVideoData\n");
      r = ASIGetVideoData(cidx, buff, bsize, 20 * exposure + 500);
      clock_gettime(CLOCK_MONOTONIC, &stop);
      delta = (stop.tv_sec - start.tv_sec) * 1000 +
	(stop.tv_nsec - start.tv_nsec) / 1000000;
    }
    n++;
    if (delta > max)
      max = delta;
    sum += delta;
    fprintf(stderr, ":%2d %6.0f(%6d) %ld\n",
	    r, sum / n, max, delta);
  }
  return 0;
}
예제 #3
0
int  main()
{
	int width;
	char* bayer[] = {"RG","BG","GR","GB"};


	int height;
	int i;
	char c;
	bool bresult;

	int time1,time2;
	int count=0;

	char buf[128]={0};

	int CamNum=0;
	
	///long exposure, exp_min, exp_max, exp_step, exp_flag, exp_default;
	//long gain, gain_min, gain_max,gain_step, gain_flag, gain_default;

	IplImage *pRgb;


	int numDevices = ASIGetNumOfConnectedCameras();
	if(numDevices <= 0)
	{
		printf("no camera connected, press any key to exit\n");
		getchar();
		return -1;
	}
	else
		printf("attached cameras:\n");

	ASI_CAMERA_INFO ASICameraInfo;
	

	for(i = 0; i < numDevices; i++)
	{
		ASIGetCameraProperty(&ASICameraInfo, i);	
		printf("%d %s\n",i, ASICameraInfo.Name);
	}

	printf("\nselect one to privew\n");
	scanf("%d", &CamNum);



	if(ASIOpenCamera(CamNum) != ASI_SUCCESS)
	{
		printf("OpenCamera error,are you root?,press any key to exit\n");
		getchar();
		return -1;
	}
	ASIInitCamera(CamNum);


	printf("%s information\n",ASICameraInfo.Name);
	int iMaxWidth, iMaxHeight;

	iMaxWidth = ASICameraInfo.MaxWidth;
	iMaxHeight =  ASICameraInfo.MaxHeight;
	printf("resolution:%dX%d\n", iMaxWidth, iMaxHeight);

	if(ASICameraInfo.IsColorCam)
		printf("Color Camera: bayer pattern:%s\n",bayer[ASICameraInfo.BayerPattern]);	
	else
		printf("Mono camera\n");

	ASI_CONTROL_CAPS ControlCaps;
	int iNumOfCtrl = 0;
	ASIGetNumOfControls(CamNum, &iNumOfCtrl);
	for( i = 0; i < iNumOfCtrl; i++)
	{
		ASIGetControlCaps(CamNum, i, &ControlCaps);
		printf("%s\n", ControlCaps.Name);
	}	


	long ltemp = 0;
	ASI_BOOL bAuto = ASI_FALSE;
	ASIGetControlValue(CamNum, ASI_TEMPERATURE, &ltemp, &bAuto);
	printf("sensor temperature:%02f\n", (float)ltemp/10.0);



	printf("\nPlease input the <width height bin image_type> with one space, ie. 640 480 2 0. use max resolution if input is 0. Press ESC when video window is focused to quit capture\n");
	int bin = 1, Image_type;
	do{
	//	printf("Set format error, please check the width and height\n ASI120's data size(width*height) must be integer multiple of 1024\n");
		printf("Please input the width and height again£¬ie. 640 480\n");
		scanf("%d %d %d %d", &width, &height, &bin, &Image_type);
		if(!width || !height)
		{
			width = iMaxWidth;
			height = iMaxHeight;
		}
	}while(ASI_SUCCESS !=  ASISetROIFormat(CamNum, width, height, bin, (ASI_IMG_TYPE)Image_type));		
	
	printf("\nset image format %d %d %d %d success, start privew, press ESC to stop \n", width, height, bin, Image_type);

	ASIGetROIFormat(CamNum, &width, &height, &bin, (ASI_IMG_TYPE*)&Image_type);
	int displayWid = 640, displayHei = 480;
	if(Image_type == ASI_IMG_RAW16)
		pRgb=cvCreateImage(cvSize(displayWid, displayHei), IPL_DEPTH_16U, 1);
	else if(Image_type == ASI_IMG_RGB24)
		pRgb=cvCreateImage(cvSize(displayWid, displayHei), IPL_DEPTH_8U, 3);
	else
		pRgb=cvCreateImage(cvSize(displayWid, displayHei), IPL_DEPTH_8U, 1);

	long imgSize = width*height*(1 + (Image_type==ASI_IMG_RAW16));
	long displaySize = displayWid*displayHei*(1 + (Image_type==ASI_IMG_RAW16));
	unsigned char* imgBuf = new unsigned char[imgSize];

	
	ASISetControlValue(CamNum, ASI_GAIN, 0, ASI_FALSE);
	
	ASISetControlValue(CamNum, ASI_BANDWIDTHOVERLOAD, 45, ASI_FALSE);//

	unsigned int expms = 100;
	printf("\ninput exposure time(ms)\n");
	scanf("%d", &expms);
	

	bDisplay = 1;
#ifdef _LIN
	pthread_t thread_display;
	pthread_create(&thread_display, NULL, Display, (void*)pRgb);
#elif defined _WINDOWS
	HANDLE thread_setgainexp;
	thread_setgainexp = (HANDLE)_beginthread(Display,  NULL, (void*)pRgb);
#endif

	time1 = GetTickCount();
	int iStrLen = 0, iTextX = 40, iTextY = 60;
	void* retval;
//	int time0, iWaitMs = -1;
//	bool bGetImg;
	ASI_EXPOSURE_STATUS status;
	int iDropped = 0;
	while(bMain)
	{
		ASISetControlValue(CamNum, ASI_EXPOSURE, expms*1000, ASI_FALSE);//

		ASIStartExposure(CamNum, ASI_FALSE);
		usleep(10000);//10ms
		status = ASI_EXP_WORKING;
		while(status == ASI_EXP_WORKING)
		{
	
			ASIGetExpStatus(CamNum, &status);
				
		}
		if(status == ASI_EXP_SUCCESS)
		{
			ASIGetDataAfterExp(CamNum, imgBuf, imgSize);
			if(Image_type==ASI_IMG_RAW16)
			{
				unsigned short *pCv16bit = (unsigned short *)(pRgb->imageData);
				unsigned short *pImg16bit = (unsigned short *)imgBuf;
				for(int y = 0; y < displayHei; y++)
				{
					memcpy(pCv16bit, pImg16bit, displayWid*2);
					pCv16bit+=displayWid;
					pImg16bit+=width;
				}
			}
			else{
				unsigned char *pCv8bit = (unsigned char *)pRgb->imageData;
				unsigned char *pImg8bit = (unsigned char *)imgBuf;
				for(int y = 0; y < displayHei; y++)
				{
					memcpy(pCv8bit, pImg8bit, displayWid);
					pCv8bit+=displayWid;
					pImg8bit+=width;
				}
			}
					
		}
	//		getImageAfterExp((unsigned char*)pRgb->imageData, pRgb->imageSize);
		usleep(500000);

//		bGetImg = getImageData((unsigned char*)pRgb->imageData, pRgb->imageSize, iWaitMs);
		time2 = GetTickCount();
//		printf("waitMs%d, deltaMs%d, %d\n", iWaitMs, time2 - time0, bGetImg);
		count++;
		
		if(time2-time1 > 1000 )
		{
			ASIGetDroppedFrames(CamNum, &iDropped);
			sprintf(buf, "fps:%d dropped frames:%lu ImageType:%d",count, iDropped, Image_type);

			count = 0;
			time1=GetTickCount();	
			printf(buf);
			printf("\n");

		}
		if(Image_type != ASI_IMG_RGB24 && Image_type != ASI_IMG_RAW16)
		{
			iStrLen = strlen(buf);
			CvRect rect = cvRect(iTextX, iTextY - 15, iStrLen* 11, 20);
			cvSetImageROI(pRgb , rect);
			cvSet(pRgb, CV_RGB(180, 180, 180)); 
			cvResetImageROI(pRgb);
		}
		cvText(pRgb, buf, iTextX,iTextY );

		if(bChangeFormat)
		{
			bChangeFormat = 0;
			bDisplay = false;
			pthread_join(thread_display, &retval);
			cvReleaseImage(&pRgb);
	//		stopCapture();
			
			switch(change)
			{
				 case change_imagetype:
					Image_type++;
					if(Image_type > 3)
						Image_type = 0;
					
					break;
				case change_bin:
					if(bin == 1)
					{
						bin = 2;
						width/=2;
						height/=2;
					}
					else 
					{
						bin = 1;
						width*=2;
						height*=2;
					}
					break;
				case change_size_smaller:
					if(width > 320 && height > 240)
					{
						width/= 2;
						height/= 2;
					}
					break;
				
				case change_size_bigger:
				
					if(width*2*bin <= iMaxWidth && height*2*bin <= iMaxHeight)
					{
						width*= 2;
						height*= 2;
					}
					break;
			}
			ASISetROIFormat(CamNum, width, height, bin, (ASI_IMG_TYPE)Image_type);
			ASIGetROIFormat(CamNum, &width, &height, &bin, (ASI_IMG_TYPE*)&Image_type);
			if(Image_type == ASI_IMG_RAW16)
				pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_16U, 1);
			else if(Image_type == ASI_IMG_RGB24)
				pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 3);
			else
				pRgb=cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1);
			bDisplay = 1;
			pthread_create(&thread_display, NULL, Display, (void*)pRgb);
	//		startCapture(); //start privew
		}
	}
END:
	
	if(bDisplay)
	{
		bDisplay = 0;
#ifdef _LIN
   		pthread_join(thread_display, &retval);
#elif defined _WINDOWS
		Sleep(50);
#endif
	}
	
	//stopCapture();
	//closeCamera();
	ASIStopExposure(CamNum);
	ASICloseCamera(CamNum);
	cvReleaseImage(&pRgb);
	if(imgBuf)
		delete[] imgBuf;
	printf("main function over\n");
	return 1;
}