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