root / trunk / code / projects / colonet / server / PositionMonitor.cpp @ 509
History | View | Annotate | Download (3.7 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::stopMonitoring() {
|
40 |
//TODO: fill this in when this becomes asynchronous
|
41 |
return 0; |
42 |
} |
43 |
|
44 |
int PositionMonitor::updatePositions() {
|
45 |
VisionPosition * positions = NULL;
|
46 |
|
47 |
//TODO: check for error returned
|
48 |
int numPositions = vision_get_robot_positions(&positions);
|
49 |
printf("numPositions is %d\n", numPositions);
|
50 |
for (int i = 0; i < numPositions; i++) { |
51 |
printf("{%d,%d} ", positions[i].x, positions[i].y);
|
52 |
} |
53 |
printf("\n");
|
54 |
|
55 |
map<int, VisionPosition> newPositionMap;
|
56 |
|
57 |
pthread_mutex_lock(&position_map_lock); |
58 |
|
59 |
//TODO: also remove robots that might have disappeared
|
60 |
int i;
|
61 |
for (i = 0; i < numPositions; i++) { |
62 |
VisionPosition newPos = positions[i]; |
63 |
map<int, VisionPosition>::iterator iter;
|
64 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
65 |
VisionPosition oldPos = iter->second; |
66 |
|
67 |
if (isProbablySameRobot(newPos, oldPos)) {
|
68 |
//TODO: is this the right use of an iterator?
|
69 |
newPositionMap.insert(make_pair(iter->first, newPos)); |
70 |
break;
|
71 |
} |
72 |
} |
73 |
|
74 |
if (iter == positionMap.end()) {
|
75 |
//a position was found that probably isn't a known
|
76 |
// robot so add it in case a new robot entered the field
|
77 |
printf("Inserting new robot: %d (%d,%d)", newIdToAssign, newPos.x, newPos.y);
|
78 |
|
79 |
//a position was found that probably isn't a known robot so add it in case a new robot entered the field
|
80 |
newPositionMap.insert(make_pair(newIdToAssign, newPos)); |
81 |
newIdToAssign--; |
82 |
} |
83 |
} |
84 |
|
85 |
positionMap = newPositionMap; |
86 |
|
87 |
//TODO: remove this debug information
|
88 |
map<int, VisionPosition>::iterator iter;
|
89 |
for (iter = positionMap.begin(); iter != positionMap.end(); iter++) {
|
90 |
printf("%d has position (%d, %d)\n", iter->first, iter->second.x, iter->second.y);
|
91 |
} |
92 |
|
93 |
pthread_mutex_unlock(&position_map_lock); |
94 |
|
95 |
if (positions) {
|
96 |
free(positions); |
97 |
} |
98 |
|
99 |
return 0; |
100 |
} |
101 |
|
102 |
int PositionMonitor::assignRealId(int old_id, int real_id) { |
103 |
printf("assigning real_id %d to old_id %d\n", real_id, old_id);
|
104 |
|
105 |
pthread_mutex_lock(&position_map_lock); |
106 |
|
107 |
map<int,VisionPosition>::iterator iter = positionMap.find(old_id);
|
108 |
|
109 |
if (iter == positionMap.end()) {
|
110 |
fprintf(stderr, "assignRealId: old_id not found\n");
|
111 |
return -1; |
112 |
} |
113 |
|
114 |
positionMap.insert(make_pair(real_id, iter->second)); |
115 |
positionMap.erase(old_id); |
116 |
|
117 |
pthread_mutex_unlock(&position_map_lock); |
118 |
|
119 |
return 0; |
120 |
} |
121 |
|
122 |
map<int, VisionPosition> PositionMonitor::getAllRobotPositions() {
|
123 |
// TODO return a copy instead of the actual map for synch purposes
|
124 |
return positionMap;
|
125 |
} |
126 |
|
127 |
int PositionMonitor::getRobotPosition(int robot_id, int* xbuf, int* ybuf) { |
128 |
//TODO: figure out what a map returns if the element doesn't exist
|
129 |
|
130 |
pthread_mutex_lock(&position_map_lock); |
131 |
|
132 |
|
133 |
if (positionMap.find(robot_id) == positionMap.end()){
|
134 |
return -1; |
135 |
} |
136 |
|
137 |
VisionPosition pos = positionMap[robot_id]; |
138 |
|
139 |
pthread_mutex_unlock(&position_map_lock); |
140 |
|
141 |
*xbuf = pos.x; |
142 |
*ybuf = pos.y; |
143 |
|
144 |
return 0; |
145 |
} |
146 |
|
147 |
bool PositionMonitor::isProbablySameRobot(VisionPosition p1, VisionPosition p2) {
|
148 |
int xDiff = p1.x - p2.x;
|
149 |
int yDiff = p1.y - p2.y;
|
150 |
return (xDiff*xDiff + yDiff*yDiff < MAX_DISTANCE*MAX_DISTANCE);
|
151 |
} |