# HG changeset patch # User Eleni Maria Stea # Date 1381348602 -10800 # Node ID b0b90ef993a01179615fff08cb27a7c42ed84592 # Parent fdbd55eaa14e236115ae1b1760bce55365f95698 backup diff -r fdbd55eaa14e -r b0b90ef993a0 Makefile --- a/Makefile Sat Oct 05 19:02:40 2013 +0300 +++ b/Makefile Wed Oct 09 22:56:42 2013 +0300 @@ -3,7 +3,7 @@ dep = $(obj:.o=.d) bin = invisible -dbg = -g -std=c++11 +dbg = -g opt = -O0 CXX = g++ diff -r fdbd55eaa14e -r b0b90ef993a0 src/frame.cc --- a/src/frame.cc Sat Oct 05 19:02:40 2013 +0300 +++ b/src/frame.cc Wed Oct 09 22:56:42 2013 +0300 @@ -1,27 +1,50 @@ +#include +#include #include -#include +#include #include "frame.h" +#include "kinect.h" -static pthread_mutex_t video_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t depth_mutex = PTHREAD_MUTEX_INITIALIZER; +extern Frame *frame; +extern KinectParams *kin_params; -void init_frame_mx() +extern bool has_video; +extern bool has_depth; + +Frame::Frame() { - pthread_mutex_init(&video_mutex, NULL); - pthread_mutex_init(&depth_mutex, NULL); + if(video_buf.empty()) { + video_buf = cv::Mat(FREENECT_VIDEO_WIDTH, FREENECT_VIDEO_HEIGHT, CV_32FC3, cv::Scalar(0, 0, 0)); + } + if(depth_buf.empty()) { + depth_buf = cv::Mat(FREENECT_DEPTH_WIDTH, FREENECT_VIDEO_HEIGHT, CV_16UC1, 0); + } } -void video_cb(freenect_device *kin_dev, void *rgb, uint32_t time) +void Frame::process() { - pthread_mutex_lock(&video_mutex); - printf("Started video.\n"); - pthread_mutex_unlock(&video_mutex); + if(has_video && has_depth) + printf("Depth and Rgb\n"); +} + +void video_cb(freenect_device *kin_dev, void *video, uint32_t time) +{ + if(!video) { + has_video = false; + return; + } + + //copy *video to video_buf if fail => has_video = false + has_video = true; } void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time) { - pthread_mutex_lock(&depth_mutex); - printf("Started depth.\n"); - pthread_mutex_unlock(&depth_mutex); + if(!depth) { + has_depth = false; + return; + } + + has_depth = true; } diff -r fdbd55eaa14e -r b0b90ef993a0 src/frame.h --- a/src/frame.h Sat Oct 05 19:02:40 2013 +0300 +++ b/src/frame.h Wed Oct 09 22:56:42 2013 +0300 @@ -2,19 +2,17 @@ #define FRAME_H_ #include +#include -class Frame { -private: - int width; - int height; +struct Frame { + cv::Mat depth_buf; + cv::Mat video_buf; -public: Frame(); - + void process(); }; -void init_frame_mx(); -void video_cb(freenect_device *kin_dev, void *rgb, uint32_t time); +void video_cb(freenect_device *kin_dev, void *video, uint32_t time); void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time); #endif // FRAME_H_ diff -r fdbd55eaa14e -r b0b90ef993a0 src/kinect.cc --- a/src/kinect.cc Sat Oct 05 19:02:40 2013 +0300 +++ b/src/kinect.cc Wed Oct 09 22:56:42 2013 +0300 @@ -33,12 +33,16 @@ } if(freenect_set_led(*kin_dev, kin_params->led_color) < 0) { + fprintf(stderr, "Failed to set kinect LED\n"); stop_kinect(*kin_ctx, *kin_dev); - fprintf(stderr, "Failed to set kinect LED\n"); return false; } - init_frame_mx(); + return true; +} + +bool init_kinect_frames(freenect_context **kin_ctx, freenect_device **kin_dev, + KinectParams *kin_params) { freenect_set_video_callback(*kin_dev, video_cb); freenect_set_depth_callback(*kin_dev, depth_cb); @@ -46,7 +50,6 @@ if(freenect_set_video_mode(*kin_dev, freenect_find_video_mode(kin_params->video_res, kin_params->video_format)) < 0) { - stop_kinect(*kin_ctx, *kin_dev); fprintf(stderr, "Failed to set kinect video mode.\n"); return false; } @@ -54,7 +57,6 @@ if(freenect_set_depth_mode(*kin_dev, freenect_find_depth_mode(kin_params->depth_res, kin_params->depth_format)) < 0) { - stop_kinect(*kin_ctx, *kin_dev); fprintf(stderr, "Failed to set kinect depth mode.\n"); return false; } @@ -72,12 +74,25 @@ return true; } +void stop_kinect_video_frames(freenect_device *kin_dev) +{ + freenect_stop_video(kin_dev); +} + +void stop_kinect_depth_frames(freenect_device *kin_dev) +{ + freenect_stop_depth(kin_dev); +} + +void stop_kinect_frames(freenect_device *kin_dev) +{ + stop_kinect_video_frames(kin_dev); + stop_kinect_depth_frames(kin_dev); +} + void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev) { freenect_set_led(kin_dev, LED_OFF); - - freenect_stop_depth(kin_dev); - freenect_stop_video(kin_dev); freenect_close_device(kin_dev); freenect_shutdown(kin_ctx); } diff -r fdbd55eaa14e -r b0b90ef993a0 src/kinect.h --- a/src/kinect.h Sat Oct 05 19:02:40 2013 +0300 +++ b/src/kinect.h Wed Oct 09 22:56:42 2013 +0300 @@ -3,10 +3,20 @@ #include -#define FREENECT_ANGLE 30 +/* from specs */ +#define FREENECT_VIDEO_WIDTH 640 +#define FREENECT_VIDEO_HEIGHT 480 +#define FREENECT_DEPTH_WIDTH 640 +#define FREENECT_DEPTH_HEIGHT 480 struct KinectParams { double angle; + + int video_width; + int video_height; + int depth_width; + int depth_height; + freenect_led_options led_color; freenect_video_format video_format; freenect_depth_format depth_format; @@ -16,6 +26,12 @@ KinectParams() { angle = 10; + + video_width = FREENECT_VIDEO_WIDTH; + video_height = FREENECT_DEPTH_HEIGHT; + depth_width = FREENECT_VIDEO_WIDTH; + depth_height = FREENECT_DEPTH_HEIGHT; + led_color = LED_RED; video_format = FREENECT_VIDEO_RGB; depth_format = FREENECT_DEPTH_11BIT; @@ -25,6 +41,12 @@ }; bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params); +bool init_kinect_frames(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params); + +void stop_kinect_video_frames(freenect_device *kin_dev); +void stop_kinect_depth_frames(freenect_device *kin_dev); +void stop_kinect_frames(freenect_device *kin_dev); + void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev); #endif // KINECT_H_ diff -r fdbd55eaa14e -r b0b90ef993a0 src/main.cc --- a/src/main.cc Sat Oct 05 19:02:40 2013 +0300 +++ b/src/main.cc Wed Oct 09 22:56:42 2013 +0300 @@ -1,17 +1,100 @@ +#include +#include #include +#include + #include "kinect.h" +#include "frame.h" freenect_context *kin_ctx; freenect_device *kin_dev; KinectParams kin_params; +Frame *frame; -int main() +static void cleanup(); + +static void display(); +static void reshape(int w, int h); +static void keyb(unsigned char key, int x, int y); +static void idle(); + +bool has_video; +bool has_depth; + +int main(int argc, char **argv) { - printf("hi\n"); - if(!init_kinect(&kin_ctx, &kin_dev, &kin_params)) return 1; + if(!init_kinect_frames(&kin_ctx, &kin_dev, &kin_params)) { + stop_kinect(kin_ctx, kin_dev); + return 1; + } + + glutInitWindowSize(800, 600); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); + glutCreateWindow("Test Kinect"); + + glewInit(); + + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + glutIdleFunc(idle); + + glClearColor(1, 1, 1, 1); + + atexit(cleanup); + glutMainLoop(); +} + +static void display() +{ + glClear(GL_COLOR_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glClearColor(1, 0, 0, 1); + + has_depth = false; has_video = false; + + if(freenect_process_events(kin_ctx) != 0) { + fprintf(stderr, "Failed to process events.\n"); + exit(0); + } + frame->process(); + + glutSwapBuffers(); +} + +static void reshape(int w, int h) +{ + glViewport(0, 0, (GLsizei) w, (GLsizei) h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45, (float)w / (float)h, 1, 1000); +} + +static void keyb(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + default: + break; + } +} + +static void idle() +{ + glutPostRedisplay(); +} + +static void cleanup() +{ + stop_kinect_frames(kin_dev); stop_kinect(kin_ctx, kin_dev); }