root / trunk / code / projects / colonet / server / PositionMonitor.cpp @ 936
History | View | Annotate | Download (6.32 KB)
1 |
/**
|
---|---|
2 |
* @file PositionMonitor.cpp
|
3 |
*
|
4 |
* @author Jason Knichel
|
5 |
*
|
6 |
* @date 2/4/08
|
7 |
*/
|
8 |
|
9 |
#include <assert.h> |
10 |
#include <map> |
11 |
#include <PositionMonitor.h> |
12 |
#include <stdio.h> |
13 |
#include <stdlib.h> |
14 |
#include <vision.h> |
15 |
|
16 |
using namespace std; |
17 |
|
18 |
#define MAX_POSITIONS 20 |
19 |
#define SAME_ROBOT_DISTANCE_THRESHOLD 15 |
20 |
#define ROBOT_DELETE_BUFFER 10 |
21 |
#define CAM_UPDATE_PERIOD 100000 |
22 |
|
23 |
/**
|
24 |
* @brief Constructor for the PositionMonitor class
|
25 |
*/
|
26 |
PositionMonitor::PositionMonitor() { |
27 |
//TODO: don't hardcode this file name
|
28 |
//TODO: check for error returned from init
|
29 |
vision_init(/*"/var/www/colonet.jpg"*/);
|
30 |
newIdToAssign = -1;
|
31 |
|
32 |
pthread_mutex_init(&position_map_lock, NULL);
|
33 |
} |
34 |
|
35 |
/**
|
36 |
* @brief Destructor for the PositionMonitor class
|
37 |
*/
|
38 |
PositionMonitor::~PositionMonitor() { |
39 |
vision_close(); |
40 |
} |
41 |
|
42 |
/**
|
43 |
* @brief This function updates the positions of things in the image
|
44 |
*
|
45 |
* @return Void
|
46 |
*/
|
47 |
void PositionMonitor::run() {
|
48 |
while (1) { |
49 |
if (updatePositions() == -1) { |
50 |
fprintf(stderr, "updatePositions failed.\n");
|
51 |
} |
52 |
|
53 |
usleep(CAM_UPDATE_PERIOD); |
54 |
} |
55 |
} |
56 |
|
57 |
/**
|
58 |
* @brief This function asks the vision code for the position of things it identifies
|
59 |
*/
|
60 |
int PositionMonitor::updatePositions() {
|
61 |
VisionPosition* positions = NULL;
|
62 |
int numPositions = vision_get_robot_positions(&positions);
|
63 |
|
64 |
//debug output code
|
65 |
/*
|
66 |
printf("\nnumPositions is %d\n", numPositions);
|
67 |
for (int i = 0; i < numPositions; i++) {
|
68 |
printf("{%d,%d} ", positions[i].x, positions[i].y);
|
69 |
}
|
70 |
printf("\n");
|
71 |
*/
|
72 |
|
73 |
if (numPositions == -1) { |
74 |
fprintf(stderr, "%s: vision_get_robot_positions failed.\n", __FUNCTION__);
|
75 |
return -1; |
76 |
} else {
|
77 |
map<int, VisionPosition> newPositionMap;
|
78 |
|
79 |
pthread_mutex_lock(&position_map_lock); |
80 |
|
81 |
//go through each of the positions the vision system identified
|
82 |
for (int i = 0; i < numPositions; i++) { |
83 |
VisionPosition newPos = positions[i]; |
84 |
map<int, VisionPosition>::iterator iter;
|
85 |
|
86 |
//go through all the positions we currently have in the position map
|
87 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
88 |
VisionPosition oldPos = iter->second; |
89 |
|
90 |
//figure out if the new point is probably the same robot as one of the old positions
|
91 |
if (isProbablySameRobot(newPos, oldPos)) {
|
92 |
//DEBUG
|
93 |
//printf("probably same robot: new = (%d, %d), old = (%d, %d)\n", newPos.x, newPos.y, oldPos.x, oldPos.y);
|
94 |
|
95 |
//if the new positions is the same robot as an old one, insert this new position into the new map
|
96 |
newPositionMap.insert(make_pair(iter->first, newPos)); |
97 |
|
98 |
//erase this robot from the map that is used to smooth over several frames in case of problems identifying robots
|
99 |
deleteBufferMap.erase(iter->first); |
100 |
//insert this robot back into the map in order to refresh its delete buffer
|
101 |
deleteBufferMap.insert(make_pair(iter->first, ROBOT_DELETE_BUFFER)); |
102 |
break;
|
103 |
} |
104 |
} |
105 |
|
106 |
if (iter == positionMap.end()) {
|
107 |
//a position was found that probably isn't a known
|
108 |
// robot so add it in case a new robot entered the field
|
109 |
printf("Inserting new robot: %d (%d,%d)\n", newIdToAssign, newPos.x, newPos.y);
|
110 |
|
111 |
//a position was found that probably isn't a known robot so add it in case a new robot entered the field
|
112 |
newPositionMap.insert(make_pair(newIdToAssign, newPos)); |
113 |
deleteBufferMap.insert(make_pair(newIdToAssign, ROBOT_DELETE_BUFFER)); |
114 |
newIdToAssign--; |
115 |
} |
116 |
} |
117 |
|
118 |
|
119 |
map<int, VisionPosition>::iterator iter2;
|
120 |
for (iter2 = positionMap.begin(); iter2 != positionMap.end(); iter2++) {
|
121 |
int currId = iter2->first;
|
122 |
map<int, VisionPosition>::iterator checkContains = newPositionMap.find(currId);
|
123 |
if (checkContains == newPositionMap.end()) {
|
124 |
int bufferValue = deleteBufferMap[iter2->first];
|
125 |
bufferValue--; |
126 |
if (bufferValue > 0) { |
127 |
newPositionMap.insert(make_pair(currId, iter2->second)); |
128 |
deleteBufferMap.erase(currId); |
129 |
deleteBufferMap.insert(make_pair(currId, bufferValue)); |
130 |
} else {
|
131 |
deleteBufferMap.erase(currId); |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
positionMap = newPositionMap; |
137 |
|
138 |
/*
|
139 |
//DEBUG PRINT STATEMENTS
|
140 |
printf("positionMap size is %d and deleteBufferMap size is %d\n", positionMap.size(), deleteBufferMap.size());
|
141 |
map<int, VisionPosition>::iterator iter;
|
142 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
143 |
printf("%d has position (%d, %d) with delete buffer %d\n", iter->first, iter->second.x, iter->second.y, deleteBufferMap[iter->first]);
|
144 |
}
|
145 |
*/
|
146 |
|
147 |
|
148 |
pthread_mutex_unlock(&position_map_lock); |
149 |
|
150 |
assert(positions != NULL);
|
151 |
free(positions); |
152 |
|
153 |
return 0; |
154 |
} |
155 |
} |
156 |
|
157 |
int PositionMonitor::assignRealId(int old_id, int real_id) { |
158 |
printf("assigning real_id %d to old_id %d\n", real_id, old_id);
|
159 |
|
160 |
pthread_mutex_lock(&position_map_lock); |
161 |
|
162 |
map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
|
163 |
|
164 |
if (iter == positionMap.end()) {
|
165 |
fprintf(stderr, "assignRealId: old_id not found\n");
|
166 |
pthread_mutex_unlock(&position_map_lock); |
167 |
return -1; |
168 |
} |
169 |
|
170 |
positionMap.insert(make_pair(real_id, iter->second)); |
171 |
positionMap.erase(old_id); |
172 |
int oldDeleteBuffer = deleteBufferMap[old_id];
|
173 |
deleteBufferMap.insert(make_pair(real_id, oldDeleteBuffer)); |
174 |
deleteBufferMap.erase(old_id); |
175 |
|
176 |
pthread_mutex_unlock(&position_map_lock); |
177 |
|
178 |
return 0; |
179 |
} |
180 |
|
181 |
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
|
182 |
// TODO return a copy instead of the actual map for synch purposes
|
183 |
return positionMap;
|
184 |
} |
185 |
|
186 |
int PositionMonitor::getNumVisibleRobots() {
|
187 |
return positionMap.size();
|
188 |
} |
189 |
|
190 |
VisionPosition PositionMonitor::getFirstPosition(void) {
|
191 |
return positionMap.begin()->second;
|
192 |
} |
193 |
|
194 |
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) { |
195 |
//TODO: figure out what a map returns if the element doesn't exist
|
196 |
|
197 |
pthread_mutex_lock(&position_map_lock); |
198 |
|
199 |
if (positionMap.find(robot_id) == positionMap.end()){
|
200 |
pthread_mutex_unlock(&position_map_lock); |
201 |
return -1; |
202 |
} else {
|
203 |
VisionPosition pos = positionMap[robot_id]; |
204 |
|
205 |
pthread_mutex_unlock(&position_map_lock); |
206 |
|
207 |
*xbuf = pos.x; |
208 |
*ybuf = pos.y; |
209 |
|
210 |
return 0; |
211 |
} |
212 |
} |
213 |
|
214 |
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
|
215 |
int xDiff = p1.x - p2.x;
|
216 |
int yDiff = p1.y - p2.y;
|
217 |
return (xDiff*xDiff + yDiff*yDiff < SAME_ROBOT_DISTANCE_THRESHOLD*SAME_ROBOT_DISTANCE_THRESHOLD);
|
218 |
} |