Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- #include <OpenNI.h>
- #include "OniSampleUtilities.h"
- //#include <winsock2.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h> //inet_addr
- #define PORT 6122
- #define BUF_SIZE 1024
- #define SAMPLE_READ_WAIT_TIMEOUT 2000 //2000ms
- int traffic(int s)
- {
- if( s == 0)
- {
- printf("Now is : Green\n");
- return 1;
- }
- else if( s == 1)
- {
- printf("Now is : Red\n");
- return 0;
- }
- else
- {
- printf("There is something wrong in signal\n");
- return -1;
- }
- }
- using namespace openni;
- int main()
- {
- //Setting socket variable
- int sockfd;
- int temp;
- char buf[BUF_SIZE];
- char IP[100];
- struct sockaddr_in addr;
- int i;
- char buf2[BUF_SIZE]={0};
- char ibuf[BUF_SIZE]={0};
- memset(buf, 0, sizeof(buf));
- // Initialize
- Status rc = OpenNI::initialize();
- if (rc != STATUS_OK)
- {
- printf("Initialize failed\n%s\n", OpenNI::getExtendedError());
- return 1;
- }
- // Open a device
- Device device;
- rc = device.open(ANY_DEVICE);
- if (rc != STATUS_OK)
- {
- printf("Couldn't open device\n%s\n", OpenNI::getExtendedError());
- return 2;
- }
- // Create depth stream
- VideoStream depth;
- if (device.getSensorInfo(SENSOR_DEPTH) != NULL)
- {
- rc = depth.create(device, SENSOR_DEPTH);
- if (rc != STATUS_OK)
- {
- printf("Couldn't create depth stream\n%s\n", OpenNI::getExtendedError());
- return 3;
- }
- }
- rc = depth.start();
- if (rc != STATUS_OK)
- {
- printf("Couldn't start the depth stream\n%s\n", OpenNI::getExtendedError());
- return 4;
- }
- // Main loop , continue read
- VideoFrameRef frame;
- int Counter_D = 0; //count 30 times nearly close 1 s
- int Counter_nD = 0;
- while(!wasKeyboardHit()) //using flag to control working times
- {
- /* create socket */
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1) {
- perror("socket error");
- system("Pause");
- exit(1);
- }
- /* initialize structure */
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr("192.168.0.4");
- addr.sin_port = htons(PORT);
- /* Connecting to server */
- printf("Connect server...\n");
- if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
- perror("connect error");
- system("Pause");
- exit(1);
- }
- while (1) //stop while ketboaard is hit
- {
- int changedStreamDummy;
- VideoStream* pStream = &depth;
- rc = OpenNI::waitForAnyStream(&pStream, 1, &changedStreamDummy, SAMPLE_READ_WAIT_TIMEOUT);
- if (rc != STATUS_OK)
- {
- printf("Wait failed! (timeout is %d ms)\n%s\n", SAMPLE_READ_WAIT_TIMEOUT, OpenNI::getExtendedError());
- continue;
- }
- rc = depth.readFrame(&frame);
- if (rc != STATUS_OK)
- {
- printf("Read failed!\n%s\n", OpenNI::getExtendedError());
- continue;
- }
- if (frame.getVideoMode().getPixelFormat() != PIXEL_FORMAT_DEPTH_1_MM && frame.getVideoMode().getPixelFormat() != PIXEL_FORMAT_DEPTH_100_UM)
- {
- printf("Unexpected frame format\n");
- continue;
- }
- DepthPixel* pDepth = (DepthPixel*)frame.getData();
- int middleIndex = (frame.getHeight()+1)*frame.getWidth()/2;
- //printf("Index = %d Height = %d Width = %d\n",middleIndex,frame.getHeight(),frame.getWidth());
- printf("[%08llu] %8d\n", (long long)frame.getTimestamp(), pDepth[middleIndex]);
- //return us
- if( pDepth[middleIndex] < 800 )
- {
- if(Counter_D > 90)
- {
- Counter_D = 0;
- Counter_nD = 0;
- sprintf(buf2,"%d",1);
- printf("There is a car or something\n");
- break;
- }
- Counter_D++;
- }
- else
- {
- Counter_nD++;
- if(Counter_nD > 30)
- {
- Counter_D = 0;
- Counter_nD = 0;
- sprintf(buf2,"%d",0);
- printf("\tThere has no car more than 1 seconds \n");
- break;
- }
- }
- }
- /* Send data to server */
- if(send(sockfd, buf2, strlen(buf2), 0) == -1) {
- perror("send error");
- system("Pause");
- exit(1);
- }
- /* Receive data from server */
- if(recv(sockfd, buf2, sizeof(buf2), 0) == -1) {
- perror("Error in receiving");
- system("Pause");
- exit(1);
- }
- printf("Response from server: %s\n", buf2);
- int temp = atoi(buf2);
- traffic(temp);
- /* Close connection */
- close(sockfd);
- //flag++;
- //Delay(3);
- }
- printf("Socket is closed...\n");
- depth.stop();
- // Close
- depth.destroy();
- device.close();
- // Shutdown
- OpenNI::shutdown();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment