RenHao

Project_v1

May 14th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.51 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7.  
  8. #include <OpenNI.h>
  9.  
  10. #include "OniSampleUtilities.h"
  11.  
  12.  
  13. //#include <winsock2.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>  //inet_addr
  18. #define PORT 6122
  19. #define BUF_SIZE 1024
  20.  
  21. #define SAMPLE_READ_WAIT_TIMEOUT 2000 //2000ms
  22.  
  23. int traffic(int s)
  24. {
  25.     if( s == 0)
  26.     {
  27.         printf("Now is : Green\n");
  28.         return 1;
  29.     }
  30.     else if( s == 1)
  31.     {
  32.         printf("Now is : Red\n");
  33.         return 0;
  34.     }
  35.     else
  36.     {
  37.         printf("There is something wrong in signal\n");
  38.         return -1;
  39.     }
  40. }
  41.  
  42.  
  43. using namespace openni;
  44.  
  45. int main()
  46. {
  47.     //Setting socket variable
  48.     int sockfd;
  49.     int temp;
  50.     char buf[BUF_SIZE];
  51.     char IP[100];
  52.     struct sockaddr_in addr;
  53.     int i;
  54.     char buf2[BUF_SIZE]={0};
  55.     char ibuf[BUF_SIZE]={0};
  56.    
  57.     memset(buf, 0, sizeof(buf));
  58.    
  59.     // Initialize
  60.     Status rc = OpenNI::initialize();
  61.     if (rc != STATUS_OK)
  62.     {
  63.         printf("Initialize failed\n%s\n", OpenNI::getExtendedError());
  64.         return 1;
  65.     }
  66.    
  67.     // Open a device
  68.     Device device;
  69.     rc = device.open(ANY_DEVICE);
  70.     if (rc != STATUS_OK)
  71.     {
  72.         printf("Couldn't open device\n%s\n", OpenNI::getExtendedError());
  73.         return 2;
  74.     }
  75.  
  76.     // Create depth stream
  77.     VideoStream depth;
  78.  
  79.     if (device.getSensorInfo(SENSOR_DEPTH) != NULL)
  80.     {
  81.         rc = depth.create(device, SENSOR_DEPTH);
  82.         if (rc != STATUS_OK)
  83.         {
  84.             printf("Couldn't create depth stream\n%s\n", OpenNI::getExtendedError());
  85.             return 3;
  86.         }
  87.     }
  88.    
  89.     rc = depth.start();
  90.     if (rc != STATUS_OK)
  91.     {
  92.         printf("Couldn't start the depth stream\n%s\n", OpenNI::getExtendedError());
  93.         return 4;
  94.     }
  95.    
  96.     // Main loop , continue read
  97.     VideoFrameRef frame;
  98.     int Counter_D = 0;  //count 30 times nearly close 1 s
  99.     int Counter_nD = 0;
  100.     while(!wasKeyboardHit()) //using flag to control working times
  101.     {
  102.         /* create socket  */
  103.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  104.         if(sockfd == -1) {
  105.             perror("socket error");
  106.             system("Pause");
  107.             exit(1);
  108.         }
  109.        
  110.         /* initialize structure */
  111.         memset(&addr, 0, sizeof(addr));
  112.         addr.sin_family = AF_INET;
  113.         addr.sin_addr.s_addr = inet_addr("192.168.0.4");
  114.         addr.sin_port = htons(PORT);
  115.        
  116.         /* Connecting to server */
  117.         printf("Connect server...\n");
  118.         if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
  119.             perror("connect error");
  120.             system("Pause");
  121.             exit(1);
  122.         }
  123.        
  124.         while (1) //stop while ketboaard is hit
  125.         {
  126.             int changedStreamDummy;
  127.             VideoStream* pStream = &depth;
  128.             rc = OpenNI::waitForAnyStream(&pStream, 1, &changedStreamDummy, SAMPLE_READ_WAIT_TIMEOUT);
  129.             if (rc != STATUS_OK)
  130.             {
  131.                 printf("Wait failed! (timeout is %d ms)\n%s\n", SAMPLE_READ_WAIT_TIMEOUT, OpenNI::getExtendedError());
  132.                 continue;
  133.             }
  134.            
  135.             rc = depth.readFrame(&frame);
  136.             if (rc != STATUS_OK)
  137.             {
  138.                 printf("Read failed!\n%s\n", OpenNI::getExtendedError());
  139.                 continue;
  140.             }
  141.            
  142.             if (frame.getVideoMode().getPixelFormat() != PIXEL_FORMAT_DEPTH_1_MM && frame.getVideoMode().getPixelFormat() != PIXEL_FORMAT_DEPTH_100_UM)
  143.             {
  144.                 printf("Unexpected frame format\n");
  145.                 continue;
  146.             }
  147.            
  148.             DepthPixel* pDepth = (DepthPixel*)frame.getData();
  149.            
  150.             int middleIndex = (frame.getHeight()+1)*frame.getWidth()/2;
  151.             //printf("Index = %d Height = %d Width = %d\n",middleIndex,frame.getHeight(),frame.getWidth());
  152.             printf("[%08llu] %8d\n", (long long)frame.getTimestamp(), pDepth[middleIndex]);
  153.             //return us
  154.            
  155.             if( pDepth[middleIndex] < 800 )
  156.             {
  157.                 if(Counter_D > 90)
  158.                 {
  159.                     Counter_D = 0;
  160.                     Counter_nD = 0;
  161.             sprintf(buf2,"%d",1);
  162.                     printf("There is a car or something\n");
  163.                     break;
  164.                 }
  165.                 Counter_D++;
  166.             }
  167.             else
  168.             {
  169.                 Counter_nD++;
  170.                 if(Counter_nD > 30)
  171.                 {
  172.                     Counter_D = 0;
  173.                     Counter_nD = 0;
  174.             sprintf(buf2,"%d",0);
  175.                     printf("\tThere has no car more than 1 seconds \n");
  176.                     break;
  177.         }
  178.             }
  179.         }
  180.        
  181.         /* Send data to server */
  182.         if(send(sockfd, buf2, strlen(buf2), 0) == -1) {
  183.             perror("send error");
  184.             system("Pause");
  185.             exit(1);
  186.         }
  187.        
  188.        
  189.         /* Receive data from server */
  190.        
  191.         if(recv(sockfd, buf2, sizeof(buf2), 0) == -1) {
  192.             perror("Error in receiving");
  193.             system("Pause");
  194.             exit(1);
  195.         }
  196.         printf("Response from server: %s\n", buf2);
  197.         int temp = atoi(buf2);
  198.         traffic(temp);
  199.        
  200.         /* Close connection */
  201.         close(sockfd);
  202.        
  203.         //flag++;
  204.         //Delay(3);
  205.     }
  206.    
  207.     printf("Socket is closed...\n");
  208.  
  209.     depth.stop();
  210.    
  211.     // Close
  212.     depth.destroy();
  213.     device.close();
  214.    
  215.     // Shutdown
  216.     OpenNI::shutdown();
  217.  
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment