invisible

changeset 0:80df8030105b

init kinect
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sat, 05 Oct 2013 16:03:08 +0300
parents
children fdbd55eaa14e
files .hgignore Makefile src/kinect.cc src/kinect.h src/main.cc
diffstat 5 files changed, 180 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Sat Oct 05 16:03:08 2013 +0300
     1.3 @@ -0,0 +1,8 @@
     1.4 +syntax: glob
     1.5 +*.o
     1.6 +*.settings
     1.7 +*.cproject
     1.8 +*.project
     1.9 +*.d
    1.10 +invisible
    1.11 +*.swp
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Sat Oct 05 16:03:08 2013 +0300
     2.3 @@ -0,0 +1,23 @@
     2.4 +src = $(wildcard src/*.cc)
     2.5 +obj = $(src:.cc=.o)
     2.6 +dep = $(obj:.o=.d)
     2.7 +bin = invisible
     2.8 +
     2.9 +dbg = -g -std=c++11
    2.10 +opt = -O0
    2.11 +
    2.12 +CXX = g++
    2.13 +INCLUDE = -I/usr/include/
    2.14 +CXXFLAGS = -fPIC -pedantic -Wall $(dbg) `pkg-config --cflags opencv` $(INCLUDE)
    2.15 +LDFLAGS = -L/usr/lib $(libs) -lGL -lGLU -lglut -lGLEW -lfreenect -lpthread `pkg-config --libs opencv`
    2.16 +
    2.17 +$(bin): $(obj)
    2.18 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    2.19 +
    2.20 +%.d: %.cc
    2.21 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.22 +
    2.23 +.PHONY: clean
    2.24 +clean:
    2.25 +	rm -f $(obj) $(bin) $(dep)
    2.26 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/kinect.cc	Sat Oct 05 16:03:08 2013 +0300
     3.3 @@ -0,0 +1,102 @@
     3.4 +#include <stdio.h>
     3.5 +#include <pthread.h>
     3.6 +#include <libfreenect.h>
     3.7 +#include "kinect.h"
     3.8 +
     3.9 +static void video_cb(freenect_device *kin_dev, void *rgb, uint32_t time);
    3.10 +static void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time);
    3.11 +
    3.12 +static pthread_mutex_t video_mutex = PTHREAD_MUTEX_INITIALIZER;
    3.13 +static pthread_mutex_t depth_mutex = PTHREAD_MUTEX_INITIALIZER;
    3.14 +
    3.15 +bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params)
    3.16 +{
    3.17 +	if(freenect_init(kin_ctx, NULL) < 0) {
    3.18 +		fprintf(stderr, "Failed to initialize kinect context.\n");
    3.19 +		return false;
    3.20 +	}
    3.21 +
    3.22 +	freenect_set_log_level(*kin_ctx, FREENECT_LOG_INFO);
    3.23 +
    3.24 +	int num_dev = freenect_num_devices(*kin_ctx);
    3.25 +	if(num_dev < 1) {
    3.26 +		fprintf(stderr, "Kinect device not found.\n");
    3.27 +		return false;
    3.28 +	}
    3.29 +	printf("%d devices found, will use the first.\n", num_dev);
    3.30 +
    3.31 +	if(freenect_open_device(*kin_ctx, kin_dev, 0) < 0) {
    3.32 +		fprintf(stderr, "Couldn't open kinect device.\n");
    3.33 +		return false;
    3.34 +	}
    3.35 +
    3.36 +	if(freenect_set_tilt_degs(*kin_dev, kin_params->angle) < 0) {
    3.37 +		stop_kinect(*kin_ctx, *kin_dev);
    3.38 +		fprintf(stderr, "Failed to set kinect angle\n");
    3.39 +		return false;
    3.40 +	}
    3.41 +
    3.42 +	if(freenect_set_led(*kin_dev, kin_params->led_color) < 0) {
    3.43 +		stop_kinect(*kin_ctx, *kin_dev);
    3.44 +		fprintf(stderr, "Failed to set kinect LED\n");
    3.45 +		return false;
    3.46 +	}
    3.47 +
    3.48 +	pthread_mutex_init(&video_mutex, NULL);
    3.49 +	pthread_mutex_init(&depth_mutex, NULL);
    3.50 +
    3.51 +	freenect_set_video_callback(*kin_dev, video_cb);
    3.52 +	freenect_set_depth_callback(*kin_dev, depth_cb);
    3.53 +
    3.54 +	if(freenect_set_video_mode(*kin_dev,
    3.55 +			freenect_find_video_mode(kin_params->video_res,
    3.56 +				kin_params->video_format)) < 0) {
    3.57 +		stop_kinect(*kin_ctx, *kin_dev);
    3.58 +		fprintf(stderr, "Failed to set kinect video mode.\n");
    3.59 +		return false;
    3.60 +	}
    3.61 +
    3.62 +	if(freenect_set_depth_mode(*kin_dev,
    3.63 +				freenect_find_depth_mode(kin_params->depth_res,
    3.64 +					kin_params->depth_format)) < 0) {
    3.65 +		stop_kinect(*kin_ctx, *kin_dev);
    3.66 +		fprintf(stderr, "Failed to set kinect depth mode.\n");
    3.67 +		return false;
    3.68 +	}
    3.69 +
    3.70 +	if(freenect_start_video(*kin_dev) < 0) {
    3.71 +		fprintf(stderr, "Failed to start kinect video.\n");
    3.72 +		return false;
    3.73 +	}
    3.74 +
    3.75 +	if(freenect_start_depth(*kin_dev) < 0) {
    3.76 +		fprintf(stderr, "Failed to start kinect depth video.\n");
    3.77 +		return false;
    3.78 +	}
    3.79 +
    3.80 +	return true;
    3.81 +}
    3.82 +
    3.83 +void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev)
    3.84 +{
    3.85 +	freenect_set_led(kin_dev, LED_OFF);
    3.86 +
    3.87 +	freenect_stop_depth(kin_dev);
    3.88 +	freenect_stop_video(kin_dev);
    3.89 +	freenect_close_device(kin_dev);
    3.90 +	freenect_shutdown(kin_ctx);
    3.91 +}
    3.92 +
    3.93 +void video_cb(freenect_device *kin_dev, void *rgb, uint32_t time)
    3.94 +{
    3.95 +	pthread_mutex_lock(&video_mutex);
    3.96 +	printf("Started video.\n");
    3.97 +	pthread_mutex_unlock(&video_mutex);
    3.98 +}
    3.99 +
   3.100 +void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
   3.101 +{
   3.102 +	pthread_mutex_lock(&depth_mutex);
   3.103 +	printf("Started depth.\n");
   3.104 +	pthread_mutex_unlock(&depth_mutex);
   3.105 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/kinect.h	Sat Oct 05 16:03:08 2013 +0300
     4.3 @@ -0,0 +1,30 @@
     4.4 +#ifndef KINECT_H_
     4.5 +#define KINECT_H_
     4.6 +
     4.7 +#include <libfreenect.h>
     4.8 +
     4.9 +#define FREENECT_ANGLE 30
    4.10 +
    4.11 +struct KinectParams {
    4.12 +	double angle;
    4.13 +	freenect_led_options led_color;
    4.14 +	freenect_video_format video_format;
    4.15 +	freenect_depth_format depth_format;
    4.16 +	freenect_resolution video_res;
    4.17 +	freenect_resolution depth_res;
    4.18 +
    4.19 +	KinectParams()
    4.20 +	{
    4.21 +		angle = 10;
    4.22 +		led_color = LED_RED;
    4.23 +		video_format = FREENECT_VIDEO_RGB;
    4.24 +		depth_format = FREENECT_DEPTH_11BIT;
    4.25 +		video_res = FREENECT_RESOLUTION_MEDIUM;
    4.26 +		depth_res = FREENECT_RESOLUTION_MEDIUM;
    4.27 +	}
    4.28 +};
    4.29 +
    4.30 +bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params);
    4.31 +void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev);
    4.32 +
    4.33 +#endif // KINECT_H_
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/main.cc	Sat Oct 05 16:03:08 2013 +0300
     5.3 @@ -0,0 +1,17 @@
     5.4 +#include <stdio.h>
     5.5 +
     5.6 +#include "kinect.h"
     5.7 +
     5.8 +freenect_context *kin_ctx;
     5.9 +freenect_device *kin_dev;
    5.10 +KinectParams kin_params;
    5.11 +
    5.12 +int main()
    5.13 +{
    5.14 +	printf("hi\n");
    5.15 +
    5.16 +	if(!init_kinect(&kin_ctx, &kin_dev, &kin_params))
    5.17 +		return 1;
    5.18 +
    5.19 +	stop_kinect(kin_ctx, kin_dev);
    5.20 +}