RenHao

Project_v2

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