-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoprocessorVision.cpp
112 lines (99 loc) · 2.65 KB
/
CoprocessorVision.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "CoprocessorVision.h"
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolvLib.h>
#include <arpa/inet.h>
#include <sockLib.h>
#include <inetLib.h>
#include <hostLib.h>
#include <ioLib.h>
#include <string>
#include <algorithm>
#include <sstream>
#define BUFFERSIZE 1024
#define MY_PORT 9999
#include <ctype.h>
CoprocessorVision::CoprocessorVision(){}
CoprocessorVision * CoprocessorVision::instance = 0;
CoprocessorVision * CoprocessorVision::getInstance()
{
return instance;
}
void alltoupper(std::string s)
{
std::string out;
out.resize( s.size() );
std::transform( s.begin(), s.end(), out.begin(), (int (*)(int)) std::toupper );
}
int CoprocessorVision::run()
{
char buffer[BUFFERSIZE];
struct sockaddr_in clientaddr;
struct sockaddr_in addr;
int sd, bytes_read;
int addr_size;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if ( sd == ERROR )
{
perror("socket");
return -1;
}
addr_size = sizeof (sockaddr_in);
bzero ((char*) &addr, sizeof (addr));
bzero ((char*) &clientaddr, sizeof (clientaddr));
addr.sin_family = AF_INET;
addr.sin_len = sizeof (struct sockaddr_in);
addr.sin_port = htons(500);
addr.sin_addr.s_addr = htonl (INADDR_ANY);
if ( bind(sd, (struct sockaddr*)&addr, addr_size) == ERROR )
{
perror("bind");
return -1;
}
do
{
bzero(buffer, BUFFERSIZE);
// printf("sd = %d, BUFFERSIZE = %d", sd, BUFFERSIZE);
bytes_read = recvfrom(sd, buffer, BUFFERSIZE, 0, (struct sockaddr*)&clientaddr, &addr_size);
if ( bytes_read > 0 )
{
float distance, angle, tension, anglePerp;
int rCounter;
rCounter = sscanf(buffer, "Rect Distance=%f:Angle=%f:Tension=%f", &distance, &angle, &tension);
if(rCounter == 3)
{
SmartDashboard::PutNumber("Distance", distance);
SmartDashboard::PutNumber("Angle", angle);
SmartDashboard::PutNumber("Tension", tension);
SmartDashboard::PutNumber("GyroAngle", Robot::drive->getGyroAngle());
}
else
{
rCounter = sscanf(buffer, "Pole Distance=%f:Angle=%f:AnglePerp=%f", &distance, &angle, &anglePerp);
if(rCounter == 3)
{
SmartDashboard::PutNumber("Pole Distance", distance);
SmartDashboard::PutNumber("Pole Angle", angle);
SmartDashboard::PutNumber("Pole AnglePerp", anglePerp);
SmartDashboard::PutNumber("GyroAngle", Robot::drive->getGyroAngle());
}
}
std::stringstream ss;
ss << buffer;
alltoupper( ss.str() );
if ( sendto(sd, buffer, bytes_read, 0, (struct sockaddr*)&clientaddr, addr_size) == ERROR )
perror("reply");
}
else
{
printf ("Error: %d \n", errno);
perror("recvfrom");
}
}
while ( bytes_read > 0 );
close(sd);
return 0;
}