invisible
view src/kinect.cc @ 18:ad78eb81988d
backup
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Sun, 10 Nov 2013 00:38:23 +0200 |
parents | 4f1b8ddcd32e |
children |
line source
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <libfreenect.h>
5 #include "kinect.h"
6 #include "frame.h"
8 KinectParams::KinectParams()
9 {
10 angle = 10;
12 video_width = KINECT_VIDEO_WIDTH;
13 video_height = KINECT_VIDEO_HEIGHT;
14 depth_width = KINECT_DEPTH_WIDTH;
15 depth_height = KINECT_DEPTH_HEIGHT;
17 led_color = LED_RED;
18 video_format = FREENECT_VIDEO_RGB;
19 depth_format = FREENECT_DEPTH_11BIT;
20 video_res = FREENECT_RESOLUTION_MEDIUM;
21 depth_res = FREENECT_RESOLUTION_MEDIUM;
22 }
24 bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params)
25 {
26 if(freenect_init(kin_ctx, NULL) < 0) {
27 fprintf(stderr, "Failed to initialize kinect context.\n");
28 return false;
29 }
31 freenect_set_log_level(*kin_ctx, FREENECT_LOG_INFO);
33 int num_dev = freenect_num_devices(*kin_ctx);
34 if(num_dev < 1) {
35 fprintf(stderr, "Kinect device not found.\n");
36 return false;
37 }
38 printf("%d devices found, will use the first.\n", num_dev);
40 if(freenect_open_device(*kin_ctx, kin_dev, 0) < 0) {
41 fprintf(stderr, "Couldn't open kinect device.\n");
42 return false;
43 }
45 /* if(freenect_set_tilt_degs(*kin_dev, kin_params->angle) < 0) {
46 stop_kinect(*kin_ctx, *kin_dev);
47 fprintf(stderr, "Failed to set kinect angle\n");
48 return false;
49 }
50 */
51 if(freenect_set_led(*kin_dev, kin_params->led_color) < 0) {
52 fprintf(stderr, "Failed to set kinect LED\n");
53 stop_kinect(*kin_ctx, *kin_dev);
54 return false;
55 }
57 return true;
58 }
60 bool init_kinect_frames(freenect_context *kin_ctx, freenect_device *kin_dev,
61 KinectParams *kin_params) {
63 freenect_set_video_callback(kin_dev, video_cb);
64 freenect_set_depth_callback(kin_dev, depth_cb);
66 if(freenect_set_video_mode(kin_dev,
67 freenect_find_video_mode(kin_params->video_res,
68 kin_params->video_format)) < 0) {
69 fprintf(stderr, "Failed to set kinect video mode.\n");
70 return false;
71 }
73 if(freenect_set_depth_mode(kin_dev,
74 freenect_find_depth_mode(kin_params->depth_res,
75 kin_params->depth_format)) < 0) {
76 fprintf(stderr, "Failed to set kinect depth mode.\n");
77 return false;
78 }
80 if(freenect_start_video(kin_dev) < 0) {
81 fprintf(stderr, "Failed to start kinect video.\n");
82 return false;
83 }
85 if(freenect_start_depth(kin_dev) < 0) {
86 fprintf(stderr, "Failed to start kinect depth video.\n");
87 return false;
88 }
90 return true;
91 }
93 void stop_kinect_video_frames(freenect_device *kin_dev)
94 {
95 freenect_stop_video(kin_dev);
96 }
98 void stop_kinect_depth_frames(freenect_device *kin_dev)
99 {
100 freenect_stop_depth(kin_dev);
101 }
103 void stop_kinect_frames(freenect_device *kin_dev)
104 {
105 stop_kinect_video_frames(kin_dev);
106 stop_kinect_depth_frames(kin_dev);
107 }
109 void stop_kinect(freenect_context *kin_ctx, freenect_device *kin_dev)
110 {
111 freenect_set_led(kin_dev, LED_OFF);
112 freenect_close_device(kin_dev);
113 freenect_shutdown(kin_ctx);
114 }