00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <unistd.h>
00030 #include <string.h>
00031 #include "libfreenect.h"
00032 #include <pthread.h>
00033 #include <arpa/inet.h>
00034 #include <signal.h>
00035
00036 #include <math.h>
00037
00038 int g_argc;
00039 char **g_argv;
00040
00041 freenect_context *f_ctx;
00042 freenect_device *f_dev;
00043
00044 int got_frames = 0;
00045
00046 #define AS3_BITMAPDATA_LEN 640 * 480 * 4
00047
00048 struct sockaddr_in si_depth, si_rgb, si_data;
00049 pthread_t depth_thread, rgb_thread, data_thread;
00050 pthread_mutex_t depth_mutex = PTHREAD_MUTEX_INITIALIZER,
00051 rgb_mutex = PTHREAD_MUTEX_INITIALIZER;
00052 pthread_cond_t depth_cond = PTHREAD_COND_INITIALIZER,
00053 rgb_cond = PTHREAD_COND_INITIALIZER;
00054 char *conf_ip = "127.0.0.1";
00055 int s_depth = -1,
00056 s_rgb = -1,
00057 s_data = -1,
00058 conf_port_depth = 6001,
00059 conf_port_rgb = 6002,
00060 conf_port_data = 6003;
00061
00062 uint8_t buf_depth[AS3_BITMAPDATA_LEN];
00063 uint8_t buf_rgb[AS3_BITMAPDATA_LEN];
00064
00065 int die = 0;
00066
00067 int depth_child;
00068 int depth_connected = 0;
00069
00070 int rgb_child;
00071 int rgb_connected = 0;
00072
00073 int data_child;
00074 int data_connected = 0;
00075
00076
00077 int psent = 0;
00078
00079 void send_policy_file(int child){
00080 if(psent == 0){
00081 int n;
00082 char * str = "<?xml version='1.0'?><!DOCTYPE cross-domain-policy SYSTEM '/xml/dtds/cross-domain-policy.dtd'><cross-domain-policy><site-control permitted-cross-domain-policies='all'/><allow-access-from domain='*' to-ports='*'/></cross-domain-policy>\n";
00083 n = write(child,str , 237);
00084 if ( n < 0 || n != 237)
00085 {
00086 fprintf(stderr, "Error on write() for depth (%d instead of %d)\n",n, 237);
00087
00088 }
00089 psent = 1;
00090 }
00091 }
00092
00093 void *network_depth(void *arg)
00094 {
00095 int childlen;
00096 struct sockaddr_in childaddr;
00097
00098 childlen = sizeof(childaddr);
00099 while ( !die )
00100 {
00101 printf("### Wait depth client\n");
00102 depth_child = accept(s_depth, (struct sockaddr *)&childaddr, (unsigned int *)&childlen);
00103 if ( network_depth < 0 )
00104 {
00105 fprintf(stderr, "Error on accept() for depth, exit depth thread.\n");
00106 break;
00107 }
00108
00109 printf("### Got depth client\n");
00110 send_policy_file(depth_child);
00111 depth_connected = 1;
00112 freenect_start_depth(f_dev);
00113 }
00114
00115 return NULL;
00116 }
00117
00118 void *network_rgb(void *arg)
00119 {
00120 int childlen;
00121 struct sockaddr_in childaddr;
00122
00123 childlen = sizeof(childaddr);
00124 while ( !die )
00125 {
00126 printf("### Wait rgb client\n");
00127 rgb_child = accept(s_rgb, (struct sockaddr *)&childaddr, (unsigned int *)&childlen);
00128 if ( rgb_child < 0 )
00129 {
00130 fprintf(stderr, "Error on accept() for rgb, exit rgb thread.\n");
00131 break;
00132 }
00133
00134 printf("### Got rgb client\n");
00135 send_policy_file(rgb_child);
00136 rgb_connected = 1;
00137 freenect_start_rgb(f_dev);
00138 }
00139
00140 return NULL;
00141 }
00142
00143 void *network_data(void *arg)
00144 {
00145 int childlen;
00146 struct sockaddr_in childaddr;
00147
00148 childlen = sizeof(childaddr);
00149 while ( !die )
00150 {
00151 printf("### Wait data client\n");
00152 data_child = accept(s_data, (struct sockaddr *)&childaddr, (unsigned int *)&childlen);
00153 if ( data_child < 0 )
00154 {
00155 fprintf(stderr, "Error on accept() for data, exit data thread.\n");
00156 break;
00157 }
00158
00159 printf("### Got data client\n");
00160
00161 while(!die && freenect_process_events(f_ctx) >= 0 )
00162 {
00163 int n;
00164 int16_t ax,ay,az;
00165 freenect_get_raw_accel(f_dev, &ax, &ay, &az);
00166 double dx,dy,dz;
00167 freenect_get_mks_accel(f_dev, &dx, &dy, &dz);
00168 char buffer_send[3*2+3*8];
00169 memcpy(&buffer_send,&ax, sizeof(int16_t));
00170 memcpy(&buffer_send[2],&ay, sizeof(int16_t));
00171 memcpy(&buffer_send[4],&az, sizeof(int16_t));
00172 memcpy(&buffer_send[6],&dx, sizeof(double));
00173 memcpy(&buffer_send[14],&dy, sizeof(double));
00174 memcpy(&buffer_send[22],&dz, sizeof(double));
00175 n = write(data_child, buffer_send, 3*2+3*8);
00176 }
00177 }
00178
00179 return NULL;
00180 }
00181
00182 int network_init()
00183 {
00184 int optval = 1;
00185
00186 signal(SIGPIPE, SIG_IGN);
00187
00188 if ( (s_depth = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
00189 {
00190 fprintf(stderr, "Unable to create depth socket\n");
00191 return -1;
00192 }
00193
00194 if ( (s_rgb = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
00195 {
00196 fprintf(stderr, "Unable to create rgb socket\n");
00197 return -1;
00198 }
00199
00200 if ( (s_data = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
00201 {
00202 fprintf(stderr, "Unable to create data socket\n");
00203 return -1;
00204 }
00205
00206 setsockopt(s_depth, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(optval));
00207 setsockopt(s_rgb, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(optval));
00208 setsockopt(s_data, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(optval));
00209
00210 memset((char *) &si_depth, 0, sizeof(si_depth));
00211 memset((char *) &si_rgb, 0, sizeof(si_rgb));
00212 memset((char *) &si_data, 0, sizeof(si_data));
00213
00214 si_depth.sin_family = AF_INET;
00215 si_depth.sin_port = htons(conf_port_depth);
00216 si_depth.sin_addr.s_addr = inet_addr(conf_ip);
00217
00218 si_rgb.sin_family = AF_INET;
00219 si_rgb.sin_port = htons(conf_port_rgb);
00220 si_rgb.sin_addr.s_addr = inet_addr(conf_ip);
00221
00222 si_data.sin_family = AF_INET;
00223 si_data.sin_port = htons(conf_port_data);
00224 si_data.sin_addr.s_addr = inet_addr(conf_ip);
00225
00226 if ( bind(s_depth, (struct sockaddr *)&si_depth,
00227 sizeof(si_depth)) < 0 )
00228 {
00229 fprintf(stderr, "Error at bind() for depth\n");
00230 return -1;
00231 }
00232
00233 if ( bind(s_rgb, (struct sockaddr *)&si_rgb,
00234 sizeof(si_rgb)) < 0 )
00235 {
00236 fprintf(stderr, "Error at bind() for rgb\n");
00237 return -1;
00238 }
00239
00240 if ( bind(s_data, (struct sockaddr *)&si_data,
00241 sizeof(si_data)) < 0 )
00242 {
00243 fprintf(stderr, "Error at bind() for data\n");
00244 return -1;
00245 }
00246
00247 if ( listen(s_depth, 1) < 0 )
00248 {
00249 fprintf(stderr, "Error on listen() for depth\n");
00250 return -1;
00251 }
00252
00253 if ( listen(s_rgb, 1) < 0 )
00254 {
00255 fprintf(stderr, "Error on listen() for rgb\n");
00256 return -1;
00257 }
00258
00259 if ( listen(s_data, 1) < 0 )
00260 {
00261 fprintf(stderr, "Error on listen() for data\n");
00262 return -1;
00263 }
00264
00265
00266
00267
00268 if ( pthread_create(&depth_thread, NULL, network_depth, NULL) )
00269 {
00270 fprintf(stderr, "Error on pthread_create() for depth\n");
00271 return -1;
00272 }
00273
00274 if ( pthread_create(&rgb_thread, NULL, network_rgb, NULL) )
00275 {
00276 fprintf(stderr, "Error on pthread_create() for rgb\n");
00277 return -1;
00278 }
00279
00280 if ( pthread_create(&data_thread, NULL, network_data, NULL) )
00281 {
00282 fprintf(stderr, "Error on pthread_create() for data\n");
00283 return -1;
00284 }
00285
00286
00287 return 0;
00288 }
00289
00290 void network_close()
00291 {
00292 die = 1;
00293 if ( s_depth != -1 )
00294 close(s_depth), s_depth = -1;
00295 if ( s_rgb != -1 )
00296 close(s_rgb), s_rgb = -1;
00297 }
00298
00299 uint16_t t_gamma[2048];
00300
00301 void depth_cb(freenect_device *dev, void *v_depth, uint32_t timestamp)
00302 {
00303 int i, n;
00304 freenect_depth *depth = v_depth;
00305
00306 pthread_mutex_lock(&depth_mutex);
00307 for (i=0; i<FREENECT_FRAME_PIX; i++) {
00308 int pval = t_gamma[depth[i]];
00309 int lb = pval & 0xff;
00310 switch (pval>>8) {
00311 case 0:
00312 buf_depth[4 * i + 0] = 255;
00313 buf_depth[4 * i + 1] = 255-lb;
00314 buf_depth[4 * i + 2] = 255-lb;
00315 break;
00316 case 1:
00317 buf_depth[4 * i + 0] = 255;
00318 buf_depth[4 * i + 1] = lb;
00319 buf_depth[4 * i + 2] = 0;
00320 break;
00321 case 2:
00322 buf_depth[4 * i + 0] = 255-lb;
00323 buf_depth[4 * i + 1] = 255;
00324 buf_depth[4 * i + 2] = 0;
00325 break;
00326 case 3:
00327 buf_depth[4 * i + 0] = 0;
00328 buf_depth[4 * i + 1] = 255;
00329 buf_depth[4 * i + 2] = lb;
00330 break;
00331 case 4:
00332 buf_depth[4 * i + 0] = 0;
00333 buf_depth[4 * i + 1] = 255-lb;
00334 buf_depth[4 * i + 2] = 255;
00335 break;
00336 case 5:
00337 buf_depth[4 * i + 0] = 0;
00338 buf_depth[4 * i + 1] = 0;
00339 buf_depth[4 * i + 2] = 255-lb;
00340 break;
00341 default:
00342 buf_depth[4 * i + 0] = 0;
00343 buf_depth[4 * i + 1] = 0;
00344 buf_depth[4 * i + 2] = 0;
00345 break;
00346 }
00347 buf_depth[4 * i + 3] = 0x00;
00348 }
00349 got_frames++;
00350
00351 if ( depth_connected == 1 )
00352 {
00353 n = write(depth_child, buf_depth, AS3_BITMAPDATA_LEN);
00354 if ( n < 0 || n != AS3_BITMAPDATA_LEN)
00355 {
00356 fprintf(stderr, "Error on write() for depth (%d instead of %d)\n",n, AS3_BITMAPDATA_LEN);
00357
00358 }
00359 }
00360 pthread_cond_signal(&depth_cond);
00361 pthread_mutex_unlock(&depth_mutex);
00362 }
00363
00364 void rgb_cb(freenect_device *dev, freenect_pixel *rgb, uint32_t timestamp)
00365 {
00366 int n;
00367 pthread_mutex_lock(&depth_mutex);
00368 got_frames++;
00369
00370
00371
00372
00373 int x;
00374 for (x=0; x<640 * 480; x++) {
00375 buf_rgb[4 * x + 0] = rgb[3 * x + 0];
00376 buf_rgb[4 * x + 1] = rgb[3 * x + 1];
00377 buf_rgb[4 * x + 2] = rgb[3 * x + 2];
00378 buf_rgb[4 * x + 3] = 0x00;
00379 }
00380 printf("rgb received\n ");
00381 if ( rgb_connected == 1 )
00382 {
00383 n = write(rgb_child, buf_rgb, AS3_BITMAPDATA_LEN);
00384 if ( n < 0 || n != AS3_BITMAPDATA_LEN)
00385 {
00386 fprintf(stderr, "Error on write() for rgb (%d instead of %d)\n",n, AS3_BITMAPDATA_LEN);
00387
00388 }
00389 }
00390 pthread_cond_signal(&depth_cond);
00391 pthread_mutex_unlock(&depth_mutex);
00392 }
00393
00394 int main(int argc, char **argv)
00395 {
00396 int i;
00397 for (i=0; i<2048; i++) {
00398 float v = i/2048.0;
00399 v = powf(v, 3)* 6;
00400 t_gamma[i] = v*6*256;
00401 }
00402
00403 g_argc = argc;
00404 g_argv = argv;
00405
00406 if (freenect_init(&f_ctx, NULL) < 0) {
00407 printf("freenect_init() failed\n");
00408 return 1;
00409 }
00410
00411 freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
00412
00413 int nr_devices = freenect_num_devices (f_ctx);
00414 printf ("Number of devices found: %d\n", nr_devices);
00415
00416 int user_device_number = 0;
00417 if (argc > 1)
00418 user_device_number = atoi(argv[1]);
00419
00420 if (nr_devices < 1)
00421 return 1;
00422
00423 if (freenect_open_device(f_ctx, &f_dev, user_device_number) < 0) {
00424 printf("Could not open device\n");
00425 return 1;
00426 }
00427
00428 if ( network_init() < 0 )
00429 return -1;
00430
00431 freenect_set_depth_callback(f_dev, depth_cb);
00432 freenect_set_rgb_callback(f_dev, rgb_cb);
00433 freenect_set_rgb_format(f_dev, FREENECT_FORMAT_RGB);
00434 freenect_set_depth_format(f_dev, FREENECT_FORMAT_11_BIT);
00435
00436
00437
00438
00439 while(!die && freenect_process_events(f_ctx) >= 0 ){
00440 char buffer[6];
00441 int n = read(data_child, buffer, 1024);
00442
00443 if(n == 6){
00444 if (buffer[0] == 1) {
00445 if (buffer[1] == 1) {
00446 int angle;
00447 memcpy(&angle, &buffer[2], sizeof(int));
00448 freenect_set_tilt_degs(f_dev,ntohl(angle));
00449 }
00450 }
00451 }
00452 }
00453
00454 network_close();
00455
00456 printf("-- done!\n");
00457
00458 pthread_exit(NULL);
00459 }