RenHao

SimpleRead_Client

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