root / trunk / code / projects / colonet / ColonetServer / PositionMonitor.cpp @ 466
History | View | Annotate | Download (3.42 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 |
|
27 |
PositionMonitor::~PositionMonitor() { |
28 |
} |
29 |
|
30 |
int PositionMonitor::startMonitoring() {
|
31 |
//TODO: fill this in when this becomes asynchronous
|
32 |
return 0; |
33 |
} |
34 |
|
35 |
int PositionMonitor::stopMonitoring() {
|
36 |
//TODO: fill this in when this becomes asynchronous
|
37 |
return 0; |
38 |
} |
39 |
|
40 |
int PositionMonitor::updatePositions() {
|
41 |
VisionPosition * positions = NULL;
|
42 |
|
43 |
//TODO: check for error returned
|
44 |
int numPositions = vision_get_robot_positions(&positions);
|
45 |
printf("numPositions is %d\n", numPositions);
|
46 |
|
47 |
/*
|
48 |
//TODO: compare current set of positions to previous set of
|
49 |
// positions to match up who is who
|
50 |
positionMap.clear();
|
51 |
|
52 |
int i;
|
53 |
for (i = 0; i < numPositions; i++) {
|
54 |
//TODO: would this insert by copy or just point to the memory in positions[i]?
|
55 |
// If this just points to the memory in positions[i] then we have a bug
|
56 |
positionMap[i] = positions[i];
|
57 |
}
|
58 |
*/
|
59 |
|
60 |
//TODO: also remove robots that might have disappeared
|
61 |
int i;
|
62 |
for (i = 0; i < numPositions; i++) { |
63 |
VisionPosition newPos = positions[i]; |
64 |
map<int, VisionPosition>::iterator iter;
|
65 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
66 |
VisionPosition oldPos = iter->second; |
67 |
|
68 |
if (isProbablySameRobot(newPos, oldPos)) {
|
69 |
//TODO: is this the right use of an iterator?
|
70 |
iter->second = newPos; |
71 |
break;
|
72 |
} |
73 |
} |
74 |
|
75 |
if (iter == positionMap.end()) {
|
76 |
printf("Inserting new robot: %d (%d,%d)", newIdToAssign, newPos.x, newPos.y);
|
77 |
|
78 |
//a position was found that probably isn't a known robot so add it in case a new robot entered the field
|
79 |
positionMap.insert(make_pair(newIdToAssign, newPos)); |
80 |
newIdToAssign--; |
81 |
} |
82 |
} |
83 |
|
84 |
//TODO: remove this debug information
|
85 |
map<int, VisionPosition>::iterator iter;
|
86 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
87 |
printf("%d has position (%d, %d)\n", iter->first, iter->second.x, iter->second.y);
|
88 |
} |
89 |
|
90 |
if (positions)
|
91 |
free(positions); |
92 |
|
93 |
return 0; |
94 |
} |
95 |
|
96 |
int PositionMonitor::assignRealId(int old_id, int real_id) { |
97 |
printf("assigning real_id %d to old_id %d\n", real_id, old_id);
|
98 |
|
99 |
map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
|
100 |
|
101 |
if (iter == positionMap.end()) {
|
102 |
fprintf(stderr, "assignRealId: old_id not found\n");
|
103 |
return -1; |
104 |
} |
105 |
|
106 |
positionMap.insert(make_pair(real_id, iter->second)); |
107 |
positionMap.erase(old_id); |
108 |
|
109 |
return 0; |
110 |
} |
111 |
|
112 |
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
|
113 |
//TODO do this in another thread!
|
114 |
updatePositions(); |
115 |
|
116 |
return positionMap;
|
117 |
} |
118 |
|
119 |
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) { |
120 |
//TODO: figure out what a map returns if the element doesn't exist
|
121 |
if (positionMap.find(robot_id) == positionMap.end()){
|
122 |
return -1; |
123 |
} |
124 |
|
125 |
VisionPosition pos = positionMap[robot_id]; |
126 |
|
127 |
*xbuf = pos.x; |
128 |
*ybuf = pos.y; |
129 |
|
130 |
return 0; |
131 |
} |
132 |
|
133 |
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
|
134 |
int xDiff = p1.x - p2.x;
|
135 |
int yDiff = p1.y - p2.y;
|
136 |
return (xDiff*xDiff + yDiff*yDiff < MAX_DISTANCE*MAX_DISTANCE);
|
137 |
} |