Exemplo n.º 1
0
void FreenectGrabber :: setIRMode(bool ir)
{
    if (!isConnected()) return;

    QWriteLocker locker(&m_lock);
    m_ir_mode = ir;
    freenect_stop_video(f_dev);
    if (!m_ir_mode)
        freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
    else
        freenect_set_video_format(f_dev, FREENECT_VIDEO_IR_8BIT);
    freenect_start_video(f_dev);
}
Exemplo n.º 2
0
void *freenect_threadfunc(void *arg)
{
	int accelCount = 0;

	freenect_set_tilt_degs(f_dev,freenect_angle);
	freenect_set_led(f_dev,LED_RED);
	freenect_set_depth_callback(f_dev, depth_cb);
	freenect_set_video_callback(f_dev, rgb_cb);
	freenect_set_video_format(f_dev, current_format);
	freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);
	freenect_set_video_buffer(f_dev, rgb_back);

	freenect_start_depth(f_dev);
	freenect_start_video(f_dev);

	printf("'w'-tilt up, 's'-level, 'x'-tilt down, '0'-'6'-select LED mode, 'f'-video format\n");

	while (!die && freenect_process_events(f_ctx) >= 0) {
		//Throttle the text output
		if (accelCount++ >= 2000)
		{
			accelCount = 0;
			freenect_raw_tilt_state* state;
			freenect_update_tilt_state(f_dev);
			state = freenect_get_tilt_state(f_dev);
			double dx,dy,dz;
			freenect_get_mks_accel(state, &dx, &dy, &dz);
			printf("\r raw acceleration: %4d %4d %4d  mks acceleration: %4f %4f %4f", state->accelerometer_x, state->accelerometer_y, state->accelerometer_z, dx, dy, dz);
			fflush(stdout);
		}

		if (requested_format != current_format) {
			freenect_stop_video(f_dev);
			freenect_set_video_format(f_dev, requested_format);
			freenect_start_video(f_dev);
			current_format = requested_format;
		}
	}

	printf("\nshutting down streams...\n");

	freenect_stop_depth(f_dev);
	freenect_stop_video(f_dev);

	freenect_close_device(f_dev);
	freenect_shutdown(f_ctx);

	printf("-- done!\n");
	return NULL;
}
Exemplo n.º 3
0
void init()
{
	freenect_context *ctx;
	freenect_device *dev;
	if (freenect_init(&ctx, 0)) {
		printf("Error: Cannot get context\n");
		return;
	}

	if (freenect_open_device(ctx, &dev, 0)) {
		printf("Error: Cannot get device\n");
		return;
	}
	freenect_set_depth_format(dev, FREENECT_DEPTH_11BIT);
	freenect_start_depth(dev);
	freenect_set_video_format(dev, FREENECT_VIDEO_RGB);
	freenect_start_video(dev);
	if (use_ffmpeg) {
		init_ffmpeg_streams();
		freenect_set_depth_callback(dev, depth_cb_ffmpeg);
		freenect_set_video_callback(dev, rgb_cb_ffmpeg);
	} else {
		freenect_set_depth_callback(dev, depth_cb);
		freenect_set_video_callback(dev, rgb_cb);
	}
	while (running && freenect_process_events(ctx) >= 0)
		snapshot_accel(dev);
	freenect_stop_depth(dev);
	freenect_stop_video(dev);
	freenect_close_device(dev);
	freenect_shutdown(ctx);
}
Exemplo n.º 4
0
		void setVideoFormat(freenect_video_format requested_format) {
			if (requested_format != m_video_format) {
				freenect_stop_video(m_dev);
				if (freenect_set_video_format(m_dev, requested_format) < 0) throw std::runtime_error("Cannot set video format");
				freenect_start_video(m_dev);
				m_video_format = requested_format;
			}
		}
Exemplo n.º 5
0
		FreenectDevice(freenect_context *_ctx, int _index) {
			if(freenect_open_device(_ctx, &m_dev, _index) < 0) throw std::runtime_error("Cannot open Kinect");
			freenect_set_user(m_dev, this);
			freenect_set_video_format(m_dev, FREENECT_VIDEO_RGB);
			freenect_set_depth_format(m_dev, FREENECT_DEPTH_11BIT);
			freenect_set_depth_callback(m_dev, freenect_depth_callback);
			freenect_set_video_callback(m_dev, freenect_video_callback);
		}
Exemplo n.º 6
0
//---------------------------------------------------------------------------
void ofxKinect::threadedFunction(){	

	
	freenect_set_led(kinectDevice, LED_GREEN);
	freenect_set_video_format(kinectDevice, bInfrared?FREENECT_VIDEO_IR_8BIT:FREENECT_VIDEO_YUV_RGB);
	freenect_set_depth_format(kinectDevice, FREENECT_DEPTH_11BIT);
	
	ofLog(OF_LOG_VERBOSE, "ofxKinect: Connection opened");

	freenect_start_depth(kinectDevice);
	if(bGrabVideo) {
		freenect_start_video(kinectDevice);
	}

	// call platform specific processors (needed for Win)
	if(freenect_process_events(kinectContext) != 0){
		ofLog(OF_LOG_ERROR, "ofxKinect: freenect_process_events failed!");
		return;
	}
	
	while(isThreadRunning()){
		if(bTiltNeedsApplying){
			freenect_set_tilt_degs(kinectDevice, targetTiltAngleDeg);
			bTiltNeedsApplying = false;
		}
        if(bLedNeedsApplying){
            freenect_set_led(kinectDevice, targetLedStatus);
            bLedNeedsApplying = false;
        }

		freenect_update_tilt_state(kinectDevice);
		freenect_raw_tilt_state * tilt = freenect_get_tilt_state(kinectDevice);

		rawAccel.set(tilt->accelerometer_x, tilt->accelerometer_y, tilt->accelerometer_z);
		
		double dx,dy,dz;
		freenect_get_mks_accel(tilt, &dx, &dy, &dz);
		mksAccel.set(dx, dy, dz);
		
		ofSleepMillis(10);

//		printf("\r raw acceleration: %4d %4d %4d  mks acceleration: %4f %4f %4f", ax, ay, az, dx, dy, dz);
	}
	
	// finish up a tilt on exit
	if(bTiltNeedsApplying){
		freenect_set_tilt_degs(kinectDevice, targetTiltAngleDeg);
		bTiltNeedsApplying = false;
	}

	freenect_stop_depth(kinectDevice);
	freenect_stop_video(kinectDevice);
	freenect_set_led(kinectDevice, LED_YELLOW);

	freenect_close_device(kinectDevice);
	
	ofLog(OF_LOG_VERBOSE, "ofxKinect: Connection closed");
}
Exemplo n.º 7
0
static int change_video_format(sync_kinect_t *kinect, freenect_video_format fmt)
{
	freenect_stop_video(kinect->dev);
	free_buffer_ring(&kinect->video);
	if (alloc_buffer_ring_video(fmt, &kinect->video))
		return -1;
	freenect_set_video_format(kinect->dev, fmt);
	freenect_set_video_buffer(kinect->dev, kinect->video.bufs[2]);
	freenect_start_video(kinect->dev);
	return 0;
}
Exemplo n.º 8
0
//---------------------------------------------------------------------------
void ofxKinect::threadedFunction() {


    freenect_set_led(kinectDevice, LED_GREEN);
    freenect_set_video_format(kinectDevice, bInfrared?FREENECT_VIDEO_IR_8BIT:FREENECT_VIDEO_RGB);
    freenect_set_depth_format(kinectDevice, FREENECT_DEPTH_11BIT);
    freenect_set_depth_callback(kinectDevice, &grabDepthFrame);
    freenect_set_video_callback(kinectDevice, &grabRgbFrame);

    ofLog(OF_LOG_VERBOSE, "ofxKinect: Connection opened");

    freenect_start_depth(kinectDevice);
    freenect_start_video(kinectDevice);

    while (isThreadRunning()) {
        if( bTiltNeedsApplying ) {

            freenect_set_tilt_degs(kinectDevice, targetTiltAngleDeg);
            bTiltNeedsApplying = false;
        }

        freenect_update_tilt_state(kinectDevice);
        freenect_raw_tilt_state * tilt = freenect_get_tilt_state(kinectDevice);

        rawAccel.set(tilt->accelerometer_x, tilt->accelerometer_y, tilt->accelerometer_z);

        double dx,dy,dz;
        freenect_get_mks_accel(tilt, &dx, &dy, &dz);
        mksAccel.set(dx, dy, dz);

        ofSleepMillis(10);

//		printf("\r raw acceleration: %4d %4d %4d  mks acceleration: %4f %4f %4f", ax, ay, az, dx, dy, dz);
    }

//TODO: uncomment these when they are implemented in freenect
    freenect_set_tilt_degs(kinectDevice, 0);
    freenect_update_tilt_state(kinectDevice);
    freenect_stop_depth(kinectDevice);
    freenect_stop_video(kinectDevice);
    freenect_set_led(kinectDevice, LED_YELLOW);

    freenect_close_device(kinectDevice);
    freenect_shutdown(kinectContext);

    ofLog(OF_LOG_VERBOSE, "ofxKinect: Connection closed");
}
Exemplo n.º 9
0
int initFreenect() {
  //  int res = 0;

  //setup Freenect...
  if (freenect_init(&f_ctx, NULL) < 0) {
    printf("freenect_init() failed\n");
    return 1;
  }

  freenect_set_log_level(f_ctx, FREENECT_LOG_ERROR);

  int nr_devices = freenect_num_devices (f_ctx);
  printf ("Number of devices found: %d\n", nr_devices);

  int user_device_number = 0;
  //  if (argc > 1)
  //    user_device_number = atoi(argv[1]);
  //
  //  if (nr_devices < 1)
  //    return 1;

  if (freenect_open_device(f_ctx, &f_dev, user_device_number) < 0) {
    printf("Could not open device\n");
    return 1;
  }

  freenect_set_tilt_degs(f_dev,freenect_angle);
  freenect_set_led(f_dev,LED_RED);
  freenect_set_depth_callback(f_dev, depth_cb);
  //freenect_set_rgb_callback(f_dev, rgb_cb);
  //freenect_set_rgb_format(f_dev, FREENECT_FORMAT_RGB);
  freenect_set_video_callback(f_dev, rgb_cb);
  freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
  freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);

  freenect_start_depth(f_dev);
  freenect_start_video(f_dev);

  //start the freenect thread to poll for events
  //  res = pthread_create(&ocv_thread, NULL, freenect_threadfunc, NULL);
  //  if (res) {
  //    printf("pthread_create failed\n");
  //    return 1;
  //  }
  return 0;
}
Exemplo n.º 10
0
/*
 * Main method; primarily just initializes our thread and handles Kinect details.
 */
int main(int argc, char **argv)
{
	freenect_context *f_ctx;
	freenect_device *f_dev;

	display = 0;

	int res = 0;
	int die = 0;
	printf("Code Red Kinect Vision init\n");

	if (argc > 1 && strcmp(argv[1],"--display") == 0) {
		display = 1;
	}
			
	if (freenect_init(&f_ctx, NULL) < 0) {
		printf("freenect_init() failed\n");
		return 1;
	}

	if (freenect_open_device(f_ctx, &f_dev, 0) < 0) {
		printf("Could not open device\n");
		return 1;
	}


	if (display) { cvNamedWindow( FREENECTOPENCV_WINDOW_N, CV_WINDOW_AUTOSIZE ); }
	rgbimg = cvCreateImage(cvSize(FREENECTOPENCV_RGB_WIDTH, FREENECTOPENCV_RGB_HEIGHT), IPL_DEPTH_8U, FREENECTOPENCV_IR_DEPTH);
	tempimg = cvCreateImage(cvSize(FREENECTOPENCV_RGB_WIDTH, FREENECTOPENCV_RGB_HEIGHT), IPL_DEPTH_8U, FREENECTOPENCV_RGB_DEPTH);

	// create opencv display thread
	res = pthread_create(&cv_thread, NULL, cv_threadfunc, (void*) tempimg);
	if (res) {
		printf("pthread_create failed\n");
		return 1;
	}

	freenect_set_video_callback(f_dev, rgb_cb);
	freenect_set_video_format(f_dev, FREENECT_VIDEO_IR_8BIT);

	printf("init done\n");

	freenect_start_video(f_dev);

	while(!die && freenect_process_events(f_ctx) >= 0 );
}
Exemplo n.º 11
0
  /* Function: mdlStart =======================================================
   * Abstract:
   *    This function is called once at start of model execution. If you
   *    have states that should be initialized once, this is the place
   *    to do it.
   */
  static void mdlStart(SimStruct *S)
  {
    SS_SimMode mode= ssGetSimMode(S);
    if(mode==0)
    {
        kinFail=0;
        kinRunning=1;
        newFrame=0;
//change
        memset (kinR,'1',307200);
        memset (kinG,'1',307200);
        memset (kinB,'1',307200);
        memset (kinD,'1',307200);


        
        
        /*-----INIT---------------*/

        if (freenect_init(&kinCtx, 0)) {
            printf("Error: Cannot get context\n");
            kinFail=1;
            return;
        }

        if (freenect_open_device(kinCtx, &kinDev, 0)) {
            printf("Error: Cannot get device\n");
            kinFail=1;
            return;
        }
        freenect_set_depth_format(kinDev, FREENECT_DEPTH_11BIT);
        freenect_start_depth(kinDev);
//change
        freenect_set_video_format(kinDev, FREENECT_VIDEO_YUV_RGB);
        freenect_start_video(kinDev);
        freenect_set_depth_callback(kinDev, depth_cb);
        freenect_set_video_callback(kinDev, rgb_cb);

        /*-----END INIT------------*/

        pthread_create( &kinThread, NULL, runLoop, NULL);
    }
    
    
    
  }
Exemplo n.º 12
0
int main(int argc, char **argv)
{

    cv_depth_mat = cvCreateMat(480, 640, CV_16UC1);
    cv_rgb_mat = cvCreateMat(480, 640, CV_8UC3);

    int res;
	g_argc = argc;
	g_argv = argv;

	if (freenect_init(&f_ctx, NULL) < 0) {
		printf("freenect_init() failed\n");
		return 1;
	}

	freenect_set_log_level(f_ctx, FREENECT_LOG_INFO);

	int nr_devices = freenect_num_devices (f_ctx);
	printf ("Number of devices found: %d\n", nr_devices);

	int user_device_number = 0;
	if (argc > 1)
		user_device_number = atoi(argv[1]);

	if (nr_devices < 1)
		return 1;

	if (freenect_open_device(f_ctx, &f_dev, user_device_number) < 0) {
		printf("Could not open device\n");
		return 1;
	}

	//freenect_set_tilt_degs(f_dev,15);
	freenect_set_tilt_degs(f_dev,0);
	freenect_set_led(f_dev,LED_RED);
	freenect_set_depth_callback(f_dev, depth_cb);
	freenect_set_video_callback(f_dev, rgb_cb);
	freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB);
	freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT);

	freenect_start_depth(f_dev);
	freenect_start_video(f_dev);

    cvNamedWindow("rgb", CV_WINDOW_NORMAL);
    cvNamedWindow("depth", CV_WINDOW_NORMAL);
    cvNamedWindow("depth_th", CV_WINDOW_NORMAL);
    cvNamedWindow("contourWin", CV_WINDOW_NORMAL);

    CvMat* cv_depth_threshold_mat = cvCreateMat(480,640, CV_8UC1);

	res = pthread_create(&freenect_thread, NULL, freenect_threadfunc, NULL);
	if (res) {
		printf("pthread_create failed\n");
		return 1;
	}

    // Variables for contour finding
    CvSeq* contours = NULL;
    CvMemStorage* memStorage = cvCreateMemStorage(0);
    IplImage* contour_image = cvCreateImage(cvSize(640,480), 8, 1);


    //hist_segment_init();
    //feature_extract_init();

    optflow_init(cvSize(640,480));

	while (!die)
	{
        cvConvertScale(cv_depth_mat, cv_depth_threshold_mat, 255.0/2048.0, 0);

        // 120 ~ 2.7 m
        cvThreshold( cv_depth_threshold_mat, cv_depth_threshold_mat, 120.0, 120.0, CV_THRESH_TOZERO_INV);

        //cvThreshold( cv_depth_threshold_mat, cv_depth_threshold_mat, 120.0, 255.0, CV_THRESH_BINARY_INV);

        // Find contours
        /*cvClearMemStorage(memStorage);
        cvFindContours( cv_depth_threshold_mat, memStorage, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
        cvZero(contour_image);

        // Draw the contours
        if (contours)
        {
            cvDrawContours(contour_image, contours, cvScalarAll(255.0), cvScalarAll(255.0), 1, 1, 8, cvPoint(0,0));
        }
        cvShowImage("contourWin",contour_image);*/

        //extractFeatures(cv_depth_threshold_mat);
        //hist_segment(cv_depth_threshold_mat, NULL);
        optflow_calculate(cv_depth_threshold_mat, NULL);


        cvShowImage("rgb", cv_rgb_mat);

		cvShowImage("depth_th", cv_depth_threshold_mat);


        char k = cvWaitKey(5);
        if( k == 27 ) break;

    }

    optflow_deinit();
    //hist_segment_deinit();
    //feature_extract_deinit();


	printf("-- done!\n");

	cvDestroyWindow("rgb");
	cvDestroyWindow("depth");
	cvDestroyWindow("depth_th");

    cvReleaseMat(&cv_depth_mat);
    cvReleaseMat(&cv_rgb_mat);

    cvReleaseMat(&cv_depth_threshold_mat);

    // Release Contour variables
    cvDestroyWindow("contourWin");

	pthread_join(freenect_thread, NULL);
	pthread_exit(NULL);
}