Advertisement
j0h

NavKeysKinect

j0h
Jul 11th, 2023
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. /*
  2. * This program sends characters to the Arduino which is listening to the serial port for
  3. * Character input to control motor functions. Depending on depth detected, start or stop the
  4. * motors. For use in Robot navigation using the Xbox360 kinect
  5. */
  6. #include <fcntl.h>
  7. #include <libfreenect.h>
  8. #include <libfreenect_sync.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <termios.h>
  13. #include <unistd.h>
  14. //gcc -Wall -g -o "%e" "%f"  -lfreenect -lfreenect_sync  
  15.  int dtime=5;
  16.  int arduino;
  17.  
  18.  
  19. //int current_tilt=&current_tilt);
  20. freenect_context *f_ctx;
  21. freenect_device *f_dev;
  22.  
  23. #define WIDTH 640
  24. #define HEIGHT 480
  25. //(0,0) is top left also index 0
  26.      char w='w';
  27.      char a='a';
  28.      char s='s';
  29.      char d='d';
  30.      char b='b';
  31. int die = 0;
  32. int kInit();
  33. void kClose();
  34. void go(char x);
  35. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp);
  36. void no_kinect_quit(void);
  37. int main(){
  38.     kInit();  //init kinect lib, and device
  39.     arduino = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
  40.     if (arduino == -1) {
  41.         perror("Is the Arduino Connected?");
  42.         return 1;
  43.     }
  44.     struct termios options;
  45.     tcgetattr(arduino, &options);
  46.     cfsetispeed(&options, B9600);
  47.     cfsetospeed(&options, B9600);
  48.     options.c_cflag |= (CLOCAL | CREAD);
  49.     tcsetattr(arduino, TCSANOW, &options);
  50.  
  51.     // Set the depth callback function
  52.     freenect_set_depth_callback(f_dev, depth_cb);
  53.     // Start the depth stream
  54.     freenect_start_depth(f_dev);
  55.      while (!die)    {
  56.         // Update the Kinect device
  57.         if (freenect_process_events(f_ctx) < 0)
  58.             break;
  59.     }
  60.  
  61. kClose();
  62.  
  63.     return 0;
  64. }
  65.  
  66. void go(char x){
  67.      char keystrokes[] = "b";
  68.       keystrokes[0]=x;
  69.   //  printf("%c\n",x);
  70.     write(arduino, keystrokes, strlen(keystrokes));
  71.   //  sleep(dtime);
  72.     }
  73.  
  74. //Kinect Stuff
  75. int kInit(){
  76.         // Initialize libfreenect
  77.     if (freenect_init(&f_ctx, NULL) < 0)    {
  78.         printf("Failed to initialize libfreenect!\n");
  79.         return 1;
  80.     }
  81.  
  82.     // Set the log level (optional)
  83.   //  freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
  84.  
  85.     // Open the Kinect device
  86.     if (freenect_open_device(f_ctx, &f_dev, 0) < 0)    {
  87.         printf("Failed to open Kinect device!\n");
  88.         return 1;
  89.     }
  90.     return 0;
  91.     }
  92.    
  93. void kClose(){
  94.         //shutdown functions
  95.     freenect_stop_depth(f_dev);
  96.     freenect_close_device(f_dev);
  97.     freenect_shutdown(f_ctx);
  98.         }
  99. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
  100.     // Cast the depth data to unsigned short (16-bit)
  101.     uint16_t *depth_data = (uint16_t *)depth;
  102.  
  103.     // Print the depth value at a specific pixel (e.g., pixel at (320, 240) (Center of frame))
  104.     int x = 320;
  105.     int y = 240;
  106.     int index = y * WIDTH + x;  //index 0 to 307840
  107.     uint16_t depth_value = depth_data[index];
  108. if(depth_value>=2046){
  109.     //nearing buffer overflow. this happens as object approaches camera
  110.     depth_value=2046;
  111.     }
  112.  
  113. if(depth_value>600&& depth_value<2000){
  114.     printf("%u\n", depth_value);
  115.     go(w);
  116.     }
  117.     if(depth_value>2000){
  118.     printf("%u\n", depth_value);
  119.     go(b);
  120.     }
  121.  
  122.     // You can further process the depth data or perform other actions here
  123. }
  124. void no_kinect_quit(void){
  125.     printf("Error: Kinect not connected?\n");
  126.     exit(1);
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement