root / trunk / code / projects / colonet / server / PositionMonitor.cpp @ 534
History | View | Annotate | Download (4.89 KB)
1 |
/**
|
---|---|
2 |
* @file PositionMonitor.cpp
|
3 |
*
|
4 |
* @author Jason Knichel
|
5 |
*
|
6 |
* @date 2/4/08
|
7 |
*/
|
8 |
|
9 |
//TODO: make this file asynchronous
|
10 |
|
11 |
#include <PositionMonitor.h> |
12 |
#include <stdlib.h> |
13 |
#include <vision.h> |
14 |
|
15 |
#include <stdio.h> |
16 |
|
17 |
#include <map> |
18 |
using namespace std; |
19 |
|
20 |
PositionMonitor::PositionMonitor() { |
21 |
//TODO: don't hardcode this file name
|
22 |
//TODO: check for error returned from init
|
23 |
vision_init("/var/www/colonet.jpg");
|
24 |
newIdToAssign = -1;
|
25 |
|
26 |
pthread_mutex_init(&position_map_lock, NULL);
|
27 |
} |
28 |
|
29 |
PositionMonitor::~PositionMonitor() { |
30 |
} |
31 |
|
32 |
void PositionMonitor::run() {
|
33 |
while (1) { |
34 |
updatePositions(); |
35 |
usleep(500000);
|
36 |
} |
37 |
} |
38 |
|
39 |
int PositionMonitor::updatePositions() {
|
40 |
VisionPosition * positions = NULL;
|
41 |
|
42 |
//TODO: check for error returned
|
43 |
int numPositions = vision_get_robot_positions(&positions);
|
44 |
printf("numPositions is %d\n", numPositions);
|
45 |
for (int i = 0; i < numPositions; i++) { |
46 |
printf("{%d,%d} ", positions[i].x, positions[i].y);
|
47 |
} |
48 |
printf("\n");
|
49 |
|
50 |
map<int, VisionPosition> newPositionMap;
|
51 |
|
52 |
pthread_mutex_lock(&position_map_lock); |
53 |
|
54 |
//TODO: also remove robots that might have disappeared
|
55 |
int i;
|
56 |
for (i = 0; i < numPositions; i++) { |
57 |
VisionPosition newPos = positions[i]; |
58 |
map<int, VisionPosition>::iterator iter;
|
59 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
60 |
VisionPosition oldPos = iter->second; |
61 |
|
62 |
if (isProbablySameRobot(newPos, oldPos)) {
|
63 |
//TODO: is this the right use of an iterator?
|
64 |
newPositionMap.insert(make_pair(iter->first, newPos)); |
65 |
deleteBufferMap.insert(make_pair(iter->first, ROBOT_DELETE_BUFFER)); |
66 |
break;
|
67 |
} |
68 |
} |
69 |
|
70 |
if (iter == positionMap.end()) {
|
71 |
//a position was found that probably isn't a known
|
72 |
// robot so add it in case a new robot entered the field
|
73 |
printf("Inserting new robot: %d (%d,%d)", newIdToAssign, newPos.x, newPos.y);
|
74 |
|
75 |
//a position was found that probably isn't a known robot so add it in case a new robot entered the field
|
76 |
newPositionMap.insert(make_pair(newIdToAssign, newPos)); |
77 |
deleteBufferMap.insert(make_pair(newIdToAssign, ROBOT_DELETE_BUFFER)); |
78 |
newIdToAssign--; |
79 |
} |
80 |
} |
81 |
|
82 |
map<int, VisionPosition>::iterator iter2;
|
83 |
for (iter2 = positionMap.begin(); iter2 != positionMap.end(); iter2++) {
|
84 |
int currId = iter2->first;
|
85 |
map<int, VisionPosition>::iterator checkContains = newPositionMap.find(currId);
|
86 |
if (checkContains == newPositionMap.end()) {
|
87 |
int bufferValue = deleteBufferMap[iter2->first];
|
88 |
bufferValue--; |
89 |
if (bufferValue > 0) { |
90 |
newPositionMap.insert(make_pair(currId, iter2->second)); |
91 |
deleteBufferMap.erase(currId); |
92 |
deleteBufferMap.insert(make_pair(currId, bufferValue)); |
93 |
} else {
|
94 |
deleteBufferMap.erase(currId); |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
positionMap = newPositionMap; |
100 |
|
101 |
printf("\npositionMap size is %d and deleteBufferMap size is %d\n", positionMap.size(), deleteBufferMap.size());
|
102 |
|
103 |
//TODO: remove this debug information
|
104 |
map<int, VisionPosition>::iterator iter;
|
105 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
106 |
printf("%d has position (%d, %d)\n", iter->first, iter->second.x, iter->second.y);
|
107 |
} |
108 |
|
109 |
pthread_mutex_unlock(&position_map_lock); |
110 |
|
111 |
if (positions) {
|
112 |
free(positions); |
113 |
} |
114 |
|
115 |
return 0; |
116 |
} |
117 |
|
118 |
int PositionMonitor::assignRealId(int old_id, int real_id) { |
119 |
printf("assigning real_id %d to old_id %d\n", real_id, old_id);
|
120 |
|
121 |
pthread_mutex_lock(&position_map_lock); |
122 |
|
123 |
map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
|
124 |
|
125 |
if (iter == positionMap.end()) {
|
126 |
fprintf(stderr, "assignRealId: old_id not found\n");
|
127 |
pthread_mutex_unlock(&position_map_lock); |
128 |
return -1; |
129 |
} |
130 |
|
131 |
positionMap.insert(make_pair(real_id, iter->second)); |
132 |
positionMap.erase(old_id); |
133 |
int oldDeleteBuffer = deleteBufferMap[old_id];
|
134 |
deleteBufferMap.insert(make_pair(real_id, oldDeleteBuffer)); |
135 |
deleteBufferMap.erase(old_id); |
136 |
|
137 |
pthread_mutex_unlock(&position_map_lock); |
138 |
|
139 |
return 0; |
140 |
} |
141 |
|
142 |
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
|
143 |
// TODO return a copy instead of the actual map for synch purposes
|
144 |
return positionMap;
|
145 |
} |
146 |
|
147 |
int PositionMonitor::getNumVisibleRobots() {
|
148 |
return positionMap.size();
|
149 |
} |
150 |
|
151 |
VisionPosition PositionMonitor::getFirstPosition(void) {
|
152 |
return positionMap.begin()->second;
|
153 |
} |
154 |
|
155 |
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) { |
156 |
//TODO: figure out what a map returns if the element doesn't exist
|
157 |
|
158 |
pthread_mutex_lock(&position_map_lock); |
159 |
|
160 |
if (positionMap.find(robot_id) == positionMap.end()){
|
161 |
pthread_mutex_unlock(&position_map_lock); |
162 |
return -1; |
163 |
} else {
|
164 |
VisionPosition pos = positionMap[robot_id]; |
165 |
|
166 |
pthread_mutex_unlock(&position_map_lock); |
167 |
|
168 |
*xbuf = pos.x; |
169 |
*ybuf = pos.y; |
170 |
|
171 |
return 0; |
172 |
} |
173 |
} |
174 |
|
175 |
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
|
176 |
int xDiff = p1.x - p2.x;
|
177 |
int yDiff = p1.y - p2.y;
|
178 |
return (xDiff*xDiff + yDiff*yDiff < SAME_ROBOT_DISTANCE_THRESHOLD*SAME_ROBOT_DISTANCE_THRESHOLD);
|
179 |
} |