Exemplo n.º 1
0
bool ASICCD::StartExposure(float duration)
{
  ASI_ERROR_CODE errCode= ASI_SUCCESS;

  if (duration < MIN_DURATION)
  {
    DEBUGF(INDI::Logger::DBG_WARNING, "Exposure shorter than minimum duration %g s requested. Setting exposure time to %g s.", duration, MIN_DURATION);
    duration = MIN_DURATION;
  }

  if (PrimaryCCD.getFrameType() == CCDChip::BIAS_FRAME)
  {
    duration = MIN_DURATION;
    DEBUGF(INDI::Logger::DBG_SESSION, "Bias Frame (s) : %g", MIN_DURATION);
  }

  PrimaryCCD.setExposureDuration(duration);
  ExposureRequest = duration;
  ASISetControlValue(m_camInfo->CameraID, ASI_EXPOSURE, duration*1000*1000, ASI_FALSE);

  // Try exposure for 3 times
  for (int i=0; i < 3; i++)
  {
    if ( (errCode = ASIStartExposure(m_camInfo->CameraID, (PrimaryCCD.getFrameType() == CCDChip::DARK_FRAME) ? ASI_TRUE : ASI_FALSE )) != ASI_SUCCESS)
    {
          DEBUGF(INDI::Logger::DBG_ERROR, "ASIStartExposure error (%d)", errCode);
          // Wait 100ms before trying again
          usleep(100000);
          continue;
    }
  }

  if (errCode != ASI_SUCCESS)
      return false;

  gettimeofday(&ExpStart, NULL);
  if (ExposureRequest > VERBOSE_EXPOSURE)
    DEBUGF(INDI::Logger::DBG_SESSION, "Taking a %g seconds frame...", ExposureRequest);

  InExposure = true;

  //updateControls();

  return true;
}
Exemplo n.º 2
0
bool ASICCD::StartExposure(float duration)
{
  ASI_ERROR_CODE errCode= ASI_SUCCESS;

  PrimaryCCD.setExposureDuration(duration);
  ExposureRequest = duration;

  DEBUGF(INDI::Logger::DBG_DEBUG, "StartExposure->setexp : %.3fs", duration);
  ASISetControlValue(m_camInfo->CameraID, ASI_EXPOSURE, duration*1000*1000, ASI_FALSE);

  // Try exposure for 3 times
  for (int i=0; i < 3; i++)
  {
    if ( (errCode = ASIStartExposure(m_camInfo->CameraID, (PrimaryCCD.getFrameType() == CCDChip::DARK_FRAME) ? ASI_TRUE : ASI_FALSE )) != ASI_SUCCESS)
    {
          DEBUGF(INDI::Logger::DBG_ERROR, "ASIStartExposure error (%d)", errCode);
          // Wait 100ms before trying again
          usleep(100000);
          continue;
    }
    break;
  }

  if (errCode != ASI_SUCCESS)
      return false;

  gettimeofday(&ExpStart, NULL);
  if (ExposureRequest > VERBOSE_EXPOSURE)
    DEBUGF(INDI::Logger::DBG_SESSION, "Taking a %g seconds frame...", ExposureRequest);

  InExposure = true;

  //updateControls();

  return true;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
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;
}