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